on master help sign in
roundTripEq test for Version type, along with Arbitrary Version instance.
commit11c9240b17c78b9d7730062ba3aaeb4bd2578970 authorDaniel Díaz <dhelta.diaz@gmail.com> dateFri 23 Oct 2015, 10:37 (11 years ago) parent297694a
2 files changed+21−2
tests/Instances.hs+19−2 tests/Properties.hs+2
tests/Instances.hs+19−2
@@ -1,4 +1,4 @@
1−{-# Language OverloadedStrings, RecordWildCards, StandaloneDeriving #-}
1+{-# Language OverloadedStrings, RecordWildCards, StandaloneDeriving, CPP #-}
22{-# OPTIONS_GHC -fno-warn-orphans #-}
33
44module Instances where
@@ -6,12 +6,16 @@ module Instances where
66import Types
77import Data.Function (on)
88import Control.Monad
9−import Test.QuickCheck (Arbitrary(..), Gen, choose, oneof, elements)
9+import Test.QuickCheck
10+ ( Arbitrary(..), Gen, choose, oneof, elements
11+ , resize, listOf1, getNonNegative
12+ )
1013import Data.Time.Clock (DiffTime, UTCTime(..), picosecondsToDiffTime)
1114import Data.Fixed (Pico)
1215import Data.Time (ZonedTime(..), LocalTime(..), TimeZone(..),
1316 hoursToTimeZone, Day(..), TimeOfDay(..),
1417 NominalDiffTime)
18+import Data.Version
1519import qualified Data.Text as T
1620import qualified Data.Map as Map
1721import Data.Text (Text)
@@ -153,3 +157,16 @@ instance ApproxEq Char where
153157
154158instance (ApproxEq a) => ApproxEq [a] where
155159 a =~ b = length a == length b && all (uncurry (=~)) (zip a b)
160+
161+instance Arbitrary Version where
162+ arbitrary = makeVersion . fmap getNonNegative <$> resize 4 (listOf1 arbitrary)
163+
164+-- Version tags are deprecated, so we avoid using them in the Arbitrary
165+-- instance. However, the recommended constructor 'makeVersion' is not
166+-- exported by "Data.Version" until base-4.8.0.0. For previous versions,
167+-- a definition is given below.
168+
169+#if !MIN_VERSION_base(4,8,0)
170+makeVersion :: [Int] -> Version
171+makeVersion b = Version b []
172+#endif
tests/Properties.hs+2
@@ -10,6 +10,7 @@ import Data.Aeson.Types
1010import Data.ByteString.Builder (toLazyByteString)
1111import Data.Int (Int8)
1212import Data.Time (Day, LocalTime, NominalDiffTime, UTCTime, ZonedTime)
13+import Data.Version (Version)
1314import Encoders
1415import Instances ()
1516import Test.Framework (Test, testGroup)
@@ -132,6 +133,7 @@ tests = testGroup "properties" [
132133 , testProperty "UTCTime" $ roundTripEq (undefined :: UTCTime)
133134 , testProperty "ZonedTime" $ roundTripEq (undefined :: ZonedTime)
134135 , testProperty "NominalDiffTime" $ roundTripEq (undefined :: NominalDiffTime)
136+ , testProperty "Version" $ roundTripEq (undefined :: Version)
135137 , testGroup "ghcGenerics" [
136138 testProperty "OneConstructor" $ roundTripEq OneConstructor
137139 , testProperty "Product2" $ roundTripEq (undefined :: Product2 Int Bool)