Can I check if an exception is thrown by QuickCheck?

Suppose I have a function that should calculate some value in one case and an exception differently. I would like to use QuickCheck to ensure my function behaves correctly, however it is not obvious how to perform this kind of check. Is it possible, and if so, how to verify that an exception of a certain type is thrown and contains the correct information about its cause?

+4
source share
1 answer

Really ioPropertyis the key to this type of test. You will need to use it in combination with catchor try. Here I show the last:

prop_exceptional :: Int -> Property
prop_exceptional n = ioProperty $ do
  result <- try . evaluate $ myDangerousFunction n
  return $ r === result
  where r | n == 0    = Left  MyException
          | otherwise = Right 42

, myDangerousFunction MyException , 0 42 . evaluate, IO , .

0

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


All Articles