elm-community/elm-test/3.1.0

NOTE: this package is not compatible with Elm 0.19.1

Expect

A library to create Expectations, which describe a claim to be tested.

Quick Reference

Basic Expectations

type alias Expectation =
Expectation

The result of a single test run: either a pass or a fail.

equal : a -> a -> Expectation

Passes if the arguments are equal.

Expect.equal 0 (List.length [])


-- Passes because (0 == 0) is True

Failures resemble code written in pipeline style, so you can tell which argument is which:

-- Fails because the expected value didn't split the space in "Betty Botter"
String.split " " "Betty Botter bought some butter"
    |> Expect.equal [ "Betty Botter", "bought", "some", "butter" ]


{-


[ "Betty", "Botter", "bought", "some", "butter" ]
╷
│ Expect.equal
╵
[ "Betty Botter", "bought", "some", "butter" ]


-}
notEqual : a -> a -> Expectation

Passes if the arguments are not equal.

-- Passes because (11 /= 100) is True
90 + 10
    |> Expect.notEqual 11




-- Fails because (100 /= 100) is False
90 + 10
    |> Expect.notEqual 100


{-


100
╷
│ Expect.notEqual
╵
100


-}
all : List (subject -> Expectation) -> subject -> Expectation

Passes if each of the given functions passes when applied to the subject.

NOTE: Passing an empty list is assumed to be a mistake, so Expect.all [] will always return a failed expectation no matter what else it is passed.

Expect.all
    [ Expect.greaterThan -2
    , Expect.lessThan 5
    ]
    (List.length [])


-- Passes because (0 > -2) is True and (0 < 5) is also True

Failures resemble code written in pipeline style, so you can tell which argument is which:

-- Fails because (0 > -10) is False
List.length []
    |> Expect.all
        [ Expect.greaterThan -2
        , Expect.lessThan -10
        , Expect.equal 0
        ]


{-


0
╷
│ Expect.lessThan
╵
-10


-}

Comparisons

lessThan : comparable -> comparable -> Expectation

Passes if the second argument is less than the first.

Expect.lessThan 1 (List.length [])


-- Passes because (0 < 1) is True

Failures resemble code written in pipeline style, so you can tell which argument is which:

-- Fails because (0 < -1) is False
List.length []
    |> Expect.lessThan -1




{-


0
╷
│ Expect.lessThan
╵
-1


-}
atMost : comparable -> comparable -> Expectation

Passes if the second argument is less than or equal to the first.

Expect.atMost 1 (List.length [])


-- Passes because (0 <= 1) is True

Failures resemble code written in pipeline style, so you can tell which argument is which:

-- Fails because (0 <= -3) is False
List.length []
    |> Expect.atMost -3


{-


0
╷
│ Expect.atMost
╵
-3


-}
greaterThan : comparable -> comparable -> Expectation

Passes if the second argument is greater than the first.

Expect.greaterThan -2 List.length []


-- Passes because (0 > -2) is True

Failures resemble code written in pipeline style, so you can tell which argument is which:

-- Fails because (0 > 1) is False
List.length []
    |> Expect.greaterThan 1


{-


0
╷
│ Expect.greaterThan
╵
1


-}
atLeast : comparable -> comparable -> Expectation

Passes if the second argument is greater than or equal to the first.

Expect.atLeast -2 (List.length [])


-- Passes because (0 >= -2) is True

Failures resemble code written in pipeline style, so you can tell which argument is which:

-- Fails because (0 >= 3) is False
List.length []
    |> Expect.atLeast 3


{-


0
╷
│ Expect.atLeast
╵
3


-}

Booleans

true : String -> Bool -> Expectation

Passes if the argument is 'True', and otherwise fails with the given message.

Expect.true "Expected the list to be empty." (List.isEmpty [])


-- Passes because (List.isEmpty []) is True

Failures resemble code written in pipeline style, so you can tell which argument is which:

-- Fails because List.isEmpty returns False, but we expect True.
List.isEmpty [ 42 ]
    |> Expect.true "Expected the list to be empty."


{-


Expected the list to be empty.


-}
false : String -> Bool -> Expectation

