Skip to content

Commit c17f1cc

Browse files
committed
Add tests for fromFoldableWithIndex
1 parent 0e6ade6 commit c17f1cc

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Add `fromFoldableWithIndex` (#30 by @ptrfrncsmrph)
1011

1112
Bugfixes:
1213

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"devDependencies": {
3737
"purescript-assert": "^6.0.0",
3838
"purescript-minibench": "^4.0.0",
39+
"purescript-ordered-collections": "^3.0.0",
3940
"purescript-quickcheck": "^8.0.1"
4041
}
4142
}

test/Test/Foreign/Object.purs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import Data.FoldableWithIndex (foldlWithIndex, foldrWithIndex, foldMapWithIndex)
1010
import Data.Function (on)
1111
import Data.List as L
1212
import Data.List.NonEmpty as NEL
13+
import Data.Map as M
1314
import Data.Maybe (Maybe(..), fromMaybe)
15+
import Data.Profunctor.Strong (first)
1416
import Data.Semigroup.First (First(..))
1517
import Data.Semigroup.Last (Last(..))
18+
import Data.String (toUpper)
1619
import Data.Traversable (sequence, traverse)
1720
import Data.TraversableWithIndex (traverseWithIndex)
1821
import Data.Tuple (Tuple(..), fst, snd, uncurry)
@@ -150,6 +153,14 @@ objectTests = do
150153
quickCheck (O.lookup "1" nums == Just "one" <?> "invalid lookup - 1")
151154
quickCheck (O.lookup "2" nums == Nothing <?> "invalid lookup - 2")
152155

156+
log "fromFoldableWithIndex & key collision"
157+
do
158+
let numsMap = M.fromFoldable [Tuple 0 "zero", Tuple 1 "what", Tuple 1 "one"]
159+
nums = O.fromFoldableWithIndex show numsMap
160+
quickCheck (O.lookup "0" nums == Just "zero" <?> "invalid lookup - 0")
161+
quickCheck (O.lookup "1" nums == Just "one" <?> "invalid lookup - 1")
162+
quickCheck (O.lookup "2" nums == Nothing <?> "invalid lookup - 2")
163+
153164
log "fromFoldableWith const [] = empty"
154165
quickCheck (O.fromFoldableWith const [] == (O.empty :: O.Object Unit)
155166
<?> "was not empty")
@@ -170,6 +181,15 @@ objectTests = do
170181
let f m1 = O.fromFoldable ((O.toUnfoldable m1) :: L.List (Tuple String Int)) in
171182
O.toUnfoldable (f m) == (O.toUnfoldable m :: L.List (Tuple String Int)) <?> show m
172183

184+
log "fromFoldableWithIndex id = id"
185+
quickCheck $ \(TestObject m :: _ Int) ->
186+
O.fromFoldableWithIndex identity m == identity m <?> show m
187+
188+
log "fromFoldableWithIndex f ~ fromFoldable . map (first f) . toUnfoldable"
189+
quickCheck $ \(TestObject m) ->
190+
O.fromFoldableWithIndex toUpper m ==
191+
O.fromFoldable (first toUpper <$> (O.toUnfoldable m :: L.List (Tuple String Int))) <?> show m
192+
173193
log "fromFoldableWith const = fromFoldable"
174194
quickCheck $ \arr -> O.fromFoldableWith const arr ==
175195
O.fromFoldable (arr :: L.List (Tuple String Int)) <?> show arr

0 commit comments

Comments
 (0)