Skip to content

Commit

Permalink
Add quickCheckCounterexample
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianAlgehed committed Mar 23, 2024
1 parent dc0c3f2 commit 118c9f9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Test/QuickCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ module Test.QuickCheck
, quickCheckWith
, quickCheckWithResult
, quickCheckResult
#ifndef NO_TYPEABLE
, quickCheckCounterexample
, quickCheckWithCounterexample
#endif
, recheck
, isSuccess
-- ** Running tests verbosely
Expand Down
7 changes: 7 additions & 0 deletions src/Test/QuickCheck/Property.hs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ coerceCounterexample (Cex a) = case cast a of
castCounterexample :: Typeable a => Counterexample -> Maybe a
castCounterexample (Cex a) = cast a

data Counterexamples = NoCounterexamples
| forall a. (Typeable a, Show a) => a :! Counterexamples

toCounterexamples :: [Counterexample] -> Counterexamples
toCounterexamples [] = NoCounterexamples
toCounterexamples (Cex a : ces) = a :! toCounterexamples ces

#define COUNTEREXAMPLES(a) , theCounterexamples a
#else
#define COUNTEREXAMPLES(a)
Expand Down
17 changes: 17 additions & 0 deletions src/Test/QuickCheck/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ quickCheckWithResult :: Testable prop => Args -> prop -> IO Result
quickCheckWithResult a p =
withState a (\s -> test s (property p))

#ifndef NO_TYPEABLE
-- | Test a property and get counterexamples as a result. Can be used like:
--
-- @
-- $> x :! _ <- quickCheckCounterexample $ \ x -> withCounterexample (x :: Int) (x > 0)
-- *** Failed! Falsified (after 1 test):
-- 0
-- $> x
-- 0
quickCheckCounterexample :: Testable prop => prop -> IO Counterexamples
quickCheckCounterexample = quickCheckWithCounterexample stdArgs

-- | Test a property, using test arguments, and get counterexamples as a result.
quickCheckWithCounterexample :: Testable prop => Args -> prop -> IO Counterexamples
quickCheckWithCounterexample args p = toCounterexamples . counterexamples <$> quickCheckWithResult args p
#endif

-- | Re-run a property with the seed and size that failed in a run of 'quickCheckResult'.
recheck :: Testable prop => Result -> prop -> IO ()
recheck res@Failure{} = quickCheckWith stdArgs{ replay = Just (usedSeed res, usedSize res)} . once
Expand Down

0 comments on commit 118c9f9

Please sign in to comment.