2
2
{-# LANGUAGE RankNTypes #-}
3
3
4
4
{-# OPTIONS_GHC -fno-warn-orphans #-}
5
+ -- Need this to avoid a warning on the `typeName` helper function.
6
+ {-# OPTIONS_GHC -Wno-redundant-constraints #-}
5
7
6
8
module Test.Pos.Helpers
7
9
( canonicalJsonTest
10
+ , canonicalJsonTest'
8
11
) where
9
12
10
13
import Universum
11
14
12
15
import Data.Functor.Identity (Identity (.. ))
16
+ import Data.Typeable (typeRep )
13
17
import Test.Hspec (Spec )
14
- import Test.QuickCheck (Property , (.&&.) , (===) )
18
+ import Test.Hspec.QuickCheck (prop )
19
+ import Test.QuickCheck (Gen , Property , forAll , (.&&.) , (===) )
15
20
import qualified Text.JSON.Canonical as CanonicalJSON
16
21
17
22
import Pos.Core.Genesis ()
@@ -34,27 +39,52 @@ canonicalJsonTest ::
34
39
canonicalJsonTest =
35
40
identityTest @ a $ \ x ->
36
41
canonicalJsonRenderAndDecode x .&&. canonicalJsonPrettyAndDecode x
42
+
43
+ -- | Basically the same as `canonicalJsonTest` but tests a given `Gen a`.
44
+ canonicalJsonTest'
45
+ :: forall a . (IdTestingRequiredClassesAlmost a , ToAndFromCanonicalJson a )
46
+ => Gen a
47
+ -> Spec
48
+ canonicalJsonTest' genA =
49
+ prop (typeName @ a ) $ forAll genA $ \ x ->
50
+ canonicalJsonRenderAndDecode x .&&. canonicalJsonPrettyAndDecode x
37
51
where
38
- canonicalJsonRenderAndDecode x =
39
- let encodedX =
40
- CanonicalJSON. renderCanonicalJSON $
41
- runIdentity $ CanonicalJSON. toJSON x
42
- in canonicalJsonDecodeAndCompare x encodedX
43
- canonicalJsonPrettyAndDecode x =
44
- let encodedX =
45
- encodeUtf8 $
46
- CanonicalJSON. prettyCanonicalJSON $
47
- runIdentity $ CanonicalJSON. toJSON x
48
- in canonicalJsonDecodeAndCompare x encodedX
49
- canonicalJsonDecodeAndCompare ::
50
- a
51
- -> LByteString
52
- -> Property
53
- canonicalJsonDecodeAndCompare x encodedX =
54
- let decodedValue =
55
- either (error . toText) identity $
56
- CanonicalJSON. parseCanonicalJSON encodedX
57
- decodedX =
58
- either (error . pretty @ SchemaError ) identity $
59
- CanonicalJSON. fromJSON decodedValue
60
- in decodedX === x
52
+ -- GHC 8.2.2 says the `Typeable x` constraint is not necessary, but won't compile
53
+ -- this without it.
54
+ typeName :: forall x . Typeable x => String
55
+ typeName = show $ typeRep (Proxy @ a )
56
+
57
+ canonicalJsonRenderAndDecode
58
+ :: forall a . (IdTestingRequiredClassesAlmost a , ToAndFromCanonicalJson a )
59
+ => a
60
+ -> Property
61
+ canonicalJsonRenderAndDecode x =
62
+ let encodedX =
63
+ CanonicalJSON. renderCanonicalJSON $
64
+ runIdentity $ CanonicalJSON. toJSON x
65
+ in canonicalJsonDecodeAndCompare x encodedX
66
+
67
+ canonicalJsonPrettyAndDecode
68
+ :: forall a . (IdTestingRequiredClassesAlmost a , ToAndFromCanonicalJson a )
69
+ => a
70
+ -> Property
71
+ canonicalJsonPrettyAndDecode x =
72
+ let encodedX =
73
+ encodeUtf8 $
74
+ CanonicalJSON. prettyCanonicalJSON $
75
+ runIdentity $ CanonicalJSON. toJSON x
76
+ in canonicalJsonDecodeAndCompare x encodedX
77
+
78
+ canonicalJsonDecodeAndCompare
79
+ :: forall a . (IdTestingRequiredClassesAlmost a , ToAndFromCanonicalJson a )
80
+ => a
81
+ -> LByteString
82
+ -> Property
83
+ canonicalJsonDecodeAndCompare x encodedX =
84
+ let decodedValue =
85
+ either (error . toText) identity $
86
+ CanonicalJSON. parseCanonicalJSON encodedX
87
+ decodedX =
88
+ either (error . pretty @ SchemaError ) identity $
89
+ CanonicalJSON. fromJSON decodedValue
90
+ in decodedX === x
0 commit comments