Is it possible to check cases when exception is thrown with QuickCheck?

Mark Karpov

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

Mark Karpov

Indeed ioProperty is the key to this sort of test. You will need to use it in combination with catch or try. Here I show the latter:

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

Quite obviously, myDangerousFunction should throw MyException whenever it gets 0 and return 42 otherwise. Note the useful function evaluate, which you need to use to evaluate pure function in IO context to catch exceptions produced there.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is it possible to automatically break into the debugger when a exception is thrown?

From Dev

When is "throws Exception" thrown?

From Dev

PHP check thrown exception type

From Dev

What happens when an exception is thrown?

From Dev

Exception thrown when calling getFriends

From Dev

View Exception when stopped on Exception Thrown

From Dev

How to check which exception type was thrown in Java?

From Dev

How to check that an exception is not thrown using mockito?

From Dev

How to check if a property of a class has thrown an exception

From Dev

How to check that an exception is not thrown using mockito?

From Dev

Check if exception is thrown in try..catch..finally

From Dev

Exception has been thrown by the target of an invocation thrown when scaffolding a controller

From Dev

Ways to avoid memory leak when exception thrown

From Dev

SenTestCase isn't failing when an exception is thrown

From Dev

Exception thrown when using Glimpse and Postal

From Dev

Destructors not executed (no stack unwinding) when exception is thrown

From Dev

Why is exception not thrown when the connection is terminated?

From Dev

is a java scanner closed when an exception is thrown?

From Dev

Stop task when there is an exception thrown in ItemProcessor

From Dev

Android CoroutineWorker retry when exception is thrown

From Dev

Exception thrown when using Glimpse and Postal

From Dev

Why is exception not thrown when the connection is terminated?

From Dev

No exception thrown when connecting to an offline SQL Server

From Dev

Inconsistent behaviour when an exception is thrown in a for comprehension

From Dev

Error handling when Exception / Error is thrown

From Dev

Class becomes null when exception is thrown

From Dev

Exception thrown when Decimal type is used in model

From Dev

Task not in a faulted state when an exception is thrown

From Dev

RxJava: application dies when exception is thrown

Related Related

  1. 1

    Is it possible to automatically break into the debugger when a exception is thrown?

  2. 2

    When is "throws Exception" thrown?

  3. 3

    PHP check thrown exception type

  4. 4

    What happens when an exception is thrown?

  5. 5

    Exception thrown when calling getFriends

  6. 6

    View Exception when stopped on Exception Thrown

  7. 7

    How to check which exception type was thrown in Java?

  8. 8

    How to check that an exception is not thrown using mockito?

  9. 9

    How to check if a property of a class has thrown an exception

  10. 10

    How to check that an exception is not thrown using mockito?

  11. 11

    Check if exception is thrown in try..catch..finally

  12. 12

    Exception has been thrown by the target of an invocation thrown when scaffolding a controller

  13. 13

    Ways to avoid memory leak when exception thrown

  14. 14

    SenTestCase isn't failing when an exception is thrown

  15. 15

    Exception thrown when using Glimpse and Postal

  16. 16

    Destructors not executed (no stack unwinding) when exception is thrown

  17. 17

    Why is exception not thrown when the connection is terminated?

  18. 18

    is a java scanner closed when an exception is thrown?

  19. 19

    Stop task when there is an exception thrown in ItemProcessor

  20. 20

    Android CoroutineWorker retry when exception is thrown

  21. 21

    Exception thrown when using Glimpse and Postal

  22. 22

    Why is exception not thrown when the connection is terminated?

  23. 23

    No exception thrown when connecting to an offline SQL Server

  24. 24

    Inconsistent behaviour when an exception is thrown in a for comprehension

  25. 25

    Error handling when Exception / Error is thrown

  26. 26

    Class becomes null when exception is thrown

  27. 27

    Exception thrown when Decimal type is used in model

  28. 28

    Task not in a faulted state when an exception is thrown

  29. 29

    RxJava: application dies when exception is thrown

HotTag

Archive