Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Test.Tasty.QuickCheck
Description
This module allows to use QuickCheck properties in tasty.
Synopsis
- testProperty :: Testable a => TestName -> a -> TestTree
- testProperties :: TestName -> [(String, Property)] -> TestTree
- newtype QuickCheckTests = QuickCheckTests Int
- data QuickCheckReplay
- = QuickCheckReplayNone
- | QuickCheckReplayLegacy Int
- | QuickCheckReplay (QCGen, Int)
- newtype QuickCheckShowReplay = QuickCheckShowReplay Bool
- newtype QuickCheckMaxSize = QuickCheckMaxSize Int
- newtype QuickCheckMaxRatio = QuickCheckMaxRatio Int
- newtype QuickCheckVerbose = QuickCheckVerbose Bool
- newtype QuickCheckMaxShrinks = QuickCheckMaxShrinks Int
- data Fun a b = Fun (a :-> b, b, Shrunk) (a -> b)
- data Gen a
- class CoArbitrary a where
- coarbitrary :: a -> Gen b -> Gen b
- class Arbitrary2 (f :: Type -> Type -> Type) where
- liftArbitrary2 :: Gen a -> Gen b -> Gen (f a b)
- liftShrink2 :: (a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
- class Arbitrary1 (f :: Type -> Type) where
- liftArbitrary :: Gen a -> Gen (f a)
- liftShrink :: (a -> [a]) -> f a -> [f a]
- class Arbitrary a where
- newtype PrintableString = PrintableString {}
- newtype UnicodeString = UnicodeString {}
- newtype ASCIIString = ASCIIString {}
- class ShrinkState s a where
- shrinkInit :: a -> s
- shrinkState :: a -> s -> [(a, s)]
- data Shrinking s a = Shrinking s a
- data Smart a = Smart Int a
- newtype Shrink2 a = Shrink2 {
- getShrink2 :: a
- newtype Small a = Small {
- getSmall :: a
- newtype Large a = Large {
- getLarge :: a
- newtype NonPositive a = NonPositive {
- getNonPositive :: a
- newtype NonNegative a = NonNegative {
- getNonNegative :: a
- newtype NonZero a = NonZero {
- getNonZero :: a
- newtype Negative a = Negative {
- getNegative :: a
- newtype Positive a = Positive {
- getPositive :: a
- newtype SortedList a = Sorted {
- getSorted :: [a]
- data InfiniteList a = InfiniteList {
- getInfiniteList :: [a]
- infiniteListInternalData :: InfiniteListInternalData a
- newtype NonEmptyList a = NonEmpty {
- getNonEmpty :: [a]
- newtype OrderedList a = Ordered {
- getOrdered :: [a]
- newtype Fixed a = Fixed {
- getFixed :: a
- newtype Blind a = Blind {
- getBlind :: a
- class Function a where
- data Confidence = Confidence {}
- data Witness = (Typeable a, Show a) => Wit a
- data Discard = Discard
- class Testable prop where
- data Property
- pattern Fn3 :: (a -> b -> c -> d) -> Fun (a, b, c) d
- pattern Fn2 :: (a -> b -> c) -> Fun (a, b) c
- pattern Fn :: (a -> b) -> Fun a b
- pattern Success :: Int -> Int -> !(Map [String] Int) -> !(Map String Int) -> !(Map String (Map String Int)) -> String -> Result
- pattern GaveUp :: Int -> Int -> !(Map [String] Int) -> !(Map String Int) -> !(Map String (Map String Int)) -> String -> Result
- pattern Failure :: Int -> Int -> Int -> Int -> Int -> QCGen -> Int -> String -> Maybe AnException -> String -> [String] -> [String] -> Set String -> [Witness] -> Result
- pattern NoExpectedFailure :: Int -> Int -> !(Map [String] Int) -> !(Map String Int) -> !(Map String (Map String Int)) -> String -> Result
- numTests :: Result -> Int
- numDiscarded :: Result -> Int
- labels :: Result -> Map [String] Int
- classes :: Result -> Map String Int
- tables :: Result -> Map String (Map String Int)
- output :: Result -> String
- numShrinks :: Result -> Int
- numShrinkTries :: Result -> Int
- numShrinkFinal :: Result -> Int
- usedSeed :: Result -> QCGen
- usedSize :: Result -> Int
- reason :: Result -> String
- theException :: Result -> Maybe AnException
- failingTestCase :: Result -> [String]
- failingLabels :: Result -> [String]
- failingClasses :: Result -> Set String
- witnesses :: Result -> [Witness]
- (==>) :: Testable prop => Bool -> prop -> Property
- discard :: a
- variant :: Integral n => n -> Gen a -> Gen a
- sized :: (Int -> Gen a) -> Gen a
- getSize :: Gen Int
- resize :: HasCallStack => Int -> Gen a -> Gen a
- scale :: (Int -> Int) -> Gen a -> Gen a
- choose :: Random a => (a, a) -> Gen a
- chooseAny :: Random a => Gen a
- chooseEnum :: Enum a => (a, a) -> Gen a
- chooseInt :: (Int, Int) -> Gen Int
- chooseBoundedIntegral :: (Bounded a, Integral a) => (a, a) -> Gen a
- chooseInteger :: (Integer, Integer) -> Gen Integer
- generate :: Gen a -> IO a
- sample' :: Gen a -> IO [a]
- sample :: Show a => Gen a -> IO ()
- suchThat :: Gen a -> (a -> Bool) -> Gen a
- suchThatMap :: Gen a -> (a -> Maybe b) -> Gen b
- suchThatMaybe :: Gen a -> (a -> Bool) -> Gen (Maybe a)
- oneof :: HasCallStack => [Gen a] -> Gen a
- frequency :: HasCallStack => [(Int, Gen a)] -> Gen a
- elements :: HasCallStack => [a] -> Gen a
- sublistOf :: [a] -> Gen [a]
- shuffle :: [a] -> Gen [a]
- growingElements :: HasCallStack => [a] -> Gen a
- listOf :: Gen a -> Gen [a]
- listOf1 :: Gen a -> Gen [a]
- vectorOf :: Int -> Gen a -> Gen [a]
- infiniteListOf :: Gen a -> Gen [a]
- arbitrary1 :: (Arbitrary1 f, Arbitrary a) => Gen (f a)
- shrink1 :: (Arbitrary1 f, Arbitrary a) => f a -> [f a]
- arbitrary2 :: (Arbitrary2 f, Arbitrary a, Arbitrary b) => Gen (f a b)
- shrink2 :: (Arbitrary2 f, Arbitrary a, Arbitrary b) => f a b -> [f a b]
- genericShrink :: (Generic a, RecursivelyShrink (Rep a), GSubterms (Rep a) a) => a -> [a]
- recursivelyShrink :: (Generic a, RecursivelyShrink (Rep a)) => a -> [a]
- subterms :: (Generic a, GSubterms (Rep a) a) => a -> [a]
- shrinkList :: (a -> [a]) -> [a] -> [[a]]
- applyArbitrary2 :: (Arbitrary a, Arbitrary b) => (a -> b -> r) -> Gen r
- applyArbitrary3 :: (Arbitrary a, Arbitrary b, Arbitrary c) => (a -> b -> c -> r) -> Gen r
- applyArbitrary4 :: (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d) => (a -> b -> c -> d -> r) -> Gen r
- arbitrarySizedIntegral :: Integral a => Gen a
- arbitrarySizedNatural :: Integral a => Gen a
- arbitrarySizedFractional :: Fractional a => Gen a
- arbitraryBoundedIntegral :: (Bounded a, Integral a) => Gen a
- arbitraryBoundedRandom :: (Bounded a, Random a) => Gen a
- arbitraryBoundedEnum :: (Bounded a, Enum a) => Gen a
- arbitrarySizedBoundedIntegral :: (Bounded a, Integral a) => Gen a
- arbitraryUnicodeChar :: Gen Char
- arbitraryASCIIChar :: Gen Char
- arbitraryPrintableChar :: Gen Char
- shrinkNothing :: a -> [a]
- shrinkMap :: Arbitrary a => (a -> b) -> (b -> a) -> b -> [b]
- shrinkMapBy :: (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b]
- shrinkIntegral :: Integral a => a -> [a]
- shrinkBoundedEnum :: (Bounded a, Enum a, Eq a) => a -> [a]
- shrinkRealFrac :: RealFrac a => a -> [a]
- shrinkDecimal :: RealFrac a => a -> [a]
- genericCoarbitrary :: (Generic a, GCoArbitrary (Rep a)) => a -> Gen b -> Gen b
- (><) :: (Gen a -> Gen a) -> (Gen a -> Gen a) -> Gen a -> Gen a
- coarbitraryIntegral :: Integral a => a -> Gen b -> Gen b
- coarbitraryReal :: Real a => a -> Gen b -> Gen b
- coarbitraryShow :: Show a => a -> Gen b -> Gen b
- coarbitraryEnum :: Enum a => a -> Gen b -> Gen b
- vector :: Arbitrary a => Int -> Gen [a]
- orderedList :: (Ord a, Arbitrary a) => Gen [a]
- infiniteList :: Arbitrary a => Gen [a]
- functionBoundedEnum :: (Eq a, Bounded a, Enum a) => (a -> b) -> a :-> b
- functionRealFrac :: RealFrac a => (a -> b) -> a :-> b
- functionIntegral :: Integral a => (a -> b) -> a :-> b
- functionShow :: (Show a, Read a) => (a -> c) -> a :-> c
- functionVoid :: (forall b. void -> b) -> void :-> c
- functionMap :: Function b => (a -> b) -> (b -> a) -> (a -> c) -> a :-> c
- applyFun :: Fun a b -> a -> b
- applyFun2 :: Fun (a, b) c -> a -> b -> c
- applyFun3 :: Fun (a, b, c) d -> a -> b -> c -> d
- ioProperty :: Testable prop => IO prop -> Property
- idempotentIOProperty :: Testable prop => IO prop -> Property
- coerceWitness :: Typeable a => Witness -> a
- castWitness :: Typeable a => Witness -> Maybe a
- mapSize :: Testable prop => (Int -> Int) -> prop -> Property
- shrinking :: Testable prop => (a -> [a]) -> a -> (a -> prop) -> Property
- noShrinking :: Testable prop => prop -> Property
- counterexample :: Testable prop => String -> prop -> Property
- printTestCase :: Testable prop => String -> prop -> Property
- whenFail :: Testable prop => IO () -> prop -> Property
- whenFail' :: Testable prop => IO () -> prop -> Property
- verboseShrinking :: Testable prop => prop -> Property
- expectFailure :: Testable prop => prop -> Property
- once :: Testable prop => prop -> Property
- again :: Testable prop => prop -> Property
- withMaxSuccess :: Testable prop => Int -> prop -> Property
- withDiscardRatio :: Testable prop => Int -> prop -> Property
- withMaxShrinks :: Testable prop => Int -> prop -> Property
- withMaxSize :: Testable prop => Int -> prop -> Property
- witness :: (Typeable a, Show a, Testable prop) => a -> prop -> Property
- checkCoverage :: Testable prop => prop -> Property
- checkCoverageWith :: Testable prop => Confidence -> prop -> Property
- stdConfidence :: Confidence
- label :: Testable prop => String -> prop -> Property
- collect :: (Show a, Testable prop) => a -> prop -> Property
- classify :: Testable prop => Bool -> String -> prop -> Property
- cover :: Testable prop => Double -> Bool -> String -> prop -> Property
- tabulate :: Testable prop => String -> [String] -> prop -> Property
- coverTable :: Testable prop => String -> [(String, Double)] -> prop -> Property
- within :: Testable prop => Int -> prop -> Property
- discardAfter :: Testable prop => Int -> prop -> Property
- forAll :: (Show a, Testable prop) => Gen a -> (a -> prop) -> Property
- forAllShow :: Testable prop => Gen a -> (a -> String) -> (a -> prop) -> Property
- forAllBlind :: Testable prop => Gen a -> (a -> prop) -> Property
- forAllShrink :: (Show a, Testable prop) => Gen a -> (a -> [a]) -> (a -> prop) -> Property
- forAllShrinkShow :: Testable prop => Gen a -> (a -> [a]) -> (a -> String) -> (a -> prop) -> Property
- forAllShrinkBlind :: Testable prop => Gen a -> (a -> [a]) -> (a -> prop) -> Property
- (.&.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> Property
- (.&&.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> Property
- conjoin :: Testable prop => [prop] -> Property
- (.||.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> Property
- disjoin :: Testable prop => [prop] -> Property
- (===) :: (Eq a, Show a) => a -> a -> Property
- (=/=) :: (Eq a, Show a) => a -> a -> Property
- total :: NFData a => a -> Property
- isSuccess :: Result -> Bool
- recheck :: Testable prop => Result -> prop -> IO ()
- polyQuickCheck :: Name -> ExpQ
- polyVerboseCheck :: Name -> ExpQ
- monomorphic :: Name -> ExpQ
- labelledExamples :: Testable prop => prop -> IO ()
- labelledExamplesWith :: Testable prop => Args -> prop -> IO ()
- labelledExamplesResult :: Testable prop => prop -> IO Result
- labelledExamplesWithResult :: Testable prop => Args -> prop -> IO Result
- newtype QC = QC Property
- optionSetToArgs :: OptionSet -> IO (Int, Args)
Documentation
testProperties :: TestName -> [(String, Property)] -> TestTree Source #
Create a test from a list of QuickCheck properties. To be used
with allProperties
. E.g.
tests :: TestTree tests = testProperties "Foo" $allProperties
newtype QuickCheckTests Source #
Number of test cases for QuickCheck to generate
Constructors
QuickCheckTests Int |
Instances
data QuickCheckReplay Source #
Replay seed
Constructors
QuickCheckReplayNone | No seed Since: 0.11 |
QuickCheckReplayLegacy Int | Legacy integer seed Since: 0.11 |
QuickCheckReplay (QCGen, Int) |
Since: 0.11 |
Instances
IsOption QuickCheckReplay Source # | |
Defined in Test.Tasty.QuickCheck |
newtype QuickCheckShowReplay Source #
If a test case fails unexpectedly, show the replay token
Constructors
QuickCheckShowReplay Bool |
Instances
IsOption QuickCheckShowReplay Source # | |
Defined in Test.Tasty.QuickCheck |
newtype QuickCheckMaxSize Source #
Size of the biggest test cases
Constructors
QuickCheckMaxSize Int |
Instances
newtype QuickCheckMaxRatio Source #
Maximum number of of discarded tests per successful test before giving up.
Constructors
QuickCheckMaxRatio Int |
Instances
newtype QuickCheckVerbose Source #
Show the test cases that QuickCheck generates
Constructors
QuickCheckVerbose Bool |
Instances
IsOption QuickCheckVerbose Source # | |
Defined in Test.Tasty.QuickCheck |
newtype QuickCheckMaxShrinks Source #
Number of shrinks allowed before QuickCheck will fail a test.
Since: 0.10.2
Constructors
QuickCheckMaxShrinks Int |
Instances
Re-export of Test.QuickCheck
Generation of random shrinkable, showable functions.
To generate random values of type
,
you must have an instance Fun
a b
.Function
a
A generator for values of type a
.
The third-party packages
QuickCheck-GenT
and
quickcheck-transformer
provide monad transformer versions of Gen
.
class CoArbitrary a where #
Used for random generation of functions.
You should consider using Fun
instead, which
can show the generated functions as strings.
If you are using a recent GHC, there is a default definition of
coarbitrary
using genericCoarbitrary
, so if your type has a
Generic
instance it's enough to say
instance CoArbitrary MyType
You should only use genericCoarbitrary
for data types where
equality is structural, i.e. if you can't have two different
representations of the same value. An example where it's not
safe is sets implemented using binary search trees: the same
set can be represented as several different trees.
Here you would have to explicitly define
coarbitrary s = coarbitrary (toList s)
.
Minimal complete definition
Nothing
Methods
coarbitrary :: a -> Gen b -> Gen b #
Used to generate a function of type a -> b
.
The first argument is a value, the second a generator.
You should use variant
to perturb the random generator;
the goal is that different values for the first argument will
lead to different calls to variant
. An example will help:
instance CoArbitrary a => CoArbitrary [a] where coarbitrary [] =variant
0 coarbitrary (x:xs) =variant
1 . coarbitrary (x,xs)
Instances
class Arbitrary2 (f :: Type -> Type -> Type) where #
Lifting of the Arbitrary
class to binary type constructors.
Minimal complete definition
Methods
liftArbitrary2 :: Gen a -> Gen b -> Gen (f a b) #
liftShrink2 :: (a -> [a]) -> (b -> [b]) -> f a b -> [f a b] #
Instances
Arbitrary2 Either | |
Defined in Test.QuickCheck.Arbitrary Methods liftArbitrary2 :: Gen a -> Gen b -> Gen (Either a b) # liftShrink2 :: (a -> [a]) -> (b -> [b]) -> Either a b -> [Either a b] # | |
Arbitrary2 (,) | |
Defined in Test.QuickCheck.Arbitrary Methods liftArbitrary2 :: Gen a -> Gen b -> Gen (a, b) # liftShrink2 :: (a -> [a]) -> (b -> [b]) -> (a, b) -> [(a, b)] # | |
Arbitrary2 (Const :: Type -> Type -> Type) | |
Defined in Test.QuickCheck.Arbitrary Methods liftArbitrary2 :: Gen a -> Gen b -> Gen (Const a b) # liftShrink2 :: (a -> [a]) -> (b -> [b]) -> Const a b -> [Const a b] # | |
Arbitrary2 (Constant :: Type -> Type -> Type) | |
Defined in Test.QuickCheck.Arbitrary Methods liftArbitrary2 :: Gen a -> Gen b -> Gen (Constant a b) # liftShrink2 :: (a -> [a]) -> (b -> [b]) -> Constant a b -> [Constant a b] # |
class Arbitrary1 (f :: Type -> Type) where #
Lifting of the Arbitrary
class to unary type constructors.
Minimal complete definition
Instances
Random generation and shrinking of values.
QuickCheck provides Arbitrary
instances for most types in base
,
except those which incur extra dependencies.
For a wider range of Arbitrary
instances see the
quickcheck-instances
package.
Minimal complete definition
Methods
A generator for values of the given type.
It is worth spending time thinking about what sort of test data
you want - good generators are often the difference between
finding bugs and not finding them. You can use sample
,
label
and classify
to check the quality of your test data.
There is no generic arbitrary
implementation included because we don't
know how to make a high-quality one. If you want one, consider using the
testing-feat or
generic-random packages.
The QuickCheck manual goes into detail on how to write good generators. Make sure to look at it, especially if your type is recursive!
Produces a (possibly) empty list of all the possible immediate shrinks of the given value.
The default implementation returns the empty list, so will not try to
shrink the value. If your data type has no special invariants, you can
enable shrinking by defining shrink =
, but by customising
the behaviour of genericShrink
shrink
you can often get simpler counterexamples.
Most implementations of shrink
should try at least three things:
- Shrink a term to any of its immediate subterms.
You can use
subterms
to do this. - Recursively apply
shrink
to all immediate subterms. You can userecursivelyShrink
to do this. - Type-specific shrinkings such as replacing a constructor by a simpler constructor.
For example, suppose we have the following implementation of binary trees:
data Tree a = Nil | Branch a (Tree a) (Tree a)
We can then define shrink
as follows:
shrink Nil = [] shrink (Branch x l r) = -- shrink Branch to Nil [Nil] ++ -- shrink to subterms [l, r] ++ -- recursively shrink subterms [Branch x' l' r' | (x', l', r') <- shrink (x, l, r)]
There are a couple of subtleties here:
- QuickCheck tries the shrinking candidates in the order they
appear in the list, so we put more aggressive shrinking steps
(such as replacing the whole tree by
Nil
) before smaller ones (such as recursively shrinking the subtrees). - It is tempting to write the last line as
[Branch x' l' r' | x' <- shrink x, l' <- shrink l, r' <- shrink r]
but this is the wrong thing! It will force QuickCheck to shrinkx
,l
andr
in tandem, and shrinking will stop once one of the three is fully shrunk.
There is a fair bit of boilerplate in the code above.
We can avoid it with the help of some generic functions.
The function genericShrink
tries shrinking a term to all of its
subterms and, failing that, recursively shrinks the subterms.
Using it, we can define shrink
as:
shrink x = shrinkToNil x ++ genericShrink x where shrinkToNil Nil = [] shrinkToNil (Branch _ l r) = [Nil]
genericShrink
is a combination of subterms
, which shrinks
a term to any of its subterms, and recursivelyShrink
, which shrinks
all subterms of a term. These may be useful if you need a bit more
control over shrinking than genericShrink
gives you.
A final gotcha: we cannot define shrink
as simply
as this shrinks shrink
x = Nil:genericShrink
xNil
to Nil
, and shrinking will go into an
infinite loop.
If all this leaves you bewildered, you might try
to begin with,
after deriving shrink
= genericShrink
Generic
for your type. However, if your data type has any
special invariants, you will need to check that genericShrink
can't break those invariants.
Instances
newtype PrintableString #
PrintableString
: generates a printable unicode String.
The string will not contain surrogate pairs.
Constructors
PrintableString | |
Fields |
Instances
newtype UnicodeString #
UnicodeString
: generates a unicode String.
The string will not contain surrogate pairs.
Constructors
UnicodeString | |
Fields |
Instances
Arbitrary UnicodeString | |
Defined in Test.QuickCheck.Modifiers | |
Read UnicodeString | |
Defined in Test.QuickCheck.Modifiers Methods readsPrec :: Int -> ReadS UnicodeString # readList :: ReadS [UnicodeString] # | |
Show UnicodeString | |
Defined in Test.QuickCheck.Modifiers Methods showsPrec :: Int -> UnicodeString -> ShowS # show :: UnicodeString -> String # showList :: [UnicodeString] -> ShowS # | |
Eq UnicodeString | |
Defined in Test.QuickCheck.Modifiers Methods (==) :: UnicodeString -> UnicodeString -> Bool # (/=) :: UnicodeString -> UnicodeString -> Bool # | |
Ord UnicodeString | |
Defined in Test.QuickCheck.Modifiers Methods compare :: UnicodeString -> UnicodeString -> Ordering # (<) :: UnicodeString -> UnicodeString -> Bool # (<=) :: UnicodeString -> UnicodeString -> Bool # (>) :: UnicodeString -> UnicodeString -> Bool # (>=) :: UnicodeString -> UnicodeString -> Bool # max :: UnicodeString -> UnicodeString -> UnicodeString # min :: UnicodeString -> UnicodeString -> UnicodeString # |
newtype ASCIIString #
ASCIIString
: generates an ASCII string.
Constructors
ASCIIString | |
Fields |
Instances
Arbitrary ASCIIString | |
Defined in Test.QuickCheck.Modifiers | |
Read ASCIIString | |
Defined in Test.QuickCheck.Modifiers Methods readsPrec :: Int -> ReadS ASCIIString # readList :: ReadS [ASCIIString] # readPrec :: ReadPrec ASCIIString # readListPrec :: ReadPrec [ASCIIString] # | |
Show ASCIIString | |
Defined in Test.QuickCheck.Modifiers Methods showsPrec :: Int -> ASCIIString -> ShowS # show :: ASCIIString -> String # showList :: [ASCIIString] -> ShowS # | |
Eq ASCIIString | |
Defined in Test.QuickCheck.Modifiers | |
Ord ASCIIString | |
Defined in Test.QuickCheck.Modifiers Methods compare :: ASCIIString -> ASCIIString -> Ordering # (<) :: ASCIIString -> ASCIIString -> Bool # (<=) :: ASCIIString -> ASCIIString -> Bool # (>) :: ASCIIString -> ASCIIString -> Bool # (>=) :: ASCIIString -> ASCIIString -> Bool # max :: ASCIIString -> ASCIIString -> ASCIIString # min :: ASCIIString -> ASCIIString -> ASCIIString # |
class ShrinkState s a where #
Shrinking _ x
: allows for maintaining a state during shrinking.
Constructors
Shrinking s a |
Smart _ x
: tries a different order when shrinking.
Shrink2 x
: allows 2 shrinking steps at the same time when shrinking x
Constructors
Shrink2 | |
Fields
|
Instances
Small x
: generates values of x
drawn from a small range.
The opposite of Large
.
Instances
Functor Small | |
Integral a => Arbitrary (Small a) | |
Enum a => Enum (Small a) | |
Ix a => Ix (Small a) | |
Defined in Test.QuickCheck.Modifiers | |
Num a => Num (Small a) | |
Read a => Read (Small a) | |
Integral a => Integral (Small a) | |
Defined in Test.QuickCheck.Modifiers | |
Real a => Real (Small a) | |
Defined in Test.QuickCheck.Modifiers Methods toRational :: Small a -> Rational # | |
Show a => Show (Small a) | |
Eq a => Eq (Small a) | |
Ord a => Ord (Small a) | |
Defined in Test.QuickCheck.Modifiers |
Large x
: by default, QuickCheck generates Int
s drawn from a small
range. Large Int
gives you values drawn from the entire range instead.
Instances
Functor Large | |
(Integral a, Bounded a) => Arbitrary (Large a) | |
Enum a => Enum (Large a) | |
Ix a => Ix (Large a) | |
Defined in Test.QuickCheck.Modifiers | |
Num a => Num (Large a) | |
Read a => Read (Large a) | |
Integral a => Integral (Large a) | |
Defined in Test.QuickCheck.Modifiers | |
Real a => Real (Large a) | |
Defined in Test.QuickCheck.Modifiers Methods toRational :: Large a -> Rational # | |
Show a => Show (Large a) | |
Eq a => Eq (Large a) | |
Ord a => Ord (Large a) | |
Defined in Test.QuickCheck.Modifiers |
newtype NonPositive a #
NonPositive x
: guarantees that x <= 0
.
Constructors
NonPositive | |
Fields
|
Instances
newtype NonNegative a #
NonNegative x
: guarantees that x >= 0
.
Constructors
NonNegative | |
Fields
|
Instances
NonZero x
: guarantees that x /= 0
.
Constructors
NonZero | |
Fields
|
Instances
Functor NonZero | |
(Num a, Eq a, Arbitrary a) => Arbitrary (NonZero a) | |
Enum a => Enum (NonZero a) | |
Defined in Test.QuickCheck.Modifiers Methods succ :: NonZero a -> NonZero a # pred :: NonZero a -> NonZero a # fromEnum :: NonZero a -> Int # enumFrom :: NonZero a -> [NonZero a] # enumFromThen :: NonZero a -> NonZero a -> [NonZero a] # enumFromTo :: NonZero a -> NonZero a -> [NonZero a] # enumFromThenTo :: NonZero a -> NonZero a -> NonZero a -> [NonZero a] # | |
Read a => Read (NonZero a) | |
Show a => Show (NonZero a) | |
Eq a => Eq (NonZero a) | |
Ord a => Ord (NonZero a) | |
Negative x
: guarantees that x < 0
.
Constructors
Negative | |
Fields
|
Instances
Functor Negative | |
(Num a, Ord a, Arbitrary a) => Arbitrary (Negative a) | |
Enum a => Enum (Negative a) | |
Defined in Test.QuickCheck.Modifiers Methods succ :: Negative a -> Negative a # pred :: Negative a -> Negative a # fromEnum :: Negative a -> Int # enumFrom :: Negative a -> [Negative a] # enumFromThen :: Negative a -> Negative a -> [Negative a] # enumFromTo :: Negative a -> Negative a -> [Negative a] # enumFromThenTo :: Negative a -> Negative a -> Negative a -> [Negative a] # | |
Read a => Read (Negative a) | |
Show a => Show (Negative a) | |
Eq a => Eq (Negative a) | |
Ord a => Ord (Negative a) | |
Defined in Test.QuickCheck.Modifiers |
Positive x
: guarantees that x > 0
.
Constructors
Positive | |
Fields
|
Instances
Functor Positive | |
(Num a, Ord a, Arbitrary a) => Arbitrary (Positive a) | |
Enum a => Enum (Positive a) | |
Defined in Test.QuickCheck.Modifiers Methods succ :: Positive a -> Positive a # pred :: Positive a -> Positive a # fromEnum :: Positive a -> Int # enumFrom :: Positive a -> [Positive a] # enumFromThen :: Positive a -> Positive a -> [Positive a] # enumFromTo :: Positive a -> Positive a -> [Positive a] # enumFromThenTo :: Positive a -> Positive a -> Positive a -> [Positive a] # | |
Read a => Read (Positive a) | |
Show a => Show (Positive a) | |
Eq a => Eq (Positive a) | |
Ord a => Ord (Positive a) | |
Defined in Test.QuickCheck.Modifiers |
newtype SortedList a #
Sorted xs
: guarantees that xs is sorted.
Instances
data InfiniteList a #
InfiniteList xs _
: guarantees that xs is an infinite list.
When a counterexample is found, only prints the prefix of xs
that was used by the program.
Here is a contrived example property:
prop_take_10 :: InfiniteList Char -> Bool prop_take_10 (InfiniteList xs _) = or [ x == 'a' | x <- take 10 xs ]
In the following counterexample, the list must start with "bbbbbbbbbb"
but
the remaining (infinite) part can contain anything:
>>>
quickCheck prop_take_10
*** Failed! Falsified (after 1 test and 14 shrinks): "bbbbbbbbbb" ++ ...
Constructors
InfiniteList | |
Fields
|
Instances
Arbitrary a => Arbitrary (InfiniteList a) | |
Defined in Test.QuickCheck.Modifiers | |
Show a => Show (InfiniteList a) | |
Defined in Test.QuickCheck.Modifiers Methods showsPrec :: Int -> InfiniteList a -> ShowS # show :: InfiniteList a -> String # showList :: [InfiniteList a] -> ShowS # |
newtype NonEmptyList a #
NonEmpty xs
: guarantees that xs is non-empty.
Constructors
NonEmpty | |
Fields
|
Instances
newtype OrderedList a #
Ordered xs
: guarantees that xs is ordered.
Constructors
Ordered | |
Fields
|
Instances
Fixed x
: as x, but will not be shrunk.
Instances
Functor Fixed | |
Arbitrary a => Arbitrary (Fixed a) | |
Enum a => Enum (Fixed a) | |
Num a => Num (Fixed a) | |
Read a => Read (Fixed a) | |
Integral a => Integral (Fixed a) | |
Defined in Test.QuickCheck.Modifiers | |
Real a => Real (Fixed a) | |
Defined in Test.QuickCheck.Modifiers Methods toRational :: Fixed a -> Rational # | |
Show a => Show (Fixed a) | |
Eq a => Eq (Fixed a) | |
Ord a => Ord (Fixed a) | |
Defined in Test.QuickCheck.Modifiers |
Blind x
: as x, but x does not have to be in the Show
class.
Instances
Functor Blind | |
Arbitrary a => Arbitrary (Blind a) | |
Enum a => Enum (Blind a) | |
Num a => Num (Blind a) | |
Integral a => Integral (Blind a) | |
Defined in Test.QuickCheck.Modifiers | |
Real a => Real (Blind a) | |
Defined in Test.QuickCheck.Modifiers Methods toRational :: Blind a -> Rational # | |
Show (Blind a) | |
Eq a => Eq (Blind a) | |
Ord a => Ord (Blind a) | |
Defined in Test.QuickCheck.Modifiers |
The class Function a
is used for random generation of showable
functions of type a -> b
.
There is a default implementation for function
, which you can use
if your type has structural equality. Otherwise, you can normally
use functionMap
or functionShow
.
Minimal complete definition
Nothing
Instances
data Confidence #
The statistical parameters used by checkCoverage
.
Constructors
Confidence | |
Fields
|
Instances
Show Confidence | |
Defined in Test.QuickCheck.State Methods showsPrec :: Int -> Confidence -> ShowS # show :: Confidence -> String # showList :: [Confidence] -> ShowS # |
If a property returns Discard
, the current test case is discarded,
the same as if a precondition was false.
An example is the definition of ==>
:
(==>) :: Testable prop => Bool -> prop -> Property False ==> _ = property Discard True ==> p = property p
Constructors
Discard |
The class of properties, i.e., types which QuickCheck knows how to test.
Typically a property will be a function returning Bool
or Property
.
Minimal complete definition
Methods
property :: prop -> Property #
Convert the thing to a property.
propertyForAllShrinkShow :: Gen a -> (a -> [a]) -> (a -> [String]) -> (a -> prop) -> Property #
Optional; used internally in order to improve shrinking.
Tests a property but also quantifies over an extra value
(with a custom shrink and show function).
The Testable
instance for functions defines
propertyForAllShrinkShow
in a way that improves shrinking.
Instances
Testable Discard | |
Testable Prop | |
Defined in Test.QuickCheck.Property | |
Testable Property | |
Testable Result | |
Defined in Test.QuickCheck.Property | |
Testable () | |
Defined in Test.QuickCheck.Property | |
Testable Bool | |
Testable prop => Testable (Gen prop) | |
Testable prop => Testable (Maybe prop) | |
(Arbitrary a, Show a, Testable prop) => Testable (a -> prop) | |
Defined in Test.QuickCheck.Property |
The type of properties.
pattern Fn2 :: (a -> b -> c) -> Fun (a, b) c #
A modifier for testing binary functions.
prop_zipWith :: Fun (Int, Bool) Char -> [Int] -> [Bool] -> Bool prop_zipWith (Fn2 f) xs ys = zipWith f xs ys == [ f x y | (x, y) <- zip xs ys]
pattern Fn :: (a -> b) -> Fun a b #
A modifier for testing functions.
prop :: Fun String Integer -> Bool prop (Fn f) = f "banana" == f "monkey" || f "banana" == f "elephant"
pattern Success :: Int -> Int -> !(Map [String] Int) -> !(Map String Int) -> !(Map String (Map String Int)) -> String -> Result #
A successful test run
pattern GaveUp :: Int -> Int -> !(Map [String] Int) -> !(Map String Int) -> !(Map String (Map String Int)) -> String -> Result #
Given up
pattern Failure :: Int -> Int -> Int -> Int -> Int -> QCGen -> Int -> String -> Maybe AnException -> String -> [String] -> [String] -> Set String -> [Witness] -> Result #
A failed test run
pattern NoExpectedFailure :: Int -> Int -> !(Map [String] Int) -> !(Map String Int) -> !(Map String (Map String Int)) -> String -> Result #
A property that should have failed did not
numDiscarded :: Result -> Int #
Number of tests skipped
labels :: Result -> Map [String] Int #
The number of test cases having each combination of labels (see label
)
numShrinks :: Result -> Int #
Number of successful shrinking steps performed
numShrinkTries :: Result -> Int #
Number of unsuccessful shrinking steps performed
numShrinkFinal :: Result -> Int #
Number of unsuccessful shrinking steps performed since last successful shrink
theException :: Result -> Maybe AnException #
The exception the property threw, if any
failingTestCase :: Result -> [String] #
The test case which provoked the failure
failingLabels :: Result -> [String] #
The test case's labels (see label
)
(==>) :: Testable prop => Bool -> prop -> Property infixr 0 #
Implication for properties: The resulting property holds if
the first argument is False
(in which case the test case is discarded),
or if the given property holds. Note that using implication carelessly can
severely skew test case distribution: consider using cover
to make sure
that your test data is still good quality.
sized :: (Int -> Gen a) -> Gen a #
Used to construct generators that depend on the size parameter.
For example, listOf
, which uses the size parameter as an upper bound on
length of lists it generates, can be defined like this:
listOf :: Gen a -> Gen [a] listOf gen = sized $ \n -> do k <- choose (0,n) vectorOf k gen
You can also do this using getSize
.
Returns the size parameter. Used to construct generators that depend on the size parameter.
For example, listOf
, which uses the size parameter as an upper bound on
length of lists it generates, can be defined like this:
listOf :: Gen a -> Gen [a] listOf gen = do n <- getSize k <- choose (0,n) vectorOf k gen
You can also do this using sized
.
resize :: HasCallStack => Int -> Gen a -> Gen a #
Overrides the size parameter. Returns a generator which uses the given size instead of the runtime-size parameter.
scale :: (Int -> Int) -> Gen a -> Gen a #
Adjust the size parameter, by transforming it with the given function.
choose :: Random a => (a, a) -> Gen a #
Generates a random element in the given inclusive range.
For integral and enumerated types, the specialised variants of
choose
below run much quicker.
chooseEnum :: Enum a => (a, a) -> Gen a #
A fast implementation of choose
for enumerated types.
chooseBoundedIntegral :: (Bounded a, Integral a) => (a, a) -> Gen a #
A fast implementation of choose
for bounded integral types.
Run a generator. The size passed to the generator is always 30;
if you want another size then you should explicitly use resize
.
suchThatMap :: Gen a -> (a -> Maybe b) -> Gen b #
Generates a value for which the given function returns a Just
, and then
applies the function.
suchThatMaybe :: Gen a -> (a -> Bool) -> Gen (Maybe a) #
Tries to generate a value that satisfies a predicate.
If it fails to do so after enough attempts, returns Nothing
.
oneof :: HasCallStack => [Gen a] -> Gen a #
Randomly uses one of the given generators. The input list must be non-empty.
frequency :: HasCallStack => [(Int, Gen a)] -> Gen a #
Chooses one of the given generators, with a weighted random distribution. The input list must be non-empty.
elements :: HasCallStack => [a] -> Gen a #
Generates one of the given values. The input list must be non-empty.
growingElements :: HasCallStack => [a] -> Gen a #
Takes a list of elements of increasing size, and chooses among an initial segment of the list. The size of this initial segment increases with the size parameter. The input list must be non-empty.
Generates a list of random length. The maximum length depends on the size parameter.
Generates a non-empty list of random length. The maximum length depends on the size parameter.
infiniteListOf :: Gen a -> Gen [a] #
Generates an infinite list.
arbitrary1 :: (Arbitrary1 f, Arbitrary a) => Gen (f a) #
shrink1 :: (Arbitrary1 f, Arbitrary a) => f a -> [f a] #
arbitrary2 :: (Arbitrary2 f, Arbitrary a, Arbitrary b) => Gen (f a b) #
shrink2 :: (Arbitrary2 f, Arbitrary a, Arbitrary b) => f a b -> [f a b] #
genericShrink :: (Generic a, RecursivelyShrink (Rep a), GSubterms (Rep a) a) => a -> [a] #
Shrink a term to any of its immediate subterms, and also recursively shrink all subterms.
recursivelyShrink :: (Generic a, RecursivelyShrink (Rep a)) => a -> [a] #
Recursively shrink all immediate subterms.
shrinkList :: (a -> [a]) -> [a] -> [[a]] #
Shrink a list of values given a shrinking function for individual values.
applyArbitrary2 :: (Arbitrary a, Arbitrary b) => (a -> b -> r) -> Gen r #
Apply a binary function to random arguments.
applyArbitrary3 :: (Arbitrary a, Arbitrary b, Arbitrary c) => (a -> b -> c -> r) -> Gen r #
Apply a ternary function to random arguments.
applyArbitrary4 :: (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d) => (a -> b -> c -> d -> r) -> Gen r #
Apply a function of arity 4 to random arguments.
arbitrarySizedIntegral :: Integral a => Gen a #
Generates an integral number. The number can be positive or negative and its maximum absolute value depends on the size parameter.
arbitrarySizedNatural :: Integral a => Gen a #
Generates a natural number. The number's maximum value depends on the size parameter.
arbitrarySizedFractional :: Fractional a => Gen a #
Uniformly generates a fractional number. The number can be positive or negative and its maximum absolute value depends on the size parameter.
arbitraryBoundedIntegral :: (Bounded a, Integral a) => Gen a #
Generates an integral number. The number is chosen uniformly from
the entire range of the type. You may want to use
arbitrarySizedBoundedIntegral
instead.
arbitraryBoundedRandom :: (Bounded a, Random a) => Gen a #
Generates an element of a bounded type. The element is chosen from the entire range of the type.
arbitraryBoundedEnum :: (Bounded a, Enum a) => Gen a #
Generates an element of a bounded enumeration.
arbitrarySizedBoundedIntegral :: (Bounded a, Integral a) => Gen a #
Generates an integral number from a bounded domain. The number is chosen from the entire range of the type, but small numbers are generated more often than big numbers. Inspired by demands from Phil Wadler.
arbitraryUnicodeChar :: Gen Char #
Generates any Unicode character (but not a surrogate)
arbitraryASCIIChar :: Gen Char #
Generates a random ASCII character (0-127).
arbitraryPrintableChar :: Gen Char #
Generates a printable Unicode character.
shrinkNothing :: a -> [a] #
Returns no shrinking alternatives.
shrinkMap :: Arbitrary a => (a -> b) -> (b -> a) -> b -> [b] #
Map a shrink function to another domain. This is handy if your data type has special invariants, but is almost isomorphic to some other type.
shrinkOrderedList :: (Ord a, Arbitrary a) => [a] -> [[a]] shrinkOrderedList = shrinkMap sort id shrinkSet :: (Ord a, Arbitrary a) => Set a -> [Set a] shrinkSet = shrinkMap fromList toList
shrinkMapBy :: (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b] #
Non-overloaded version of shrinkMap
.
shrinkIntegral :: Integral a => a -> [a] #
Shrink an integral number.
shrinkBoundedEnum :: (Bounded a, Enum a, Eq a) => a -> [a] #
Shrink an element of a bounded enumeration.
Example
data MyEnum = E0 | E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9 deriving (Bounded, Enum, Eq, Ord, Show)
>>>
shrinkBoundedEnum E9
[E0,E5,E7,E8]
>>>
shrinkBoundedEnum E5
[E0,E3,E4]
>>>
shrinkBoundedEnum E0
[]
shrinkRealFrac :: RealFrac a => a -> [a] #
Shrink a fraction, preferring numbers with smaller
numerators or denominators. See also shrinkDecimal
.
shrinkDecimal :: RealFrac a => a -> [a] #
Shrink a real number, preferring numbers with shorter
decimal representations. See also shrinkRealFrac
.
genericCoarbitrary :: (Generic a, GCoArbitrary (Rep a)) => a -> Gen b -> Gen b #
Generic CoArbitrary implementation.
(><) :: (Gen a -> Gen a) -> (Gen a -> Gen a) -> Gen a -> Gen a #
Combine two generator perturbing functions, for example the
results of calls to variant
or coarbitrary
.
coarbitraryIntegral :: Integral a => a -> Gen b -> Gen b #
A coarbitrary
implementation for integral numbers.
coarbitraryReal :: Real a => a -> Gen b -> Gen b #
A coarbitrary
implementation for real numbers.
coarbitraryShow :: Show a => a -> Gen b -> Gen b #
coarbitrary
helper for lazy people :-).
coarbitraryEnum :: Enum a => a -> Gen b -> Gen b #
A coarbitrary
implementation for enums.
orderedList :: (Ord a, Arbitrary a) => Gen [a] #
Generates an ordered list.
infiniteList :: Arbitrary a => Gen [a] #
Generates an infinite list.
functionRealFrac :: RealFrac a => (a -> b) -> a :-> b #
functionIntegral :: Integral a => (a -> b) -> a :-> b #
functionShow :: (Show a, Read a) => (a -> c) -> a :-> c #
functionVoid :: (forall b. void -> b) -> void :-> c #
functionMap :: Function b => (a -> b) -> (b -> a) -> (a -> c) -> a :-> c #
applyFun :: Fun a b -> a -> b #
Extracts the value of a function.
Fn
is the pattern equivalent of this function.
prop :: Fun String Integer -> Bool prop f = applyFun f "banana" == applyFun f "monkey" || applyFun f "banana" == applyFun f "elephant"
applyFun2 :: Fun (a, b) c -> a -> b -> c #
Extracts the value of a binary function.
Fn2
is the pattern equivalent of this function.
prop_zipWith :: Fun (Int, Bool) Char -> [Int] -> [Bool] -> Bool prop_zipWith f xs ys = zipWith (applyFun2 f) xs ys == [ applyFun2 f x y | (x, y) <- zip xs ys]
applyFun3 :: Fun (a, b, c) d -> a -> b -> c -> d #
Extracts the value of a ternary function. Fn3
is the
pattern equivalent of this function.
ioProperty :: Testable prop => IO prop -> Property #
Do I/O inside a property.
Warning: any random values generated inside of the argument to ioProperty
will not currently be shrunk. For best results, generate all random values
before calling ioProperty
, or use idempotentIOProperty
if that is safe.
idempotentIOProperty :: Testable prop => IO prop -> Property #
Do I/O inside a property.
Warning: during shrinking, the I/O may not always be re-executed.
Instead, the I/O may be executed once and then its result retained.
If this is not acceptable, use ioProperty
instead.
coerceWitness :: Typeable a => Witness -> a #
castWitness :: Typeable a => Witness -> Maybe a #
mapSize :: Testable prop => (Int -> Int) -> prop -> Property #
Adjust the test case size for a property, by transforming it with the given function.
Arguments
:: Testable prop | |
=> (a -> [a]) |
|
-> a | The original argument |
-> (a -> prop) | |
-> Property |
Shrinks the argument to a property if it fails. Shrinking is done automatically for most types. This function is only needed when you want to override the default behavior.
noShrinking :: Testable prop => prop -> Property #
Disables shrinking for a property altogether.
Only quantification inside the call to noShrinking
is affected.
counterexample :: Testable prop => String -> prop -> Property #
Adds the given string to the counterexample if the property fails.
printTestCase :: Testable prop => String -> prop -> Property #
Adds the given string to the counterexample if the property fails.
whenFail :: Testable prop => IO () -> prop -> Property #
Performs an IO
action after the last failure of a property.
whenFail' :: Testable prop => IO () -> prop -> Property #
Performs an IO
action every time a property fails. Thus,
if shrinking is done, this can be used to keep track of the
failures along the way.
verboseShrinking :: Testable prop => prop -> Property #
Prints out the generated test case every time the property fails, including during shrinking.
Only variables quantified over inside the verboseShrinking
are printed.
Note: for technical reasons, the test case is printed out after
the property is tested. To debug a property that goes into an
infinite loop, use within
to add a timeout instead.
expectFailure :: Testable prop => prop -> Property #
Indicates that a property is supposed to fail. QuickCheck will report an error if it does not fail.
once :: Testable prop => prop -> Property #
Modifies a property so that it only will be tested once.
Opposite of again
.
again :: Testable prop => prop -> Property #
Modifies a property so that it will be tested repeatedly.
Opposite of once
.
withMaxSuccess :: Testable prop => Int -> prop -> Property #
Configures how many times a property will be tested.
For example,
quickCheck (withMaxSuccess 1000 p)
will test p
up to 1000 times.
withDiscardRatio :: Testable prop => Int -> prop -> Property #
Configures how many times a property is allowed to be discarded before failing.
For example,
quickCheck (withDiscardRatio 10 p)
will allow p
to fail up to 10 times per successful test.
withMaxShrinks :: Testable prop => Int -> prop -> Property #
Configure the maximum number of times a property will be shrunk.
For example,
quickCheck (withMaxShrinks 100 p)
will cause p
to only attempt 100 shrinks on failure.
withMaxSize :: Testable prop => Int -> prop -> Property #
Configure the maximum size a property will be tested at.
witness :: (Typeable a, Show a, Testable prop) => a -> prop -> Property #
Return a value in the witnesses
field of the Result
returned by quickCheckResult
. Witnesses
are returned outer-most first.
In ghci, for example:
>>>
[Wit x] <- fmap witnesses . quickCheckResult $ \ x -> witness x $ x == (0 :: Int)
*** Failed! Falsified (after 2 tests): 1>>>
x
1>>>
:t x
x :: Int
checkCoverage :: Testable prop => prop -> Property #
Check that all coverage requirements defined by cover
and coverTable
are met, using a statistically sound test, and fail if they are not met.
Ordinarily, a failed coverage check does not cause the property to fail.
This is because the coverage requirement is not tested in a statistically
sound way. If you use cover
to express that a certain value must appear 20%
of the time, QuickCheck will warn you if the value only appears in 19 out of
100 test cases - but since the coverage varies randomly, you may have just
been unlucky, and there may not be any real problem with your test
generation.
When you use checkCoverage
, QuickCheck uses a statistical test to account
for the role of luck in coverage failures. It will run as many tests as
needed until it is sure about whether the coverage requirements are met. If a
coverage requirement is not met, the property fails.
Example:
quickCheck (checkCoverage prop_foo)
checkCoverageWith :: Testable prop => Confidence -> prop -> Property #
Check coverage requirements using a custom confidence level.
See stdConfidence
.
An example of making the statistical test less stringent in order to improve performance:
quickCheck (checkCoverageWith stdConfidence{certainty = 10^6} prop_foo)
The standard parameters used by checkCoverage
: certainty = 10^9
,
tolerance = 0.9
. See Confidence
for the meaning of the parameters.
label :: Testable prop => String -> prop -> Property #
Attaches a label to a test case. This is used for reporting test case distribution.
For example:
prop_reverse_reverse :: [Int] -> Property prop_reverse_reverse xs = label ("length of input is " ++ show (length xs)) $ reverse (reverse xs) === xs
>>>
quickCheck prop_reverse_reverse
+++ OK, passed 100 tests: 7% length of input is 7 6% length of input is 3 5% length of input is 4 4% length of input is 6 ...
Each use of label
in your property results in a separate
table of test case distribution in the output. If this is
not what you want, use tabulate
.
collect :: (Show a, Testable prop) => a -> prop -> Property #
Attaches a label to a test case. This is used for reporting test case distribution.
collect x = label (show x)
For example:
prop_reverse_reverse :: [Int] -> Property prop_reverse_reverse xs = collect (length xs) $ reverse (reverse xs) === xs
>>>
quickCheck prop_reverse_reverse
+++ OK, passed 100 tests: 7% 7 6% 3 5% 4 4% 6 ...
Each use of collect
in your property results in a separate
table of test case distribution in the output. If this is
not what you want, use tabulate
.
Arguments
:: Testable prop | |
=> Bool |
|
-> String | Label. |
-> prop | |
-> Property |
Reports how many test cases satisfy a given condition.
For example:
prop_sorted_sort :: [Int] -> Property prop_sorted_sort xs = sorted xs ==> classify (length xs > 1) "non-trivial" $ sort xs === xs
>>>
quickCheck prop_sorted_sort
+++ OK, passed 100 tests (22% non-trivial).
Arguments
:: Testable prop | |
=> Double | The required percentage (0-100) of test cases. |
-> Bool |
|
-> String | Label for the test case class. |
-> prop | |
-> Property |
Checks that at least the given proportion of successful test cases belong to the given class. Discarded tests (i.e. ones with a false precondition) do not affect coverage.
Note: If the coverage check fails, QuickCheck prints out a warning, but
the property does not fail. To make the property fail, use checkCoverage
.
For example:
prop_sorted_sort :: [Int] -> Property prop_sorted_sort xs = sorted xs ==> cover 50 (length xs > 1) "non-trivial" $ sort xs === xs
>>>
quickCheck prop_sorted_sort
+++ OK, passed 100 tests; 135 discarded (26% non-trivial). Only 26% non-trivial, but expected 50%
tabulate :: Testable prop => String -> [String] -> prop -> Property #
Collects information about test case distribution into a table.
The arguments to tabulate
are the table's name and a list of values
associated with the current test case. After testing, QuickCheck prints the
frequency of all collected values. The frequencies are expressed as a
percentage of the total number of values collected.
You should prefer tabulate
to label
when each test case is associated
with a varying number of values. Here is a (not terribly useful) example,
where the test data is a list of integers and we record all values that
occur in the list:
prop_sorted_sort :: [Int] -> Property prop_sorted_sort xs = sorted xs ==> tabulate "List elements" (map show xs) $ sort xs === xs
>>>
quickCheck prop_sorted_sort
+++ OK, passed 100 tests; 1684 discarded. List elements (109 in total): 3.7% 0 3.7% 17 3.7% 2 3.7% 6 2.8% -6 2.8% -7
Here is a more useful example. We are testing a chatroom, where the user can log in, log out, or send a message:
data Command = LogIn | LogOut | SendMessage String deriving (Data, Show) instance Arbitrary Command where ...
There are some restrictions on command sequences; for example, the user must
log in before doing anything else. The function valid :: [Command] -> Bool
checks that a command sequence is allowed. Our property then has the form:
prop_chatroom :: [Command] -> Property prop_chatroom cmds = valid cmds ==> ...
The use of ==>
may skew test case distribution. We use collect
to see the
length of the command sequences, and tabulate
to get the frequencies of the
individual commands:
prop_chatroom :: [Command] -> Property prop_chatroom cmds = wellFormed cmds LoggedOut ==> 'collect' (length cmds) $ 'tabulate' "Commands" (map (show . 'Data.Data.toConstr') cmds) $ ...
>>>
quickCheckWith stdArgs{maxDiscardRatio = 1000} prop_chatroom
+++ OK, passed 100 tests; 2775 discarded: 60% 0 20% 1 15% 2 3% 3 1% 4 1% 5 Commands (68 in total): 62% LogIn 22% SendMessage 16% LogOut
coverTable :: Testable prop => String -> [(String, Double)] -> prop -> Property #
Checks that the values in a given table
appear a certain proportion of
the time. A call to coverTable
table
[(x1, p1), ..., (xn, pn)]
asserts
that of the values in table
, x1
should appear at least p1
percent of
the time that table
appears, x2
at least p2
percent of the time that
table
appears, and so on.
Note: If the coverage check fails, QuickCheck prints out a warning, but
the property does not fail. To make the property fail, use checkCoverage
.
Continuing the example from the tabular
combinator...
data Command = LogIn | LogOut | SendMessage String deriving (Data, Show) prop_chatroom :: [Command] -> Property prop_chatroom cmds = wellFormed cmds LoggedOut ==> 'tabulate' "Commands" (map (show . 'Data.Data.toConstr') cmds) $ ...
...we can add a coverage requirement as follows, which checks that LogIn
,
LogOut
and SendMessage
each occur at least 25% of the time:
prop_chatroom :: [Command] -> Property prop_chatroom cmds = wellFormed cmds LoggedOut ==> coverTable "Commands" [("LogIn", 25), ("LogOut", 25), ("SendMessage", 25)] $ 'tabulate' "Commands" (map (show . 'Data.Data.toConstr') cmds) $ ... property goes here ...
>>>
quickCheck prop_chatroom
+++ OK, passed 100 tests; 2909 discarded: 56% 0 17% 1 10% 2 6% 3 5% 4 3% 5 3% 7 Commands (111 in total): 51.4% LogIn 30.6% SendMessage 18.0% LogOut Table 'Commands' had only 18.0% LogOut, but expected 25.0%
within :: Testable prop => Int -> prop -> Property #
Considers a property failed if it does not complete within the given number of microseconds.
Note: if the property times out, variables quantified inside the
within
will not be printed. Therefore, you should use within
only in the body of your property.
Good: prop_foo a b c = within 1000000 ...
Bad: prop_foo = within 1000000 $ \a b c -> ...
Bad: prop_foo a b c = ...; main = quickCheck (within 1000000 prop_foo)
discardAfter :: Testable prop => Int -> prop -> Property #
Discards the test case if it does not complete within the given number of microseconds. This can be useful when testing algorithms that have pathological cases where they run extremely slowly.
forAll :: (Show a, Testable prop) => Gen a -> (a -> prop) -> Property #
Explicit universal quantification: uses an explicitly given test case generator.
forAllShow :: Testable prop => Gen a -> (a -> String) -> (a -> prop) -> Property #
Like forAll
, but with an explicitly given show function.
forAllBlind :: Testable prop => Gen a -> (a -> prop) -> Property #
Like forAll
, but without printing the generated value.
forAllShrink :: (Show a, Testable prop) => Gen a -> (a -> [a]) -> (a -> prop) -> Property #
Like forAll
, but tries to shrink the argument for failing test cases.
forAllShrinkShow :: Testable prop => Gen a -> (a -> [a]) -> (a -> String) -> (a -> prop) -> Property #
Like forAllShrink
, but with an explicitly given show function.
forAllShrinkBlind :: Testable prop => Gen a -> (a -> [a]) -> (a -> prop) -> Property #
Like forAllShrink
, but without printing the generated value.
(.&.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> Property infixr 1 #
Nondeterministic choice: p1
.&.
p2
picks randomly one of
p1
and p2
to test. If you test the property 100 times it
makes 100 random choices.
(.&&.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> Property infixr 1 #
Conjunction: p1
.&&.
p2
passes if both p1
and p2
pass.
(.||.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> Property infixr 1 #
Disjunction: p1
.||.
p2
passes unless p1
and p2
simultaneously fail.
(===) :: (Eq a, Show a) => a -> a -> Property infix 4 #
Like ==
, but prints a counterexample when it fails.
(=/=) :: (Eq a, Show a) => a -> a -> Property infix 4 #
Like /=
, but prints a counterexample when it fails.
total :: NFData a => a -> Property #
Checks that a value is total, i.e., doesn't crash when evaluated.
recheck :: Testable prop => Result -> prop -> IO () #
Re-run a property with the seed and size that failed in a run of quickCheckResult
.
polyQuickCheck :: Name -> ExpQ #
Test a polymorphic property, defaulting all type variables to Integer
.
Invoke as $(
, where polyQuickCheck
'prop)prop
is a property.
Note that just evaluating
in GHCi will seem to
work, but will silently default all type variables to quickCheck
prop()
!
$(
means the same as
polyQuickCheck
'prop)
.
If you want to supply custom arguments to quickCheck
$(monomorphic
'prop)polyQuickCheck
,
you will have to combine quickCheckWith
and monomorphic
yourself.
If you want to use polyQuickCheck
in the same file where you defined the
property, the same scoping problems pop up as in quickCheckAll
:
see the note there about return []
.
polyVerboseCheck :: Name -> ExpQ #
Test a polymorphic property, defaulting all type variables to Integer
.
This is just a convenience function that combines verboseCheck
and monomorphic
.
If you want to use polyVerboseCheck
in the same file where you defined the
property, the same scoping problems pop up as in quickCheckAll
:
see the note there about return []
.
monomorphic :: Name -> ExpQ #
Monomorphise an arbitrary property by defaulting all type variables to Integer
.
For example, if f
has type
then Ord
a => [a] -> [a]$(
has type monomorphic
'f)[
.Integer
] -> [Integer
]
If you want to use monomorphic
in the same file where you defined the
property, the same scoping problems pop up as in quickCheckAll
:
see the note there about return []
.
labelledExamples :: Testable prop => prop -> IO () #
Given a property, which must use label
, collect
, classify
or cover
to associate labels with test cases, find an example test case for each possible label.
The example test cases are minimised using shrinking.
For example, suppose we test
and record the number
of times that delete
x xsx
occurs in xs
:
prop_delete :: Int -> [Int] -> Property prop_delete x xs = classify (count x xs == 0) "count x xs == 0" $ classify (count x xs == 1) "count x xs == 1" $ classify (count x xs >= 2) "count x xs >= 2" $ counterexample (show (delete x xs)) $ count x (delete x xs) == max 0 (count x xs-1) where count x xs = length (filter (== x) xs)
labelledExamples
generates three example test cases, one for each label:
>>>
labelledExamples prop_delete
*** Found example of count x xs == 0 0 [] [] *** Found example of count x xs == 1 0 [0] [] *** Found example of count x xs >= 2 5 [5,5] [5] +++ OK, passed 100 tests: 78% count x xs == 0 21% count x xs == 1 1% count x xs >= 2
labelledExamplesWith :: Testable prop => Args -> prop -> IO () #
A variant of labelledExamples
that takes test arguments.
labelledExamplesResult :: Testable prop => prop -> IO Result #
A variant of labelledExamples
that returns a result.
labelledExamplesWithResult :: Testable prop => Args -> prop -> IO Result #
A variant of labelledExamples
that takes test arguments and returns a result.
Internal
If you are building a test suite, you don't need these functions.
They may be used by other tasty add-on packages (such as tasty-hspec).
optionSetToArgs :: OptionSet -> IO (Int, Args) Source #
Convert tasty options into QuickCheck options.
This is a low-level function that was originally added for tasty-hspec but may be used by others.
The returned Int is kept only for backward compatibility purposes. It
has no use in tasty-quickcheck
.
Since: 0.9.1