This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| forAll :: (Show a, Testable testable) | |
| => Gen a -> Shrink a -> (a -> testable) -> Property | |
| forAll argGen shrink prop = ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| findFailing :: [a] -> (a -> Result) -> Maybe (a, Result) | |
| findFailing smaller runSub = | |
| let results = map runSub smaller | |
| in find (isFailure . snd) (zip smaller results) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| shrinking :: (Show a) => Shrink a -> a -> (a -> Result) -> Result | |
| shrinking = undefined |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| evalSubProp :: Testable t => (a -> t) -> StdGen -> a -> Result | |
| evalSubProp prop rand = (`runProp` rand) . property . prop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| prop_gcd_bad :: Integer -> Integer -> Bool | |
| prop_gcd_bad a b = gcd a b > 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data Tree a = Tree | |
| { treeVal :: a | |
| , children :: [Tree a] } | |
| deriving (Functor) | |
| newtype Property = Property { getGen :: Gen (Tree Result) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| visitResultTree :: Tree Result -> Result | |
| visitResultTree (Tree Success _) = Success | |
| visitResultTree (Tree failure children) = | |
| let simplerFailure = find (isFailure . treeVal) children | |
| in maybe failure visitResultTree simplerFailure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |