Skip to content

Commit

Permalink
Use quickcheck-instances and newer QuickCheck, less own orphan instances
Browse files Browse the repository at this point in the history
  • Loading branch information
phadej committed Sep 23, 2016
1 parent e8c7d07 commit eb65b71
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 44 deletions.
3 changes: 2 additions & 1 deletion http-api-data.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ test-suite spec
, hspec >= 1.3
, base >= 4 && < 5
, bytestring
, QuickCheck
, QuickCheck >=2.9
, quickcheck-instances >= 0.3.12
, unordered-containers
, http-api-data
, text
Expand Down
3 changes: 2 additions & 1 deletion stack-ghc-7.10.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ flags:
http-api-data:
use-text-show: false

extra-deps: []
extra-deps:
- QuickCheck-2.9.2
extra-package-dbs: []
2 changes: 2 additions & 0 deletions stack-ghc-7.8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ extra-deps:
- base-orphans-0.5.4
- generic-deriving-1.9.0
- doctest-0.11.0
- QuickCheck-2.9.2
- quickcheck-instances-0.3.12
extra-package-dbs: []
3 changes: 2 additions & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ flags:
http-api-data:
use-text-show: false

extra-deps: []
extra-deps:
- QuickCheck-2.9.2
extra-package-dbs: []
25 changes: 21 additions & 4 deletions test/Web/Internal/HttpApiDataSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ spec = do
checkUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
checkUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
checkUrlPiece (Proxy :: Proxy Day) "Day"
checkUrlPiece (Proxy :: Proxy LocalTime) "LocalTime"
checkUrlPiece (Proxy :: Proxy ZonedTime) "ZonedTime"
checkUrlPiece (Proxy :: Proxy UTCTime) "UTCTime"
checkUrlPiece (Proxy :: Proxy NominalDiffTime) "NominalDiffTime"
checkUrlPiece' localTimeGen "LocalTime"
checkUrlPiece' zonedTimeGen "ZonedTime"
checkUrlPiece' utcTimeGen "UTCTime"
checkUrlPiece' nominalDiffTimeGen "NominalDiffTime"
checkUrlPiece (Proxy :: Proxy Version) "Version"
checkUrlPiece' uuidGen "UUID"

Expand All @@ -90,3 +90,20 @@ spec = do

uuidGen :: Gen UUID.UUID
uuidGen = UUID.fromWords <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary

-- TODO: this generators don't generate full range items
localTimeGen :: Gen LocalTime
localTimeGen = LocalTime
<$> arbitrary
<*> liftA3 TimeOfDay (choose (0, 23)) (choose (0, 59)) (fromInteger <$> choose (0, 60))

zonedTimeGen :: Gen ZonedTime
zonedTimeGen = ZonedTime
<$> localTimeGen -- Note: not arbitrary!
<*> liftA3 TimeZone arbitrary arbitrary (vectorOf 3 (elements ['A'..'Z']))

utcTimeGen :: Gen UTCTime
utcTimeGen = UTCTime <$> arbitrary <*> fmap fromInteger (choose (0, 86400))

nominalDiffTimeGen :: Gen NominalDiffTime
nominalDiffTimeGen = fromInteger <$> arbitrary
38 changes: 1 addition & 37 deletions test/Web/Internal/TestInstances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,22 @@ module Web.Internal.TestInstances
) where

import Control.Applicative
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.Char
import qualified Data.HashMap.Strict as HashMap
import qualified Data.Text as T
import qualified Data.Text.Lazy as L
import Data.Time
import Data.Version
import GHC.Exts (fromList)
import GHC.Generics

import Test.QuickCheck
import Test.QuickCheck.Instances ()

import Web.Internal.FormUrlEncoded
import Web.Internal.HttpApiData

instance Arbitrary T.Text where
arbitrary = T.pack <$> arbitrary

instance Arbitrary L.Text where
arbitrary = L.pack <$> arbitrary

instance Arbitrary BSL.ByteString where
arbitrary = BSL.pack <$> arbitrary

instance Arbitrary Day where
arbitrary = liftA3 fromGregorian (fmap abs arbitrary) arbitrary arbitrary

instance Arbitrary LocalTime where
arbitrary = LocalTime
<$> arbitrary
<*> liftA3 TimeOfDay (choose (0, 23)) (choose (0, 59)) (fromInteger <$> choose (0, 60))

instance Eq ZonedTime where
ZonedTime t (TimeZone x _ _) == ZonedTime t' (TimeZone y _ _) = t == t' && x == y

instance Arbitrary ZonedTime where
arbitrary = ZonedTime
<$> arbitrary
<*> liftA3 TimeZone arbitrary arbitrary (vectorOf 3 (elements ['A'..'Z']))

instance Arbitrary UTCTime where
arbitrary = UTCTime <$> arbitrary <*> fmap fromInteger (choose (0, 86400))

instance Arbitrary NominalDiffTime where
arbitrary = fromInteger <$> arbitrary

instance Arbitrary Version where
arbitrary = (version . map abs) <$> nonempty
where
version branch = Version branch []
nonempty = liftA2 (:) arbitrary arbitrary

instance Arbitrary Form where
arbitrary = fromList <$> arbitrary

Expand Down

0 comments on commit eb65b71

Please sign in to comment.