Skip to content

Commit 9f6facc

Browse files
Ch 7 Tests (#147)
1 parent fc916c3 commit 9f6facc

File tree

4 files changed

+197
-13
lines changed

4 files changed

+197
-13
lines changed

exercises/chapter7/spago.dhall

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
Welcome to a Spago project!
33
You can edit this file as you like.
44
-}
5-
{ name =
6-
"my-project"
5+
{ name = "my-project"
76
, dependencies =
8-
[ "console", "effect", "psci-support", "strings", "validation" ]
9-
, packages =
10-
./packages.dhall
11-
, sources =
12-
[ "src/**/*.purs", "test/**/*.purs" ]
7+
[ "console", "effect", "psci-support", "strings", "test-unit", "validation" ]
8+
, packages = ./packages.dhall
9+
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
1310
}

exercises/chapter7/src/Data/AddressBook.purs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ data PhoneType
1717
| CellPhone
1818
| OtherPhone
1919

20+
{-| derive has not been discussed yet but will be
21+
covered in Ch 10. Here it is needed by the unit
22+
tests to define how to compare the PhoneType values
23+
(HomePhone, WorkPhone, etc).
24+
-}
25+
derive instance eqPhoneType :: Eq PhoneType
26+
2027
instance showPhoneType :: Show PhoneType where
2128
show HomePhone = "HomePhone"
2229
show WorkPhone = "WorkPhone"

exercises/chapter7/test/Main.purs

Lines changed: 179 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,185 @@
11
module Test.Main where
22

33
import Prelude
4-
54
import Effect (Effect)
6-
import Effect.Class.Console (logShow)
7-
8-
import Data.AddressBook (examplePerson)
9-
import Data.AddressBook.Validation (validatePerson)
5+
import Test.Unit (suite, test)
6+
import Test.Unit.Assert as Assert
7+
import Test.Unit.Main (runTest)
108

119
main :: Effect Unit
12-
main = logShow (validatePerson examplePerson)
10+
main =
11+
runTest do
12+
suite "Verify unit tests are set up" do
13+
test "true eq true"
14+
$ Assert.equal true
15+
$ true
16+
17+
{- Move this block comment starting point to enable more tests
18+
suite "Exercise Group 1" do
19+
suite "Exercise 1: Use lift2 to write lifted versions of numeric operators" do
20+
test "+ (Just)"
21+
$ Assert.equal (Just 5)
22+
$ (+)
23+
<$> (Just 2)
24+
<*> (Just 3)
25+
test "+ (Nothing on left)"
26+
$ Assert.equal Nothing
27+
$ (+)
28+
<$> Nothing
29+
<*> (Just 3)
30+
test "+ (Nothing on right)"
31+
$ Assert.equal Nothing
32+
$ (+)
33+
<$> (Just 2)
34+
<*> Nothing
35+
test "- (Just)"
36+
$ Assert.equal (Just (-1))
37+
$ (-)
38+
<$> (Just 2)
39+
<*> (Just 3)
40+
test "- (Nothing on left)"
41+
$ Assert.equal Nothing
42+
$ (-)
43+
<$> Nothing
44+
<*> (Just 3)
45+
test "- (Nothing on right)"
46+
$ Assert.equal Nothing
47+
$ (-)
48+
<$> (Just 2)
49+
<*> Nothing
50+
test "* (Just)"
51+
$ Assert.equal (Just 6)
52+
$ (*)
53+
<$> (Just 2)
54+
<*> (Just 3)
55+
test "* (Nothing on left)"
56+
$ Assert.equal Nothing
57+
$ (*)
58+
<$> Nothing
59+
<*> (Just 3)
60+
test "* (Nothing on right)"
61+
$ Assert.equal Nothing
62+
$ (*)
63+
<$> (Just 2)
64+
<*> Nothing
65+
test "/ (Just)"
66+
$ Assert.equal (Just 2)
67+
$ (/)
68+
<$> (Just 6)
69+
<*> (Just 3)
70+
test "/ (Nothing on left)"
71+
$ Assert.equal Nothing
72+
$ (/)
73+
<$> Nothing
74+
<*> (Just 3)
75+
test "/ (Nothing on right)"
76+
$ Assert.equal Nothing
77+
$ (/)
78+
<$> (Just 2)
79+
<*> Nothing
80+
suite "Convince yourself that the definition of lift3 type checks" do
81+
test "Substituting an Integer type for any of the strings fails to compile"
82+
$ Assert.assert "Manually compiled and manually verifed compiling failed"
83+
$ true
84+
suite "Write a function combineMaybe" do
85+
suite "Applicative Array Int" do
86+
test "Just"
87+
$ Assert.equal ([ Just 1, Just 2, Just 3 ])
88+
$ combineMaybe (Just $ [ 1, 2, 3 ])
89+
test "Nothing"
90+
$ Assert.equal ([ Nothing ])
91+
$ combineMaybe (Nothing :: Maybe (Array Int))
92+
suite "Applicative List Char" do
93+
test "Just"
94+
$ Assert.equal (Just 'a' : Just 'b' : Just 'c' : Nil)
95+
$ combineMaybe (Just $ 'a' : 'b' : 'c' : Nil)
96+
test "Nothing"
97+
$ Assert.equal (Nothing : Nil)
98+
$ combineMaybe (Nothing :: Maybe (List Char))
99+
suite "Exercise Group 2" do
100+
let
101+
addr = address "22 Fake St" "Fake City" "CA"
102+
suite "Regex validator for state code to be two all-caps letters" do
103+
test "Passes validation" do
104+
Assert.equal (pure addr)
105+
$ validateAddressRegex addr
106+
suite "Fails validation" do
107+
let
108+
fail = invalid ([ "Field 'State' did not match the required format" ])
109+
test "Too few letters"
110+
$ Assert.equal fail
111+
$ validateAddressRegex (address "22 Fake St" "Fake City" "C")
112+
test "Too many letters"
113+
$ Assert.equal fail
114+
$ validateAddressRegex (address "22 Fake St" "Fake City" "CAA")
115+
test "Contains non-letters"
116+
$ Assert.equal fail
117+
$ validateAddressRegex (address "22 Fake St" "Fake City" "C3")
118+
test "Not all caps"
119+
$ Assert.equal fail
120+
$ validateAddressRegex (address "22 Fake St" "Fake City" "Ca")
121+
suite "Regex validator to not allow only whitespace" do
122+
test "Passes validation with no leading or trailing whitespace" do
123+
Assert.equal (pure addr)
124+
$ validateAddressRegex' addr
125+
suite "Passes validation with leading and trailing whitespace" do
126+
let
127+
addr' = address "22 Fake St" " Fake City " "CA"
128+
test "Leading and trailing whitespace"
129+
$ Assert.equal (pure addr')
130+
$ validateAddressRegex' addr'
131+
suite "Fails validation" do
132+
let
133+
fail = invalid ([ "Field 'City' did not match the required format" ])
134+
test "Empty string"
135+
$ Assert.equal fail
136+
$ validateAddressRegex' (address "22 Fake St" "" "CA")
137+
test "One space character"
138+
$ Assert.equal fail
139+
$ validateAddressRegex' (address "22 Fake St" " " "CA")
140+
test "One tab character"
141+
$ Assert.equal fail
142+
$ validateAddressRegex' (address "22 Fake St" "\t" "CA")
143+
suite "Exercise Group 3" do
144+
suite "Exercise 1 - Write a Traversable instance for a binary tree structure" do
145+
test "TODO - 'empty' test which passes"
146+
$ Assert.equal true
147+
$ true
148+
suite "Exercise 2 - Verify possible maybe for person's address field" do
149+
test "Just Address" do
150+
let
151+
examplePerson =
152+
person' "John" "Smith"
153+
(Just $ address "123 Fake St." "FakeTown" "CA")
154+
[ phoneNumber HomePhone "555-555-5555"
155+
, phoneNumber CellPhone "555-555-0000"
156+
]
157+
Assert.equal (pure examplePerson)
158+
$ validatePersonWithMaybeAddress examplePerson
159+
test "Nothing" do
160+
let
161+
examplePerson =
162+
person' "John" "Smith"
163+
Nothing
164+
[ phoneNumber HomePhone "555-555-5555"
165+
, phoneNumber CellPhone "555-555-0000"
166+
]
167+
Assert.equal (pure examplePerson)
168+
$ validatePersonWithMaybeAddress examplePerson
169+
test "'Just Address' when city is empty" do
170+
Assert.equal (invalid ([ "Field 'City' cannot be empty" ]))
171+
$ validatePersonWithMaybeAddress
172+
$ person' "John" "Smith"
173+
(Just $ address "123 Fake St." "" "CA")
174+
[ phoneNumber HomePhone "555-555-5555"
175+
, phoneNumber CellPhone "555-555-0000"
176+
]
177+
suite "Exercise 3a - Write 'sequence' in terms of 'traverse'" do
178+
test "TODO - 'empty' test which passes"
179+
$ Assert.equal true
180+
$ true
181+
suite "Exercise 3b - Write 'traverse' in terms of 'sequence'" do
182+
test "TODO - 'empty' test which passes"
183+
$ Assert.equal true
184+
$ true
185+
-}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Test.Solutions where
2+
3+
import Prelude
4+
5+
data Tree a
6+
= Leaf
7+
| Branch (Tree a) a (Tree a)

0 commit comments

Comments
 (0)