| Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | |
| Ivan Smirnov | 67b5489 | 2016-09-07 21:10:16 +0100 | [diff] [blame] | 4 | def test_error_already_set(msg): |
| 5 | from pybind11_tests import throw_already_set |
| 6 | |
| 7 | with pytest.raises(RuntimeError) as excinfo: |
| 8 | throw_already_set(False) |
| 9 | assert msg(excinfo.value) == "Unknown internal error occurred" |
| 10 | |
| 11 | with pytest.raises(ValueError) as excinfo: |
| 12 | throw_already_set(True) |
| 13 | assert msg(excinfo.value) == "foo" |
| 14 | |
| 15 | |
| Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 16 | def test_custom(msg): |
| 17 | from pybind11_tests import (MyException, throws1, throws2, throws3, throws4, |
| 18 | throws_logic_error) |
| 19 | |
| 20 | # Can we catch a MyException?" |
| 21 | with pytest.raises(MyException) as excinfo: |
| 22 | throws1() |
| 23 | assert msg(excinfo.value) == "this error should go to a custom type" |
| 24 | |
| 25 | # Can we translate to standard Python exceptions? |
| 26 | with pytest.raises(RuntimeError) as excinfo: |
| 27 | throws2() |
| 28 | assert msg(excinfo.value) == "this error should go to a standard Python exception" |
| 29 | |
| 30 | # Can we handle unknown exceptions? |
| 31 | with pytest.raises(RuntimeError) as excinfo: |
| 32 | throws3() |
| 33 | assert msg(excinfo.value) == "Caught an unknown exception!" |
| 34 | |
| 35 | # Can we delegate to another handler by rethrowing? |
| 36 | with pytest.raises(MyException) as excinfo: |
| 37 | throws4() |
| 38 | assert msg(excinfo.value) == "this error is rethrown" |
| 39 | |
| 40 | # "Can we fall-through to the default handler?" |
| 41 | with pytest.raises(RuntimeError) as excinfo: |
| 42 | throws_logic_error() |
| 43 | assert msg(excinfo.value) == "this error should fall through to the standard handler" |