Skip to content

Instantly share code, notes, and snippets.

forAll :: (Show a, Testable testable)
=> Gen a -> Shrink a -> (a -> testable) -> Property
forAll argGen shrink prop = ...
findFailing :: [a] -> (a -> Result) -> Maybe (a, Result)
findFailing smaller runSub =
let results = map runSub smaller
in find (isFailure . snd) (zip smaller results)
shrinking :: (Show a) => Shrink a -> a -> (a -> Result) -> Result
shrinking = undefined
evalSubProp :: Testable t => (a -> t) -> StdGen -> a -> Result
evalSubProp prop rand = (`runProp` rand) . property . prop
prop_gcd_bad :: Integer -> Integer -> Bool
prop_gcd_bad a b = gcd a b > 1
data Tree a = Tree
{ treeVal :: a
, children :: [Tree a] }
deriving (Functor)
newtype Property = Property { getGen :: Gen (Tree Result) }
visitResultTree :: Tree Result -> Result
visitResultTree (Tree Success _) = Success
visitResultTree (Tree failure children) =
let simplerFailure = find (isFailure . treeVal) children
in maybe failure visitResultTree simplerFailure
rapidCheckImpl :: Testable prop => Int -> Int -> prop -> Result
rapidCheckImpl attemptNb startSeed prop = runAll (property prop)
where
runAll prop = foldMap (runOne prop) [startSeed .. startSeed + attemptNb - 1]
runOne prop seed =
let result = visitResultTree (runProp prop (mkStdGen seed))
in overFailure result $ \failure -> failure { seed = seed }
forAll :: (Show a, Testable t) => Gen a -> Shrink a -> (a -> t) -> Property
forAll argGen shrink prop =
Property $ Gen $ \rand -> -- Create a new property that will
let (rand1, rand2) = split rand -- Split the generator in two
arg = runGen argGen rand1 -- Use the first generator to produce an arg
tree = resultTree shrink arg prop -- Enrich the sub-property result tree
in runProp tree rand2 -- Run the property with the second generator
resultTree :: (Show a, Testable t) => Shrink a -> a -> (a -> t) -> Property
resultTree shrinker arg prop =
Property $ Gen $ \rand ->
let shrinkTree = buildTree shrinker arg -- Build the shrink tree
resultTree = fmap toResult shrinkTree -- Transform it to a result tree
toResult x = -- To compute a result tree
addCounterExample x $ -- Add the outer arg to all failures
runProp (property (prop x)) rand -- Inside the sub result tree
in joinTree resultTree -- At the end, join the result tree