ktonon / elm-test-extra / Test.Extra

Extends Test with specialized test and describe function.

Describing JSON Decoders

Write concise test for JSON decoders


type DecoderExpectation a
    = FailsToDecode
    | FailsToDecodeWith String
    | DecodesTo a

Expectation for a decoder result.

describeDecoder : String -> Json.Decode.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 : Json.Decode.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" )