-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement all the dict extra functions #7
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95e7e77
Implement all the dict extra functions
dawehner 966183d
Fix documentation
dawehner b1fb8bd
Adapt groupBy/fromListBy to be more like the DictExtra variant
dawehner 964c128
Copy the DictExtra tests
dawehner 96d3d97
Expose Set.Set so the code is a bit better to read
dawehner d3156e0
run elm-format
dawehner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
module DictExtraPackageTests exposing (tests) | ||
|
||
import Test.Runner.Node exposing (run, TestProgram) | ||
import Test exposing (Test, describe, test, fuzz, fuzz2) | ||
import Fuzz exposing (Fuzzer, intRange) | ||
import Expect | ||
import Json.Encode exposing (Value) | ||
import Dict | ||
import DictList exposing (..) | ||
import Set | ||
|
||
|
||
tests : Test | ||
tests = | ||
describe "Dict tests" | ||
[ groupByTests | ||
, fromListByTests | ||
, removeWhenTests | ||
, removeManyTests | ||
, keepOnlyTests | ||
, mapKeysTests | ||
] | ||
|
||
|
||
|
||
-- groupBy | ||
|
||
|
||
groupByTests : Test | ||
groupByTests = | ||
describe "groupBy" | ||
[ test "example" <| | ||
\() -> | ||
DictList.toList (groupBy .id [ mary, jack, jill ]) | ||
|> Expect.equal [ ( 1, [ mary, jill ] ), ( 2, [ jack ] ) ] | ||
] | ||
|
||
|
||
type alias GroupByData = | ||
{ id : Int | ||
, name : String | ||
} | ||
|
||
|
||
mary : GroupByData | ||
mary = | ||
GroupByData 1 "Mary" | ||
|
||
|
||
jack : GroupByData | ||
jack = | ||
GroupByData 2 "Jack" | ||
|
||
|
||
jill : GroupByData | ||
jill = | ||
GroupByData 1 "Jill" | ||
|
||
|
||
|
||
-- fromListBy | ||
|
||
|
||
fromListByTests : Test | ||
fromListByTests = | ||
describe "fromListBy" | ||
[ test "example" <| | ||
\() -> | ||
fromListBy .id [ jack, jill ] | ||
|> Expect.equal (DictList.fromList [ ( 2, jack ), ( 1, jill ) ]) | ||
, test "replacement" <| | ||
\() -> | ||
fromListBy .id [ jack, jill, mary ] | ||
|> Expect.equal (DictList.fromList [ ( 2, jack ), ( 1, mary ) ]) | ||
] | ||
|
||
|
||
|
||
-- removeWhen | ||
|
||
|
||
removeWhenTests : Test | ||
removeWhenTests = | ||
describe "removeWhen" | ||
[ test "example" <| | ||
\() -> | ||
removeWhen (\_ v -> v == 1) (DictList.fromList [ ( "Mary", 1 ), ( "Jack", 2 ), ( "Jill", 1 ) ]) | ||
|> Expect.equal (DictList.fromList [ ( "Jack", 2 ) ]) | ||
] | ||
|
||
|
||
|
||
-- removeMany | ||
|
||
|
||
removeManyTests : Test | ||
removeManyTests = | ||
describe "removeMany" | ||
[ test "example" <| | ||
\() -> | ||
removeMany (Set.fromList [ "Mary", "Jill" ]) (DictList.fromList [ ( "Mary", 1 ), ( "Jack", 2 ), ( "Jill", 1 ) ]) | ||
|> Expect.equal (DictList.fromList [ ( "Jack", 2 ) ]) | ||
] | ||
|
||
|
||
|
||
-- keepOnly | ||
|
||
|
||
keepOnlyTests : Test | ||
keepOnlyTests = | ||
describe "keepOnly" | ||
[ test "example" <| | ||
\() -> | ||
keepOnly (Set.fromList [ "Jack", "Jill" ]) (DictList.fromList [ ( "Mary", 1 ), ( "Jack", 2 ), ( "Jill", 1 ) ]) | ||
|> Expect.equal (DictList.fromList [ ( "Jack", 2 ), ( "Jill", 1 ) ]) | ||
] | ||
|
||
|
||
|
||
-- mapKeys | ||
|
||
|
||
mapKeysTests : Test | ||
mapKeysTests = | ||
describe "mapKeys" | ||
[ test "example" <| | ||
\() -> | ||
mapKeys ((+) 1) (DictList.fromList [ ( 1, "Jack" ), ( 2, "Jill" ) ]) | ||
|> Expect.equal (DictList.fromList [ ( 2, "Jack" ), ( 3, "Jill" ) ]) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
module DictExtraTests exposing (tests) | ||
|
||
import Fuzz exposing (Fuzzer) | ||
import DictList exposing (..) | ||
import Expect | ||
import Test exposing (..) | ||
import Set | ||
|
||
|
||
{-| Fuzz a DictList, given a fuzzer for the keys and values. | ||
-} | ||
fuzzDictList : Fuzzer comparable -> Fuzzer value -> Fuzzer (DictList comparable value) | ||
fuzzDictList fuzzKey fuzzValue = | ||
Fuzz.tuple ( fuzzKey, fuzzValue ) | ||
|> Fuzz.list | ||
|> Fuzz.map DictList.fromList | ||
|
||
|
||
fuzzIntList : Fuzzer (List Int) | ||
fuzzIntList = | ||
Fuzz.list Fuzz.int | ||
|
||
|
||
threeElementList = | ||
(fromList [ ( 1, 1 ), ( 2, 2 ), ( 3, 3 ) ]) | ||
|
||
|
||
tests = | ||
describe "List Tests" | ||
[ dictExtraUnitTests | ||
, dictExtraFuzzTests | ||
] | ||
|
||
|
||
dictExtraUnitTests : Test | ||
dictExtraUnitTests = | ||
describe "Dict Extra Unittests" | ||
[ describe "groupBy" | ||
[ test "empty" <| \() -> Expect.equal (groupBy identity []) empty | ||
, test "always equal elements" <| \() -> Expect.equal (groupBy (always 1) [ 1, 2, 3 ]) (fromList [ ( 1, [ 1, 2, 3 ] ) ]) | ||
, test "map to original key" <| \() -> Expect.equal (groupBy identity [ 1, 2, 3 ]) (fromList [ ( 3, [ 3 ] ), ( 2, [ 2 ] ), ( 1, [ 1 ] ) ]) | ||
, test "odd-even" <| \() -> Expect.equal (groupBy (\v -> v % 2) [ 1, 2, 3 ]) (fromList [ ( 1, [ 1, 3 ] ), ( 0, [ 2 ] ) ]) | ||
] | ||
, describe "fromListBy" | ||
[ test "empty" <| \() -> Expect.equal (fromListBy identity []) empty | ||
, test "simple list" <| \() -> Expect.equal (fromListBy (\v -> v + 1) [ 1, 2, 3 ]) (fromList [ ( 2, 1 ), ( 3, 2 ), ( 4, 3 ) ]) | ||
] | ||
, describe "removeWhen" | ||
[ test "empty" <| \() -> Expect.equal (removeWhen (\k v -> True) empty) empty | ||
, test "remove all" <| \() -> Expect.equal (removeWhen (\k v -> True) (fromList [ ( 1, 1 ), ( 2, 2 ), ( 3, 3 ) ])) empty | ||
, test "remove none" <| \() -> Expect.equal (removeWhen (\k v -> False) (fromList [ ( 1, 1 ), ( 2, 2 ), ( 3, 3 ) ])) (fromList [ ( 1, 1 ), ( 2, 2 ), ( 3, 3 ) ]) | ||
] | ||
, describe "removeMany" | ||
[ test "empty" <| \() -> Expect.equal (removeMany (Set.fromList [ 1, 2 ]) empty) empty | ||
, test "remove none element" <| \() -> Expect.equal (removeMany (Set.fromList [ 4 ]) threeElementList) threeElementList | ||
, test "remove one element" <| \() -> Expect.equal (removeMany (Set.fromList [ 1 ]) threeElementList) (DictList.filter (\k v -> k /= 1) threeElementList) | ||
, test "remove two elements" <| \() -> Expect.equal (removeMany (Set.fromList [ 1, 2 ]) threeElementList) (DictList.filter (\k v -> k == 3) threeElementList) | ||
, test "remove all elements" <| \() -> Expect.equal (removeMany (Set.fromList [ 1, 2, 3 ]) threeElementList) empty | ||
] | ||
, describe "keepOnly" | ||
[ test "empty" <| \() -> Expect.equal (keepOnly (Set.fromList [ 1, 2 ]) empty) empty | ||
, test "keep none element" <| \() -> Expect.equal (removeMany (Set.fromList [ 4 ]) threeElementList) threeElementList | ||
, test "keep one element" <| \() -> Expect.equal (keepOnly (Set.fromList [ 1 ]) threeElementList) (DictList.filter (\k v -> k == 1) threeElementList) | ||
, test "keep two elements" <| \() -> Expect.equal (keepOnly (Set.fromList [ 1, 2 ]) threeElementList) (DictList.filter (\k v -> k /= 3) threeElementList) | ||
, test "keep all elements" <| \() -> Expect.equal (keepOnly (Set.fromList [ 1, 2, 3 ]) threeElementList) threeElementList | ||
] | ||
, describe "mapKeys" | ||
[ test "empty" <| \() -> Expect.equal (mapKeys toString empty) empty | ||
, test "toString mapping" <| \() -> Expect.equal (mapKeys toString threeElementList) (fromList [ ( "1", 1 ), ( "2", 2 ), ( "3", 3 ) ]) | ||
] | ||
] | ||
|
||
|
||
dictExtraFuzzTests : Test | ||
dictExtraFuzzTests = | ||
-- @TODO Expand the fuzz tests | ||
describe "Dict extra fuzz tests" | ||
[ fuzz fuzzIntList "groupBy (total length doesn't change)" <| | ||
\subject -> | ||
Expect.equal (List.length subject) | ||
(groupBy (\v -> v % 2) subject | ||
|> toList | ||
|> List.map (\( k, v ) -> List.length v) | ||
|> List.foldr (+) 0 | ||
) | ||
, fuzz fuzzIntList "groupBy (no elements dissapear)" <| | ||
\subject -> | ||
Expect.equal | ||
(Set.diff (Set.fromList subject) | ||
(Set.fromList | ||
(groupBy (\v -> v % 2) subject | ||
|> toList | ||
|> List.foldr (\( k, v ) agg -> List.append v agg) [] | ||
) | ||
) | ||
) | ||
(Set.fromList []) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty good. I'd also suggest having a separate test file that copies https://github.com/elm-community/dict-extra/blob/1.3.2/tests/Main.elm and makes whatever the smallest changes are needed to match our context here. Basically, to prove that we pass
Dict.Extra
's own tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done that.