@@ -7,10 +7,11 @@ import Data.Argonaut.Core (stringify, stringifyWithIndent)
77import Data.Argonaut.Decode (parseJson )
88import Data.Bifunctor (lmap )
99import Data.Codec (decode , encode )
10- import Data.Codec.Argonaut (JsonCodec )
10+ import Data.Codec.Argonaut (JsonCodec , JsonDecodeError (..) )
1111import Data.Codec.Argonaut as C
1212import 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 (..))
1415import Data.Generic.Rep (class Generic )
1516import Data.Show.Generic (genericShow )
1617import Data.String as Str
@@ -22,7 +23,6 @@ import Test.QuickCheck (class Arbitrary, arbitrary, quickCheck)
2223import Test.QuickCheck.Arbitrary (genericArbitrary )
2324import Test.Util (propCodec )
2425import Type.Prelude (Proxy (..))
25- import Type.Proxy (Proxy )
2626
2727-- ------------------------------------------------------------------------------
2828
@@ -91,11 +91,20 @@ check codec val expectEncoded = do
9191
9292 json ← liftEither $ lmap (show >>> error) $ parseJson encodedStr
9393
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
9595
9696 when (decoded /= val) $
9797 throw (" decode check failed, expected: " <> show val <> " , got: " <> show decoded)
9898
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+
99108main ∷ Effect Unit
100109main = do
101110 log " Check sum"
@@ -184,6 +193,27 @@ main = do
184193 , " }"
185194 ]
186195
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+
187217 log " - Option: Omit empty arguments"
188218 do
189219 let
@@ -315,6 +345,26 @@ main = do
315345 , " }"
316346 ]
317347
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+
318368 log " - Option: Unwrap single arguments"
319369 do
320370 let
@@ -385,5 +435,24 @@ main = do
385435 , " }"
386436 ]
387437
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+
388457 quickCheck (propCodec arbitrary codecSampleFlat)
389458
0 commit comments