Skip to content

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gampleman committed Jul 26, 2024
1 parent 20816c5 commit 6331751
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/Dict/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ insertCombining combine key value dict =
import Dict
Dict.fromList [ ( "expenses", 38.25 ), ( "assets", 100.85 ) ]
|> updateIfExists "expenses" ((+) 2.50)
|> updateIfExists "liabilities" ((-) 2.50)
|> updateIfExists "expenses" (\amount -> amount + 2.50)
|> updateIfExists "liabilities" (\amount -> amount - 2.50)
--> Dict.fromList [ ( "expenses", 40.75 ), ( "assets", 100.85 ) ]
-}
Expand All @@ -274,8 +274,8 @@ updateIfExists key f dict =
import Dict
Dict.fromList [ ( "expenses", 38.25 ), ( "assets", 100.85 ) ]
|> upsert "expenses" 4.50 ((+) 2.50)
|> upsert "liabilities" 2.50 ((-) 2.50)
|> upsert "expenses" 4.50 (\amount -> amount + 2.50)
|> upsert "liabilities" 2.50 (\amount -> amount - 2.50)
--> Dict.fromList [ ( "expenses", 40.75 ), ( "assets", 100.85 ), ( "liabilities", 2.50 ) ]
-}
Expand Down
44 changes: 44 additions & 0 deletions tests/DictTests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module DictTests exposing (suite)

import Dict
import Dict.Extra
import Expect
import Fuzz exposing (Fuzzer)
import Set
import Set.Extra
import Test exposing (Test, describe)


dictFuzzer : Fuzzer comparable -> Fuzzer v -> Fuzzer (Dict.Dict comparable v)
dictFuzzer keyFuzzer valueFuzzer =
Fuzz.map Dict.fromList (Fuzz.list (Fuzz.pair keyFuzzer valueFuzzer))


suite : Test
suite =
describe "Dict.Extra"
[ describe "invertAll"
[ Test.fuzz (dictFuzzer Fuzz.string Fuzz.int) "does not loose information" <|
\dict ->
dict
|> Dict.Extra.invertAll
|> Dict.map (always Set.size)
|> Dict.values
|> List.sum
|> Expect.equal (Dict.size dict)
, Test.fuzz (dictFuzzer Fuzz.string Fuzz.int) "the values are keys into the original dictionary" <|
\dict ->
dict
|> Dict.Extra.invertAll
|> Dict.Extra.all
(\key vals ->
Set.Extra.all (\val -> Dict.get val dict == Just key) vals
)
|> Expect.equal True
, Test.fuzz (Fuzz.map Dict.Extra.invert (dictFuzzer Fuzz.string Fuzz.int)) "behaves the same as invert on an already inverted dictionary" <|
\dict ->
dict
|> Dict.Extra.invertAll
|> Expect.equal (dict |> Dict.Extra.invert |> Dict.map (always Set.singleton))
]
]

0 comments on commit 6331751

Please sign in to comment.