Skip to content

Commit 722b527

Browse files
committed
tests: add check for type casted type
1 parent 65bda40 commit 722b527

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

tests/test_class.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ TEST_SUBMODULE(class_, m) {
137137
struct Invalid {};
138138

139139
// test_type
140-
m.def("check_type", [](bool valid) {
141-
if (valid)
140+
m.def("check_type", [](int category) {
141+
if (category == 2)
142+
return py::type<int>();
143+
else if (category == 1)
142144
return py::type<DerivedClass1>();
143145
else
144146
return py::type<Invalid>();

tests/test_class.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ def test_instance(msg):
2525

2626

2727
def test_type():
28-
assert m.check_type(True) == m.DerivedClass1
29-
with pytest.raises(RuntimeError):
30-
m.check_type(False)
28+
assert m.check_type(1) == m.DerivedClass1
29+
with pytest.raises(RuntimeError) as execinfo:
30+
m.check_type(0)
31+
32+
assert 'pybind11::detail::get_type_info: unable to find type info' in str(execinfo.value)
33+
assert 'Invalid' in str(execinfo.value)
34+
35+
with pytest.raises(RuntimeError) as execinfo:
36+
assert m.check_type(2) == int
37+
38+
assert 'pybind11::detail::get_type_info: unable to find type info' in str(execinfo.value)
39+
assert 'int' in str(execinfo.value)
3140

3241

3342
def test_docstrings(doc):

0 commit comments

Comments
 (0)