on 0.3.2.3 help sign in
typeable/aeson/tests/Properties.hs
36 lines 1.2 kB history raw
1{-# LANGUAGE ScopedTypeVariables #-}
3import Data.Aeson.Encode
4import Data.Aeson.Parser (value)
5import Data.Aeson.Types
6import Data.Attoparsec.Number
7import Test.Framework (defaultMain, testGroup)
8import Test.Framework.Providers.QuickCheck2 (testProperty)
9import Test.QuickCheck (Arbitrary(..))
10import Test.QuickCheck.Monadic (assert, monadicIO, run)
11import qualified Data.ByteString.Lazy.Char8 as L
12import qualified Data.Attoparsec.Lazy as L
14encodeDouble d = encode (Number (D d)) == L.pack (show d)
15encodeInteger i = encode (Number (I i)) == L.pack (show i)
17roundTrip :: (Eq a, FromJSON a, ToJSON a) => a -> Bool
18roundTrip i = (fmap fromJSON . L.maybeResult . L.parse value . encode . toJSON) i == Just (Success i)
20roundTripBool (v::Bool) = roundTrip v
21roundTripDouble (v::Double) = roundTrip v
22roundTripInteger (v::Integer) = roundTrip v
24main = defaultMain tests
26tests = [
27 testGroup "encode" [
28 testProperty "encodeDouble" encodeDouble
29 , testProperty "encodeInteger" encodeInteger
30 ],
31 testGroup "roundTrip" [
32 testProperty "roundTripBool" roundTripBool
33 , testProperty "roundTripDouble" roundTripDouble
34 , testProperty "roundTripInteger" roundTripInteger
35 ]
36 ]