elm-format + add more tests

elm-test
Bram 2023-12-26 16:11:51 +01:00
parent f465c9cbb1
commit c7a3fe804b
2 changed files with 88 additions and 19 deletions

View File

@ -1,10 +1,10 @@
module Internal.Tools.Hashdict exposing module Internal.Tools.Hashdict exposing
( Hashdict ( Hashdict
, empty, singleton, insert, remove, removeKey , empty, singleton, insert, remove, removeKey
, isEmpty, member, memberKey, get, size , isEmpty, member, memberKey, get, size, isEqual
, keys, values, toList, fromList , keys, values, toList, fromList
, rehash, union , rehash, union
, encode, decoder, softDecoder, isEqual , encode, decoder, softDecoder
) )
{-| This module abstracts the `Dict` type with one function that assigns a {-| This module abstracts the `Dict` type with one function that assigns a
@ -150,6 +150,7 @@ 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 {-| Since the Hashdict contains a hash function, the == operator does not work
simply. Instead, you should use the isEqual operator. simply. Instead, you should use the isEqual operator.
-} -}
@ -157,6 +158,7 @@ isEqual : Hashdict a -> Hashdict a -> Bool
isEqual h1 h2 = isEqual h1 h2 =
toList h1 == toList h2 toList h1 == toList h2
{-| Determine if a hashdict is empty. {-| Determine if a hashdict is empty.
-} -}
isEmpty : Hashdict a -> Bool isEmpty : Hashdict a -> Bool

View File

@ -1,22 +1,25 @@
module Test.Tools.Hashdict exposing (..) module Test.Tools.Hashdict exposing (..)
import Test exposing (..) import Expect
import Fuzz exposing (Fuzzer) import Fuzz exposing (Fuzzer)
import Internal.Tools.Hashdict as Hashdict exposing (Hashdict) import Internal.Tools.Hashdict as Hashdict exposing (Hashdict)
import Test.Values.Event as TestEvent
import Internal.Values.Event as Event import Internal.Values.Event as Event
import Json.Encode as E
import Json.Decode as D import Json.Decode as D
import Expect import Json.Encode as E
import Test exposing (..)
import Test.Values.Event as TestEvent
fuzzer : (a -> String) -> Fuzzer a -> Fuzzer (Hashdict a) 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 (Hashdict Event.Event)
eventFuzzer = eventFuzzer =
fuzzer .eventId TestEvent.fuzzer fuzzer .eventId TestEvent.fuzzer
suite : Test suite : Test
suite = suite =
describe "Hashdict" describe "Hashdict"
@ -27,19 +30,22 @@ suite =
|> Expect.equal True |> Expect.equal True
|> always |> always
) )
, fuzz TestEvent.fuzzer "Nothing is member" , fuzz TestEvent.fuzzer
"Nothing is member"
(\event -> (\event ->
Hashdict.empty .eventId Hashdict.empty .eventId
|> Hashdict.member event |> Hashdict.member event
|> Expect.equal False |> Expect.equal False
) )
, fuzz Fuzz.string "No key is member" , fuzz Fuzz.string
"No key is member"
(\key -> (\key ->
Hashdict.empty identity Hashdict.empty identity
|> Hashdict.memberKey key |> Hashdict.memberKey key
|> Expect.equal False |> Expect.equal False
) )
, fuzz Fuzz.string "Get gets Nothing" , fuzz Fuzz.string
"Get gets Nothing"
(\key -> (\key ->
Hashdict.empty identity Hashdict.empty identity
|> Hashdict.get key |> Hashdict.get key
@ -83,7 +89,8 @@ suite =
|> Expect.equal True |> Expect.equal True
|> always |> always
) )
, fuzz (Fuzz.intRange 0 10) "JSON encode -> JSON decode" , fuzz (Fuzz.intRange 0 10)
"JSON encode -> JSON decode"
(\indent -> (\indent ->
Hashdict.empty identity Hashdict.empty identity
|> Hashdict.encode E.string |> Hashdict.encode E.string
@ -93,8 +100,68 @@ suite =
|> Expect.equal (Ok True) |> Expect.equal (Ok True)
) )
] ]
, describe "singleton"
[ fuzz TestEvent.fuzzer
"singletong = empty + insert"
(\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
|> Hashdict.isEqual (Hashdict.empty .sender)
|> Expect.equal True
)
, fuzz TestEvent.fuzzer
"Singletong - event (key) = empty"
(\event ->
Hashdict.singleton .eventId event
|> Hashdict.removeKey event.eventId
|> Hashdict.isEqual (Hashdict.empty .sender)
|> 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
)
]
, describe "JSON" , describe "JSON"
[ fuzz2 eventFuzzer (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