test_enum.cpp,py copied from remove_cross_extension_shared_state branch

This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-07-31 13:46:46 -07:00
parent edf03ccd34
commit 0286e5a2d6
2 changed files with 25 additions and 0 deletions

View File

@ -130,4 +130,20 @@ TEST_SUBMODULE(enums, m) {
py::enum_<ScopedBoolEnum>(m, "ScopedBoolEnum") py::enum_<ScopedBoolEnum>(m, "ScopedBoolEnum")
.value("FALSE", ScopedBoolEnum::FALSE) .value("FALSE", ScopedBoolEnum::FALSE)
.value("TRUE", ScopedBoolEnum::TRUE); .value("TRUE", ScopedBoolEnum::TRUE);
#if defined(__MINGW32__)
m.attr("obj_cast_UnscopedEnum_ptr") = "MinGW: dangling pointer to an unnamed temporary may be "
"used [-Werror=dangling-pointer=]";
#else
m.def("obj_cast_UnscopedEnum_ptr", [](const py::object &obj) {
// https://github.com/OpenImageIO/oiio/blob/30ea4ebdfab11aec291befbaff446f2a7d24835b/src/python/py_oiio.h#L300
if (py::isinstance<UnscopedEnum>(obj)) {
if (*obj.cast<UnscopedEnum *>() == UnscopedEnum::ETwo) {
return 2;
}
return 1;
}
return 0;
});
#endif
} }

View File

@ -268,3 +268,12 @@ def test_docstring_signatures():
def test_str_signature(): def test_str_signature():
for enum_type in [m.ScopedEnum, m.UnscopedEnum]: for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
assert enum_type.__str__.__doc__.startswith("__str__") assert enum_type.__str__.__doc__.startswith("__str__")
@pytest.mark.skipif(
isinstance(m.obj_cast_UnscopedEnum_ptr, str), reason=m.obj_cast_UnscopedEnum_ptr
)
def test_obj_cast_unscoped_enum_ptr():
assert m.obj_cast_UnscopedEnum_ptr(m.UnscopedEnum.ETwo) == 2
assert m.obj_cast_UnscopedEnum_ptr(m.UnscopedEnum.EOne) == 1
assert m.obj_cast_UnscopedEnum_ptr(None) == 0