Skip to content

Commit bfc24f9

Browse files
authored
Add additional unit tests for functorTree, foldableTree, and traversableTree - sequence (#206)
1 parent 47ef8bc commit bfc24f9

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

exercises/chapter7/test/Main.purs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import Control.Monad.Writer (runWriter, tell)
77
import Data.AddressBook (PhoneType(..), address, phoneNumber)
88
import Data.Array ((..))
99
import Data.Either (Either(..))
10+
import Data.Foldable (foldl, foldr, foldMap)
1011
import Data.Int (fromNumber)
1112
import Data.List (List(..), (:))
1213
import Data.Maybe (Maybe(..))
1314
import Data.String.Regex as R
14-
import Data.Traversable (traverse)
15+
import Data.Traversable (sequence, traverse)
1516
import Data.Tuple (snd)
1617
import Data.Validation.Semigroup (invalid)
1718
import Effect (Effect)
@@ -154,16 +155,39 @@ Note to reader: Delete this line to expand comment block -}
154155
let
155156
leaf :: forall a. a -> Tree a
156157
leaf x = Branch Leaf x Leaf
158+
intTree :: Tree Int
159+
intTree = Branch (Branch (leaf 1) 2 (leaf 3)) 4 (Branch (leaf 5) 6 (leaf 7))
157160
suite "Exercise - traverse" do
161+
suite "Functor Tree" do
162+
test "Functor - map" do
163+
Assert.equal
164+
(Branch (Branch (leaf "1") "2" (leaf "3")) "4" (Branch (leaf "5") "6" (leaf "7")))
165+
$ map show intTree
166+
suite "Foldable Tree" do
167+
test "Foldable - foldr" do
168+
Assert.equal "1234567"
169+
$ foldr (\x acc -> show x <> acc) "" intTree
170+
test "Foldable - foldl" do
171+
Assert.equal "7654321"
172+
$ foldl (\acc x -> show x <> acc) "" intTree
173+
test "Foldable - foldMap" do
174+
Assert.equal "1234567"
175+
$ foldMap (\x -> show x) intTree
158176
suite "Maybe side-effect" do
159-
test "Just" do
177+
test "Just - traverse" do
160178
Assert.equal (Just $ Branch (leaf 1) 2 (leaf 3))
161179
$ traverse fromNumber
162180
$ Branch (leaf 1.0) 2.0 (leaf 3.0)
163-
test "Nothing" do
181+
test "Just - sequence" do
182+
Assert.equal (Just $ Branch (leaf 1) 2 (leaf 3))
183+
$ sequence $ Branch (leaf $ Just 1) (Just 2) (leaf $ Just 3)
184+
test "Nothing - traverse" do
164185
Assert.equal Nothing
165186
$ traverse fromNumber
166187
$ Branch (leaf 1.0) 2.0 (leaf 3.7)
188+
test "Nothing - sequence" do
189+
Assert.equal Nothing
190+
$ sequence $ Branch (leaf $ Nothing) (Just 2) (leaf $ Just 3)
167191
test "Array side-effect - check traversal order" do
168192
Assert.equal (1 .. 7)
169193
$ snd

0 commit comments

Comments
 (0)