Skip to content

Commit

Permalink
exception tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Oct 12, 2022
1 parent e599c4e commit 8ca2796
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ nanobind_add_module(test_stl_ext test_stl.cpp)
nanobind_add_module(test_enum_ext test_enum.cpp)
nanobind_add_module(test_tensor_ext test_tensor.cpp)
nanobind_add_module(test_intrusive_ext test_intrusive.cpp object.cpp object.h)
nanobind_add_module(test_exception_ext test_exception.cpp object.cpp object.h)

set(TEST_FILES
test_functions.py
Expand All @@ -14,6 +15,7 @@ set(TEST_FILES
test_enum.py
test_tensor.py
test_intrusive.py
test_exception.py
)

if (NOT (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR) OR MSVC)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <nanobind/nanobind.h>

namespace nb = nanobind;

NB_MODULE(test_exception_ext, m) {
m.def("raise_generic", [] { throw std::exception(); });
m.def("raise_bad_alloc", [] { throw std::bad_alloc(); });
m.def("raise_runtime_error", [] { throw std::runtime_error("a runtime error"); });
m.def("raise_domain_error", [] { throw std::domain_error("a domain error"); });
m.def("raise_invalid_argument", [] { throw std::invalid_argument("an invalid argument error"); });
m.def("raise_length_error", [] { throw std::length_error("a length error"); });
m.def("raise_out_of_range", [] { throw std::out_of_range("an out of range error"); });
m.def("raise_range_error", [] { throw std::range_error("a range error"); });
m.def("raise_overflow_error", [] { throw std::overflow_error("an overflow error"); });
m.def("raise_index_error", [] { throw nb::index_error("an index error"); });
m.def("raise_key_error", [] { throw nb::key_error("a key error"); });
m.def("raise_value_error", [] { throw nb::value_error("a value error"); });
m.def("raise_type_error", [] { throw nb::type_error("a type error"); });
m.def("raise_import_error", [] { throw nb::import_error("an import error"); });
m.def("raise_attribute_error", [] { throw nb::attribute_error("an attribute error"); });
m.def("raise_stop_iteration", [] { throw nb::stop_iteration("a stop iteration error"); });
}
80 changes: 80 additions & 0 deletions tests/test_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import test_exception_ext as t
import pytest

def test01_base():
with pytest.raises(RuntimeError):
assert t.raise_generic()

def test02_bad_alloc():
with pytest.raises(MemoryError):
assert t.raise_bad_alloc()

def test03_runtime_error():
with pytest.raises(RuntimeError) as excinfo:
assert t.raise_runtime_error()
assert str(excinfo.value) == 'a runtime error'

def test04_domain_error():
with pytest.raises(ValueError) as excinfo:
assert t.raise_domain_error()
assert str(excinfo.value) == 'a domain error'

def test05_invalid_argument():
with pytest.raises(ValueError) as excinfo:
assert t.raise_invalid_argument()
assert str(excinfo.value) == 'an invalid argument error'

def test06_length():
with pytest.raises(ValueError) as excinfo:
assert t.raise_length_error()
assert str(excinfo.value) == 'a length error'

def test07_out_of_range():
with pytest.raises(IndexError) as excinfo:
assert t.raise_out_of_range()
assert str(excinfo.value) == 'an out of range error'

def test08_range_error():
with pytest.raises(ValueError) as excinfo:
assert t.raise_range_error()
assert str(excinfo.value) == 'a range error'

def test09_overflow_error():
with pytest.raises(OverflowError) as excinfo:
assert t.raise_overflow_error()
assert str(excinfo.value) == 'an overflow error'

def test10_index_error():
with pytest.raises(IndexError) as excinfo:
assert t.raise_index_error()
assert str(excinfo.value) == 'an index error'

def test11_key_error():
with pytest.raises(KeyError) as excinfo:
assert t.raise_key_error()
assert str(excinfo.value) == "'a key error'"

def test12_value_error():
with pytest.raises(ValueError) as excinfo:
assert t.raise_value_error()
assert str(excinfo.value) == 'a value error'

def test13_type_error():
with pytest.raises(TypeError) as excinfo:
assert t.raise_type_error()
assert str(excinfo.value) == 'a type error'

def test14_import_error():
with pytest.raises(ImportError) as excinfo:
assert t.raise_import_error()
assert str(excinfo.value) == 'an import error'

def test15_attribute_error():
with pytest.raises(AttributeError) as excinfo:
assert t.raise_attribute_error()
assert str(excinfo.value) == 'an attribute error'

def test16_stop_iteration():
with pytest.raises(StopIteration) as excinfo:
assert t.raise_stop_iteration()
assert str(excinfo.value) == 'a stop iteration error'

0 comments on commit 8ca2796

Please sign in to comment.