@@ -1,4 +1,4 @@
1−{-# LANGUAGE CPP, OverloadedStrings, ScopedTypeVariables #-}
1+{-# LANGUAGE CPP, OverloadedStrings, ScopedTypeVariables, RankNTypes #-}
22
33-- For Arbitrary Compose
44{-# LANGUAGE FlexibleContexts #-}
@@ -12,8 +12,8 @@ import Control.Applicative (Const)
1212import Data.Aeson (eitherDecode, encode)
1313import Data.Aeson.Internal (IResult(..), formatError, ifromJSON, iparse)
1414import Data.Aeson.Parser (value)
15+import Data.Aeson.Encoding (encodingToLazyByteString)
1516import Data.Aeson.Types
16−import Data.ByteString.Builder (toLazyByteString)
1717import Data.Int (Int8)
1818import Data.Sequence (Seq)
1919import Data.DList (DList)
@@ -52,13 +52,24 @@ encodeDouble num denom
5252encodeInteger :: Integer -> Property
5353encodeInteger i = encode i === L.pack (show i)
5454
55−toParseJSON :: (Arbitrary a, Eq a, Show a) =>
55+toParseJSON :: (Eq a, Show a) =>
5656 (Value -> Parser a) -> (a -> Value) -> a -> Property
5757toParseJSON parsejson tojson x =
5858 case iparse parsejson . tojson $ x of
5959 IError path msg -> failure "parse" (formatError path msg) x
6060 ISuccess x' -> x === x'
6161
62+toParseJSON1
63+ :: (Eq (f Int), Show (f Int))
64+ => (forall a. LiftParseJSON f a)
65+ -> (forall a. LiftToJSON f a)
66+ -> f Int
67+ -> Property
68+toParseJSON1 parsejson1 tojson1 = toParseJSON parsejson tojson
69+ where
70+ parsejson = parsejson1 parseJSON (listParser parseJSON)
71+ tojson = tojson1 toJSON (listValue toJSON)
72+
6273roundTripEnc :: (FromJSON a, ToJSON a, Show a) =>
6374 (a -> a -> Property) -> a -> a -> Property
6475roundTripEnc eq _ i =
@@ -106,7 +117,29 @@ modifyFailureProp orig added =
106117-- variation in JSON object key ordering.
107118sameAs :: (a -> Value) -> (a -> Encoding) -> a -> Property
108119sameAs toVal toEnc v =
109− eitherDecode (toLazyByteString (fromEncoding (toEnc v))) === Right (toVal v)
120+ eitherDecode (encodingToLazyByteString (toEnc v)) === Right (toVal v)
121+
122+sameAs1
123+ :: (forall a. LiftToJSON f a)
124+ -> (forall a. LiftToEncoding f a)
125+ -> f Int
126+ -> Property
127+sameAs1 toVal1 toEnc1 v = lhs === rhs
128+ where
129+ rhs = Right $ toVal1 toJSON (listValue toJSON) v
130+ lhs = eitherDecode . encodingToLazyByteString $
131+ toEnc1 toEncoding (listEncoding toEncoding) v
132+
133+sameAs1Agree
134+ :: ToJSON a
135+ => (f a -> Encoding)
136+ -> (forall b. LiftToEncoding f b)
137+ -> f a
138+ -> Property
139+sameAs1Agree toEnc toEnc1 v = rhs === lhs
140+ where
141+ rhs = encodingToLazyByteString $ toEnc v
142+ lhs = encodingToLazyByteString $ toEnc1 toEncoding (listEncoding toEncoding) v
110143
111144type P6 = Product6 Int Bool String (Approx Double) (Int, Approx Double) ()
112145type S4 = Sum4 Int8 ZonedTime T.Text (Map.Map String Int)
@@ -268,6 +301,12 @@ tests = testGroup "properties" [
268301 testProperty "2ElemArray" (toParseJSON gSomeTypeParseJSON2ElemArray gSomeTypeToJSON2ElemArray)
269302 , testProperty "TaggedObject" (toParseJSON gSomeTypeParseJSONTaggedObject gSomeTypeToJSONTaggedObject)
270303 , testProperty "ObjectWithSingleField" (toParseJSON gSomeTypeParseJSONObjectWithSingleField gSomeTypeToJSONObjectWithSingleField)
304+
305+#if __GLASGOW_HASKELL__ >= 706
306+ , testProperty "2ElemArray unary" (toParseJSON1 gSomeTypeLiftParseJSON2ElemArray gSomeTypeLiftToJSON2ElemArray)
307+ , testProperty "TaggedObject unary" (toParseJSON1 gSomeTypeLiftParseJSONTaggedObject gSomeTypeLiftToJSONTaggedObject)
308+ , testProperty "ObjectWithSingleField unary" (toParseJSON1 gSomeTypeLiftParseJSONObjectWithSingleField gSomeTypeLiftToJSONObjectWithSingleField)
309+#endif
271310 ]
272311 ]
273312 ]
@@ -285,13 +324,34 @@ tests = testGroup "properties" [
285324 -- gApproxToJSONUnwrap `sameAs` gApproxToEncodingUnwrap
286325 , testProperty "ApproxDefault" $
287326 gApproxToJSONDefault `sameAs` gApproxToEncodingDefault
327+
288328 , testProperty "SomeType2ElemArray" $
289329 gSomeTypeToJSON2ElemArray `sameAs` gSomeTypeToEncoding2ElemArray
330+#if __GLASGOW_HASKELL__ >= 706
331+ , testProperty "SomeType2ElemArray unary" $
332+ gSomeTypeLiftToJSON2ElemArray `sameAs1` gSomeTypeLiftToEncoding2ElemArray
333+ , testProperty "SomeType2ElemArray unary agree" $
334+ gSomeTypeToEncoding2ElemArray `sameAs1Agree` gSomeTypeLiftToEncoding2ElemArray
335+#endif
336+
290337 , testProperty "SomeTypeTaggedObject" $
291338 gSomeTypeToJSONTaggedObject `sameAs` gSomeTypeToEncodingTaggedObject
339+#if __GLASGOW_HASKELL__ >= 706
340+ , testProperty "SomeTypeTaggedObject unary" $
341+ gSomeTypeLiftToJSONTaggedObject `sameAs1` gSomeTypeLiftToEncodingTaggedObject
342+ , testProperty "SomeTypeTaggedObject unary agree" $
343+ gSomeTypeToEncodingTaggedObject `sameAs1Agree` gSomeTypeLiftToEncodingTaggedObject
344+#endif
345+
292346 , testProperty "SomeTypeObjectWithSingleField" $
293− gSomeTypeToJSONObjectWithSingleField `sameAs`
294− gSomeTypeToEncodingObjectWithSingleField
347+ gSomeTypeToJSONObjectWithSingleField `sameAs` gSomeTypeToEncodingObjectWithSingleField
348+#if __GLASGOW_HASKELL__ >= 706
349+ , testProperty "SomeTypeObjectWithSingleField unary" $
350+ gSomeTypeLiftToJSONObjectWithSingleField `sameAs1` gSomeTypeLiftToEncodingObjectWithSingleField
351+ , testProperty "SomeTypeObjectWithSingleField unary agree" $
352+ gSomeTypeToEncodingObjectWithSingleField `sameAs1Agree` gSomeTypeLiftToEncodingObjectWithSingleField
353+#endif
354+
295355 , testProperty "SomeTypeOmitNothingFields" $
296356 gSomeTypeToJSONOmitNothingFields `sameAs` gSomeTypeToEncodingOmitNothingFields
297357 ]
@@ -319,6 +379,11 @@ tests = testGroup "properties" [
319379 testProperty "2ElemArray" (toParseJSON thSomeTypeParseJSON2ElemArray thSomeTypeToJSON2ElemArray)
320380 , testProperty "TaggedObject" (toParseJSON thSomeTypeParseJSONTaggedObject thSomeTypeToJSONTaggedObject)
321381 , testProperty "ObjectWithSingleField" (toParseJSON thSomeTypeParseJSONObjectWithSingleField thSomeTypeToJSONObjectWithSingleField)
382+
383+ , testProperty "2ElemArray unary" (toParseJSON1 thSomeTypeLiftParseJSON2ElemArray thSomeTypeLiftToJSON2ElemArray)
384+ , testProperty "TaggedObject unary" (toParseJSON1 thSomeTypeLiftParseJSONTaggedObject thSomeTypeLiftToJSONTaggedObject)
385+ , testProperty "ObjectWithSingleField unary" (toParseJSON1 thSomeTypeLiftParseJSONObjectWithSingleField thSomeTypeLiftToJSONObjectWithSingleField)
386+
322387 ]
323388 , testGroup "Approx" [
324389 testProperty "string" (isString . thApproxToJSONUnwrap)
@@ -352,13 +417,27 @@ tests = testGroup "properties" [
352417 thApproxToJSONUnwrap `sameAs` thApproxToEncodingUnwrap
353418 , testProperty "ApproxDefault" $
354419 thApproxToJSONDefault `sameAs` thApproxToEncodingDefault
420+
355421 , testProperty "SomeType2ElemArray" $
356422 thSomeTypeToJSON2ElemArray `sameAs` thSomeTypeToEncoding2ElemArray
423+ , testProperty "SomeType2ElemArray unary" $
424+ thSomeTypeLiftToJSON2ElemArray `sameAs1` thSomeTypeLiftToEncoding2ElemArray
425+ , testProperty "SomeType2ElemArray unary agree" $
426+ thSomeTypeToEncoding2ElemArray `sameAs1Agree` thSomeTypeLiftToEncoding2ElemArray
427+
357428 , testProperty "SomeTypeTaggedObject" $
358429 thSomeTypeToJSONTaggedObject `sameAs` thSomeTypeToEncodingTaggedObject
430+ , testProperty "SomeTypeTaggedObject unary" $
431+ thSomeTypeLiftToJSONTaggedObject `sameAs1` thSomeTypeLiftToEncodingTaggedObject
432+ , testProperty "SomeTypeTaggedObject unary agree" $
433+ thSomeTypeToEncodingTaggedObject `sameAs1Agree` thSomeTypeLiftToEncodingTaggedObject
434+
359435 , testProperty "SomeTypeObjectWithSingleField" $
360− thSomeTypeToJSONObjectWithSingleField `sameAs`
361− thSomeTypeToEncodingObjectWithSingleField
436+ thSomeTypeToJSONObjectWithSingleField `sameAs` thSomeTypeToEncodingObjectWithSingleField
437+ , testProperty "SomeTypeObjectWithSingleField unary" $
438+ thSomeTypeLiftToJSONObjectWithSingleField `sameAs1` thSomeTypeLiftToEncodingObjectWithSingleField
439+ , testProperty "SomeTypeObjectWithSingleField unary agree" $
440+ thSomeTypeToEncodingObjectWithSingleField `sameAs1Agree` thSomeTypeLiftToEncodingObjectWithSingleField
362441 ]
363442 ]
364443 ]