haskellquickcheck

Using QuickCheck properties with coverage inside a monadic context


I'm trying to write a QuickCheck property that uses cover to make sure that the test data covers certain cases in combination with the fact that the test involves an IO action.

The problem essentially boils down to the following:

prop = do
  res <- exampleIOAction
  cover 40.0 (even res) "res is even" $ res > 0

-- some IO action that returns an Int, the actual implementation doesn't matter
exampleIOAction :: IO Int
exampleIOAction = undefined

The interesting detail is that I want to cover based on the results of an IO action. However, the code fails to compile because Couldn't match expected type ‘IO b’ with actual type ‘Property’.

I tried using Test.QuickCheck.Monadic but I wasn't able to get it to work. Is there any way to do that with QuickCheck?


Solution

  • import Test.QuickCheck.Monadic
    
    prop :: Property
    prop = monadicIO $ do
      res <- run exampleIOAction
      stop (cover 40.0 (even res) "res is even" $ res > 0)