elm-explorations / test

Write unit and fuzz tests for Elm code.
https://package.elm-lang.org/packages/elm-explorations/test/latest
BSD 3-Clause "New" or "Revised" License
237 stars 39 forks source link

atMost with tolerance #129

Closed Anton-4 closed 4 years ago

Anton-4 commented 4 years ago

I'm trying to write versions of atMost and atLeast that I can use with Floats that may be very close to each other. I have written the following code:

import Expect exposing (Expectation, FloatingPointTolerance(..))

precision =
    100000

atMostFloat : Float -> Float -> Expectation
atMostFloat a b =
    compareOrEqual (<=) a b "atMost"

atLeastFloat : Float -> Float -> Expectation
atLeastFloat a b =
    compareOrEqual (>=) a b "atLeast"

compareOrEqual : (comparable -> comparable -> Bool) -> Float -> Float -> String -> Expectation
compareOrEqual compareFun a b compStr =
    if compareFun a b then
        Expect.pass

    else
        let
            withinExp =
                Expect.within Absolute (1 / precision)
        in
        case withinExp of
            Expectation.Pass ->
                Expect.pass

            _ ->
                Expect.fail (Debug.toString a ++ "\n╷\n|" ++ compStr ++ "\n╵\n" ++ Debug.toString b)

It results in the following error:

I cannot find a `Expectation.Pass` variant:

98|             Expectation.Pass ->
...

Because Expect.Expectation is a type alias I cannot import Pass using Expectation(..). I do not know how I can work around this issue. I am still quite new to elm so I may have overlooked some simple fix.

drathier commented 4 years ago

You can compare for equality against the Expectation.pass value (can't remember exactly where it lives)

Anton-4 commented 4 years ago

Thanks, I tried this before but it did not work when using a case expression, it does work with an if expression though.