RoundtripStringTest
Strings survive an elm/bytes encode and decode when `Bytes.Decode.string` is given the string's length. The strings `""`, `"a"`, `"hello"` and `"Hello, World!"` are each round-tripped, and the program logs `True` only if all of them come back unchanged. The length passed to the decoder is `String.length`, which equals the UTF-8 byte count only because every test string is ASCII. Among what is not tested: non-ASCII text.
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. RoundtripStringTest: True
elm-0.19.2
passran in 52 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. RoundtripStringTest: True
elm-0.19.3
passran in 44 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. RoundtripStringTest: True
lamdera-1.5.0
passran in 61 msOutput
RoundtripStringTest: True
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-bytes/RoundtripStringTest] in its title.
Source
The -- CHECK: lines give the expected output.
module RoundtripStringTest exposing (main)
{-| Strings survive an elm/bytes encode and decode when `Bytes.Decode.string` is given the string's length.
The strings `""`, `"a"`, `"hello"` and `"Hello, World!"` are each round-tripped,
and the program logs `True` only if all of them come back unchanged. The length
passed to the decoder is `String.length`, which equals the UTF-8 byte count only
because every test string is ASCII.
Among what is not tested: non-ASCII text.
-}
-- CHECK: RoundtripStringTest: True
import Bytes exposing (Bytes)
import Bytes.Decode as D
import Bytes.Encode as E
import Html exposing (text)
roundtrip : String -> Bool
roundtrip s =
let
len =
String.length s
encoded =
E.encode (E.string s)
decoded =
D.decode (D.string len) encoded
in
decoded == Just s
main =
let
allPass =
roundtrip ""
&& roundtrip "a"
&& roundtrip "hello"
&& roundtrip "Hello, World!"
_ =
Debug.log "RoundtripStringTest" allPass
in
text (if allPass then "True" else "False")