elm-matrix-sdk-beta/tests/Test/Tools/Timestamp.elm

67 lines
1.9 KiB
Elm
Raw Normal View History

module Test.Tools.Timestamp exposing (..)
2023-12-22 22:44:46 +00:00
2023-12-24 10:17:43 +00:00
import Expect
2023-12-22 22:44:46 +00:00
import Fuzz exposing (Fuzzer)
2023-12-24 10:17:43 +00:00
import Internal.Tools.Timestamp as Timestamp exposing (Timestamp)
import Json.Decode as D
import Json.Encode as E
2023-12-22 22:44:46 +00:00
import Test exposing (..)
import Time
fuzzer : Fuzzer Timestamp
fuzzer =
Fuzz.map Time.millisToPosix Fuzz.int
2023-12-24 10:17:43 +00:00
suite : Test
suite =
describe "Timestamp"
[ describe "JSON"
[ fuzz2 fuzzer
Fuzz.int
"JSON encode -> JSON decode"
(\time indent ->
time
|> Timestamp.encode
|> E.encode indent
|> D.decodeString Timestamp.decoder
2024-01-19 15:22:51 +00:00
|> Expect.equal (Ok ( time, [] ))
2023-12-24 10:17:43 +00:00
)
, fuzz fuzzer
"JSON decode -> millis"
(\time ->
time
|> Timestamp.encode
|> D.decodeValue D.int
|> Expect.equal (Ok <| Time.posixToMillis time)
)
, fuzz Fuzz.int
"JSON decode -> time"
(\n ->
n
|> E.int
|> D.decodeValue Timestamp.decoder
2024-01-19 15:22:51 +00:00
|> Expect.equal (Ok ( Time.millisToPosix n, [] ))
2023-12-24 10:17:43 +00:00
)
]
, describe "Identity"
[ fuzz fuzzer
"Posix -> int -> Posix"
(\time ->
time
|> Time.posixToMillis
|> Time.millisToPosix
|> Expect.equal time
)
, fuzz Fuzz.int
"int -> Posix -> int"
(\n ->
n
|> Time.millisToPosix
|> Time.posixToMillis
|> Expect.equal n
)
]
]