Quickcheck, which defines arbitrary instances using a function whose result depends on its arguments

I have an arbExample function to generate a random Example data type, which depends on the number of functions.

I'm trying to test properties by running quickCheck prop_example , the problem is that I don't know how to define an Arbitrary instance for Example that uses arbExample .

I like to run quickCheck prop_example when specifying the Gens data structure that 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 _ {- ??? -} 
0
source share
1 answer

Use a forAll combinator that has a signature:

 forAll :: (Show a, Testable prop) => Gen a -> (a -> prop) -> Property 

Quick example:

 import Test.QuickCheck genA :: Gen Int genA = choose (-100,100) genB :: Gen Int genB = choose (1,100) prop_example :: Int -> Bool prop_example n = n > 0 testA = quickCheck $ forAll genA prop_example testB = quickCheck $ forAll genB prop_example 

testA will fail, but testB will succeed.

+5
source

Source: https://habr.com/ru/post/978791/


All Articles