@@ -1,9 +1,10 @@
11module JRecSpec (spec) where
22
33import Control.Lens ((&), (.~), (^.))
4+import Data.Aeson
5+import GHC.Stack
46import JRec
57import Test.Hspec
6−import GHC.Stack
78
89data Pair a = Pair !a !a
910 deriving (Eq, Show)
@@ -16,10 +17,10 @@ spec = do
1617 Pair (Rec (#a := 1)) (Rec (#a := 2))
1718 `shouldNotBe` Pair (Rec (#a := 2)) (Rec (#a := 2))
1819 it "polymorphic" $ do
19− (Rec (#u := True, #a := 5, #b := 6, #a := 2 ) & #u .~ 5)
20+ (Rec (#u := True, #a := 5, #b := 6, #a := 2) & #u .~ 5)
2021 `shouldBe` Rec (#u := 5, #a := 5, #b := 6, #a := 2)
21− describe "eq" $ do
22− -- eq can compare only the first matching field, discarding the rest.
22+ describe "eq" $ do
23+ -- eq can compare only the first matching field, discarding the rest.
2324 it "fails if first matching field doesn't compare" $ do
2425 Rec (#a := 1, #a := 2) `shouldNotBe` Rec (#a := 0, #a := 2)
2526 it "succeeds if first matching field compares" $ do
@@ -76,8 +77,8 @@ spec = do
7677 r2 = Rec (#c := 7, #a := 8)
7778 r1 `union` r2
7879 `shouldBe` Rec (#b := 5, #a := 6, #c := 7)
79− describe "insert" $ do
80− it "simple insert" $ do
80+ describe "insert" $ do
81+ it "simple insert" $ do
8182 (#a := 1) `insert` Rec (#b := 2, #c := 3)
8283 `shouldBe` Rec (#a := 1, #b := 2, #c := 3)
8384 describe "insertOrSet" $ do
@@ -87,3 +88,17 @@ spec = do
8788 it "overwrite" $ do
8889 insertOrSet (#c := 1) (Rec (#b := 2, #c := 3))
8990 `shouldBe` Rec (#b := 2, #c := 1)
91+ describe "optional fields" $ do
92+ it "encode" $ do
93+ encode (Rec (#a := (1 :: Int), #b := (Nothing :: Maybe Int)))
94+ `shouldBe` "{\"a\":1,\"b\":null}"
95+ it "encode" $ do
96+ encode (Rec (#a := (1 :: Int), #b := (Just 2 :: Maybe Int)))
97+ `shouldBe` "{\"a\":1,\"b\":2}"
98+ it "decode" $ do
99+ decode "{\"a\": 1, \"b\": null}"
100+ `shouldBe` Just (Rec (#a := (1 :: Int), #b := (Nothing :: Maybe Int)))
101+ decode "{\"a\": 1}"
102+ `shouldBe` Just (Rec (#a := (1 :: Int), #b := (Nothing :: Maybe Int)))
103+ decode "{\"a\": 1, \"b\": 2}"
104+ `shouldBe` Just (Rec (#a := (1 :: Int), #b := (Just 2 :: Maybe Int)))