IntegrationConditionalDecodingTest
A decoder that reads a tag byte and uses `Bytes.Decode.andThen` to choose between a 32-bit integer and a 64-bit float decodes both variants of a custom type. Tag 1 is followed by a big-endian `signedInt32` and tag 2 by a big-endian `float64`; any other tag fails. `IntVal 42` and `FloatVal 3.14` are each encoded and decoded, and the program logs `True` only if both decode to the original value. Among what is not tested: the failing branch for an unknown tag.
Results
elm-0.19.1
passran in 55 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. IntegrationConditionalDecodingTest: True
elm-0.19.2
passran in 61 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. IntegrationConditionalDecodingTest: True
elm-0.19.3
passran in 67 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. IntegrationConditionalDecodingTest: True
lamdera-1.5.0
passran in 57 msOutput
IntegrationConditionalDecodingTest: True
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-bytes/IntegrationConditionalDecodingTest] in its title.
Source
The -- CHECK: lines give the expected output.
module IntegrationConditionalDecodingTest exposing (main)
{-| A decoder that reads a tag byte and uses `Bytes.Decode.andThen` to choose between a 32-bit integer and a 64-bit float decodes both variants of a custom type.
Tag 1 is followed by a big-endian `signedInt32` and tag 2 by a big-endian
`float64`; any other tag fails. `IntVal 42` and `FloatVal 3.14` are each encoded
and decoded, and the program logs `True` only if both decode to the original
value.
Among what is not tested: the failing branch for an unknown tag.
-}
-- CHECK: IntegrationConditionalDecodingTest: True
import Bytes exposing (Bytes, Endianness(..))
import Bytes.Decode as D
import Bytes.Encode as E
import Html exposing (text)
type Value
= IntVal Int
| FloatVal Float
encodeValue : Value -> E.Encoder
encodeValue v =
case v of
IntVal n ->
E.sequence [ E.unsignedInt8 1, E.signedInt32 BE n ]
FloatVal f ->
E.sequence [ E.unsignedInt8 2, E.float64 BE f ]
decodeValue : D.Decoder Value
decodeValue =
D.unsignedInt8
|> D.andThen
(\tag ->
case tag of
1 ->
D.map IntVal (D.signedInt32 BE)
2 ->
D.map FloatVal (D.float64 BE)
_ ->
D.fail
)
main =
let
intVal =
IntVal 42
floatVal =
FloatVal 3.14
decodedInt =
D.decode decodeValue (E.encode (encodeValue intVal))
decodedFloat =
D.decode decodeValue (E.encode (encodeValue floatVal))
result =
(decodedInt == Just (IntVal 42))
&& (decodedFloat == Just (FloatVal 3.14))
_ =
Debug.log "IntegrationConditionalDecodingTest" result
in
text (if result then "True" else "False")