module Test.StateMachine.Internal.Utils
( anyP
, liftProperty
, shrinkPropertyHelper
, shrinkPropertyHelper'
, shrinkPair
, shrinkPair'
) where
import Test.QuickCheck
(Property, Result(Failure), chatty, counterexample,
output, property, quickCheckWithResult, stdArgs)
import Test.QuickCheck.Monadic
(PropertyM(MkPropertyM), monadicIO, run)
import Test.QuickCheck.Property
((.&&.), (.||.))
anyP :: (a -> Property) -> [a] -> Property
anyP p = foldr (\x ih -> p x .||. ih) (property False)
liftProperty :: Monad m => Property -> PropertyM m ()
liftProperty prop = MkPropertyM (\k -> fmap (prop .&&.) <$> k ())
shrinkPropertyHelper :: Property -> (String -> Bool) -> Property
shrinkPropertyHelper prop p = shrinkPropertyHelper' prop (property . p)
shrinkPropertyHelper' :: Property -> (String -> Property) -> Property
shrinkPropertyHelper' prop p = monadicIO $ do
result <- run $ quickCheckWithResult (stdArgs {chatty = False}) prop
case result of
Failure { output = outputLines } -> liftProperty $
counterexample ("failed: " ++ outputLines) $ p outputLines
_ -> return ()
shrinkPair' :: (a -> [a]) -> (b -> [b]) -> ((a, b) -> [(a, b)])
shrinkPair' shrinkerA shrinkerB (x, y) =
[ (x', y) | x' <- shrinkerA x ] ++
[ (x, y') | y' <- shrinkerB y ]
shrinkPair :: (a -> [a]) -> ((a, a) -> [(a, a)])
shrinkPair shrinker = shrinkPair' shrinker shrinker