elm-matrix-sdk-beta/tests/Context.elm

144 lines
4.0 KiB
Elm
Raw Normal View History

2023-12-21 22:16:21 +00:00
module Context exposing (..)
import Expect
import Fuzz exposing (Fuzzer)
import Internal.Config.Leaks as Leaks
2023-12-21 22:17:34 +00:00
import Internal.Values.Context as Context exposing (Context)
2023-12-21 22:16:21 +00:00
import Json.Decode as D
2023-12-21 22:17:34 +00:00
import Json.Encode as E
import Test exposing (..)
2023-12-21 22:16:21 +00:00
fuzzer : Fuzzer Context
fuzzer =
let
maybeString : Fuzzer (Maybe String)
maybeString =
Fuzz.maybe Fuzz.string
in
2023-12-21 22:17:34 +00:00
Fuzz.map7 Context
maybeString
maybeString
maybeString
maybeString
maybeString
maybeString
(Fuzz.maybe <| Fuzz.list Fuzz.string)
2023-12-21 22:16:21 +00:00
{-| If a leak is spotted, make sure to change the leaking value and then test
with the same seed to ensure it is not a (tiny) coincidence and a leak is in
fact coming through.
-}
leaks : Test
leaks =
describe "No leaks allowed"
2023-12-21 22:17:34 +00:00
[ fuzz2 fuzzer
Fuzz.string
"Access token"
2023-12-21 22:16:21 +00:00
(\context value ->
context
|> Context.apiFormat
|> Context.setAccessToken value
|> Context.getAccessToken
|> Expect.notEqual Leaks.accessToken
)
2023-12-21 22:17:34 +00:00
, fuzz2 fuzzer
Fuzz.string
"Base URL"
2023-12-21 22:16:21 +00:00
(\context value ->
context
|> Context.apiFormat
|> Context.setBaseUrl value
|> Context.getBaseUrl
|> Expect.notEqual Leaks.baseUrl
)
2023-12-21 22:17:34 +00:00
, fuzz2 fuzzer
Fuzz.string
"Transaction"
2023-12-21 22:16:21 +00:00
(\context value ->
context
|> Context.apiFormat
|> Context.setTransaction value
|> Context.getTransaction
|> Expect.notEqual Leaks.transaction
)
2023-12-21 22:17:34 +00:00
, fuzz2 fuzzer
(Fuzz.list Fuzz.string)
"Versions"
2023-12-21 22:16:21 +00:00
(\context value ->
context
|> Context.apiFormat
|> Context.setVersions value
|> Context.getVersions
|> Expect.notEqual Leaks.versions
)
]
2023-12-21 22:17:34 +00:00
2023-12-21 22:16:21 +00:00
apiContext : Test
apiContext =
describe "Verify writing info"
2023-12-21 22:17:34 +00:00
[ fuzz2 fuzzer
Fuzz.string
"Access token"
2023-12-21 22:16:21 +00:00
(\context value ->
context
|> Context.apiFormat
|> Context.setAccessToken value
|> Context.getAccessToken
|> Expect.equal value
)
2023-12-21 22:17:34 +00:00
, fuzz2 fuzzer
Fuzz.string
"Base URL"
2023-12-21 22:16:21 +00:00
(\context value ->
context
|> Context.apiFormat
|> Context.setBaseUrl value
|> Context.getBaseUrl
|> Expect.equal value
)
2023-12-21 22:17:34 +00:00
, fuzz2 fuzzer
Fuzz.string
"Transaction"
2023-12-21 22:16:21 +00:00
(\context value ->
context
|> Context.apiFormat
|> Context.setTransaction value
|> Context.getTransaction
|> Expect.equal value
)
2023-12-21 22:17:34 +00:00
, fuzz2 fuzzer
(Fuzz.list Fuzz.string)
"Versions"
2023-12-21 22:16:21 +00:00
(\context value ->
context
|> Context.apiFormat
|> Context.setVersions value
|> Context.getVersions
|> Expect.equal value
)
]
2023-12-21 22:17:34 +00:00
2023-12-21 22:16:21 +00:00
json : Test
json =
describe "JSON encode + JSON decode"
[ test "Empty is {}"
2023-12-21 22:17:34 +00:00
(Context.init
2023-12-21 22:16:21 +00:00
|> Context.encode
|> E.encode 0
|> Expect.equal "{}"
|> always
)
2023-12-21 22:17:34 +00:00
, fuzz fuzzer
"JSON recode"
2023-12-21 22:16:21 +00:00
(\context ->
context
|> Context.encode
|> D.decodeValue Context.decoder
|> Expect.equal (Ok context)
)
]