From 26ca6600d735dfe0339ec10b88f6815b934037c2 Mon Sep 17 00:00:00 2001 From: Bram Date: Sun, 24 Dec 2023 15:49:55 +0100 Subject: [PATCH 1/3] Add Hashdict test --- tests/Test/Tools/Hashdict.elm | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/Test/Tools/Hashdict.elm diff --git a/tests/Test/Tools/Hashdict.elm b/tests/Test/Tools/Hashdict.elm new file mode 100644 index 0000000..d389f42 --- /dev/null +++ b/tests/Test/Tools/Hashdict.elm @@ -0,0 +1,38 @@ +module Test.Tools.Hashdict exposing (..) + +import Test exposing (..) +import Fuzz exposing (Fuzzer) +import Internal.Tools.Hashdict as Hashdict exposing (Hashdict) +import Test.Values.Event as TestEvent +import Internal.Values.Event as Event +import Json.Encode as E +import Json.Decode as D +import Expect + +fuzzer : (a -> String) -> Fuzzer a -> Fuzzer (Hashdict a) +fuzzer toHash fuz = + Fuzz.map (Hashdict.fromList toHash) (Fuzz.list fuz) + +suite : Test +suite = + describe "Hashdict" + [ describe "init" + [ test "init isEmpty" + ( Hashdict.empty identity + |> Hashdict.isEmpty + |> Expect.equal True + |> always + ) + ] + , describe "JSON" + [ fuzz2 (fuzzer .eventId TestEvent.fuzzer) (Fuzz.intRange 0 10) "JSON encode -> JSON decode" + (\hashdict indent -> + hashdict + |> Hashdict.encode Event.encode + |> E.encode indent + |> D.decodeString (Hashdict.decoder .eventId Event.decoder) + |> Result.map Hashdict.toList + |> Expect.equal ( Ok <| Hashdict.toList hashdict ) + ) + ] + ] From f465c9cbb1865d5410389e769e68edeef28ef29f Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 26 Dec 2023 12:59:50 +0100 Subject: [PATCH 2/3] Add isEqual function for Hashdict + tests --- src/Internal/Tools/Hashdict.elm | 10 ++++- tests/Test/Tools/Hashdict.elm | 75 +++++++++++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/Internal/Tools/Hashdict.elm b/src/Internal/Tools/Hashdict.elm index b8be6a9..6020381 100644 --- a/src/Internal/Tools/Hashdict.elm +++ b/src/Internal/Tools/Hashdict.elm @@ -4,7 +4,7 @@ module Internal.Tools.Hashdict exposing , isEmpty, member, memberKey, get, size , keys, values, toList, fromList , rehash, union - , encode, decoder, softDecoder + , encode, decoder, softDecoder, isEqual ) {-| 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 -@docs isEmpty, member, memberKey, get, size +@docs isEmpty, member, memberKey, get, size, isEqual ## Lists @@ -150,6 +150,12 @@ insert : a -> Hashdict a -> Hashdict a insert v (Hashdict h) = 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. -} diff --git a/tests/Test/Tools/Hashdict.elm b/tests/Test/Tools/Hashdict.elm index d389f42..45a21e0 100644 --- a/tests/Test/Tools/Hashdict.elm +++ b/tests/Test/Tools/Hashdict.elm @@ -13,19 +13,88 @@ fuzzer : (a -> String) -> Fuzzer a -> Fuzzer (Hashdict a) fuzzer toHash fuz = Fuzz.map (Hashdict.fromList toHash) (Fuzz.list fuz) +eventFuzzer : Fuzzer (Hashdict Event.Event) +eventFuzzer = + fuzzer .eventId TestEvent.fuzzer + suite : Test suite = describe "Hashdict" - [ describe "init" - [ test "init isEmpty" + [ describe "empty" + [ test "empty isEmpty" ( Hashdict.empty identity |> Hashdict.isEmpty |> Expect.equal True |> 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" - [ 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 |> Hashdict.encode Event.encode From c7a3fe804b7e55e47d23c4814ef72121be0e495e Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 26 Dec 2023 16:11:51 +0100 Subject: [PATCH 3/3] elm-format + add more tests --- src/Internal/Tools/Hashdict.elm | 6 +- tests/Test/Tools/Hashdict.elm | 101 ++++++++++++++++++++++++++------ 2 files changed, 88 insertions(+), 19 deletions(-) diff --git a/src/Internal/Tools/Hashdict.elm b/src/Internal/Tools/Hashdict.elm index 6020381..f2e4fdb 100644 --- a/src/Internal/Tools/Hashdict.elm +++ b/src/Internal/Tools/Hashdict.elm @@ -1,10 +1,10 @@ module Internal.Tools.Hashdict exposing ( Hashdict , empty, singleton, insert, remove, removeKey - , isEmpty, member, memberKey, get, size + , isEmpty, member, memberKey, get, size, isEqual , keys, values, toList, fromList , rehash, union - , encode, decoder, softDecoder, isEqual + , encode, decoder, softDecoder ) {-| 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) = 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. -} @@ -157,6 +158,7 @@ isEqual : Hashdict a -> Hashdict a -> Bool isEqual h1 h2 = toList h1 == toList h2 + {-| Determine if a hashdict is empty. -} isEmpty : Hashdict a -> Bool diff --git a/tests/Test/Tools/Hashdict.elm b/tests/Test/Tools/Hashdict.elm index 45a21e0..500503c 100644 --- a/tests/Test/Tools/Hashdict.elm +++ b/tests/Test/Tools/Hashdict.elm @@ -1,89 +1,96 @@ module Test.Tools.Hashdict exposing (..) -import Test exposing (..) +import Expect import Fuzz exposing (Fuzzer) import Internal.Tools.Hashdict as Hashdict exposing (Hashdict) -import Test.Values.Event as TestEvent import Internal.Values.Event as Event -import Json.Encode as E 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 toHash fuz = Fuzz.map (Hashdict.fromList toHash) (Fuzz.list fuz) + eventFuzzer : Fuzzer (Hashdict Event.Event) eventFuzzer = fuzzer .eventId TestEvent.fuzzer + suite : Test suite = describe "Hashdict" [ describe "empty" [ test "empty isEmpty" - ( Hashdict.empty identity + (Hashdict.empty identity |> Hashdict.isEmpty |> Expect.equal True |> always ) - , fuzz TestEvent.fuzzer "Nothing is member" + , fuzz TestEvent.fuzzer + "Nothing is member" (\event -> Hashdict.empty .eventId |> Hashdict.member event |> Expect.equal False ) - , fuzz Fuzz.string "No key is member" + , fuzz Fuzz.string + "No key is member" (\key -> Hashdict.empty identity |> Hashdict.memberKey key |> Expect.equal False ) - , fuzz Fuzz.string "Get gets Nothing" + , fuzz Fuzz.string + "Get gets Nothing" (\key -> Hashdict.empty identity |> Hashdict.get key |> Expect.equal Nothing ) , test "Size is zero" - ( Hashdict.empty identity + (Hashdict.empty identity |> Hashdict.size |> Expect.equal 0 |> always ) , test "No keys" - ( Hashdict.empty identity + (Hashdict.empty identity |> Hashdict.keys |> Expect.equal [] |> always ) , test "No values" - ( Hashdict.empty identity + (Hashdict.empty identity |> Hashdict.values |> Expect.equal [] |> always ) , test "To list is []" - ( Hashdict.empty identity + (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.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" + , fuzz (Fuzz.intRange 0 10) + "JSON encode -> JSON decode" (\indent -> Hashdict.empty identity |> Hashdict.encode E.string @@ -93,15 +100,75 @@ suite = |> 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" - [ fuzz2 eventFuzzer (Fuzz.intRange 0 10) "JSON encode -> JSON decode" + [ fuzz2 eventFuzzer + (Fuzz.intRange 0 10) + "JSON encode -> JSON decode" (\hashdict indent -> hashdict |> Hashdict.encode Event.encode |> E.encode indent |> D.decodeString (Hashdict.decoder .eventId Event.decoder) |> Result.map Hashdict.toList - |> Expect.equal ( Ok <| Hashdict.toList hashdict ) + |> Expect.equal (Ok <| Hashdict.toList hashdict) ) ] ]