ktonon/elm-test-extra/2.0.0

NOTE: the latest version is 2.0.1

Test.Extra

Extends Test with specialized test and describe function.

Describing JSON Decoders

Write concise test for JSON decoders

= FailsToDecode
| FailsToDecodeWith String
| DecodesTo a

Expectation for a decoder result.

  • FailsToDecode - expect the decoder to fail, the failure message can be anything
  • FailsToDecodeWith String - expect the decoder to fail with a specific message
  • DecodesTo a - expect the decoder to succeed, decoding to the provided value
String
-> Decoder a
-> (a -> String)
-> List ( String, DecoderExpectation a )
-> Test

Exercise a decoder over a list of input/expectation pairs.

For example

describeDecoder "int"
    Json.Decode.int
    Debug.toString
    [ ( "", FailsToDecode )
    , ( "foo", FailsToDecode )
    , ( "1", DecodesTo 1 )
    , ( "1.5", FailsToDecode )
    ]
testDecoder : Decoder a -> (a -> String) -> ( String, DecoderExpectation a ) -> Test

Exercise a decoder with a JSON encoded string.

For example

testDecoder Json.Decode.string
    Debug.toString
    ( "\"foo\"", DecodesTo "foo" )