module Test.QuickCheck.Counterexamples(module Test.QuickCheck.Counterexamples, module Test.QuickCheck) where
import Data.IORef
import Test.QuickCheck hiding
( quickCheck, quickCheckWith, quickCheckResult, quickCheckWithResult
, verboseCheck, verboseCheckWith, verboseCheckResult, verboseCheckWithResult
, polyQuickCheck, polyVerboseCheck
, Property, Testable(..)
, forAll
, forAllShrink
, shrinking
, (==>)
, (===)
, ioProperty
, verbose
, once
, again
, within
, noShrinking
, (.&.)
, (.&&.)
, conjoin
, (.||.)
, disjoin
, counterexample
, printTestCase
, whenFail
, whenFail'
, expectFailure
, label
, collect
, classify
, cover
, mapSize
)
import qualified Test.QuickCheck as QC
import Language.Haskell.TH
newtype PropertyOf cex =
MkProperty {
unProperty :: (cex -> IO ()) -> QC.Property }
deriving Functor
type Property = PropertyOf ()
type PropertyFrom prop = PropertyOf (Counterexample prop)
class QC.Testable prop => Testable prop where
type Counterexample prop
property :: prop -> PropertyFrom prop
instance Testable Discard where
type Counterexample Discard = ()
property prop = MkProperty (\_ -> QC.property prop)
instance Testable Bool where
type Counterexample Bool = ()
property prop = MkProperty (\f -> QC.whenFail (f ()) prop)
instance Testable QC.Property where
type Counterexample QC.Property = ()
property prop = MkProperty (\f -> QC.whenFail (f ()) prop)
instance Testable prop => Testable (Gen prop) where
type Counterexample (Gen prop) = Counterexample prop
property prop = MkProperty $ \k ->
QC.property (unProperty . property <$> prop <*> pure k)
instance QC.Testable (PropertyOf cex) where
property prop = unProperty prop (\_ -> return ())
instance Testable (PropertyOf cex) where
type Counterexample (PropertyOf cex) = cex
property = id
instance (Show a, QC.Arbitrary a, Testable b) => Testable (a -> b) where
type Counterexample (a -> b) = a :&: Counterexample b
property prop = forAllShrink arbitrary shrink prop
infixr 6 :&:
data a :&: b = a :&: b deriving (Eq, Ord, Show, Read)
typedCounterexample :: Testable prop => a -> prop -> PropertyOf (a :&: Counterexample prop)
typedCounterexample x prop = fmap (x :&:) (property prop)
onProperty :: Testable prop => (QC.Property -> QC.Property) -> prop -> PropertyFrom prop
onProperty f prop =
MkProperty (\k -> f (unProperty (property prop) k))
quickCheck :: Testable prop => prop -> IO (Maybe (Counterexample prop))
quickCheck = quickCheckWith stdArgs
quickCheckWith :: Testable prop => Args -> prop -> IO (Maybe (Counterexample prop))
quickCheckWith args prop = fmap fst (quickCheckWithResult args prop)
quickCheckResult :: Testable prop => prop -> IO (Maybe (Counterexample prop), Result)
quickCheckResult = quickCheckWithResult stdArgs
quickCheckWithResult :: Testable prop => Args -> prop -> IO (Maybe (Counterexample prop), Result)
quickCheckWithResult args prop = do
ref <- newIORef Nothing
let
modify x Nothing = Just x
modify _ (Just _) =
error "Internal error in quickcheck-with-counterexamples: IORef written to twice"
res <- QC.quickCheckWithResult args $ ioProperty $ do
return $ unProperty (property prop) (modifyIORef ref . modify)
cex <- readIORef ref
return (cex, res)
verboseCheck :: Testable prop => prop -> IO (Maybe (Counterexample prop))
verboseCheck p = quickCheck (verbose p)
verboseCheckWith :: Testable prop => Args -> prop -> IO (Maybe (Counterexample prop))
verboseCheckWith args p = quickCheckWith args (verbose p)
verboseCheckResult :: Testable prop => prop -> IO (Maybe (Counterexample prop), Result)
verboseCheckResult p = quickCheckResult (verbose p)
verboseCheckWithResult :: Testable prop => Args -> prop -> IO (Maybe (Counterexample prop), Result)
verboseCheckWithResult a p = quickCheckWithResult a (verbose p)
polyQuickCheck :: Name -> ExpQ
polyQuickCheck x = [| quickCheck $(monomorphic x) |]
polyVerboseCheck :: Name -> ExpQ
polyVerboseCheck x = [| verboseCheck $(monomorphic x) |]
forAll :: (Testable prop, Show a) => Gen a -> (a -> prop) -> PropertyOf (a :&: Counterexample prop)
forAll arb prop = forAllShrink arb shrinkNothing prop
forAllShrink :: (Testable prop, Show a) => Gen a -> (a -> [a]) -> (a -> prop) -> PropertyOf (a :&: Counterexample prop)
forAllShrink arb shr prop =
MkProperty $ \f ->
QC.forAllShrink arb shr $ \x ->
unProperty (property (prop x)) (\y -> f (x :&: y))
shrinking :: Testable prop => (a -> [a]) -> a -> (a -> prop) -> PropertyFrom prop
shrinking shr x prop =
MkProperty $ \k -> QC.shrinking shr x $ \x ->
unProperty (property (prop x)) k
infixr 0 ==>
(==>) :: Testable prop => Bool -> prop -> PropertyFrom prop
x ==> prop = onProperty (x QC.==>) prop
infix 4 ===
(===) :: (Eq a, Show a) => a -> a -> Property
x === y = property (x QC.=== y)
ioProperty :: Testable prop => IO prop -> PropertyFrom prop
ioProperty ioprop =
MkProperty $ \k -> QC.ioProperty $ do
prop <- ioprop
return (unProperty (property prop) k)
verbose :: Testable prop => prop -> PropertyFrom prop
verbose = onProperty QC.verbose
once :: Testable prop => prop -> PropertyFrom prop
once = onProperty QC.once
again :: Testable prop => prop -> PropertyFrom prop
again = onProperty QC.again
within :: Testable prop => Int -> prop -> PropertyFrom prop
within n = onProperty (QC.within n)
noShrinking :: Testable prop => prop -> PropertyFrom prop
noShrinking = onProperty QC.noShrinking
counterexample :: Testable prop => String -> prop -> PropertyFrom prop
counterexample msg = onProperty (QC.counterexample msg)
whenFail :: Testable prop => IO () -> prop -> PropertyFrom prop
whenFail m = onProperty (QC.whenFail m)
whenFail' :: Testable prop => IO () -> prop -> PropertyFrom prop
whenFail' m = onProperty (QC.whenFail' m)
expectFailure :: Testable prop => prop -> PropertyFrom prop
expectFailure = onProperty QC.expectFailure
label :: Testable prop => String -> prop -> PropertyFrom prop
label lab = onProperty (QC.label lab)
collect :: (Show a, Testable prop) => a -> prop -> PropertyFrom prop
collect x = onProperty (QC.collect x)
classify :: Testable prop => Bool -> String -> prop -> PropertyFrom prop
classify cond lab = onProperty (QC.classify cond lab)
cover :: Testable prop => Bool -> Int -> String -> prop -> PropertyFrom prop
cover cond percent lab = onProperty (QC.cover cond percent lab)
mapSize :: Testable prop => (Int -> Int) -> prop -> PropertyFrom prop
mapSize f = onProperty (QC.mapSize f)