/
Assertions.purs
146 lines (132 loc) · 2.81 KB
/
Assertions.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
module Test.Spec.Assertions
( AnyShow(..)
, expectError
, fail
, shouldContain
, shouldEqual
, shouldNotContain
, shouldNotEqual
, shouldNotReturn
, shouldNotSatisfy
, shouldReturn
, shouldSatisfy
)
where
import Prelude
import Control.Monad.Error.Class (class MonadError, class MonadThrow, throwError, try)
import Data.Either (Either(..))
import Data.Foldable (class Foldable, notElem, elem)
import Data.Newtype (class Newtype)
import Effect.Exception (Error, error)
fail :: forall m. MonadThrow Error m => String -> m Unit
fail = throwError <<< error
foreign import unsafeStringify :: forall a. a -> String
-- | A newtype with an unsafe `Show` instance for any type.
-- | Useful if you want to test a type for which you cannot provide a `Show` instance.
-- | Usage:
-- | ```purescript
-- | (AnyShow $ MyInt 3) `A.shouldEqual` (AnyShow $ MyInt 3)
-- | ```
newtype AnyShow a = AnyShow a
instance Newtype (AnyShow a) a
derive newtype instance (Eq a) => Eq (AnyShow a)
instance Show (AnyShow a) where
show = unsafeStringify
shouldEqual
:: forall m t
. MonadThrow Error m
=> Show t
=> Eq t
=> t
-> t
-> m Unit
shouldEqual v1 v2 =
when (v1 /= v2) $
fail $ show v1 <> " ≠ " <> show v2
shouldNotEqual
:: forall m t
. MonadThrow Error m
=> Show t
=> Eq t
=> t
-> t
-> m Unit
shouldNotEqual v1 v2 =
when (v1 == v2) $
fail $ show v1 <> " = " <> show v2
shouldSatisfy
:: forall m t
. MonadThrow Error m
=> Show t
=> t
-> (t -> Boolean)
-> m Unit
shouldSatisfy v pred =
unless (pred v) $
fail $ show v <> " doesn't satisfy predicate"
shouldNotSatisfy
:: forall m t
. MonadThrow Error m
=> Show t
=> t
-> (t -> Boolean)
-> m Unit
shouldNotSatisfy v pred =
when (pred v) $
fail $ show v <> " satisfies predicate, but should not"
shouldContain
:: forall m f a
. MonadThrow Error m
=> Show a
=> Eq a
=> Show (f a)
=> Foldable f
=> f a
-> a
-> m Unit
shouldContain c e =
when (e `notElem` c) $
fail $ (show e) <> " ∉ " <> (show c)
shouldNotContain
:: forall m f a
. MonadThrow Error m
=> Show a
=> Eq a
=> Show (f a)
=> Foldable f
=> f a
-> a
-> m Unit
shouldNotContain c e =
when (e `elem` c) $
fail $ (show e) <> " ∈ " <> (show c)
expectError
:: forall m t
. MonadError Error m
=> m t
-> m Unit
expectError a = do
e <- try a
case e of
Left _ -> pure unit
Right _ -> throwError $ error "Expected error"
-- | Asserts that `m t` returns `t`
shouldReturn
:: forall m t
. MonadThrow Error m
=> Eq t
=> Show t
=> m t
-> t
-> m Unit
shouldReturn ft t = ft >>= (_ `shouldEqual` t)
-- | Asserts that `m t` does not return `t`
shouldNotReturn
:: forall m t
. MonadThrow Error m
=> Eq t
=> Show t
=> m t
-> t
-> m Unit
shouldNotReturn ft t = ft >>= (_ `shouldNotEqual` t)