Skip to content

Commit 8368c76

Browse files
committed
Add tests on error being thrown registering duplicate classes
1 parent 1eaacb7 commit 8368c76

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

tests/test_class.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,28 @@ TEST_SUBMODULE(class_, m) {
444444
.def_static("get_name", []() { return "BaseWithNested::Nested"; });
445445
py::class_<DerivedWithNested::Nested>(derivedWithNested_class, "Nested")
446446
.def_static("get_name", []() { return "DerivedWithNested::Nested"; });
447+
448+
// test_register_duplicate_class
449+
struct Duplicate {};
450+
struct OtherDuplicate {};
451+
struct DuplicateNested {};
452+
struct OtherDuplicateNested {};
453+
m.def("register_duplicate_class_name", [](py::module_ m) {
454+
py::class_<Duplicate>(m, "Duplicate");
455+
py::class_<OtherDuplicate>(m, "Duplicate");
456+
});
457+
m.def("register_duplicate_class_type", [](py::module_ m) {
458+
py::class_<OtherDuplicate>(m, "OtherDuplicate");
459+
py::class_<OtherDuplicate>(m, "YetAnotherDuplicate");
460+
});
461+
m.def("register_duplicate_nested_class_name", [](py::object gt) {
462+
py::class_<DuplicateNested>(gt, "DuplicateNested");
463+
py::class_<OtherDuplicateNested>(gt, "DuplicateNested");
464+
});
465+
m.def("register_duplicate_nested_class_type", [](py::object gt) {
466+
py::class_<OtherDuplicateNested>(gt, "OtherDuplicateNested");
467+
py::class_<OtherDuplicateNested>(gt, "YetAnotherDuplicateNested");
468+
});
447469
}
448470

449471
template <int N> class BreaksBase { public:

tests/test_class.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,29 @@ def test_base_and_derived_nested_scope():
391391
assert m.BaseWithNested.Nested != m.DerivedWithNested.Nested
392392
assert m.BaseWithNested.Nested.get_name() == "BaseWithNested::Nested"
393393
assert m.DerivedWithNested.Nested.get_name() == "DerivedWithNested::Nested"
394+
395+
396+
def test_register_duplicate_class():
397+
import types
398+
module_scope = types.ModuleType("module_scope")
399+
with pytest.raises(RuntimeError) as exc_info:
400+
m.register_duplicate_class_name(module_scope)
401+
expected = ('generic_type: cannot initialize type "Duplicate": '
402+
'an object with that name is already defined')
403+
assert str(exc_info.value) == expected
404+
with pytest.raises(RuntimeError) as exc_info:
405+
m.register_duplicate_class_type(module_scope)
406+
expected = 'generic_type: type "YetAnotherDuplicate" is already registered!'
407+
assert str(exc_info.value) == expected
408+
409+
class ClassScope:
410+
pass
411+
with pytest.raises(RuntimeError) as exc_info:
412+
m.register_duplicate_nested_class_name(ClassScope)
413+
expected = ('generic_type: cannot initialize type "DuplicateNested": '
414+
'an object with that name is already defined')
415+
assert str(exc_info.value) == expected
416+
with pytest.raises(RuntimeError) as exc_info:
417+
m.register_duplicate_nested_class_type(ClassScope)
418+
expected = 'generic_type: type "YetAnotherDuplicateNested" is already registered!'
419+
assert str(exc_info.value) == expected

0 commit comments

Comments
 (0)