Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit aeaa792

Browse files
committed
Add enum tests, convert existing "tests" into assertions
1 parent 52c0d41 commit aeaa792

File tree

2 files changed

+97
-9
lines changed

2 files changed

+97
-9
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"purescript-symbols": "^3.0.0"
2020
},
2121
"devDependencies": {
22+
"purescript-assert": "^3.0.0",
2223
"purescript-console": "^3.0.0"
2324
}
2425
}

test/Main.purs

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
module Test.Main where
22

33
import Prelude
4+
45
import Control.Monad.Eff (Eff)
5-
import Control.Monad.Eff.Console (CONSOLE, logShow)
6+
import Control.Monad.Eff.Console (CONSOLE, log, logShow)
7+
import Data.Enum (class BoundedEnum, class Enum, Cardinality(..), cardinality, fromEnum, pred, succ, toEnum)
68
import Data.Generic.Rep as G
9+
import Data.Generic.Rep.Bounded as GBounded
10+
import Data.Generic.Rep.Enum as GEnum
711
import Data.Generic.Rep.Eq as GEq
812
import Data.Generic.Rep.Ord as GOrd
913
import Data.Generic.Rep.Show as GShow
10-
import Data.Generic.Rep.Bounded as GBounded
14+
import Data.Maybe (Maybe(..))
15+
import Test.Assert (ASSERT, assert)
1116

1217
data List a = Nil | Cons { head :: a, tail :: List a }
1318

@@ -36,16 +41,98 @@ instance showSimpleBounded :: Show SimpleBounded where
3641
instance boundedSimpleBounded :: Bounded SimpleBounded where
3742
bottom = GBounded.genericBottom
3843
top = GBounded.genericTop
44+
instance enumSimpleBounded :: Enum SimpleBounded where
45+
pred = GEnum.genericPred
46+
succ = GEnum.genericSucc
47+
instance boundedEnumSimpleBounded :: BoundedEnum SimpleBounded where
48+
cardinality = GEnum.genericCardinality
49+
toEnum = GEnum.genericToEnum
50+
fromEnum = GEnum.genericFromEnum
51+
52+
data Option a = None | Some a
53+
derive instance genericOption :: G.Generic (Option a) _
54+
instance eqOption :: Eq a => Eq (Option a) where
55+
eq x y = GEq.genericEq x y
56+
instance ordOption :: Ord a => Ord (Option a) where
57+
compare x y = GOrd.genericCompare x y
58+
instance showOption :: Show a => Show (Option a) where
59+
show x = GShow.genericShow x
60+
instance boundedOption :: Bounded a => Bounded (Option a) where
61+
bottom = GBounded.genericBottom
62+
top = GBounded.genericTop
63+
instance enumOption :: (Bounded a, Enum a) => Enum (Option a) where
64+
pred = GEnum.genericPred
65+
succ = GEnum.genericSucc
66+
instance boundedEnumOption :: BoundedEnum a => BoundedEnum (Option a) where
67+
cardinality = GEnum.genericCardinality
68+
toEnum = GEnum.genericToEnum
69+
fromEnum = GEnum.genericFromEnum
3970

40-
main :: Eff (console :: CONSOLE) Unit
71+
main :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
4172
main = do
4273
logShow (cons 1 (cons 2 Nil))
4374

44-
logShow (cons 1 (cons 2 Nil) == cons 1 (cons 2 Nil))
45-
logShow (cons 1 (cons 2 Nil) == cons 1 Nil)
75+
log "Checking equality"
76+
assert $ cons 1 (cons 2 Nil) == cons 1 (cons 2 Nil)
77+
78+
log "Checking inequality"
79+
assert $ cons 1 (cons 2 Nil) /= cons 1 Nil
80+
81+
log "Checking comparison EQ"
82+
assert $ (cons 1 (cons 2 Nil) `compare` cons 1 (cons 2 Nil)) == EQ
83+
84+
log "Checking comparison GT"
85+
assert $ (cons 1 (cons 2 Nil) `compare` cons 1 Nil) == GT
86+
87+
log "Checking comparison LT"
88+
assert $ (cons 1 Nil `compare` cons 1 (cons 2 Nil)) == LT
89+
90+
log "Checking simple bottom"
91+
assert $ bottom == A
92+
93+
log "Checking simple top"
94+
assert $ top == D
95+
96+
log "Checking composite bottom"
97+
assert $ bottom == None :: Option SimpleBounded
98+
99+
log "Checking composite top"
100+
assert $ top == Some D
101+
102+
log "Checking simple pred bottom"
103+
assert $ pred (bottom :: SimpleBounded) == Nothing
104+
105+
log "Checking simple (pred =<< succ bottom)"
106+
assert $ (pred =<< succ bottom) == Just A
107+
108+
log "Checking simple succ top"
109+
assert $ succ (top :: SimpleBounded) == Nothing
110+
111+
log "Checking simple (succ =<< pred top)"
112+
assert $ (succ =<< pred top) == Just D
113+
114+
log "Checking composite pred bottom"
115+
assert $ pred (bottom :: Option SimpleBounded) == Nothing
116+
117+
log "Checking composite (pred =<< succ bottom)"
118+
assert $ (pred =<< succ (bottom :: Option SimpleBounded)) == Just None
119+
120+
log "Checking composite succ top"
121+
assert $ succ (top :: Option SimpleBounded) == Nothing
122+
123+
log "Checking composite (succ =<< pred top)"
124+
assert $ (succ =<< pred top) == Just (Some D)
125+
126+
log "Checking simple cardinality"
127+
assert $ (cardinality :: Cardinality SimpleBounded) == Cardinality 4
128+
129+
log "Checking composite cardinality"
130+
assert $ (cardinality :: Cardinality (Option SimpleBounded)) == Cardinality 5
46131

47-
logShow (cons 1 (cons 2 Nil) `compare` cons 1 (cons 2 Nil))
48-
logShow (cons 1 (cons 2 Nil) `compare` cons 1 Nil)
132+
log "Checking simple toEnum/fromEnum roundtrip"
133+
assert $ toEnum (fromEnum A) == Just A
134+
assert $ toEnum (fromEnum B) == Just B
49135

50-
logShow (bottom :: SimpleBounded)
51-
logShow (top :: SimpleBounded)
136+
log "Checking composite toEnum/fromEnum roundtrip"
137+
assert $ toEnum (fromEnum (None :: Option SimpleBounded)) == Just (None :: Option SimpleBounded)
138+
assert $ toEnum (fromEnum (Some A)) == Just (Some A)

0 commit comments

Comments
 (0)