@@ -1,9 +1,10 @@
1module JRecSpec (spec) where
1module JRecSpec (spec) where
3import Control.Lens ((&), (.~), (^.))
3import Control.Lens ((&), (.~), (^.))
5import Test.Hspec
7import Test.Hspec
8data Pair a = Pair !a !a
9data Pair a = Pair !a !a
9 deriving (Eq, Show)
10 deriving (Eq, Show)
@@ -16,10 +17,10 @@ spec = do
16 Pair (Rec (#a := 1)) (Rec (#a := 2))
17 Pair (Rec (#a := 1)) (Rec (#a := 2))
17 `shouldNotBe` Pair (Rec (#a := 2)) (Rec (#a := 2))
18 `shouldNotBe` Pair (Rec (#a := 2)) (Rec (#a := 2))
18 it "polymorphic" $ do
19 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)
20 `shouldBe` Rec (#u := 5, #a := 5, #b := 6, #a := 2)
21 `shouldBe` Rec (#u := 5, #a := 5, #b := 6, #a := 2)
21− describe "eq" $ do
22+ describe "eq" $ do
22− -- eq can compare only the first matching field, discarding the rest.
23+ -- eq can compare only the first matching field, discarding the rest.
23 it "fails if first matching field doesn't compare" $ do
24 it "fails if first matching field doesn't compare" $ do
24 Rec (#a := 1, #a := 2) `shouldNotBe` Rec (#a := 0, #a := 2)
25 Rec (#a := 1, #a := 2) `shouldNotBe` Rec (#a := 0, #a := 2)
25 it "succeeds if first matching field compares" $ do
26 it "succeeds if first matching field compares" $ do
@@ -76,8 +77,8 @@ spec = do
76 r2 = Rec (#c := 7, #a := 8)
77 r2 = Rec (#c := 7, #a := 8)
77 r1 `union` r2
78 r1 `union` r2
78 `shouldBe` Rec (#b := 5, #a := 6, #c := 7)
79 `shouldBe` Rec (#b := 5, #a := 6, #c := 7)
79− describe "insert" $ do
80+ describe "insert" $ do
80− it "simple insert" $ do
81+ it "simple insert" $ do
81 (#a := 1) `insert` Rec (#b := 2, #c := 3)
82 (#a := 1) `insert` Rec (#b := 2, #c := 3)
82 `shouldBe` Rec (#a := 1, #b := 2, #c := 3)
83 `shouldBe` Rec (#a := 1, #b := 2, #c := 3)
83 describe "insertOrSet" $ do
84 describe "insertOrSet" $ do
@@ -87,3 +88,17 @@ spec = do
87 it "overwrite" $ do
88 it "overwrite" $ do
88 insertOrSet (#c := 1) (Rec (#b := 2, #c := 3))
89 insertOrSet (#c := 1) (Rec (#b := 2, #c := 3))
89 `shouldBe` Rec (#b := 2, #c := 1)
90 `shouldBe` Rec (#b := 2, #c := 1)
91+ describe "optional fields" $ do
93+ encode (Rec (#a := (1 :: Int), #b := (Nothing :: Maybe Int)))
94+ `shouldBe` "{\"a\":1,\"b\":null}"
96+ encode (Rec (#a := (1 :: Int), #b := (Just 2 :: Maybe Int)))
97+ `shouldBe` "{\"a\":1,\"b\":2}"
99+ decode "{\"a\": 1, \"b\": null}"
100+ `shouldBe` Just (Rec (#a := (1 :: Int), #b := (Nothing :: Maybe Int)))
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)))