on master help sign in
Use quickcheck-instances (fixes duplicate orphan issues in master)
7739b45Oleg Grenrus
aeson.cabal+2−1
@@ -156,7 +156,8 @@ test-suite tests
156 text,
156 text,
157 time,
157 time,
158 unordered-containers,
158 unordered-containers,
159− vector
159+ vector,
160+ quickcheck-instances >=0.3.12
160
161
161 if flag(old-locale)
162 if flag(old-locale)
162 build-depends: time < 1.5, old-locale
163 build-depends: time < 1.5, old-locale
tests/Instances.hs+9−49
@@ -6,67 +6,27 @@ module Instances where
6import Types
6import Types
7import Data.Function (on)
7import Data.Function (on)
8import Control.Monad
8import Control.Monad
9−import Test.QuickCheck (Arbitrary(..), Gen, choose, getNonNegative, elements,
9+import Test.QuickCheck (Arbitrary(..), getNonNegative, elements,
10 listOf1, oneof, resize)
10 listOf1, oneof, resize)
11−import Data.Time.Clock (DiffTime, UTCTime(..), picosecondsToDiffTime)
11+import Data.Time.Clock (UTCTime(..))
12−import Data.Fixed (Pico)
13−import Data.Time (ZonedTime(..), LocalTime(..), TimeZone(..),
12+import Data.Time (ZonedTime(..), TimeZone(..))
14− hoursToTimeZone, Day(..), TimeOfDay(..),
15− NominalDiffTime)
16import Data.Version
13import Data.Version
17−import qualified Data.Text as T
18−import Data.Text (Text)
19import Data.Aeson.Types
14import Data.Aeson.Types
20import Control.Applicative
15import Control.Applicative
21import Functions
16import Functions
22
17
23−#if !MIN_VERSION_QuickCheck(2,8,2)
18+import Test.QuickCheck.Instances ()
24−import qualified Data.Map as Map
25−
26−instance (Ord k, Arbitrary k, Arbitrary v) => Arbitrary (Map.Map k v) where
27− arbitrary = Map.fromList <$> arbitrary
28−#endif
29
19
30-- "System" types.
20-- "System" types.
31
21
32−instance Arbitrary Text where
33− arbitrary = T.pack <$> arbitrary
34−
35−instance Arbitrary TimeOfDay where
36− arbitrary = do
37− h <- choose (0, 23)
38− m <- choose (0, 59)
39− s <- fromRational . toRational <$> choose (0, 59 :: Double)
40− return $ TimeOfDay h m s
41−
42−instance Arbitrary LocalTime where
43− arbitrary = LocalTime <$> arbitrary <*> arbitrary
44−
45−instance Arbitrary TimeZone where
46− arbitrary = do
47− offset <- choose (0,2) :: Gen Int
48− return $ hoursToTimeZone offset
49−
50−instance Arbitrary Day where
51− arbitrary = ModifiedJulianDay `liftM` arbitrary
52−
53−instance Arbitrary DiffTime where
54− arbitrary = (picosecondsToDiffTime . (* 1000000000)) <$>
55− choose (0, 86400000)
56−
57−instance Arbitrary UTCTime where
58− arbitrary = liftM2 UTCTime arbitrary arbitrary
59−
60instance Arbitrary DotNetTime where
22instance Arbitrary DotNetTime where
61 arbitrary = DotNetTime `liftM` arbitrary
23 arbitrary = DotNetTime `liftM` arbitrary
24+ shrink = map DotNetTime . shrink . fromDotNetTime
62
25
63−instance Arbitrary ZonedTime where
26+-- | Compare timezone part only on 'timeZoneMinutes'
64− arbitrary = liftM2 ZonedTime arbitrary arbitrary
65−
66−instance Arbitrary NominalDiffTime where
67− arbitrary = realToFrac <$> (arbitrary :: Gen Pico)
68−
69−deriving instance Eq ZonedTime
27+instance Eq ZonedTime where
28+ ZonedTime a (TimeZone a' _ _) == ZonedTime b (TimeZone b' _ _) =
29+ a == b && a' == b'
70
30
71-- Compare equality to within a millisecond, allowing for rounding
31-- Compare equality to within a millisecond, allowing for rounding
72-- error (ECMA 262 requires milliseconds to rounded to zero, not
32-- error (ECMA 262 requires milliseconds to rounded to zero, not
tests/Properties.hs+10−2
@@ -16,7 +16,7 @@ import Encoders
16import Instances ()
16import Instances ()
17import Test.Framework (Test, testGroup)
17import Test.Framework (Test, testGroup)
18import Test.Framework.Providers.QuickCheck2 (testProperty)
18import Test.Framework.Providers.QuickCheck2 (testProperty)
19−import Test.QuickCheck (Arbitrary(..), Property, (===), (.&&.))
19+import Test.QuickCheck (Arbitrary(..), Property, (===), (.&&.), counterexample)
20import Types
20import Types
21import qualified Data.Attoparsec.Lazy as L
21import qualified Data.Attoparsec.Lazy as L
22import qualified Data.ByteString.Lazy.Char8 as L
22import qualified Data.ByteString.Lazy.Char8 as L
@@ -59,6 +59,14 @@ roundTripNoEnc eq _ i =
59roundTripEq :: (Eq a, FromJSON a, ToJSON a, Show a) => a -> a -> Property
59roundTripEq :: (Eq a, FromJSON a, ToJSON a, Show a) => a -> a -> Property
60roundTripEq x y = roundTripEnc (===) x y .&&. roundTripNoEnc (===) x y
60roundTripEq x y = roundTripEnc (===) x y .&&. roundTripNoEnc (===) x y
61
61
62+infix 4 ==~
63+(==~) :: (ApproxEq a, Show a) => a -> a -> Property
64+x ==~ y =
65+ counterexample (show x ++ " /= " ++ show y) (x =~ y)
66+
67+roundTripApproxEq :: (ApproxEq a, FromJSON a, ToJSON a, Show a) => a -> a -> Property
68+roundTripApproxEq x y = roundTripEnc (==~) x y .&&. roundTripNoEnc (==~) x y
69+
62toFromJSON :: (Arbitrary a, Eq a, FromJSON a, ToJSON a, Show a) => a -> Property
70toFromJSON :: (Arbitrary a, Eq a, FromJSON a, ToJSON a, Show a) => a -> Property
63toFromJSON x = case ifromJSON (toJSON x) of
71toFromJSON x = case ifromJSON (toJSON x) of
64 IError path err -> failure "fromJSON" (formatError path err) x
72 IError path err -> failure "fromJSON" (formatError path err) x
@@ -124,7 +132,7 @@ tests = testGroup "properties" [
124 , testProperty "Text" $ roundTripEq T.empty
132 , testProperty "Text" $ roundTripEq T.empty
125 , testProperty "Foo" $ roundTripEq (undefined :: Foo)
133 , testProperty "Foo" $ roundTripEq (undefined :: Foo)
126 , testProperty "Day" $ roundTripEq (undefined :: Day)
134 , testProperty "Day" $ roundTripEq (undefined :: Day)
127− , testProperty "DotNetTime" $ roundTripEq (undefined :: DotNetTime)
135+ , testProperty "DotNetTime" $ roundTripApproxEq (undefined :: DotNetTime)
128 , testProperty "LocalTime" $ roundTripEq (undefined :: LocalTime)
136 , testProperty "LocalTime" $ roundTripEq (undefined :: LocalTime)
129 , testProperty "TimeOfDay" $ roundTripEq (undefined :: TimeOfDay)
137 , testProperty "TimeOfDay" $ roundTripEq (undefined :: TimeOfDay)
130 , testProperty "UTCTime" $ roundTripEq (undefined :: UTCTime)
138 , testProperty "UTCTime" $ roundTripEq (undefined :: UTCTime)