on master help sign in
Use quickcheck-instances (fixes duplicate orphan issues in master)
commit7739b45a410e105a6b6113f336b2e36ba504edc8 authorOleg Grenrus <oleg.grenrus@iki.fi> dateFri 22 Jan 2016, 11:24 (11 years ago) parent9c34bed
3 files changed+21−52
aeson.cabal+2−1 tests/Instances.hs+9−49 tests/Properties.hs+10−2
aeson.cabal+2−1
@@ -156,7 +156,8 @@ test-suite tests
156156 text,
157157 time,
158158 unordered-containers,
159− vector
159+ vector,
160+ quickcheck-instances >=0.3.12
160161
161162 if flag(old-locale)
162163 build-depends: time < 1.5, old-locale
tests/Instances.hs+9−49
@@ -6,67 +6,27 @@ module Instances where
66import Types
77import Data.Function (on)
88import Control.Monad
9−import Test.QuickCheck (Arbitrary(..), Gen, choose, getNonNegative, elements,
9+import Test.QuickCheck (Arbitrary(..), getNonNegative, elements,
1010 listOf1, oneof, resize)
11−import Data.Time.Clock (DiffTime, UTCTime(..), picosecondsToDiffTime)
12−import Data.Fixed (Pico)
13−import Data.Time (ZonedTime(..), LocalTime(..), TimeZone(..),
14− hoursToTimeZone, Day(..), TimeOfDay(..),
15− NominalDiffTime)
11+import Data.Time.Clock (UTCTime(..))
12+import Data.Time (ZonedTime(..), TimeZone(..))
1613import Data.Version
17−import qualified Data.Text as T
18−import Data.Text (Text)
1914import Data.Aeson.Types
2015import Control.Applicative
2116import Functions
2217
23−#if !MIN_VERSION_QuickCheck(2,8,2)
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
18+import Test.QuickCheck.Instances ()
2919
3020-- "System" types.
3121
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−
6022instance Arbitrary DotNetTime where
6123 arbitrary = DotNetTime `liftM` arbitrary
24+ shrink = map DotNetTime . shrink . fromDotNetTime
6225
63−instance Arbitrary ZonedTime where
64− arbitrary = liftM2 ZonedTime arbitrary arbitrary
65−
66−instance Arbitrary NominalDiffTime where
67− arbitrary = realToFrac <$> (arbitrary :: Gen Pico)
68−
69−deriving instance Eq ZonedTime
26+-- | Compare timezone part only on 'timeZoneMinutes'
27+instance Eq ZonedTime where
28+ ZonedTime a (TimeZone a' _ _) == ZonedTime b (TimeZone b' _ _) =
29+ a == b && a' == b'
7030
7131-- Compare equality to within a millisecond, allowing for rounding
7232-- 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)