Commit 88aedf1b authored by Duncan Coutts's avatar Duncan Coutts
Browse files

Add QC Arbitrary instances for Version and VersionRange

parent 65e52af0
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Test.Distribution.Version where
import Distribution.Version
import Test.QuickCheck
import Test.QuickCheck.Utils
import Control.Monad (liftM, liftM2)
instance Arbitrary Version where
arbitrary = do
branch <- smallListOf1 $
frequency [(3, return 0)
,(3, return 1)
,(2, return 2)
,(1, return 3)]
return (Version branch []) -- deliberate []
where
smallListOf1 = adjustSize (\n -> min 5 (n `div` 3)) . listOf1
shrink (Version branch []) =
[ Version branch' [] | branch' <- shrink branch ]
shrink (Version branch _tags) =
[ Version branch [] ]
instance Arbitrary VersionRange where
arbitrary = sized verRangeExp
where
verRangeExp n = frequency $
[ (2, return anyVersion)
, (1, liftM thisVersion arbitrary)
, (1, liftM laterVersion arbitrary)
, (1, liftM orLaterVersion arbitrary)
, (1, liftM earlierVersion arbitrary)
, (1, liftM orEarlierVersion arbitrary)
, (1, liftM WildcardVersion arbitrary)
] ++ if n == 0 then [] else
[ (2, liftM2 unionVersionRanges verRangeExp2 verRangeExp2)
, (2, liftM2 intersectVersionRanges verRangeExp2 verRangeExp2)
]
where
verRangeExp2 = verRangeExp (n `div` 2)
module Test.QuickCheck.Utils where
import Test.QuickCheck.Gen
-- | Adjust the size of the generated value.
--
-- In general the size gets bigger and bigger linearly. For some types
-- it is not appropriate to generate ever bigger values but instead
-- to generate lots of intermediate sized values. You could do that using:
--
-- > adjustSize (\n -> min n 5)
--
-- Similarly, for some types the linear size growth may mean getting too big
-- too quickly relative to other values. So you may want to adjust how
-- quickly the size grows. For example dividing by a constant, or even
-- something like the integer square root or log.
--
-- > adjustSize (\n -> n `div` 2)
--
-- Putting this together we can make for example a relatively short list:
--
-- > adjustSize (\n -> min 5 (n `div` 3)) (listOf1 arbitrary)
--
-- Not only do we put a limit on the length but we also scale the growth to
-- prevent it from hitting the maximum size quite so early.
--
adjustSize :: (Int -> Int) -> Gen a -> Gen a
adjustSize adjust gen = sized (\n -> resize (adjust n) gen)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment