on master help sign in
Merge pull request #791 from Boshen/add-list-and-dict-tests

Add list and dict tests

commitecaadcf2d4b0fa3f5ec40c5e38d9ad5aa645b7eb authorEvan Czaplicki <info@elm-lang.org> dateSun 21 Sep 2014, 11:26 (12 years ago) parent367d86638f7a80
2 files changed+81−5
tests/libraries/Test/Dict.elm+27−4 tests/libraries/Test/List.elm+54−1
tests/libraries/Test/Dict.elm+27−4
@@ -11,16 +11,39 @@ animals = Dict.fromList [ ("Tom", "cat"), ("Jerry", "mouse") ]
1111
1212tests : Test
1313tests =
14− let getTests =
15− [ test "get 1" <| assertEqual (Just "cat") (Dict.get "Tom" animals)
14+ let buildTests = suite "build Tests"
15+ [ test "empty" <| assertEqual (Dict.fromList []) (Dict.empty)
16+ , test "singleton" <| assertEqual (Dict.fromList [("k","v")]) (Dict.singleton "k" "v")
17+ , test "insert" <| assertEqual (Dict.fromList [("k","v")]) (Dict.insert "k" "v" Dict.empty)
18+ , test "insert replace" <| assertEqual (Dict.fromList [("k","vv")]) (Dict.insert "k" "vv" (Dict.singleton "k" "v"))
19+ , test "update" <| assertEqual (Dict.fromList [("k","vv")]) (Dict.update "k" (\v->Just "vv") (Dict.singleton "k" "v"))
20+ , test "update Nothing" <| assertEqual Dict.empty (Dict.update "k" (\v->Nothing) (Dict.singleton "k" "v"))
21+ , test "remove" <| assertEqual Dict.empty (Dict.remove "k" (Dict.singleton "k" "v"))
22+ , test "remove not found" <| assertEqual (Dict.singleton "k" "v") (Dict.remove "kk" (Dict.singleton "k" "v"))
23+ ]
24+ queryTests = suite "query Tests"
25+ [ test "member 1" <| assertEqual True (Dict.member "Tom" animals)
26+ , test "member 2" <| assertEqual False (Dict.member "Spike" animals)
27+ , test "get 1" <| assertEqual (Just "cat") (Dict.get "Tom" animals)
1628 , test "get 2" <| assertEqual Nothing (Dict.get "Spike" animals)
1729 , test "getOrElse 1" <| assertEqual "mouse" (Dict.getOrElse "dog" "Jerry" animals)
1830 , test "getOrElse 2" <| assertEqual "dog" (Dict.getOrElse "dog" "Spike" animals)
1931 , test "getOrFail" <| assertEqual "cat" (Dict.getOrFail "Tom" animals)
2032 ]
21− filterTests =
33+ combineTests = suite "combine Tests"
34+ [ test "union" <| assertEqual animals (Dict.union (Dict.singleton "Jerry" "mouse") (Dict.singleton "Tom" "cat"))
35+ , test "union collison" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.union (Dict.singleton "Tom" "cat") (Dict.singleton "Tom" "mouse"))
36+ , test "intersect" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.intersect animals (Dict.singleton "Tom" "cat"))
37+ , test "diff" <| assertEqual (Dict.singleton "Jerry" "mouse") (Dict.diff animals (Dict.singleton "Tom" "cat"))
38+ ]
39+ transformTests = suite "transform Tests"
2240 [ test "filter" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.filter (\k v -> k == "Tom") animals)
2341 , test "partition" <| assertEqual (Dict.singleton "Tom" "cat", Dict.singleton "Jerry" "mouse") (Dict.partition (\k v -> k == "Tom") animals)
2442 ]
2543 in
26− suite "Dict Tests" <| List.concat [ getTests, filterTests ]
44+ suite "Dict Tests"
45+ [ buildTests
46+ , queryTests
47+ , combineTests
48+ , transformTests
49+ ]
tests/libraries/Test/List.elm+54−1
@@ -1,6 +1,7 @@
11module Test.List (tests) where
22
33import List
4+import String
45
56import ElmTest.Assertion (..)
67import ElmTest.Test (..)
@@ -12,8 +13,18 @@ falseList = repeat largeNumber False
1213lessThanThree x = x < 3
1314isEven n = n % 2 == 0
1415
16+alice = { name="Alice", height=1.62 }
17+bob = { name="Bob" , height=1.85 }
18+chuck = { name="Chuck", height=1.76 }
19+
20+flippedComparison a b =
21+ case compare a b of
22+ LT -> GT
23+ EQ -> EQ
24+ GT -> LT
25+
1526tests : Test
16−tests =
27+tests =
1728 let partitionTests = suite "partition Tests"
1829 [ test "simple partition" <| assertEqual ([True],[False]) (List.partition identity [False, True])
1930 , test "order check" <| assertEqual ([2,1], [5,6]) (List.partition lessThanThree [2,5,6,1])
@@ -25,13 +36,55 @@ tests =
2536 [ test "unzip doc check" <| assertEqual ([0,17,1337],[True,False,True]) (unzip [(0, True), (17, False), (1337, True)])
2637 , test "unzip stress test" <| assertEqual (trueList, falseList) (unzip (zip trueList falseList))
2738 ]
39+ concatTests = suite "concat Tests"
40+ [ test "concat doc check 1" <| assertEqual [1,2,3,4,5] (concat [[1,2],[3],[4,5]])
41+ , test "concat doc check 2" <| assertEqual "treehouse" (concat ["tree","house"])
42+ ]
43+ joinTests = suite "join Tests"
44+ [ test "join doc check" <| assertEqual "Hawaiian" (join "a" ["H","w","ii","n"])
45+ , test "join list check" <| assertEqual [1,0,2,0,3] (join [0] [[1],[2],[3]])
46+ ]
2847 intersperseTests = suite "intersperse Tests"
2948 [ test "intersperse doc check" <| assertEqual ["turtles","on","turtles","on","turtles"] (intersperse "on" ["turtles","turtles","turtles"])
3049 , test "intersperse stress test" <| assertEqual (tail <| List.concat <| zipWith (\x y -> [x,y]) falseList trueList) (intersperse False trueList)
3150 ]
51+ zipTests = suite "zip Tests"
52+ [ test "zip doc check 1" <| assertEqual [(1,6),(2,7)] (zip [1,2,3] [6,7])
53+ , test "zip doc check 2" <| assertEqual (zipWith (,) [1,2,3] [6,7]) (zip [1,2,3] [6,7])
54+ ]
55+ filterMapTests = suite "filterMap Tests"
56+ [ test "filterMap doc check" <| assertEqual [3,5] (filterMap String.toInt ["3","4.0","5","hats"])
57+ ]
58+ concatMapTests = suite "concatMap Tests"
59+ [ test "simple concatMap check" <| assertEqual [1,1,2,2] (concatMap (repeat 2) [1,2])
60+ ]
61+ indexedMapTests = suite "indexedMap Tests"
62+ [ test "indexedMap doc check" <| assertEqual [(0,"Tom"),(1,"Sue"),(2,"Bob")] (indexedMap (,) ["Tom", "Sue", "Bob"])
63+ ]
64+ sortTests = suite "sort Tests"
65+ [ test "sort doc check" <| assertEqual [1,3,5] (sort [3,1,5])
66+ , test "sort string check" <| assertEqual ["a","c","e"] (sort ["c","a","e"])
67+ ]
68+ sortByTests = suite "sortBy Tests"
69+ [ test "sortBy doc check" <| assertEqual ["cat","mouse"] (sortBy String.length ["mouse","cat"])
70+ , test "sortby derived property check 1" <| assertEqual [alice,bob,chuck] (sortBy .name [chuck,alice,bob])
71+ , test "sortby derived property check 2" <| assertEqual [alice,chuck,bob] (sortBy .height [chuck,alice,bob])
72+ ]
73+ sortWithTests = suite "sortWith Tests"
74+ [ test "sortWith doc check" <| assertEqual [5,4,3,2,1] (sortWith flippedComparison [1..5])
75+ ]
3276 in
3377 suite "List Tests"
3478 [ partitionTests
3579 , unzipTests
80+ , concatTests
81+ , joinTests
3682 , intersperseTests
83+ , zipTests
84+ , filterMapTests
85+ , concatMapTests
86+ , indexedMapTests
87+ , sortTests
88+ , sortByTests
89+ , sortWithTests
3790 ]