@@ -7,10 +7,11 @@ import Data.Argonaut.Core (stringify, stringifyWithIndent)
7
7
import Data.Argonaut.Decode (parseJson )
8
8
import Data.Bifunctor (lmap )
9
9
import Data.Codec (decode , encode )
10
- import Data.Codec.Argonaut (JsonCodec )
10
+ import Data.Codec.Argonaut (JsonCodec , JsonDecodeError (..) )
11
11
import Data.Codec.Argonaut as C
12
12
import Data.Codec.Argonaut.Record as CR
13
- import Data.Codec.Argonaut.Sum (Encoding (..), defaultEncoding , sumFlat , sumFlatWith , sumWith )
13
+ import Data.Codec.Argonaut.Sum (Encoding (..), defaultEncoding , sumFlatWith , sumWith )
14
+ import Data.Either (Either (..))
14
15
import Data.Generic.Rep (class Generic )
15
16
import Data.Show.Generic (genericShow )
16
17
import Data.String as Str
@@ -22,7 +23,6 @@ import Test.QuickCheck (class Arbitrary, arbitrary, quickCheck)
22
23
import Test.QuickCheck.Arbitrary (genericArbitrary )
23
24
import Test.Util (propCodec )
24
25
import Type.Prelude (Proxy (..))
25
- import Type.Proxy (Proxy )
26
26
27
27
-- ------------------------------------------------------------------------------
28
28
@@ -91,11 +91,20 @@ check codec val expectEncoded = do
91
91
92
92
json ← liftEither $ lmap (show >>> error) $ parseJson encodedStr
93
93
94
- decoded ← liftEither $ lmap (\err -> error (" decode failed: " <> show err <> " JSON was: " <> stringify json)) $ decode codec json
94
+ decoded ← liftEither $ lmap (\err → error (" decode failed: " <> show err <> " JSON was: " <> stringify json)) $ decode codec json
95
95
96
96
when (decoded /= val) $
97
97
throw (" decode check failed, expected: " <> show val <> " , got: " <> show decoded)
98
98
99
+ checkFailure ∷ ∀ a . Show a ⇒ Eq a ⇒ JsonCodec a → JsonDecodeError → String → Effect Unit
100
+ checkFailure codec err encodedStr = do
101
+ json ← liftEither $ lmap (show >>> error) $ parseJson encodedStr
102
+
103
+ let result = decode codec json
104
+
105
+ when (result /= Left err) $
106
+ throw (" decode check failed, expected: " <> show err <> " , got: " <> show result)
107
+
99
108
main ∷ Effect Unit
100
109
main = do
101
110
log " Check sum"
@@ -184,6 +193,27 @@ main = do
184
193
, " }"
185
194
]
186
195
196
+ checkFailure
197
+ (codecSample opts)
198
+ (Named " Sample" (TypeMismatch " Expecting at least one element" ))
199
+ $ Str .joinWith " \n "
200
+ [ " {"
201
+ , " \" customTag\" : \" Baz\" ,"
202
+ , " \" customValues\" : ["
203
+ , " true"
204
+ , " ]"
205
+ , " }"
206
+ ]
207
+
208
+ checkFailure
209
+ (codecSample opts)
210
+ (Named " Sample" (TypeMismatch " No case matched" ))
211
+ $ Str .joinWith " \n "
212
+ [ " {"
213
+ , " \" customTag\" : \" Qux\" "
214
+ , " }"
215
+ ]
216
+
187
217
log " - Option: Omit empty arguments"
188
218
do
189
219
let
@@ -315,6 +345,26 @@ main = do
315
345
, " }"
316
346
]
317
347
348
+ checkFailure
349
+ (codecSample opts)
350
+ (Named " Sample" (TypeMismatch " Expecting at least one element" ))
351
+ $ Str .joinWith " \n "
352
+ [ " {"
353
+ , " \" Baz\" : ["
354
+ , " true"
355
+ , " ]"
356
+ , " }"
357
+ ]
358
+
359
+ checkFailure
360
+ (codecSample opts)
361
+ (Named " Sample" (TypeMismatch " No case matched" ))
362
+ $ Str .joinWith " \n "
363
+ [ " {"
364
+ , " \" Qux\" : []"
365
+ , " }"
366
+ ]
367
+
318
368
log " - Option: Unwrap single arguments"
319
369
do
320
370
let
@@ -385,5 +435,24 @@ main = do
385
435
, " }"
386
436
]
387
437
438
+ checkFailure
439
+ codecSampleFlat
440
+ (Named " Sample" (Named " case FlatBaz" (AtKey " pos" MissingValue )))
441
+ $ Str .joinWith " \n "
442
+ [ " {"
443
+ , " \" tag\" : \" FlatBaz\" ,"
444
+ , " \" active\" : true"
445
+ , " }"
446
+ ]
447
+
448
+ checkFailure
449
+ codecSampleFlat
450
+ (Named " Sample" (TypeMismatch " No case matched" ))
451
+ $ Str .joinWith " \n "
452
+ [ " {"
453
+ , " \" tag\" : \" FlatQux\" "
454
+ , " }"
455
+ ]
456
+
388
457
quickCheck (propCodec arbitrary codecSampleFlat)
389
458
0 commit comments