Skip to content

Commit 3f34cca

Browse files
committed
Add tests for the extended enum_ API
1 parent 5fa4eee commit 3f34cca

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

tests/test_enum.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,15 @@ test_initializer enums([](py::module &m) {
4747
py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic())
4848
.value("EOne", EOne)
4949
.value("ETwo", ETwo)
50-
.export_values();
50+
.export_values()
51+
.into_class()
52+
.def("x", [](const UnscopedEnum& e) { return static_cast<int>(e) + 1; })
53+
.def_property_readonly("y", [](const UnscopedEnum& e) { return static_cast<int>(e) + 2; })
54+
.def_static("a", []() { return 41; })
55+
.def_property_readonly_static("b", [](py::object /* unused */) { return 42; });
5156

52-
py::enum_<ScopedEnum>(m, "ScopedEnum", py::arithmetic())
57+
auto scoped_enum = py::enum_<ScopedEnum>(m, "ScopedEnum", py::arithmetic());
58+
scoped_enum
5359
.value("Two", ScopedEnum::Two)
5460
.value("Three", ScopedEnum::Three);
5561

tests/test_enum.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ def test_unscoped_enum():
4040
assert not (2 < UnscopedEnum.EOne)
4141

4242

43+
def test_extra_defs():
44+
from pybind11_tests import UnscopedEnum
45+
assert UnscopedEnum.EOne.x() == 2 and UnscopedEnum.ETwo.x() == 3
46+
assert UnscopedEnum.EOne.y == 3 and UnscopedEnum.ETwo.y == 4
47+
assert UnscopedEnum.a() == 41
48+
assert UnscopedEnum.b == 42
49+
50+
4351
def test_scoped_enum():
4452
from pybind11_tests import ScopedEnum, test_scoped_enum
4553

0 commit comments

Comments
 (0)