Passes if the argument is 'False', and otherwise fails with the given message.

Expect.false "Expected the list not to be empty." (List.isEmpty [ 42 ])


-- Passes because (List.isEmpty [ 42 ]) is False

Failures resemble code written in pipeline style, so you can tell which argument is which:

-- Fails because (List.isEmpty []) is True
List.isEmpty []
    |> Expect.false "Expected the list not to be empty."


{-


Expected the list not to be empty.


-}

Collections

equalLists : List a -> List a -> Expectation

Passes if the arguments are equal lists.

-- Passes
[1, 2, 3]
    |> Expect.equalLists [1, 2, 3]

Failures resemble code written in pipeline style, so you can tell which argument is which, and reports which index the lists first differed at or which list was longer:

-- Fails
[ 1, 2, 4, 6 ]
    |> Expect.equalLists [ 1, 2, 5 ]


{-


[1,2,4,6]
first diff at index index 2: +`4`, -`5`
╷
│ Expect.equalLists
╵
first diff at index index 2: +`5`, -`4`
[1,2,5]


-}
equalDicts : Dict comparable a -> Dict comparable a -> Expectation

Passes if the arguments are equal dicts.

-- Passes
(Dict.fromList [ ( 1, "one" ), ( 2, "two" ) ])
    |> Expect.equalDicts (Dict.fromList [ ( 1, "one" ), ( 2, "two" ) ])

Failures resemble code written in pipeline style, so you can tell which argument is which, and reports which keys were missing from or added to each dict:

-- Fails
(Dict.fromList [ ( 1, "one" ), ( 2, "too" ) ])
    |> Expect.equalDicts (Dict.fromList [ ( 1, "one" ), ( 2, "two" ), ( 3, "three" ) ])


{-


Dict.fromList [(1,"one"),(2,"too")]
diff: -[ (2,"two"), (3,"three") ] +[ (2,"too") ]
╷
│ Expect.equalDicts
╵
diff: +[ (2,"two"), (3,"three") ] -[ (2,"too") ]
Dict.fromList [(1,"one"),(2,"two"),(3,"three")]


-}
equalSets : Set comparable -> Set comparable -> Expectation

Passes if the arguments are equal sets.

-- Passes
(Set.fromList [1, 2])
    |> Expect.equalSets (Set.fromList [1, 2])

Failures resemble code written in pipeline style, so you can tell which argument is which, and reports which keys were missing from or added to each set:

-- Fails
(Set.fromList [ 1, 2, 4, 6 ])
    |> Expect.equalSets (Set.fromList [ 1, 2, 5 ])


{-


Set.fromList [1,2,4,6]
diff: -[ 5 ] +[ 4, 6 ]
╷
│ Expect.equalSets
╵
diff: +[ 5 ] -[ 4, 6 ]
Set.fromList [1,2,5]


-}

Customizing

pass : Expectation

Always passes.

import Json.Decode exposing (decodeString, int)
import Test exposing (test)
import Expect




test "Json.Decode.int can decode the number 42." <|
    \() ->
        case decodeString int "42" of
            Ok _ ->
                Expect.pass


            Err err ->
                Expect.fail err
fail : String -> Expectation

Fails with the given message.

import Json.Decode exposing (decodeString, int)
import Test exposing (test)
import Expect




test "Json.Decode.int can decode the number 42." <|
    \() ->
        case decodeString int "42" of
            Ok _ ->
                Expect.pass


            Err err ->
                Expect.fail err
onFail : String -> Expectation -> Expectation

If the given expectation fails, replace its failure message with a custom one.

"something"
    |> Expect.equal "something else"
    |> Expect.onFail "thought those two strings would be the same"
Expectation
-> Maybe
{ given : String
, message : String
}

Return Nothing if the given Expectation is a pass.

If it is a fail, return a record containing the failure message, along with the given inputs if it was a fuzz test. (If no inputs were involved, the record's given field will be "").

For example, if a fuzz test generates random integers, this might return { message = "it was supposed to be positive", given = "-1" }

getFailure (Expect.fail "this failed")
-- Just { message = "this failed", given = "" }


getFailure (Expect.pass)
-- Nothing