@@ -4,37 +4,42 @@ import Prelude
4
4
5
5
import Control.Monad.Gen.Common (genMaybe )
6
6
import Data.Argonaut.Core (Json , isObject , stringify , toObject )
7
- import Data.Argonaut.Decode (decodeJson )
7
+ import Data.Argonaut.Decode (class DecodeJson , decodeJson , (.?) )
8
8
import Data.Argonaut.Encode (encodeJson , (:=), (:=?), (~>), (~>?))
9
9
import Data.Argonaut.Gen (genJson )
10
+ import Data.Argonaut.Parser (jsonParser )
10
11
import Data.Bifunctor (rmap )
11
12
import Data.Either (Either (..))
12
13
import Data.Foldable (foldl )
13
14
import Data.Maybe (Maybe (..), isJust , isNothing , maybe )
14
15
import Data.String.Gen (genUnicodeString )
15
16
import Data.Tuple (Tuple (..))
16
17
import Effect (Effect )
17
- import Effect.Console (log )
18
18
import Foreign.Object as FO
19
- import Test.QuickCheck (Result (..), quickCheck , (<?>), (===))
19
+ import Test.QuickCheck (Result (..), (<?>), (===))
20
20
import Test.QuickCheck.Gen (Gen , resize , suchThat )
21
+ import Test.Unit (TestSuite , test , suite , failure )
22
+ import Test.Unit.Assert as Assert
23
+ import Test.Unit.Main (runTest )
24
+ import Test.Unit.QuickCheck (quickCheck )
21
25
22
26
main :: Effect Unit
23
- main = do
24
- eitherCheck
25
- encodeDecodeCheck
26
- combinatorsCheck
27
+ main = runTest do
28
+ suite " Either Check" eitherCheck
29
+ suite " Encode/Decode Checks" encodeDecodeCheck
30
+ suite " Combinators Checks" combinatorsCheck
31
+ suite " Error Message Checks" errorMsgCheck
27
32
28
33
genTestJson :: Gen Json
29
34
genTestJson = resize 5 genJson
30
35
31
- encodeDecodeCheck :: Effect Unit
36
+ encodeDecodeCheck :: TestSuite
32
37
encodeDecodeCheck = do
33
- log " Testing that any JSON can be encoded and then decoded"
34
- quickCheck prop_encode_then_decode
38
+ test " Testing that any JSON can be encoded and then decoded" do
39
+ quickCheck prop_encode_then_decode
35
40
36
- log " Testing that any JSON can be decoded and then encoded"
37
- quickCheck prop_decode_then_encode
41
+ test " Testing that any JSON can be decoded and then encoded" do
42
+ quickCheck prop_decode_then_encode
38
43
39
44
where
40
45
@@ -54,18 +59,18 @@ encodeDecodeCheck = do
54
59
genObj :: Gen Json
55
60
genObj = suchThat (resize 5 genJson) isObject
56
61
57
- combinatorsCheck :: Effect Unit
62
+ combinatorsCheck :: TestSuite
58
63
combinatorsCheck = do
59
- log " Check assoc builder `:=`"
60
- quickCheck prop_assoc_builder_str
61
- log " Check assocOptional builder `:=?`"
62
- quickCheck prop_assoc_optional_builder_str
63
- log " Check JAssoc append `~>`"
64
- quickCheck prop_assoc_append
65
- log " Check JAssoc appendOptional `~>?`"
66
- quickCheck prop_assoc_append_optional
67
- log " Check get field `obj .? 'foo'`"
68
- quickCheck prop_get_jobject_field
64
+ test " Check assoc builder `:=`" do
65
+ quickCheck prop_assoc_builder_str
66
+ test " Check assocOptional builder `:=?`" do
67
+ quickCheck prop_assoc_optional_builder_str
68
+ test " Check JAssoc append `~>`" do
69
+ quickCheck prop_assoc_append
70
+ test " Check JAssoc appendOptional `~>?`" do
71
+ quickCheck prop_assoc_append_optional
72
+ test " Check get field `obj .? 'foo'`" do
73
+ quickCheck prop_get_jobject_field
69
74
70
75
where
71
76
@@ -116,13 +121,53 @@ combinatorsCheck = do
116
121
let keys = FO .keys object
117
122
in foldl (\ok key -> ok && isJust (FO .lookup key object)) true keys
118
123
119
- eitherCheck :: Effect Unit
124
+ eitherCheck :: TestSuite
120
125
eitherCheck = do
121
- log " Test EncodeJson/DecodeJson Either instance"
122
- quickCheck \(x :: Either String String ) ->
123
- case decodeJson (encodeJson x) of
124
- Right decoded ->
125
- decoded == x
126
- <?> (" x = " <> show x <> " , decoded = " <> show decoded)
127
- Left err ->
128
- false <?> err
126
+ test " Test EncodeJson/DecodeJson Either test" do
127
+ quickCheck \(x :: Either String String ) ->
128
+ case decodeJson (encodeJson x) of
129
+ Right decoded ->
130
+ decoded == x
131
+ <?> (" x = " <> show x <> " , decoded = " <> show decoded)
132
+ Left err ->
133
+ false <?> err
134
+
135
+ errorMsgCheck :: TestSuite
136
+ errorMsgCheck = do
137
+ test " Test that decoding array fails with the proper message" do
138
+ case notBar of
139
+ Left err -> Assert .equal barErr err
140
+ _ -> failure " Should have failed to decode"
141
+ test " Test that decoding record fails with the proper message" do
142
+ case notBaz of
143
+ Left err -> Assert .equal bazErr err
144
+ _ -> failure " Should have failed to decode"
145
+
146
+ where
147
+
148
+ barErr :: String
149
+ barErr = " Failed to decode key 'bar': "
150
+ <> " Couldn't decode Array (Failed at index 1): "
151
+ <> " Value is not a Number"
152
+
153
+ bazErr :: String
154
+ bazErr = " Failed to decode key 'baz': "
155
+ <> " Value is not a Boolean"
156
+
157
+ notBar :: Either String Foo
158
+ notBar = decodeJson =<< jsonParser " { \" bar\" : [1, true, 3], \" baz\" : false }"
159
+
160
+ notBaz :: Either String Foo
161
+ notBaz = decodeJson =<< jsonParser " { \" bar\" : [1, 2, 3], \" baz\" : 42 }"
162
+
163
+ newtype Foo = Foo
164
+ { bar :: Array Int
165
+ , baz :: Boolean
166
+ }
167
+
168
+ instance decodeJsonFoo :: DecodeJson Foo where
169
+ decodeJson json = do
170
+ x <- decodeJson json
171
+ bar <- x .? " bar"
172
+ baz <- x .? " baz"
173
+ pure $ Foo { bar, baz }
0 commit comments