I have a function arbExample
to generate a random Example
data type which depends on a numbers of functions.
I am trying to do some property testing by doing quickCheck prop_example
, the problem is I don't know how to define an Arbitrary
instance for Example
which uses arbExample
.
I like to be able run quickCheck prop_example
while specifying the Gens
data structure which arbExample
uses.
data Example = Example
{ myInt :: Int
, myList :: [String]
} deriving (Show)
data Gens = Gens
{ gen1 :: Gen Int
, gen2 :: Gen String }
arbExample :: Gens -> Gen Example
arbExample gens = do
i <- gen1 gens
xs <- vectorOf i (gen2 gens)
return Example{myInt=i, myList=xs}
prop_example :: Example -> Property
prop_example example = do
let len = length (myList example)
monadicIO $ do
-- result of running some program
successful <- run $ (\e -> return False) example
case successful of
True -> return ()
False -> fail "failure "
instance Arbitrary Example where
arbitrary = arbExample _ {- ??? -}
Use the
forAll
combinator which has the signature:A quick example:
testA
will fail buttestB
will succeed.