on master help sign in
Add golden tests for error messages with Generic FromJSON
commitb799d6da97945ffc093ab437f2623c210eb51b4d authorLysxia <lysxia@gmail.com> dateSat 28 Apr 2018, 18:14 (8 years ago) parent42a9b4b
3 files changed+131
tests/Encoders.hs+10 tests/ErrorMessages.hs+86 tests/golden/generic.expected+35
tests/Encoders.hs+10
@@ -414,3 +414,13 @@ gOneConstructorToEncodingTagged = genericToEncoding optsTagSingleConstructors
414414
415415gOneConstructorParseJSONTagged :: Value -> Parser OneConstructor
416416gOneConstructorParseJSONTagged = genericParseJSON optsTagSingleConstructors
417+
418+--------------------------------------------------------------------------------
419+-- Product2 encoders/decoders
420+--------------------------------------------------------------------------------
421+
422+thProduct2ParseJSON :: Value -> Parser (Product2 Int Bool)
423+thProduct2ParseJSON = $(mkParseJSON defaultOptions ''Product2)
424+
425+gProduct2ParseJSON :: Value -> Parser (Product2 Int Bool)
426+gProduct2ParseJSON = genericParseJSON defaultOptions
tests/ErrorMessages.hs+86
@@ -24,9 +24,13 @@ import Test.Tasty.Golden.Advanced (goldenTest)
2424import qualified Data.ByteString.Lazy.Char8 as L
2525import qualified Data.HashMap.Strict as HM
2626
27+import Encoders
28+import Types
29+
2730tests :: [TestTree]
2831tests =
2932 [ aesonGoldenTest "simple" "tests/golden/simple.expected" output
33+ , aesonGoldenTest "generic" "tests/golden/generic.expected" (outputGeneric G)
3034 ]
3135
3236output :: Output
@@ -70,6 +74,85 @@ output = concat
7074 ]
7175 ]
7276
77+data Choice = TH | G
78+
79+outputGeneric :: Choice -> Output
80+outputGeneric choice = concat
81+ [ testWith "OneConstructor"
82+ (select
83+ thOneConstructorParseJSONDefault
84+ gOneConstructorParseJSONDefault)
85+ [ "\"X\""
86+ , "[0]"
87+ ]
88+
89+ , testWith "Nullary"
90+ (select
91+ thNullaryParseJSONString
92+ gNullaryParseJSONString)
93+ [ "\"X\""
94+ , "[]"
95+ ]
96+
97+ , testWithSomeType "SomeType (tagged)"
98+ (select
99+ thSomeTypeParseJSONTaggedObject
100+ gSomeTypeParseJSONTaggedObject)
101+ [ "{\"tag\": \"unary\", \"contents\": true}"
102+ , "{\"tag\": \"unary\"}"
103+ , "{\"tag\": \"record\"}"
104+ , "{\"tag\": \"record\", \"testone\": true, \"testtwo\": null, \"testthree\": null}"
105+ , "{\"tag\": \"X\"}"
106+ , "{}"
107+ , "[]"
108+ ]
109+
110+ , testWithSomeType "SomeType (single-field)"
111+ (select
112+ thSomeTypeParseJSONObjectWithSingleField
113+ gSomeTypeParseJSONObjectWithSingleField)
114+ [ "{\"unary\": {}}"
115+ , "{\"unary\": []}"
116+ , "{\"X\": []}"
117+ , "{\"record\": {}, \"W\":{}}"
118+ , "{}"
119+ , "[]"
120+ ]
121+
122+ , testWithSomeType "SomeType (two-element array)"
123+ (select
124+ thSomeTypeParseJSON2ElemArray
125+ gSomeTypeParseJSON2ElemArray)
126+ [ "[\"unary\", true]"
127+ , "[\"record\", null]"
128+ , "[\"X\", 0]"
129+ , "[null, 0]"
130+ , "[]"
131+ , "{}"
132+ ]
133+
134+ , testWith "EitherTextInt"
135+ (select
136+ thEitherTextIntParseJSONUntaggedValue
137+ gEitherTextIntParseJSONUntaggedValue)
138+ [ "\"X\""
139+ , "[]"
140+ ]
141+
142+ , testWith "Product2 Int Bool"
143+ (select
144+ thProduct2ParseJSON
145+ gProduct2ParseJSON)
146+ [ "[1, null]"
147+ , "[]"
148+ , "{}"
149+ ]
150+ ]
151+ where
152+ select a b = case choice of
153+ TH -> a
154+ G -> b
155+
73156-- Test infrastructure
74157
75158type Output = [String]
@@ -105,3 +188,6 @@ testWith name parser ts =
105188testFor :: forall a proxy. (FromJSON a, Show a)
106189 => String -> proxy a -> [L.ByteString] -> Output
107190testFor name _ = testWith name (parseJSON :: Value -> Parser a)
191+
192+testWithSomeType :: String -> (Value -> Parser (SomeType Int)) -> [L.ByteString] -> Output
193+testWithSomeType = testWith
tests/golden/generic.expectednew file+35
@@ -0,0 +1,35 @@
1+OneConstructor
2+Error in $: parsing Types.OneConstructor(OneConstructor) failed, expected Array, but encountered String
3+Error in $: parsing Types.OneConstructor(OneConstructor) failed, expected an empty Array, but encountered an Array of length 1
4+Nullary
5+Error in $: parsing Types.Nullary failed, expected one of the tags ["C1","C2","C3"], but found tag "X"
6+Error in $: parsing Types.Nullary failed, expected String, but encountered Array
7+SomeType (tagged)
8+Error in $.contents: parsing Int failed, expected Number, but encountered Boolean
9+Error in $: parsing Types.SomeType(Unary) failed, key "contents" not present
10+Error in $: parsing Types.SomeType(Record) failed, key "testone" not present
11+Error in $.testone: parsing Double failed, unexpected Boolean
12+Error in $.tag: parsing Types.SomeType failed, expected tag field to be one of ["nullary","unary","product","record","list"], but found tag "X"
13+Error in $: parsing Types.SomeType failed, key "tag" not present
14+Error in $: parsing Types.SomeType failed, expected Object, but encountered Array
15+SomeType (single-field)
16+Error in $.unary: parsing Int failed, expected Number, but encountered Object
17+Error in $.unary: parsing Int failed, expected Number, but encountered Array
18+Error in $: parsing Types.SomeType failed, expected an Object with a single pair where the tag is one of ["nullary","unary","product","record","list"], but found tag "X"
19+Error in $: parsing Types.SomeType failed, expected an Object with a single pair, but found 2 pairs
20+Error in $: parsing Types.SomeType failed, expected an Object with a single pair, but found 0 pairs
21+Error in $: parsing Types.SomeType failed, expected Object, but encountered Array
22+SomeType (two-element array)
23+Error in $[1]: parsing Int failed, expected Number, but encountered Boolean
24+Error in $[1]: parsing Types.SomeType(Record) failed, expected Object, but encountered Null
25+Error in $[0]: parsing Types.SomeType failed, expected tag of the 2-element Array to be one of ["nullary","unary","product","record","list"], but found tag "X"
26+Error in $[0]: parsing Types.SomeType failed, tag element is not a String
27+Error in $: parsing Types.SomeType failed, expected a 2-element Array, but encountered an Array of length 0
28+Error in $: parsing Types.SomeType failed, expected Array, but encountered Object
29+EitherTextInt
30+Error in $: parsing Types.EitherTextInt(NoneNullary) failed, expected tag "nonenullary", but found tag "X"
31+Error in $: parsing Types.EitherTextInt(NoneNullary) failed, expected String, but encountered Array
32+Product2 Int Bool
33+Error in $[1]: expected Bool, but encountered Null
34+Error in $: parsing Types.Product2(Product2) failed, expected an Array of length 2, but encountered an Array of length 0
35+Error in $: parsing Types.Product2(Product2) failed, expected Array, but encountered Object