-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAssertions.hs
More file actions
175 lines (136 loc) · 6.01 KB
/
Copy pathAssertions.hs
File metadata and controls
175 lines (136 loc) · 6.01 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{-# LANGUAGE GADTs #-}
{-|
@json-assertions@ is a library for validating that your JSON encoding matches
what you are actually expecting. It does this by providing you with a DSL to
traverse a JSON document at the same time as you traverse the value that was
encoded. As you traverse the JSON document, you are building up assertions (by
asserting that you expect certain keys and array indices to exist), and you can
also add your own assertions to check the contents of object properties.
'JSONTest' is an indexed monad, so you will need to enable @RebindableSyntax@
and bring indexed monadic bind into scope:
> {-# LANGUAGE RebindableSyntax #-}
> import Prelude hiding (Monad(..))
> import Control.Monad.Indexed ((>>>=), ireturn)
> import Test.JSON.Assertions
> import Data.Aeson
>
> return :: a -> JSONTest i i a
> return = ireturn
>
> (>>=) :: m i j a -> (a -> m j k b) -> m i k b
> (>>=) = (>>>=)
You can now write tests as an action in the 'JSONTest' monad. The first index
is the type of the object you wish to encode, and the second parameter is the
type that the test ends in. For example, consider the following:
> data Person = Person { personName :: String }
> instance ToJSON Person where
> toJSON p = object [ "name" .= personName p ]
We can write a test to check that the JSON encoding of a @Person@'s name is
correct:
> personTest :: JSONTest Person String String
> personTest = do
> expectedName <- key "name"
> assertEq expectedName
For more information, you may wish to read <http://ocharles.org.uk/blog/posts/2013-11-24-using-indexed-free-monads-to-quickcheck-json.html>.
-}
module Test.JSON.Assertions
( -- * Tests and Traversals
key
, nth
, assertEq
, stop
, jsonTest
-- * Test Interpreters
, testJSON
, JSONTest
) where
import Control.Monad.Indexed (IxFunctor(..), (>>>=))
import Control.MonadPlus.Indexed.Free (IxFree(..))
import Data.Monoid (First)
import qualified Control.Lens as Lens
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Lens as Aeson
import qualified Data.Text as Text
--------------------------------------------------------------------------------
data JSONF i j a where
Key :: String -> (i -> j) -> (j -> a) -> JSONF i j a
Index :: Int -> (i -> j) -> (j -> a) -> JSONF i j a
Assert :: (Aeson.Value -> Either String ()) -> a -> JSONF i i a
Stop :: JSONF i () a
instance IxFunctor JSONF where
imap g (Key keyS f k) = Key keyS f (g . k)
imap g (Index n f k) = Index n f (g . k)
imap f (Assert p k) = Assert p (f k)
imap _ Stop = Stop
--------------------------------------------------------------------------------
type JSONTest = IxFree JSONF
--------------------------------------------------------------------------------
-- | Traverse into the value underneath a specific key in the JSON structure.
-- The return value is the value inside the Haskell value - that is, the result
-- applying the associated morphism.
key :: String -- ^ JSON Key
-> (i -> j) -- ^ An associated morphism into a substructure of the test environment
-> JSONTest i j j
key k f = Free (Key k f Pure)
--------------------------------------------------------------------------------
-- | Traverse the specific index of a JSON array.
-- The return value is the value inside the Haskell value - that is, the result
-- applying the associated morphism.
nth :: Int -- ^ JSON array index
-> (i -> j) -- ^ An associated morphism into a substructure of the test environment
-> JSONTest i j j
nth i f = Free (Index i f Pure)
--------------------------------------------------------------------------------
-- | Assert that the current JSON value is exactly equal to the result of
-- calling 'Aeson.toJSON' on a value.
assertEq :: Aeson.ToJSON a => a -> JSONTest i i ()
assertEq expected =
let expectedJSON = Aeson.toJSON expected
p actual | actual == expectedJSON = Right ()
| otherwise = Left $ unlines
[ "Expected: " ++ show expectedJSON
, " Got: " ++ show actual
]
in Free (Assert p (Pure ()))
--------------------------------------------------------------------------------
-- | Using 'stop' discards the indices in the monad, which can help when you
-- need to 'isum' multiple tests that end in different states.
stop :: JSONTest a () r
stop = Free Stop
--------------------------------------------------------------------------------
-- | Finalize a 'JSONTest' by calling 'stop' at the end.
jsonTest :: JSONTest i j a -> JSONTest i () a
jsonTest = (>>>= const stop)
--------------------------------------------------------------------------------
-- | Run a 'JSONTest' against a Haskell value that can be encoded to JSON.
-- Returns a list of strings describing the failed assertions, or the empty list
-- if all assertions were satisfied.
testJSON :: Aeson.ToJSON i => JSONTest i j a -> i -> [String]
testJSON tests env = go tests (Aeson.toJSON env) env "subject"
where
go :: JSONTest i j a -> Aeson.Value -> i -> String -> [String]
go (Pure _) _ _ _ = []
go (Free (Key keyS f k)) actual expected descr =
tryLens (Aeson.key (Text.pack keyS)) f actual expected k $
descr ++ "[\"" ++ keyS ++ "\"]"
go (Free (Index n f k)) actual expected descr =
tryLens (Aeson.nth n) f actual expected k $
descr ++ " failed to match any targets"
go (Free (Assert p k)) actual expected descr =
either
(return . ((descr ++ " failed assertion\n") ++))
(const $ go k actual expected descr)
(p actual)
go (Free Stop) _ _ _ = []
go (Plus steps) actual expected descr =
concatMap (\s -> go s actual expected descr) steps
tryLens :: Lens.Getting (First Aeson.Value) Aeson.Value Aeson.Value
-> (i -> j) -> Aeson.Value
-> i -> (j -> JSONTest j k a)
-> String
-> [String]
tryLens l f actual expected k path =
case Lens.preview l actual of
Nothing -> [path ++ " failed to match any targets"]
Just matched ->
go (k (f expected)) matched (f expected) path