on weeder-instances help sign in
typeable/aeson/tests/UnitTests/NullaryConstructors.hs
63 lines 2.9 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
3{-# OPTIONS_GHC -fno-warn-deprecations #-}
5module UnitTests.NullaryConstructors
6 (
7 nullaryConstructors
8 ) where
10import Prelude ()
11import Prelude.Compat
13import Data.Aeson (decode, eitherDecode, fromEncoding, Value)
14import Data.Aeson.Internal (IResult (..), iparse)
15import Data.Aeson.Types (Parser)
16import Data.ByteString.Builder (toLazyByteString)
17import Data.Maybe (fromJust)
18import Encoders
19import Test.HUnit ((@=?), Assertion)
20import Types
21import qualified Data.ByteString.Lazy.Char8 as L
23nullaryConstructors :: [Assertion]
24nullaryConstructors =
25 [ dec "\"C1\"" @=? thNullaryToJSONString C1
26 , dec "\"C1\"" @=? gNullaryToJSONString C1
27 , dec "{\"c1\":[]}" @=? thNullaryToJSONObjectWithSingleField C1
28 , dec "{\"c1\":[]}" @=? gNullaryToJSONObjectWithSingleField C1
29 , dec "[\"c1\",[]]" @=? gNullaryToJSON2ElemArray C1
30 , dec "[\"c1\",[]]" @=? thNullaryToJSON2ElemArray C1
31 , dec "{\"tag\":\"c1\"}" @=? thNullaryToJSONTaggedObject C1
32 , dec "{\"tag\":\"c1\"}" @=? gNullaryToJSONTaggedObject C1
34 , decE "\"C1\"" @=? enc (gNullaryToEncodingString C1)
35 , decE "\"C1\"" @=? enc (thNullaryToEncodingString C1)
36 , decE "[\"c1\",[]]" @=? enc (gNullaryToEncoding2ElemArray C1)
37 , decE "[\"c1\",[]]" @=? enc (thNullaryToEncoding2ElemArray C1)
38 , decE "{\"c1\":[]}" @=? enc (thNullaryToEncodingObjectWithSingleField C1)
39 , decE "{\"c1\":[]}" @=? enc (gNullaryToEncodingObjectWithSingleField C1)
40 , decE "{\"tag\":\"c1\"}" @=? enc (thNullaryToEncodingTaggedObject C1)
41 , decE "{\"tag\":\"c1\"}" @=? enc (gNullaryToEncodingTaggedObject C1)
43 , ISuccess C1 @=? parse thNullaryParseJSONTaggedObject (dec "{\"tag\":\"c1\"}")
44 , ISuccess C1 @=? parse gNullaryParseJSONTaggedObject (dec "{\"tag\":\"c1\"}")
46 , ISuccess C1 @=? parse thNullaryParseJSONString (dec "\"C1\"")
47 , ISuccess C1 @=? parse gNullaryParseJSONString (dec "\"C1\"")
48 , ISuccess C1 @=? parse thNullaryParseJSON2ElemArray (dec "[\"c1\",[]]")
49 , ISuccess C1 @=? parse gNullaryParseJSON2ElemArray (dec "[\"c1\",[]]")
50 , ISuccess C1 @=? parse thNullaryParseJSONObjectWithSingleField (dec "{\"c1\":[]}")
51 , ISuccess C1 @=? parse gNullaryParseJSONObjectWithSingleField (dec "{\"c1\":[]}")
52 -- Make sure that the old `"contents" : []' is still allowed
53 , ISuccess C1 @=? parse thNullaryParseJSONTaggedObject (dec "{\"tag\":\"c1\",\"contents\":[]}")
54 , ISuccess C1 @=? parse gNullaryParseJSONTaggedObject (dec "{\"tag\":\"c1\",\"contents\":[]}")
55 ]
56 where
57 enc = eitherDecode . toLazyByteString . fromEncoding
58 dec :: L.ByteString -> Value
59 dec = fromJust . decode
60 decE :: L.ByteString -> Either String Value
61 decE = eitherDecode
62 parse :: (a -> Parser b) -> a -> IResult b
63 parse parsejson v = iparse parsejson v