newtype Property
The type of properties.
Combinators for constructing properties.
newtype PropertyThe type of properties.
MkProperty :: Gen Prop -> PropertyunProperty :: Property -> Gen Propclass Testable propThe class of properties, i.e., types which QuickCheck knows how to test.
Typically a property will be a function returning Bool or Property.
property :: prop -> PropertyConvert the thing to a property.
propertyForAllShrinkShow :: Gen a -> (a -> [a]) -> (a -> [String]) -> (a -> prop) -> PropertyOptional; 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.
data DiscardIf 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 pDiscard :: DiscardmorallyDubiousIOProperty :: Testable prop => IO prop -> PropertyUse 'ioProperty' instead
Do I/O inside a property.
ioProperty :: Testable prop => IO prop -> PropertyDo 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 -> PropertyDo 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.
protect :: (AnException -> a) -> IO a -> IO anewtype PropMkProp :: Rose Result -> PropunProp :: Prop -> Rose Resultdata Rose aMkRose :: a -> [Rose a] -> Rose aIORose :: (IO (Rose a)) -> Rose aioRose :: IO (Rose Result) -> Rose ResultjoinRose :: Rose (Rose a) -> Rose areduceRose :: Rose Result -> IO (Rose Result)Execute the IORose bits of a rose tree, returning a tree
constructed by MkRose.
onRose :: (a -> [Rose a] -> Rose a) -> Rose a -> Rose aApply a function to the outermost MkRose constructor of a rose tree. The function must be total!
protectRose :: IO (Rose Result) -> IO (Rose Result)Wrap a rose tree in an exception handler.
protectProp :: Prop -> PropWrap the top level of a Prop in an exception handler.
protectResults :: Rose Result -> Rose ResultWrap all the Results in a rose tree in exception handlers.
data CallbackDifferent kinds of callbacks
PostTest :: CallbackKind -> (State -> Result -> IO ()) -> Callback(State -> Result -> IO ())Called just after a test
PostFinalFailure :: CallbackKind -> (State -> Result -> IO ()) -> Callback(State -> Result -> IO ())Called with the final failing test-case
data CallbackKindCounterexample :: CallbackKindAffected by the verbose combinator
NotCounterexample :: CallbackKindNot affected by the verbose combinator
data WitnessWit :: forall a. (Typeable a, Show a) => a -> WitnesscoerceWitness :: Typeable a => Witness -> acastWitness :: Typeable a => Witness -> Maybe adata ResultThe result of a single test.
MkResult :: Maybe Bool -> Bool -> String -> Maybe AnException -> Bool -> Maybe Int -> Maybe Confidence -> Maybe Int -> Maybe Int -> Maybe Int -> [String] -> [(String, Bool)] -> [(String, String)] -> [(Maybe String, String, Double)] -> [Callback] -> [String] -> [Witness] -> Resultok :: Result -> Maybe Boolresult of the test case; Nothing = discard
expect :: Result -> Boolindicates what the expected result of the property is
reason :: Result -> Stringa message indicating what went wrong
theException :: Result -> Maybe AnExceptionthe exception thrown, if any
abort :: Result -> Boolif True, the test should not be repeated
maybeNumTests :: Result -> Maybe Intstop after this many tests
maybeCheckCoverage :: Result -> Maybe Confidencerequired coverage confidence
maybeDiscardedRatio :: Result -> Maybe Intmaximum number of discarded tests per successful test
maybeMaxShrinks :: Result -> Maybe Intmaximum number of shrinks
maybeMaxTestSize :: Result -> Maybe Intmaximum test size
labels :: Result -> [String]test case labels
classes :: Result -> [(String, Bool)]test case classes
tables :: Result -> [(String, String)]test case tables
requiredCoverage :: Result -> [(Maybe String, String, Double)]required coverage
callbacks :: Result -> [Callback]the callbacks for this test case
testCase :: Result -> [String]the generated test case
theWitnesses :: Result -> [Witness]exception :: String -> AnException -> ResultformatException :: String -> AnException -> StringprotectResult :: IO Result -> IO Resultsucceeded :: Resultfailed :: Resultrejected :: ResultliftBool :: Bool -> ResultmapResult :: Testable prop => (Result -> Result) -> prop -> PropertymapTotalResult :: Testable prop => (Result -> Result) -> prop -> PropertymapRoseResult :: Testable prop => (Rose Result -> Rose Result) -> prop -> PropertymapProp :: Testable prop => (Prop -> Prop) -> prop -> PropertymapSize :: Testable prop => (Int -> Int) -> prop -> PropertyAdjust the test case size for a property, by transforming it with the given function.
shrinking :: Testable prop => (a -> [a]) -> a -> (a -> prop) -> PropertyShrinks 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.
(a -> [a])shrink-like function.
aThe original argument
noShrinking :: Testable prop => prop -> PropertyDisables shrinking for a property altogether.
Only quantification inside the call to noShrinking is affected.
callback :: Testable prop => Callback -> prop -> PropertyAdds a callback
counterexample :: Testable prop => String -> prop -> PropertyAdds the given string to the counterexample if the property fails.
showCounterexample :: String -> IO StringprintTestCase :: Testable prop => String -> prop -> PropertyUse counterexample instead
Adds the given string to the counterexample if the property fails.
whenFail :: Testable prop => IO () -> prop -> PropertyPerforms an IO action after the last failure of a property.
whenFail' :: Testable prop => IO () -> prop -> PropertyPerforms 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.
withProgress :: Testable prop => (TestProgress -> IO ()) -> prop -> PropertyPerforms an IO action every time a property is tested, after every test.
The IO action is allowed to depend on TestProgress, which contains information
regarding how testing is progressing.
Note: QC invokes callbacks before the internal state has been updated to reflect the
most recent test. The means that e.g. currentPassed will, after the first test has
been executed, still show 0.
verbose :: Testable prop => prop -> PropertyPrints out the generated test case every time the property is tested.
Only variables quantified over inside the verbose 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.
verboseShrinking :: Testable prop => prop -> PropertyPrints 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 -> PropertyIndicates that a property is supposed to fail. QuickCheck will report an error if it does not fail.
once :: Testable prop => prop -> PropertyModifies a property so that it only will be tested once.
Opposite of again.
again :: Testable prop => prop -> PropertyModifies a property so that it will be tested repeatedly.
Opposite of once.
withMaxSuccess :: Testable prop => Int -> prop -> PropertyUse withNumTests instead
Configures how many times a property will be tested.
For example,
quickCheck (withMaxSuccess 1000 p)will test p up to 1000 times.
withNumTests :: Testable prop => Int -> prop -> PropertyConfigures how many times a property will be tested.
For example,
quickCheck (withNumTests 1000 p)will test p up to 1000 times.
withDiscardRatio :: Testable prop => Int -> prop -> PropertyConfigures 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 -> PropertyConfigure 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 -> PropertyConfigure the maximum size a property will be tested at.
witness :: (Typeable a, Show a, Testable prop) => a -> prop -> PropertyReturn a value in the witnesses field of the Result
returned by quickCheckResult. Witnesses are returned
outer-most first.
In ghci, for example:
checkCoverage :: Testable prop => prop -> PropertyCheck 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 -> PropertyCheck 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)stdConfidence :: ConfidenceThe standard parameters used by checkCoverage: certainty = 10^9,
tolerance = 0.9. See Confidence for the meaning of the parameters.
label :: Testable prop => String -> prop -> PropertyAttaches 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
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 -> PropertyAttaches 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
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.
classify :: Testable prop => Bool -> String -> prop -> PropertyReports 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
BoolTrue if the test case should be labelled.
StringLabel.
cover :: Testable prop => Double -> Bool -> String -> prop -> PropertyChecks 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
DoubleThe required percentage (0-100) of test cases.
BoolTrue if the test case belongs to the class.
StringLabel for the test case class.
tabulate :: Testable prop => String -> [String] -> prop -> PropertyCollects 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
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) $
...
coverTable :: Testable prop => String -> [(String, Double)] -> prop -> PropertyChecks 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 tabulate 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 ...
(==>) :: Testable prop => Bool -> prop -> PropertyImplication 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.
within :: Testable prop => Int -> prop -> PropertyConsiders 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 -> PropertyDiscards 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.
onTimeout :: Testable prop => Result -> Int -> prop -> PropertyforAll :: (Show a, Testable prop) => Gen a -> (a -> prop) -> PropertyExplicit universal quantification: uses an explicitly given test case generator.
forAllShow :: Testable prop => Gen a -> (a -> String) -> (a -> prop) -> PropertyLike forAll, but with an explicitly given show function.
forAllBlind :: Testable prop => Gen a -> (a -> prop) -> PropertyLike forAll, but without printing the generated value.
forAllShrink :: (Show a, Testable prop) => Gen a -> (a -> [a]) -> (a -> prop) -> PropertyLike forAll, but tries to shrink the argument for failing test cases.
forAllShrinkShow :: Testable prop => Gen a -> (a -> [a]) -> (a -> String) -> (a -> prop) -> PropertyLike forAllShrink, but with an explicitly given show function.
forAllShrinkBlind :: Testable prop => Gen a -> (a -> [a]) -> (a -> prop) -> PropertyLike forAllShrink, but without printing the generated value.
(.&.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> PropertyNondeterministic 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 -> PropertyConjunction: p1 .&&. p2 passes if both p1 and p2 pass.
conjoin :: Testable prop => [prop] -> PropertyTake the conjunction of several properties.
(.||.) :: (Testable prop1, Testable prop2) => prop1 -> prop2 -> PropertyDisjunction: p1 .||. p2 passes unless p1 and p2 simultaneously fail.
disjoin :: Testable prop => [prop] -> PropertyTake the disjunction of several properties.
(===) :: (Eq a, Show a) => a -> a -> Property(=/=) :: (Eq a, Show a) => a -> a -> Propertytotal :: NFData a => a -> PropertyChecks that a value is total, i.e., doesn't crash when evaluated.
instance Typeable Propertyinstance Testable Discardinstance Testable ()instance Testable prop => Testable (Maybe prop)instance Testable Boolinstance Testable Resultinstance Testable Propinstance Testable prop => Testable (Gen prop)instance Testable Propertyinstance (Arbitrary a, Show a, Testable prop) => Testable (a -> prop)instance Functor Roseinstance Applicative Roseinstance Monad Roseinstance Show WitnessCommunity contribution. The service has not verified this documentation. The generator reported 30 diagnostics.
Target: Linux on x86-64. Some inferred types and resolved links are not available.
Generator: aihc-haddock-aa6debbd-web-3
Metadata snapshot: f17ab6aeb2a7b6daf9fba7614097e138beeef85fa92c3fb7fdfed9ed0e49728c
Open the dependency plan · Download the documentation model