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

196 lines
7.1 KiB
Elm
Raw Normal View History

2023-12-24 14:49:55 +00:00
module Test.Tools.Hashdict exposing (..)
2023-12-26 15:11:51 +00:00
import Expect
2023-12-24 14:49:55 +00:00
import Fuzz exposing (Fuzzer)
import Internal.Tools.Hashdict as Hashdict exposing (Hashdict)
2024-01-19 15:22:51 +00:00
import Internal.Tools.Json as Json
2023-12-24 14:49:55 +00:00
import Internal.Values.Event as Event
import Json.Decode as D
2023-12-26 15:11:51 +00:00
import Json.Encode as E
import Test exposing (..)
import Test.Values.Event as TestEvent
2023-12-24 14:49:55 +00:00
fuzzer : (a -> String) -> Fuzzer a -> Fuzzer (Hashdict a)
fuzzer toHash fuz =
Fuzz.map (Hashdict.fromList toHash) (Fuzz.list fuz)
2023-12-26 15:11:51 +00:00
eventFuzzer : Fuzzer (Hashdict Event.Event)
eventFuzzer =
fuzzer .eventId TestEvent.fuzzer
2023-12-26 15:11:51 +00:00
2023-12-24 14:49:55 +00:00
suite : Test
suite =
describe "Hashdict"
[ describe "empty"
[ test "empty isEmpty"
2023-12-26 15:11:51 +00:00
(Hashdict.empty identity
2023-12-24 14:49:55 +00:00
|> Hashdict.isEmpty
|> Expect.equal True
|> always
)
2023-12-26 15:11:51 +00:00
, fuzz TestEvent.fuzzer
"Nothing is member"
(\event ->
Hashdict.empty .eventId
|> Hashdict.member event
|> Expect.equal False
)
2023-12-26 15:11:51 +00:00
, fuzz Fuzz.string
"No key is member"
(\key ->
Hashdict.empty identity
|> Hashdict.memberKey key
|> Expect.equal False
)
2023-12-26 15:11:51 +00:00
, fuzz Fuzz.string
"Get gets Nothing"
(\key ->
Hashdict.empty identity
|> Hashdict.get key
|> Expect.equal Nothing
)
, test "Size is zero"
2023-12-26 15:11:51 +00:00
(Hashdict.empty identity
|> Hashdict.size
|> Expect.equal 0
|> always
)
, test "No keys"
2023-12-26 15:11:51 +00:00
(Hashdict.empty identity
|> Hashdict.keys
|> Expect.equal []
|> always
)
, test "No values"
2023-12-26 15:11:51 +00:00
(Hashdict.empty identity
|> Hashdict.values
|> Expect.equal []
|> always
)
, test "To list is []"
2023-12-26 15:11:51 +00:00
(Hashdict.empty identity
|> Hashdict.toList
|> Expect.equal []
|> always
)
, test "From list is empty"
2023-12-26 15:11:51 +00:00
([]
|> Hashdict.fromList (\x -> x)
|> Hashdict.isEqual (Hashdict.empty identity)
|> Expect.equal True
|> always
)
, test "Empty + empty == empty"
2023-12-26 15:11:51 +00:00
(Hashdict.empty identity
|> Hashdict.union (Hashdict.empty String.toUpper)
|> Hashdict.isEqual (Hashdict.empty String.toLower)
|> Expect.equal True
|> always
)
2023-12-26 15:11:51 +00:00
, fuzz (Fuzz.intRange 0 10)
"JSON encode -> JSON decode"
(\indent ->
Hashdict.empty identity
2024-01-19 15:22:51 +00:00
|> Json.encode (Hashdict.coder identity Json.string)
|> E.encode indent
2024-01-19 15:22:51 +00:00
|> D.decodeString (Json.decode <| Hashdict.coder identity Json.string)
|> Result.map (Tuple.mapFirst (Hashdict.isEqual (Hashdict.empty String.toUpper)))
|> Expect.equal (Ok ( True, [] ))
)
2023-12-24 14:49:55 +00:00
]
2023-12-26 15:11:51 +00:00
, describe "singleton"
[ fuzz TestEvent.fuzzer
2024-05-08 05:57:08 +00:00
"singleton = empty + insert"
2023-12-26 15:11:51 +00:00
(\event ->
Hashdict.empty .eventId
|> Hashdict.insert event
|> Hashdict.isEqual (Hashdict.singleton .eventId event)
|> Expect.equal True
)
, fuzz TestEvent.fuzzer
"Singleton - event = empty"
(\event ->
Hashdict.singleton .eventId event
|> Hashdict.remove event
2024-04-12 11:57:38 +00:00
|> Hashdict.isEqual (Hashdict.empty .roomId)
2023-12-26 15:11:51 +00:00
|> Expect.equal True
)
, fuzz TestEvent.fuzzer
"Singletong - event (key) = empty"
(\event ->
Hashdict.singleton .eventId event
|> Hashdict.removeKey event.eventId
2024-04-12 11:57:38 +00:00
|> Hashdict.isEqual (Hashdict.empty .roomId)
2023-12-26 15:11:51 +00:00
|> Expect.equal True
)
, fuzz TestEvent.fuzzer
"not isEmpty"
(\event ->
Hashdict.singleton .eventId event
|> Hashdict.isEmpty
|> Expect.equal False
)
, fuzz TestEvent.fuzzer
"member"
(\event ->
Hashdict.singleton .eventId event
|> Hashdict.member event
|> Expect.equal True
)
, fuzz TestEvent.fuzzer
"memberKey"
(\event ->
Hashdict.singleton .eventId event
|> Hashdict.memberKey event.eventId
|> Expect.equal True
)
, fuzz TestEvent.fuzzer
"False memberKey"
(\event ->
if event.eventId == event.roomId then
Expect.pass
else
Hashdict.singleton .eventId event
|> Hashdict.memberKey event.roomId
|> Expect.equal False
)
]
2024-05-08 05:57:08 +00:00
, describe "update"
[ fuzz2 (fuzzer identity Fuzz.string)
Fuzz.string
"add = insert"
(\hashdict value ->
Hashdict.isEqual
(Hashdict.insert value hashdict)
(Hashdict.update value (always (Just value)) hashdict)
|> Expect.equal True
)
, fuzz2 (fuzzer identity Fuzz.string)
Fuzz.string
"remove = removeKey"
(\hashdict value ->
Hashdict.isEqual
(Hashdict.removeKey value hashdict)
(Hashdict.update value (always Nothing) hashdict)
|> Expect.equal True
)
]
2023-12-24 14:49:55 +00:00
, describe "JSON"
2023-12-26 15:11:51 +00:00
[ fuzz2 eventFuzzer
(Fuzz.intRange 0 10)
"JSON encode -> JSON decode"
2023-12-24 14:49:55 +00:00
(\hashdict indent ->
hashdict
2024-01-19 15:22:51 +00:00
|> Json.encode (Hashdict.coder .eventId Event.coder)
2023-12-24 14:49:55 +00:00
|> E.encode indent
2024-01-19 15:22:51 +00:00
|> D.decodeString (Json.decode <| Hashdict.coder .eventId Event.coder)
|> Result.map (Tuple.first >> Hashdict.toList)
|> Expect.equal (Ok (Hashdict.toList hashdict))
2023-12-24 14:49:55 +00:00
)
]
]