on master help sign in
Add tests for .:? and .:!
commit2f638fd0c53cf5bf5cbe386bda339caf340a8021 authorOleg Grenrus <oleg.grenrus@iki.fi> dateSat 19 Sep 2015, 18:41 (11 years ago) parent921b759
1 file changed+34−3
tests/UnitTests.hs+34−3
tests/UnitTests.hs+34−3
@@ -5,10 +5,10 @@
55module UnitTests (ioTests, tests) where
66
77import Control.Monad (forM)
8−import Data.Aeson (decode, eitherDecode, encode, genericToJSON, genericToEncoding)
9−import Data.Aeson.TH ( deriveJSON )
8+import Data.Aeson (decode, eitherDecode, encode, genericToJSON, genericToEncoding, FromJSON(..), withObject, (.:), (.:?), (.:!))
109import Data.Aeson.Encode (encodeToTextBuilder)
11−import Data.Aeson.Types (ToJSON(..), FromJSON, Value, camelTo, camelTo2, defaultOptions, omitNothingFields)
10+import Data.Aeson.TH (deriveJSON)
11+import Data.Aeson.Types (ToJSON(..), Value, camelTo, camelTo2, defaultOptions, omitNothingFields)
1212import Data.Char (toUpper)
1313import Data.Time (UTCTime)
1414import Data.Time.Format (parseTime)
@@ -49,6 +49,7 @@ tests = testGroup "unit" [
4949 testCase "good" $ utcTimeGood
5050 , testCase "bad" $ utcTimeBad
5151 ]
52+ , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
5253 ]
5354
5455roundTripCamel :: String -> Assertion
@@ -148,6 +149,36 @@ utcTimeBad = do
148149 let (dec :: Maybe UTCTime) = decode . LT.encodeUtf8 $ (LT.concat ["\"", s, "\""]) in
149150 assertEqual "verify failure" Nothing dec
150151
152+------------------------------------------------------------------------------
153+-- Comparison (.:?) and (.:!)
154+------------------------------------------------------------------------------
155+
156+newtype T1 = T1 (Maybe Int) deriving (Eq, Show)
157+newtype T2 = T2 (Maybe Int) deriving (Eq, Show)
158+newtype T3 = T3 (Maybe Int) deriving (Eq, Show)
159+
160+instance FromJSON T1 where parseJSON = fmap T1 . withObject "T1" (.: "value")
161+instance FromJSON T2 where parseJSON = fmap T2 . withObject "T2" (.:? "value")
162+instance FromJSON T3 where parseJSON = fmap T3 . withObject "T3" (.:! "value")
163+
164+dotColonMark :: [Assertion]
165+dotColonMark = [
166+ assertEqual ".: not-present" Nothing (decode ex1 :: Maybe T1)
167+ , assertEqual ".: 42" (Just (T1 (Just 42))) (decode ex2 :: Maybe T1)
168+ , assertEqual ".: null" (Just (T1 Nothing)) (decode ex3 :: Maybe T1)
169+
170+ , assertEqual ".:? not-present" (Just (T2 (Nothing))) (decode ex1 :: Maybe T2)
171+ , assertEqual ".:? 42" (Just (T2 (Just 42))) (decode ex2 :: Maybe T2)
172+ , assertEqual ".:? null" (Just (T2 Nothing)) (decode ex3 :: Maybe T2)
173+
174+ , assertEqual ".:! not-present" (Just (T3 (Nothing))) (decode ex1 :: Maybe T3)
175+ , assertEqual ".:! 42" (Just (T3 (Just 42))) (decode ex2 :: Maybe T3)
176+ , assertEqual ".:! null" Nothing (decode ex3 :: Maybe T3)
177+ ]
178+ where ex1 = "{}"
179+ ex2 = "{\"value\": 42 }"
180+ ex3 = "{\"value\": null }"
181+
151182
152183------------------------------------------------------------------------------
153184-- Comparison between bytestring and text encoders