on master help sign in
Refactor examples
commit2081a14d07994c702aa700429508cd40154ad521 authorOleg Grenrus <oleg.grenrus@iki.fi> dateTue 14 Jun 2016, 08:27 (10 years ago) parent599aa8f
1 file changed+33−20
tests/UnitTests.hs+33−20
tests/UnitTests.hs+33−20
@@ -74,8 +74,8 @@ tests = testGroup "unit" [
7474 testCase "example 1" $ formatErrorExample
7575 ]
7676 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
77− , testGroup "To JSON representation" $ fmap (testCase "-") jsonEncoding
78− , testGroup "From JSON representation" $ fmap (testCase "-") jsonDecoding
77+ , testGroup "To JSON representation" $ fmap assertJsonEncodingExample jsonEncodingExamples
78+ , testGroup "From JSON representation" $ fmap assertJsonDecodingExample jsonDecodingExamples
7979 , testGroup "To/From JSON representation" $ fmap assertJsonExample jsonExamples
8080 , testGroup "JSONPath" $ fmap (testCase "-") jsonPath
8181 , testGroup "Hashable laws" $ fmap (testCase "-") hashableLaws
@@ -240,15 +240,6 @@ dotColonMark = [
240240-- These tests assert that the JSON serialization doesn't change by accident.
241241-----------------------------------------------------------------------------
242242
243−jsonEncoding :: [Assertion]
244−jsonEncoding =
245− [
246− -- https://github.com/bos/aeson/issues/376
247− assertEqual "Just Nothing" "null" $ encode (Just Nothing :: Maybe (Maybe Int))
248− -- infinities cannot be recovered, null is decoded as NaN
249− , assertEqual "inf :: Double" "null" $ encode (Approx $ 1/0 :: Approx Double)
250− ]
251−
252243data Example where
253244 Example
254245 :: (Eq a, Show a, ToJSON a, FromJSON a)
@@ -256,9 +247,38 @@ data Example where
256247
257248assertJsonExample :: Example -> Test
258249assertJsonExample (Example name bs val) = testCase name $ do
259− assertEqual "encode" bs (encode val)
250+ assertEqual "encode" bs (encode val)
251+ assertEqual "encode/via value" bs (encode $ toJSON val)
252+ assertEqual "decode" (Just val) (decode bs)
253+
254+assertJsonEncodingExample :: Example -> Test
255+assertJsonEncodingExample (Example name bs val) = testCase name $ do
256+ assertEqual "encode" bs (encode val)
257+ assertEqual "encode/via value" bs (encode $ toJSON val)
258+
259+assertJsonDecodingExample :: Example -> Test
260+assertJsonDecodingExample (Example name bs val) = testCase name $
260261 assertEqual "decode" (Just val) (decode bs)
261262
263+jsonEncodingExamples :: [Example]
264+jsonEncodingExamples =
265+ [
266+ -- Maybe serialising is lossy
267+ -- https://github.com/bos/aeson/issues/376
268+ Example "Just Nothing" "null" (Just Nothing :: Maybe (Maybe Int))
269+ -- infinities cannot be recovered, null is decoded as NaN
270+ , Example "inf :: Double" "null" (Approx $ 1/0 :: Approx Double)
271+ ]
272+
273+jsonDecodingExamples :: [Example]
274+jsonDecodingExamples = [
275+ -- Maybe serialising is lossy
276+ -- https://github.com/bos/aeson/issues/376
277+ Example "Nothing" "null" (Nothing :: Maybe Int)
278+ , Example "Just" "1" (Just 1 :: Maybe Int)
279+ , Example "Just Nothing" "null" (Nothing :: Maybe (Maybe Int))
280+ ]
281+
262282jsonExamples :: [Example]
263283jsonExamples =
264284 [
@@ -333,14 +353,7 @@ jsonExamples =
333353 , Example "Compose3' [] [] [] Char" "[[\"x\"]]" (pure 'x' :: Compose3' [] [] [] Char)
334354 ]
335355
336−jsonDecoding :: [Assertion]
337−jsonDecoding = [
338− assertEqual "Nothing" (Nothing :: Maybe Int) (decode "null")
339− , assertEqual "Just" (Just 1 :: Maybe Int) (decode "1")
340− , assertEqual "Just Nothing" (Just Nothing :: Maybe (Maybe Int)) (decode "null")
341− , assertEqual "NonEmpty" (Just (1 :| [2, 3]) :: Maybe (NonEmpty Int)) (decode "[1,2,3]")
342− , assertEqual "()" (Just ()) (decode "[]")
343− ]
356+
344357
345358------------------------------------------------------------------------------
346359-- These tests check that JSONPath is tracked correctly