1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Main (main, ex) where
import Prelude ()
import Prelude.Compat
import Control.Applicative (Const)
import Data.List.NonEmpty (NonEmpty)
import Data.Proxy (Proxy (..))
import Data.Tagged (Tagged)
import Data.Text (Text)
import Data.Time (Day, LocalTime, NominalDiffTime)
import Data.Version (Version)
import Numeric.Natural (Natural)
import Data.Orphans ()
import Test.QuickCheck.Instances ()
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck
import Data.Aeson.Compat
#if MIN_VERSION_aeson(2,0,0)
import Data.Aeson (Key)
#endif
main :: IO ()
main = defaultMain $ testGroup "Tests"
[ dotColonMark
, testGroup "Roundtrip"
[ testProperty "Day" $ roundtripBroken10 (undefined :: Day)
, testProperty "LocalTime" $ roundtripBroken10 (undefined :: LocalTime)
, testProperty "Version" $ roundtrip (undefined :: Version)
, testProperty "Ordering" $ roundtrip (undefined :: Ordering)
, testProperty "Natural" $ roundtrip (undefined :: Natural)
, testProperty "Const" $ roundtrip (undefined :: Const Int Int)
, testProperty "Proxy" $ roundtrip (undefined :: Proxy Int)
, testProperty "Tagged" $ roundtrip (undefined :: Tagged Int Int)
, testProperty "NonEmpty" $ roundtrip (undefined :: NonEmpty Int)
, testProperty "NominalDiffTime" $ roundtrip (undefined :: NominalDiffTime)
]
]
------------------------------------------------------------------------------
-- Comparison (.:?) and (.:!)
------------------------------------------------------------------------------
newtype T1 = T1 (Maybe Int) deriving (Eq, Show)
newtype T2 = T2 (Maybe Int) deriving (Eq, Show)
newtype T3 = T3 (Maybe Int) deriving (Eq, Show)
instance FromJSON T1 where parseJSON = fmap T1 . withObject "T1" (.: "value")
instance FromJSON T2 where parseJSON = fmap T2 . withObject "T2" (.:? "value")
instance FromJSON T3 where parseJSON = fmap T3 . withObject "T3" (.:! "value")
dotColonMark :: TestTree
dotColonMark = testGroup "Operators" $ fmap t [
assertEqual ".: not-present" Nothing (decode ex1 :: Maybe T1)
, assertEqual ".: 42" (Just (T1 (Just 42))) (decode ex2 :: Maybe T1)
, assertEqual ".: null" (Just (T1 Nothing)) (decode ex3 :: Maybe T1)
, assertEqual ".:? not-present" (Just (T2 (Nothing))) (decode ex1 :: Maybe T2)
, assertEqual ".:? 42" (Just (T2 (Just 42))) (decode ex2 :: Maybe T2)
, assertEqual ".:? null" (Just (T2 Nothing)) (decode ex3 :: Maybe T2)
, assertEqual ".:! not-present" (Just (T3 (Nothing))) (decode ex1 :: Maybe T3)
, assertEqual ".:! 42" (Just (T3 (Just 42))) (decode ex2 :: Maybe T3)
, assertEqual ".:! null" Nothing (decode ex3 :: Maybe T3)
]
where ex1 = "{}"
ex2 = "{\"value\": 42 }"
ex3 = "{\"value\": null }"
t = testCase "-"
roundtrip :: (Arbitrary a, Eq a, Show a, ToJSON a, FromJSON a) => a -> a -> Property
roundtrip _ x = Right x === (eitherDecode . encode $ x)
roundtripBroken10 :: (Arbitrary a, Eq a, Show a, ToJSON a, FromJSON a) => a -> a -> Property
#if MIN_VERSION_aeson(0,10,0) && !MIN_VERSION_aeson(0,11,0)
roundtripBroken10 _ x = property $ case eitherDecode . encode $ x of
Right y -> False && x == y -- x and y of the same type!
Left _ -> True
#else
roundtripBroken10 = roundtrip
#endif
-------------------------------------------------------------------------------
-- tests that symbols are exported
-------------------------------------------------------------------------------
#if MIN_VERSION_aeson(2,0,0)
ex :: (Key, Value)
#else
ex :: (Text, Value)
#endif
ex = "foo" .= True
|