2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# OPTIONS_GHC -fno-warn-orphans #-}
8module Instances () where
13import Control.Applicative (empty)
15import Data.Aeson.Types
16import Data.Function (on)
17import Data.Time (ZonedTime(..), TimeZone(..))
18import Data.Time.Clock (UTCTime(..))
20import Test.QuickCheck (Arbitrary(..), elements, oneof)
22import qualified Data.DList as DList
23import qualified Data.HashMap.Strict as HM
26import Test.QuickCheck.Instances ()
27#if MIN_VERSION_base(4,7,0)
28import Data.Hashable.Time ()
33instance Arbitrary DotNetTime where
34 arbitrary = DotNetTime `liftM` arbitrary
35 shrink = map DotNetTime . shrink . fromDotNetTime
37-- | Compare timezone part only on 'timeZoneMinutes'
38instance Eq ZonedTime where
39 ZonedTime a (TimeZone a' _ _) == ZonedTime b (TimeZone b' _ _) =
42-- Compare equality to within a millisecond, allowing for rounding
43-- error (ECMA 262 requires milliseconds to rounded to zero, not
44-- rounded to nearest).
45instance ApproxEq UTCTime where
46 a =~ b = ((==) `on` utctDay) a b &&
47 (approxEqWith 1 1 `on` ((* 1e3) . utctDayTime)) a b
49instance ApproxEq DotNetTime where
50 (=~) = (=~) `on` fromDotNetTime
52instance ApproxEq Float where
54 | isNaN a && isNaN b = True
55 | otherwise = approxEq a b
57instance ApproxEq Double where
59 | isNaN a && isNaN b = True
60 | otherwise = approxEq a b
62instance (ApproxEq k, Eq v) => ApproxEq (HM.HashMap k v) where
63 a =~ b = and $ zipWith eq (HM.toList a) (HM.toList b)
65 eq (x,y) (u,v) = x =~ u && y == v
69instance Arbitrary Foo where
70 arbitrary = liftM4 Foo arbitrary arbitrary arbitrary arbitrary
73 a == b = fooInt a == fooInt b &&
74 fooDouble a `approxEq` fooDouble b &&
75 fooTuple a == fooTuple b
77instance ToJSON Foo where
78 toJSON Foo{..} = object [ "fooInt" .= fooInt
79 , "fooDouble" .= fooDouble
80 , "fooTuple" .= fooTuple
84instance FromJSON Foo where
85 parseJSON (Object v) = Foo <$>
92instance Arbitrary UFoo where
93 arbitrary = UFoo <$> arbitrary <*> arbitrary
96instance Arbitrary OneConstructor where
97 arbitrary = return OneConstructor
99instance FromJSON OneConstructor
100instance ToJSON OneConstructor
102instance (Arbitrary a, Arbitrary b) => Arbitrary (Product2 a b) where
103 arbitrary = liftM2 Product2 arbitrary arbitrary
105instance (FromJSON a, FromJSON b) => FromJSON (Product2 a b)
106instance (ToJSON a, ToJSON b) => ToJSON (Product2 a b)
108instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e,
109 Arbitrary f) => Arbitrary (Product6 a b c d e f) where
110 arbitrary = Product6 <$> arbitrary <*> arbitrary <*> arbitrary <*>
111 arbitrary <*> arbitrary <*> arbitrary
113instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d, FromJSON e,
114 FromJSON f) => FromJSON (Product6 a b c d e f)
115instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d, ToJSON e,
116 ToJSON f) => ToJSON (Product6 a b c d e f)
118instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d)
119 => Arbitrary (Sum4 a b c d) where
120 arbitrary = oneof [Alt1 <$> arbitrary, Alt2 <$> arbitrary,
121 Alt3 <$> arbitrary, Alt4 <$> arbitrary]
123instance (FromJSON a, FromJSON b, FromJSON c, FromJSON d)
124 => FromJSON (Sum4 a b c d)
125instance (ToJSON a, ToJSON b, ToJSON c, ToJSON d) => ToJSON (Sum4 a b c d)
127instance (Arbitrary a) => Arbitrary (Approx a) where
128 arbitrary = Approx <$> arbitrary
130instance (FromJSON a) => FromJSON (Approx a) where
131 parseJSON a = Approx <$> parseJSON a
133instance (ToJSON a) => ToJSON (Approx a) where
134 toJSON = toJSON . fromApprox
136instance Arbitrary Nullary where
137 arbitrary = elements [C1, C2, C3]
139instance Arbitrary a => Arbitrary (SomeType a) where
140 arbitrary = oneof [ pure Nullary
141 , Unary <$> arbitrary
142 , Product <$> arbitrary <*> arbitrary <*> arbitrary
143 , Record <$> arbitrary <*> arbitrary <*> arbitrary
147instance Arbitrary EitherTextInt where
149 [ LeftBool <$> arbitrary
150 , RightInt <$> arbitrary
151 , BothTextInt <$> arbitrary <*> arbitrary
155instance Arbitrary (GADT String) where
156 arbitrary = GADT <$> arbitrary
158instance Arbitrary OptionField where
159 arbitrary = OptionField <$> arbitrary
162instance ApproxEq Char where
165instance (ApproxEq a) => ApproxEq [a] where
166 a =~ b = length a == length b && all (uncurry (=~)) (zip a b)
168instance Arbitrary a => Arbitrary (DList.DList a) where
169 arbitrary = DList.fromList <$> arbitrary
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list