on master help sign in
Basic tests for Generic toJSON.
commit4d1e71db807d055636834be65e8f0719f6ee80d9 authorEwout <e@ewout.name> dateMon 19 Oct 2015, 23:57 (11 years ago) parent63a7a43
2 files changed+75
tests/Encoders.hs+27 tests/Properties.hs+48
tests/Encoders.hs+27
@@ -55,6 +55,9 @@ thNullaryParseJSONObjectWithSingleField = $(mkParseJSON optsObjectWithSingleFiel
5555gNullaryToJSONString :: Nullary -> Value
5656gNullaryToJSONString = genericToJSON defaultOptions
5757
58+gNullaryToEncodingString :: Nullary -> Encoding
59+gNullaryToEncodingString = genericToEncoding defaultOptions
60+
5861gNullaryParseJSONString :: Value -> Parser Nullary
5962gNullaryParseJSONString = genericParseJSON defaultOptions
6063
@@ -62,6 +65,9 @@ gNullaryParseJSONString = genericParseJSON defaultOptions
6265gNullaryToJSON2ElemArray :: Nullary -> Value
6366gNullaryToJSON2ElemArray = genericToJSON opts2ElemArray
6467
68+gNullaryToEncoding2ElemArray :: Nullary -> Encoding
69+gNullaryToEncoding2ElemArray = genericToEncoding opts2ElemArray
70+
6571gNullaryParseJSON2ElemArray :: Value -> Parser Nullary
6672gNullaryParseJSON2ElemArray = genericParseJSON opts2ElemArray
6773
@@ -69,6 +75,9 @@ gNullaryParseJSON2ElemArray = genericParseJSON opts2ElemArray
6975gNullaryToJSONTaggedObject :: Nullary -> Value
7076gNullaryToJSONTaggedObject = genericToJSON optsTaggedObject
7177
78+gNullaryToEncodingTaggedObject :: Nullary -> Encoding
79+gNullaryToEncodingTaggedObject = genericToEncoding optsTaggedObject
80+
7281gNullaryParseJSONTaggedObject :: Value -> Parser Nullary
7382gNullaryParseJSONTaggedObject = genericParseJSON optsTaggedObject
7483
@@ -76,6 +85,9 @@ gNullaryParseJSONTaggedObject = genericParseJSON optsTaggedObject
7685gNullaryToJSONObjectWithSingleField :: Nullary -> Value
7786gNullaryToJSONObjectWithSingleField = genericToJSON optsObjectWithSingleField
7887
88+gNullaryToEncodingObjectWithSingleField :: Nullary -> Encoding
89+gNullaryToEncodingObjectWithSingleField = genericToEncoding optsObjectWithSingleField
90+
7991gNullaryParseJSONObjectWithSingleField :: Value -> Parser Nullary
8092gNullaryParseJSONObjectWithSingleField = genericParseJSON optsObjectWithSingleField
8193
@@ -120,6 +132,9 @@ thSomeTypeParseJSONObjectWithSingleField =
120132gSomeTypeToJSON2ElemArray :: SomeType Int -> Value
121133gSomeTypeToJSON2ElemArray = genericToJSON opts2ElemArray
122134
135+gSomeTypeToEncoding2ElemArray :: SomeType Int -> Encoding
136+gSomeTypeToEncoding2ElemArray = genericToEncoding opts2ElemArray
137+
123138gSomeTypeParseJSON2ElemArray :: Value -> Parser (SomeType Int)
124139gSomeTypeParseJSON2ElemArray = genericParseJSON opts2ElemArray
125140
@@ -127,6 +142,9 @@ gSomeTypeParseJSON2ElemArray = genericParseJSON opts2ElemArray
127142gSomeTypeToJSONTaggedObject :: SomeType Int -> Value
128143gSomeTypeToJSONTaggedObject = genericToJSON optsTaggedObject
129144
145+gSomeTypeToEncodingTaggedObject :: SomeType Int -> Encoding
146+gSomeTypeToEncodingTaggedObject = genericToEncoding optsTaggedObject
147+
130148gSomeTypeParseJSONTaggedObject :: Value -> Parser (SomeType Int)
131149gSomeTypeParseJSONTaggedObject = genericParseJSON optsTaggedObject
132150
@@ -134,6 +152,9 @@ gSomeTypeParseJSONTaggedObject = genericParseJSON optsTaggedObject
134152gSomeTypeToJSONObjectWithSingleField :: SomeType Int -> Value
135153gSomeTypeToJSONObjectWithSingleField = genericToJSON optsObjectWithSingleField
136154
155+gSomeTypeToEncodingObjectWithSingleField :: SomeType Int -> Encoding
156+gSomeTypeToEncodingObjectWithSingleField = genericToEncoding optsObjectWithSingleField
157+
137158gSomeTypeParseJSONObjectWithSingleField :: Value -> Parser (SomeType Int)
138159gSomeTypeParseJSONObjectWithSingleField = genericParseJSON optsObjectWithSingleField
139160
@@ -164,6 +185,9 @@ thApproxParseJSONDefault = $(mkParseJSON defaultOptions ''Approx)
164185gApproxToJSONUnwrap :: Approx String -> Value
165186gApproxToJSONUnwrap = genericToJSON optsUnwrapUnaryRecords
166187
188+gApproxToEncodingUnwrap :: Approx String -> Encoding
189+gApproxToEncodingUnwrap = genericToEncoding optsUnwrapUnaryRecords
190+
167191gApproxParseJSONUnwrap :: Value -> Parser (Approx String)
168192gApproxParseJSONUnwrap = genericParseJSON optsUnwrapUnaryRecords
169193
@@ -171,5 +195,8 @@ gApproxParseJSONUnwrap = genericParseJSON optsUnwrapUnaryRecords
171195gApproxToJSONDefault :: Approx String -> Value
172196gApproxToJSONDefault = genericToJSON defaultOptions
173197
198+gApproxToEncodingDefault :: Approx String -> Encoding
199+gApproxToEncodingDefault = genericToEncoding defaultOptions
200+
174201gApproxParseJSONDefault :: Value -> Parser (Approx String)
175202gApproxParseJSONDefault = genericParseJSON defaultOptions
tests/Properties.hs+48
@@ -153,6 +153,54 @@ tests = testGroup "properties" [
153153 , testGroup "failure messages" [
154154 testProperty "modify failure" modifyFailureProp
155155 ]
156+ , testGroup "generic" [
157+ testGroup "toJSON" [
158+ testGroup "Nullary" [
159+ testProperty "string" (isString . gNullaryToJSONString)
160+ , testProperty "2ElemArray" (is2ElemArray . gNullaryToJSON2ElemArray)
161+ , testProperty "TaggedObject" (isTaggedObject . gNullaryToJSONTaggedObject)
162+ , testProperty "ObjectWithSingleField" (isObjectWithSingleField . gNullaryToJSONObjectWithSingleField)
163+ , testGroup "roundTrip" [
164+ testProperty "string" (toParseJSON gNullaryParseJSONString gNullaryToJSONString)
165+ , testProperty "2ElemArray" (toParseJSON gNullaryParseJSON2ElemArray gNullaryToJSON2ElemArray)
166+ , testProperty "TaggedObject" (toParseJSON gNullaryParseJSONTaggedObject gNullaryToJSONTaggedObject)
167+ , testProperty "ObjectWithSingleField" (toParseJSON gNullaryParseJSONObjectWithSingleField gNullaryToJSONObjectWithSingleField)
168+ ]
169+ ]
170+ , testGroup "SomeType" [
171+ testProperty "2ElemArray" (is2ElemArray . gSomeTypeToJSON2ElemArray)
172+ , testProperty "TaggedObject" (isTaggedObject . gSomeTypeToJSONTaggedObject)
173+ , testProperty "ObjectWithSingleField" (isObjectWithSingleField . gSomeTypeToJSONObjectWithSingleField)
174+ , testGroup "roundTrip" [
175+ testProperty "2ElemArray" (toParseJSON gSomeTypeParseJSON2ElemArray gSomeTypeToJSON2ElemArray)
176+ , testProperty "TaggedObject" (toParseJSON gSomeTypeParseJSONTaggedObject gSomeTypeToJSONTaggedObject)
177+ , testProperty "ObjectWithSingleField" (toParseJSON gSomeTypeParseJSONObjectWithSingleField gSomeTypeToJSONObjectWithSingleField)
178+ ]
179+ ]
180+ ]
181+ , testGroup "toEncoding" [
182+ testProperty "NullaryString" $
183+ gNullaryToJSONString `sameAs` gNullaryToEncodingString
184+ , testProperty "Nullary2ElemArray" $
185+ gNullaryToJSON2ElemArray `sameAs` gNullaryToEncoding2ElemArray
186+ , testProperty "NullaryTaggedObject" $
187+ gNullaryToJSONTaggedObject `sameAs` gNullaryToEncodingTaggedObject
188+ , testProperty "NullaryObjectWithSingleField" $
189+ gNullaryToJSONObjectWithSingleField `sameAs`
190+ gNullaryToEncodingObjectWithSingleField
191+ -- , testProperty "ApproxUnwrap" $
192+ -- gApproxToJSONUnwrap `sameAs` gApproxToEncodingUnwrap
193+ , testProperty "ApproxDefault" $
194+ gApproxToJSONDefault `sameAs` gApproxToEncodingDefault
195+ , testProperty "SomeType2ElemArray" $
196+ gSomeTypeToJSON2ElemArray `sameAsV` gSomeTypeToEncoding2ElemArray
197+ , testProperty "SomeTypeTaggedObject" $
198+ gSomeTypeToJSONTaggedObject `sameAsV` gSomeTypeToEncodingTaggedObject
199+ , testProperty "SomeTypeObjectWithSingleField" $
200+ gSomeTypeToJSONObjectWithSingleField `sameAsV`
201+ gSomeTypeToEncodingObjectWithSingleField
202+ ]
203+ ]
156204 , testGroup "template-haskell" [
157205 testGroup "toJSON" [
158206 testGroup "Nullary" [