-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.hs
213 lines (202 loc) · 6.51 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
module Main where
import Prelude hiding (null, bool)
import Hedgehog
import Hedgehog.Main
import qualified Data.Aeson as A
import qualified Jsonifier as J
import qualified Data.Aeson.Key as AesonKey
import qualified Data.Aeson.KeyMap as AesonKeyMap
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import qualified Data.ByteString.Char8 as Char8ByteString
import qualified Data.Vector as Vector
import qualified Main.Util.HedgehogGens as GenExtras
main =
defaultMain $ pure $ checkParallel $ $$(discover)
prop_sample =
withTests 1 $
property $ do
sample <- liftIO $ load "samples/twitter100.json"
A.eitherDecodeStrict' (J.toByteString (aesonJson sample)) === Right sample
where
load :: FilePath -> IO A.Value
load fileName =
A.eitherDecodeFileStrict' fileName
>>= either fail return
aesonJson :: A.Value -> J.Json
aesonJson =
\ case
A.Null ->
J.null
A.Bool a ->
J.bool a
A.Number a ->
J.scientificNumber a
A.String a ->
J.textString a
A.Array a ->
J.array (fmap aesonJson a)
A.Object a ->
J.object (AesonKeyMap.foldMapWithKey (\ k -> (: []) . (,) (AesonKey.toText k) . aesonJson) a)
prop_aesonRoundtrip =
withTests 9999 $
property $ do
sample <- forAll sampleGen
let encoding = sampleJsonifier sample
annotate (Char8ByteString.unpack encoding)
aeson <- evalEither (A.eitherDecodeStrict' encoding)
evalEither (maybe (Right ()) Left (detectMismatchInSampleAndAeson sample aeson))
data Sample =
NullSample |
BoolSample Bool |
IntNumberSample Int |
WordNumberSample Word |
DoubleNumberSample Double |
ScientificNumberSample Scientific |
TextStringSample Text |
ScientificStringSample Scientific |
ArraySample [Sample] |
ObjectSample [(Text, Sample)]
deriving (Eq, Show)
sampleGen :: Gen Sample
sampleGen =
sample
where
sample =
Gen.recursive Gen.choice [
null, bool,
intNumber,
wordNumber,
doubleNumber,
scientificNumber,
textString,
scientificString
] [
array, object
]
null =
pure NullSample
bool =
Gen.bool <&> BoolSample
intNumber =
Gen.int Range.exponentialBounded <&> IntNumberSample
wordNumber =
Gen.word Range.exponentialBounded <&> WordNumberSample
doubleNumber =
GenExtras.realFloat <&> DoubleNumberSample
scientificNumber =
GenExtras.scientific <&> ScientificNumberSample
textString =
Gen.text (Range.exponential 0 9999) Gen.unicode <&> TextStringSample
scientificString =
GenExtras.scientific <&> ScientificStringSample
array =
Gen.list (Range.exponential 0 999) sample <&> ArraySample
object =
rowList (Range.exponential 0 999) <&> ObjectSample
where
rowList range =
Gen.list range row <&>
nubBy (on (==) fst)
where
row =
Gen.text (Range.exponential 0 99) Gen.unicode
& fmap (,)
& flip ap sample
sampleJsonifier :: Sample -> ByteString
sampleJsonifier =
J.toByteString . sample
where
sample =
\ case
NullSample -> J.null
BoolSample a -> J.bool a
IntNumberSample a -> J.intNumber a
WordNumberSample a -> J.wordNumber a
DoubleNumberSample a -> J.doubleNumber a
ScientificNumberSample a -> J.scientificNumber a
TextStringSample a -> J.textString a
ScientificStringSample a -> J.scientificString a
ArraySample a -> J.array (fmap sample a)
ObjectSample a -> J.object (fmap (fmap sample) a)
sampleAeson :: Sample -> A.Value
sampleAeson =
sample
where
sample =
\ case
NullSample -> A.Null
BoolSample a -> A.Bool a
IntNumberSample a -> A.Number (fromIntegral a)
WordNumberSample a -> A.Number (fromIntegral a)
DoubleNumberSample a -> realNumber a
ScientificNumberSample a -> A.Number a
ScientificStringSample a -> A.String (fromString (show a))
TextStringSample a -> A.String a
ArraySample a -> A.Array (fromList (fmap sample a))
ObjectSample a -> A.Object (AesonKeyMap.fromList (fmap (bimap AesonKey.fromText sample) a))
where
realNumber a =
A.Number $
if isNaN a || isInfinite a then 0 else (read . show) a
{-|
We have to come down to this trickery due to small differences in
the way scientific renders floating point values and how ptr-poker does.
There is no difference when floating point comparison is used instead of scientific,
so this is what we achieve here.
It must be mentioned that this only applies to floating point numbers.
-}
detectMismatchInSampleAndAeson :: Sample -> A.Value -> Maybe (Sample, A.Value)
detectMismatchInSampleAndAeson =
\ case
NullSample ->
\ case
A.Null -> Nothing
a -> Just (NullSample, a)
BoolSample a ->
\ case
A.Bool b | b == a -> Nothing
b -> Just (BoolSample a, b)
IntNumberSample a ->
\ case
A.Number b | round b == a -> Nothing
b -> Just (IntNumberSample a, b)
WordNumberSample a ->
\ case
A.Number b | round b == a -> Nothing
b -> Just (WordNumberSample a, b)
DoubleNumberSample a ->
\ case
-- This is what it's all for.
A.Number b | realToFrac b == if isNaN a || isInfinite a then 0 else a -> Nothing
b -> Just (DoubleNumberSample a, b)
ScientificNumberSample a ->
\ case
A.Number b | a == b -> Nothing
b -> Just (ScientificNumberSample a, b)
TextStringSample a ->
\ case
A.String b | a == b -> Nothing
b -> Just (TextStringSample a, b)
ScientificStringSample a ->
\ case
A.String b | (fromString . show) a == b -> Nothing
b -> Just (ScientificStringSample a, b)
ArraySample a ->
\ case
A.Array b | Vector.length b == length a ->
toList b
& zip a
& foldMap (\ (aa, bb) -> fmap First (detectMismatchInSampleAndAeson aa bb))
& fmap getFirst
b -> Just (ArraySample a, b)
ObjectSample a ->
\ case
A.Object b ->
a & foldMap (\ (ak, av) ->
case AesonKeyMap.lookup (AesonKey.fromText ak) b of
Just bv -> fmap First (detectMismatchInSampleAndAeson av bv)
Nothing -> Just (First (ObjectSample a, A.Object b))
)
& fmap getFirst
b -> Just (ObjectSample a, b)