IntegrationNestedStructureTest
A record containing another record round-trips through elm/bytes using encoders and decoders composed from per-record parts.
`Outer` is encoded as an `unsignedInt8` id followed by the nested `Inner`, whose
`x` and `y` are big-endian `unsignedInt16` values. The record with id 42 and
inner `{ x = 100, y = 200 }` is encoded and decoded, and the program logs `True`
only if all three numbers come back equal.Results
elm-0.19.1
passran in 67 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. IntegrationNestedStructureTest: True
elm-0.19.2
passran in 62 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. IntegrationNestedStructureTest: True
elm-0.19.3
passran in 56 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. IntegrationNestedStructureTest: True
lamdera-1.5.0
passran in 57 msOutput
IntegrationNestedStructureTest: True
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-bytes/IntegrationNestedStructureTest] in its title.
Source
The -- CHECK: lines give the expected output.
module IntegrationNestedStructureTest exposing (main)
{-| A record containing another record round-trips through elm/bytes using encoders and decoders composed from per-record parts.
`Outer` is encoded as an `unsignedInt8` id followed by the nested `Inner`, whose
`x` and `y` are big-endian `unsignedInt16` values. The record with id 42 and
inner `{ x = 100, y = 200 }` is encoded and decoded, and the program logs `True`
only if all three numbers come back equal.
-}
-- CHECK: IntegrationNestedStructureTest: True
import Bytes exposing (Bytes, Endianness(..))
import Bytes.Decode as D
import Bytes.Encode as E
import Html exposing (text)
type alias Inner =
{ x : Int
, y : Int
}
type alias Outer =
{ id : Int
, inner : Inner
}
encodeInner : Inner -> E.Encoder
encodeInner i =
E.sequence
[ E.unsignedInt16 BE i.x
, E.unsignedInt16 BE i.y
]
encodeOuter : Outer -> E.Encoder
encodeOuter o =
E.sequence
[ E.unsignedInt8 o.id
, encodeInner o.inner
]
decodeInner : D.Decoder Inner
decodeInner =
D.map2 Inner
(D.unsignedInt16 BE)
(D.unsignedInt16 BE)
decodeOuter : D.Decoder Outer
decodeOuter =
D.map2 Outer
D.unsignedInt8
decodeInner
main =
let
original =
{ id = 42
, inner = { x = 100, y = 200 }
}
encoded =
E.encode (encodeOuter original)
decoded =
D.decode decodeOuter encoded
result =
case decoded of
Just o ->
(o.id == 42)
&& (o.inner.x == 100)
&& (o.inner.y == 200)
Nothing ->
False
_ =
Debug.log "IntegrationNestedStructureTest" result
in
text (if result then "True" else "False")