on master help sign in
add aeson test
commitb756acae0ea5da336330ac19a929258c0187a180 authorkana-sama <andrew4chrome@gmail.com> dateFri 9 Oct 2020, 17:40 (6 years ago) parentd56df0e
2 files changed+23−7
jrec.cabal+2−1 test/JRecSpec.hs+21−6
jrec.cabal+2−1
@@ -86,7 +86,8 @@ test-suite jrec-test
8686 import: library-common
8787 type: exitcode-stdio-1.0
8888 hs-source-dirs: test, src
89− main-is: Spec.hs
89+ main-is: Spec.hs
90+ cpp-options: -DWITH_AESON
9091 build-depends:
9192 base,
9293 hspec,
test/JRecSpec.hs+21−6
@@ -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)))