@@ -27,6 +27,7 @@ import Data.Functor.Compose (Compose (..))
2727import Data.Functor.Identity (Identity (..))
2828import Data.Maybe (fromMaybe)
2929import Data.Proxy (Proxy(..))
30+import Data.Fixed (Pico)
3031import Data.Sequence (Seq)
3132import Data.Tagged (Tagged(..))
3233import Data.Text (Text)
@@ -38,16 +39,24 @@ import Test.Framework (Test, testGroup)
3839import Test.Framework.Providers.HUnit (testCase)
3940import Test.HUnit (Assertion, assertFailure, assertEqual)
4041import UnitTests.NullaryConstructors (nullaryConstructors)
42+import Data.Word (Word8)
43+import Data.Scientific (Scientific)
4144import qualified Data.ByteString.Lazy.Char8 as L
4245import qualified Data.Text.Lazy.Builder as TLB
4346import qualified Data.Text.Lazy.Encoding as TLE
4447import qualified Data.Text.Lazy as LT
4548import qualified Data.Text.Lazy.Encoding as LT
4649
47−import qualified Data.Sequence as Seq
4850import qualified Data.DList as DList
4951import 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
5055import 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
5160
5261tests :: Test
5362tests = testGroup "unit" [
@@ -74,8 +83,8 @@ tests = testGroup "unit" [
7483 testCase "example 1" $ formatErrorExample
7584 ]
7685 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
77− , testGroup "To JSON representation" $ fmap (testCase "-") jsonEncoding
78− , testGroup "From JSON representation" $ fmap (testCase "-") jsonDecoding
86+ , testGroup "To JSON representation" $ fmap assertJsonEncodingExample jsonEncodingExamples
87+ , testGroup "From JSON representation" $ fmap assertJsonDecodingExample jsonDecodingExamples
7988 , testGroup "To/From JSON representation" $ fmap assertJsonExample jsonExamples
8089 , testGroup "JSONPath" $ fmap (testCase "-") jsonPath
8190 , testGroup "Hashable laws" $ fmap (testCase "-") hashableLaws
@@ -240,15 +249,6 @@ dotColonMark = [
240249-- These tests assert that the JSON serialization doesn't change by accident.
241250-----------------------------------------------------------------------------
242251
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−
252252data Example where
253253 Example
254254 :: (Eq a, Show a, ToJSON a, FromJSON a)
@@ -256,9 +256,45 @@ data Example where
256256
257257assertJsonExample :: Example -> Test
258258assertJsonExample (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 $
260270 assertEqual "decode" (Just val) (decode bs)
261271
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+
262298jsonExamples :: [Example]
263299jsonExamples =
264300 [
@@ -288,6 +324,21 @@ jsonExamples =
288324
289325 , Example "nan :: Double" "null" (Approx $ 0/0 :: Approx Double)
290326
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+
291342 -- Three separate cases, as ordering in HashMap is not defined
292343 , Example "HashMap Float Int, NaN" "{\"NaN\":1}" (Approx $ HM.singleton (0/0) 1 :: Approx (HM.HashMap Float Int))
293344 , Example "HashMap Float Int, Infinity" "{\"Infinity\":1}" (HM.singleton (1/0) 1 :: HM.HashMap Float Int)
@@ -333,14 +384,7 @@ jsonExamples =
333384 , Example "Compose3' [] [] [] Char" "[[\"x\"]]" (pure 'x' :: Compose3' [] [] [] Char)
334385 ]
335386
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− ]
387+
344388
345389------------------------------------------------------------------------------
346390-- These tests check that JSONPath is tracked correctly