on master help sign in
Merge pull request #342 from phadej/qc-instances

Use quickcheck-instances (fixes duplicate orphan issues in master)

commit89aefad1336d25fd42a9d0413fb1dc3e2f721645 authorBryan O'Sullivan <bos@serpentine.com> dateTue 26 Jan 2016, 15:03 (11 years ago) parent1e977477739b45
3 files changed+21−52
aeson.cabal+2−1 tests/Instances.hs+9−49 tests/Properties.hs+10−2
aeson.cabal+2−1
@@ -159,7 +159,8 @@ test-suite tests
159159 text,
160160 time,
161161 unordered-containers,
162− vector
162+ vector,
163+ quickcheck-instances >=0.3.12
163164
164165 if flag(old-locale)
165166 build-depends: time < 1.5, old-locale
tests/Instances.hs+9−49
@@ -10,67 +10,27 @@ module Instances where
1010import Types
1111import Data.Function (on)
1212import Control.Monad
13−import Test.QuickCheck (Arbitrary(..), Gen, choose, getNonNegative, elements,
13+import Test.QuickCheck (Arbitrary(..), getNonNegative, elements,
1414 listOf1, oneof, resize)
15−import Data.Time.Clock (DiffTime, UTCTime(..), picosecondsToDiffTime)
16−import Data.Fixed (Pico)
17−import Data.Time (ZonedTime(..), LocalTime(..), TimeZone(..),
18− hoursToTimeZone, Day(..), TimeOfDay(..),
19− NominalDiffTime)
15+import Data.Time.Clock (UTCTime(..))
16+import Data.Time (ZonedTime(..), TimeZone(..))
2017import Data.Version
21−import qualified Data.Text as T
22−import Data.Text (Text)
2318import Data.Aeson.Types
2419import Control.Applicative
2520import Functions
2621
27−#if !MIN_VERSION_QuickCheck(2,8,2)
28−import qualified Data.Map as Map
29−
30−instance (Ord k, Arbitrary k, Arbitrary v) => Arbitrary (Map.Map k v) where
31− arbitrary = Map.fromList <$> arbitrary
32−#endif
22+import Test.QuickCheck.Instances ()
3323
3424-- "System" types.
3525
36−instance Arbitrary Text where
37− arbitrary = T.pack <$> arbitrary
38−
39−instance Arbitrary TimeOfDay where
40− arbitrary = do
41− h <- choose (0, 23)
42− m <- choose (0, 59)
43− s <- fromRational . toRational <$> choose (0, 59 :: Double)
44− return $ TimeOfDay h m s
45−
46−instance Arbitrary LocalTime where
47− arbitrary = LocalTime <$> arbitrary <*> arbitrary
48−
49−instance Arbitrary TimeZone where
50− arbitrary = do
51− offset <- choose (0,2) :: Gen Int
52− return $ hoursToTimeZone offset
53−
54−instance Arbitrary Day where
55− arbitrary = ModifiedJulianDay `liftM` arbitrary
56−
57−instance Arbitrary DiffTime where
58− arbitrary = (picosecondsToDiffTime . (* 1000000000)) <$>
59− choose (0, 86400000)
60−
61−instance Arbitrary UTCTime where
62− arbitrary = liftM2 UTCTime arbitrary arbitrary
63−
6426instance Arbitrary DotNetTime where
6527 arbitrary = DotNetTime `liftM` arbitrary
28+ shrink = map DotNetTime . shrink . fromDotNetTime
6629
67−instance Arbitrary ZonedTime where
68− arbitrary = liftM2 ZonedTime arbitrary arbitrary
69−
70−instance Arbitrary NominalDiffTime where
71− arbitrary = realToFrac <$> (arbitrary :: Gen Pico)
72−
73−deriving instance Eq ZonedTime
30+-- | Compare timezone part only on 'timeZoneMinutes'
31+instance Eq ZonedTime where
32+ ZonedTime a (TimeZone a' _ _) == ZonedTime b (TimeZone b' _ _) =
33+ a == b && a' == b'
7434
7535-- Compare equality to within a millisecond, allowing for rounding
7636-- error (ECMA 262 requires milliseconds to rounded to zero, not
tests/Properties.hs+10−2
@@ -16,7 +16,7 @@ import Encoders
1616import Instances ()
1717import Test.Framework (Test, testGroup)
1818import Test.Framework.Providers.QuickCheck2 (testProperty)
19−import Test.QuickCheck (Arbitrary(..), Property, (===), (.&&.))
19+import Test.QuickCheck (Arbitrary(..), Property, (===), (.&&.), counterexample)
2020import Types
2121import qualified Data.Attoparsec.Lazy as L
2222import qualified Data.ByteString.Lazy.Char8 as L
@@ -59,6 +59,14 @@ roundTripNoEnc eq _ i =
5959roundTripEq :: (Eq a, FromJSON a, ToJSON a, Show a) => a -> a -> Property
6060roundTripEq x y = roundTripEnc (===) x y .&&. roundTripNoEnc (===) x y
6161
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+
6270toFromJSON :: (Arbitrary a, Eq a, FromJSON a, ToJSON a, Show a) => a -> Property
6371toFromJSON x = case ifromJSON (toJSON x) of
6472 IError path err -> failure "fromJSON" (formatError path err) x
@@ -124,7 +132,7 @@ tests = testGroup "properties" [
124132 , testProperty "Text" $ roundTripEq T.empty
125133 , testProperty "Foo" $ roundTripEq (undefined :: Foo)
126134 , testProperty "Day" $ roundTripEq (undefined :: Day)
127− , testProperty "DotNetTime" $ roundTripEq (undefined :: DotNetTime)
135+ , testProperty "DotNetTime" $ roundTripApproxEq (undefined :: DotNetTime)
128136 , testProperty "LocalTime" $ roundTripEq (undefined :: LocalTime)
129137 , testProperty "TimeOfDay" $ roundTripEq (undefined :: TimeOfDay)
130138 , testProperty "UTCTime" $ roundTripEq (undefined :: UTCTime)