on master help sign in
Add some basic round-trip tests.
commit9213f47e4870f426db108002bc703fd7ac63d685 authorBryan O'Sullivan <bos@mailrank.com> dateTue 22 Mar 2011, 10:13 (16 years ago) parent7331e66
1 file changed+16
tests/Properties.hs+16
tests/Properties.hs+16
@@ -1,4 +1,7 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
2+
13import Data.Aeson.Encode
4+import Data.Aeson.Parser (value)
25import Data.Aeson.Types
36import Data.Attoparsec.Number
47import Test.Framework (defaultMain, testGroup)
@@ -6,15 +9,28 @@ import Test.Framework.Providers.QuickCheck2 (testProperty)
69import Test.QuickCheck (Arbitrary(..))
710import Test.QuickCheck.Monadic (assert, monadicIO, run)
811import qualified Data.ByteString.Lazy.Char8 as L
12+import qualified Data.Attoparsec.Lazy as L
913
1014encodeDouble d = encode (Number (D d)) == L.pack (show d)
1115encodeInteger i = encode (Number (I i)) == L.pack (show i)
1216
17+roundTrip :: (Eq a, FromJSON a, ToJSON a) => a -> Bool
18+roundTrip i = (fmap fromJSON . L.maybeResult . L.parse value . encode . toJSON) i == Just (Success i)
19+
20+roundTripBool (v::Bool) = roundTrip v
21+roundTripDouble (v::Double) = roundTrip v
22+roundTripInteger (v::Integer) = roundTrip v
23+
1324main = defaultMain tests
1425
1526tests = [
1627 testGroup "encode" [
1728 testProperty "encodeDouble" encodeDouble
1829 , testProperty "encodeInteger" encodeInteger
30+ ],
31+ testGroup "roundTrip" [
32+ testProperty "roundTripBool" roundTripBool
33+ , testProperty "roundTripDouble" roundTripDouble
34+ , testProperty "roundTripInteger" roundTripInteger
1935 ]
2036 ]