on master help sign in
Merge pull request #429 from phadej/more-examples
33515cdAdam Bergmark
aeson.cabal+1
@@ -161,6 +161,7 @@ test-suite tests
161 generic-deriving >= 1.10 && < 1.11,
161 generic-deriving >= 1.10 && < 1.11,
162 ghc-prim >= 0.2,
162 ghc-prim >= 0.2,
163 hashable >= 1.2.4.0,
163 hashable >= 1.2.4.0,
164+ scientific,
164 tagged,
165 tagged,
165 template-haskell,
166 template-haskell,
166 test-framework,
167 test-framework,
tests/UnitTests.hs+65−21
@@ -27,6 +27,7 @@ import Data.Functor.Compose (Compose (..))
27import Data.Functor.Identity (Identity (..))
27import Data.Functor.Identity (Identity (..))
28import Data.Maybe (fromMaybe)
28import Data.Maybe (fromMaybe)
29import Data.Proxy (Proxy(..))
29import Data.Proxy (Proxy(..))
30+import Data.Fixed (Pico)
30import Data.Sequence (Seq)
31import Data.Sequence (Seq)
31import Data.Tagged (Tagged(..))
32import Data.Tagged (Tagged(..))
32import Data.Text (Text)
33import Data.Text (Text)
@@ -38,16 +39,24 @@ import Test.Framework (Test, testGroup)
38import Test.Framework.Providers.HUnit (testCase)
39import Test.Framework.Providers.HUnit (testCase)
39import Test.HUnit (Assertion, assertFailure, assertEqual)
40import Test.HUnit (Assertion, assertFailure, assertEqual)
40import UnitTests.NullaryConstructors (nullaryConstructors)
41import UnitTests.NullaryConstructors (nullaryConstructors)
42+import Data.Word (Word8)
43+import Data.Scientific (Scientific)
41import qualified Data.ByteString.Lazy.Char8 as L
44import qualified Data.ByteString.Lazy.Char8 as L
42import qualified Data.Text.Lazy.Builder as TLB
45import qualified Data.Text.Lazy.Builder as TLB
43import qualified Data.Text.Lazy.Encoding as TLE
46import qualified Data.Text.Lazy.Encoding as TLE
44import qualified Data.Text.Lazy as LT
47import qualified Data.Text.Lazy as LT
45import qualified Data.Text.Lazy.Encoding as LT
48import qualified Data.Text.Lazy.Encoding as LT
46
49
47−import qualified Data.Sequence as Seq
48import qualified Data.DList as DList
50import qualified Data.DList as DList
49import qualified Data.HashMap.Strict as HM
51import qualified Data.HashMap.Strict as HM
52+import qualified Data.HashSet as HashSet
53+import qualified Data.IntMap as IntMap
54+import qualified Data.IntSet as IntSet
50import qualified Data.Map as M
55import qualified Data.Map as M
56+import qualified Data.Sequence as Seq
57+import qualified Data.Set as Set
58+import qualified Data.Tree as Tree
59+import qualified Data.Vector as Vector
51
60
52tests :: Test
61tests :: Test
53tests = testGroup "unit" [
62tests = testGroup "unit" [
@@ -74,8 +83,8 @@ tests = testGroup "unit" [
74 testCase "example 1" $ formatErrorExample
83 testCase "example 1" $ formatErrorExample
75 ]
84 ]
76 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
85 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
77− , testGroup "To JSON representation" $ fmap (testCase "-") jsonEncoding
86+ , testGroup "To JSON representation" $ fmap assertJsonEncodingExample jsonEncodingExamples
78− , testGroup "From JSON representation" $ fmap (testCase "-") jsonDecoding
87+ , testGroup "From JSON representation" $ fmap assertJsonDecodingExample jsonDecodingExamples
79 , testGroup "To/From JSON representation" $ fmap assertJsonExample jsonExamples
88 , testGroup "To/From JSON representation" $ fmap assertJsonExample jsonExamples
80 , testGroup "JSONPath" $ fmap (testCase "-") jsonPath
89 , testGroup "JSONPath" $ fmap (testCase "-") jsonPath
81 , testGroup "Hashable laws" $ fmap (testCase "-") hashableLaws
90 , testGroup "Hashable laws" $ fmap (testCase "-") hashableLaws
@@ -240,15 +249,6 @@ dotColonMark = [
240-- These tests assert that the JSON serialization doesn't change by accident.
249-- These tests assert that the JSON serialization doesn't change by accident.
241-----------------------------------------------------------------------------
250-----------------------------------------------------------------------------
242
251
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−
252data Example where
252data Example where
253 Example
253 Example
254 :: (Eq a, Show a, ToJSON a, FromJSON a)
254 :: (Eq a, Show a, ToJSON a, FromJSON a)
@@ -256,9 +256,45 @@ data Example where
256
256
257assertJsonExample :: Example -> Test
257assertJsonExample :: Example -> Test
258assertJsonExample (Example name bs val) = testCase name $ do
258assertJsonExample (Example name bs val) = testCase name $ do
259− assertEqual "encode" bs (encode val)
259+ assertEqual "encode" bs (encode val)
260+ assertEqual "encode/via value" bs (encode $ toJSON val)
261+ assertEqual "decode" (Just val) (decode bs)
262+
263+assertJsonEncodingExample :: Example -> Test
264+assertJsonEncodingExample (Example name bs val) = testCase name $ do
265+ assertEqual "encode" bs (encode val)
266+ assertEqual "encode/via value" bs (encode $ toJSON val)
267+
268+assertJsonDecodingExample :: Example -> Test
269+assertJsonDecodingExample (Example name bs val) = testCase name $
260 assertEqual "decode" (Just val) (decode bs)
270 assertEqual "decode" (Just val) (decode bs)
261
271
272+jsonEncodingExamples :: [Example]
273+jsonEncodingExamples =
274+ [
275+ -- Maybe serialising is lossy
276+ -- https://github.com/bos/aeson/issues/376
277+ Example "Just Nothing" "null" (Just Nothing :: Maybe (Maybe Int))
278+ -- infinities cannot be recovered, null is decoded as NaN
279+ , Example "inf :: Double" "null" (Approx $ 1/0 :: Approx Double)
280+ ]
281+
282+jsonDecodingExamples :: [Example]
283+jsonDecodingExamples = [
284+ -- Maybe serialising is lossy
285+ -- https://github.com/bos/aeson/issues/376
286+ Example "Nothing" "null" (Nothing :: Maybe Int)
287+ , Example "Just" "1" (Just 1 :: Maybe Int)
288+ , Example "Just Nothing" "null" (Nothing :: Maybe (Maybe Int))
289+ -- Integral values are truncated, and overflowed
290+ -- https://github.com/bos/aeson/issues/317
291+ , Example "Word8 3" "3" (Just 3 :: Maybe Word8)
292+ , Example "Word8 3.00" "3.00" (Just 3 :: Maybe Word8)
293+ , Example "Word8 3.14" "3.14" (Just 3 :: Maybe Word8) -- TODO: should be Nothing
294+ , Example "Word8 -1" "-1" (Just 255 :: Maybe Word8) -- TODO: should be Nothing
295+ , Example "Word8 300" "300" (Just 44 :: Maybe Word8) -- TODO: should be Nothing
296+ ]
297+
262jsonExamples :: [Example]
298jsonExamples :: [Example]
263jsonExamples =
299jsonExamples =
264 [
300 [
@@ -288,6 +324,21 @@ jsonExamples =
288
324
289 , Example "nan :: Double" "null" (Approx $ 0/0 :: Approx Double)
325 , Example "nan :: Double" "null" (Approx $ 0/0 :: Approx Double)
290
326
327+ , Example "Ordering LT" "\"LT\"" LT
328+ , Example "Ordering EQ" "\"EQ\"" EQ
329+ , Example "Ordering GT" "\"GT\"" GT
330+
331+ , Example "Float" "3.14" (3.14 :: Float)
332+ , Example "Pico" "3.14" (3.14 :: Pico)
333+ , Example "Scientific" "3.14" (3.14 :: Scientific)
334+
335+ , Example "Set Int" "[1,2,3]" (Set.fromList [3, 2, 1] :: Set.Set Int)
336+ , Example "IntSet" "[1,2,3]" (IntSet.fromList [3, 2, 1])
337+ , Example "IntMap" "[[1,2],[3,4]]" (IntMap.fromList [(3,4), (1,2)] :: IntMap.IntMap Int)
338+ , Example "Vector" "[1,2,3]" (Vector.fromList [1, 2, 3] :: Vector.Vector Int)
339+ , Example "HashSet Int" "[1,2,3]" (HashSet.fromList [3, 2, 1] :: HashSet.HashSet Int)
340+ , Example "Tree Int" "[1,[[2,[[3,[]],[4,[]]]],[5,[]]]]" (let n = Tree.Node in n 1 [n 2 [n 3 [], n 4 []], n 5 []] :: Tree.Tree Int)
341+
291 -- Three separate cases, as ordering in HashMap is not defined
342 -- Three separate cases, as ordering in HashMap is not defined
292 , Example "HashMap Float Int, NaN" "{\"NaN\":1}" (Approx $ HM.singleton (0/0) 1 :: Approx (HM.HashMap Float Int))
343 , Example "HashMap Float Int, NaN" "{\"NaN\":1}" (Approx $ HM.singleton (0/0) 1 :: Approx (HM.HashMap Float Int))
293 , Example "HashMap Float Int, Infinity" "{\"Infinity\":1}" (HM.singleton (1/0) 1 :: HM.HashMap Float Int)
344 , Example "HashMap Float Int, Infinity" "{\"Infinity\":1}" (HM.singleton (1/0) 1 :: HM.HashMap Float Int)
@@ -333,14 +384,7 @@ jsonExamples =
333 , Example "Compose3' [] [] [] Char" "[[\"x\"]]" (pure 'x' :: Compose3' [] [] [] Char)
384 , Example "Compose3' [] [] [] Char" "[[\"x\"]]" (pure 'x' :: Compose3' [] [] [] Char)
334 ]
385 ]
335
386
336−jsonDecoding :: [Assertion]
387+
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− ]
344
388
345------------------------------------------------------------------------------
389------------------------------------------------------------------------------
346-- These tests check that JSONPath is tracked correctly
390-- These tests check that JSONPath is tracked correctly