Add isEqual function for Hashdict + tests

elm-test
Bram 2023-12-26 12:59:50 +01:00
parent 26ca6600d7
commit f465c9cbb1
2 changed files with 80 additions and 5 deletions

View File

@ -4,7 +4,7 @@ module Internal.Tools.Hashdict exposing
, isEmpty, member, memberKey, get, size , isEmpty, member, memberKey, get, size
, keys, values, toList, fromList , keys, values, toList, fromList
, rehash, union , rehash, union
, encode, decoder, softDecoder , encode, decoder, softDecoder, isEqual
) )
{-| This module abstracts the `Dict` type with one function that assigns a {-| This module abstracts the `Dict` type with one function that assigns a
@ -25,7 +25,7 @@ This allows you to store values based on an externally defined identifier.
## Query ## Query
@docs isEmpty, member, memberKey, get, size @docs isEmpty, member, memberKey, get, size, isEqual
## Lists ## Lists
@ -150,6 +150,12 @@ insert : a -> Hashdict a -> Hashdict a
insert v (Hashdict h) = insert v (Hashdict h) =
Hashdict { h | values = Dict.insert (h.hash v) v h.values } Hashdict { h | values = Dict.insert (h.hash v) v h.values }
{-| Since the Hashdict contains a hash function, the == operator does not work
simply. Instead, you should use the isEqual operator.
-}
isEqual : Hashdict a -> Hashdict a -> Bool
isEqual h1 h2 =
toList h1 == toList h2
{-| Determine if a hashdict is empty. {-| Determine if a hashdict is empty.
-} -}

View File

@ -13,19 +13,88 @@ fuzzer : (a -> String) -> Fuzzer a -> Fuzzer (Hashdict a)
fuzzer toHash fuz = fuzzer toHash fuz =
Fuzz.map (Hashdict.fromList toHash) (Fuzz.list fuz) Fuzz.map (Hashdict.fromList toHash) (Fuzz.list fuz)
eventFuzzer : Fuzzer (Hashdict Event.Event)
eventFuzzer =
fuzzer .eventId TestEvent.fuzzer
suite : Test suite : Test
suite = suite =
describe "Hashdict" describe "Hashdict"
[ describe "init" [ describe "empty"
[ test "init isEmpty" [ test "empty isEmpty"
( Hashdict.empty identity ( Hashdict.empty identity
|> Hashdict.isEmpty |> Hashdict.isEmpty
|> Expect.equal True |> Expect.equal True
|> always |> always
) )
, fuzz TestEvent.fuzzer "Nothing is member"
(\event ->
Hashdict.empty .eventId
|> Hashdict.member event
|> Expect.equal False
)
, fuzz Fuzz.string "No key is member"
(\key ->
Hashdict.empty identity
|> Hashdict.memberKey key
|> Expect.equal False
)
, fuzz Fuzz.string "Get gets Nothing"
(\key ->
Hashdict.empty identity
|> Hashdict.get key
|> Expect.equal Nothing
)
, test "Size is zero"
( Hashdict.empty identity
|> Hashdict.size
|> Expect.equal 0
|> always
)
, test "No keys"
( Hashdict.empty identity
|> Hashdict.keys
|> Expect.equal []
|> always
)
, test "No values"
( Hashdict.empty identity
|> Hashdict.values
|> Expect.equal []
|> always
)
, test "To list is []"
( Hashdict.empty identity
|> Hashdict.toList
|> Expect.equal []
|> always
)
, test "From list is empty"
( []
|> Hashdict.fromList (\x -> x)
|> Hashdict.isEqual (Hashdict.empty identity)
|> Expect.equal True
|> always
)
, test "Empty + empty == empty"
( Hashdict.empty identity
|> Hashdict.union (Hashdict.empty String.toUpper)
|> Hashdict.isEqual (Hashdict.empty String.toLower)
|> Expect.equal True
|> always
)
, fuzz (Fuzz.intRange 0 10) "JSON encode -> JSON decode"
(\indent ->
Hashdict.empty identity
|> Hashdict.encode E.string
|> E.encode indent
|> D.decodeString (Hashdict.decoder identity D.string)
|> Result.map (Hashdict.isEqual (Hashdict.empty String.toUpper))
|> Expect.equal (Ok True)
)
] ]
, describe "JSON" , describe "JSON"
[ fuzz2 (fuzzer .eventId TestEvent.fuzzer) (Fuzz.intRange 0 10) "JSON encode -> JSON decode" [ fuzz2 eventFuzzer (Fuzz.intRange 0 10) "JSON encode -> JSON decode"
(\hashdict indent -> (\hashdict indent ->
hashdict hashdict
|> Hashdict.encode Event.encode |> Hashdict.encode Event.encode