mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-24 22:25:10 +00:00
enum: add missing Enum.value property (#2739)
* enum: Add Enum.value property * simplify * address review
This commit is contained in:
parent
b7dfe5cc84
commit
2110d2d8ba
@ -1731,6 +1731,7 @@ public:
|
|||||||
m_base.init(is_arithmetic, is_convertible);
|
m_base.init(is_arithmetic, is_convertible);
|
||||||
|
|
||||||
def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value"));
|
def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value"));
|
||||||
|
def_property_readonly("value", [](Type value) { return (Scalar) value; });
|
||||||
def("__int__", [](Type value) { return (Scalar) value; });
|
def("__int__", [](Type value) { return (Scalar) value; });
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_MAJOR_VERSION < 3
|
||||||
def("__long__", [](Type value) { return (Scalar) value; });
|
def("__long__", [](Type value) { return (Scalar) value; });
|
||||||
|
@ -13,15 +13,24 @@ def test_unscoped_enum():
|
|||||||
|
|
||||||
# name property
|
# name property
|
||||||
assert m.UnscopedEnum.EOne.name == "EOne"
|
assert m.UnscopedEnum.EOne.name == "EOne"
|
||||||
|
assert m.UnscopedEnum.EOne.value == 1
|
||||||
assert m.UnscopedEnum.ETwo.name == "ETwo"
|
assert m.UnscopedEnum.ETwo.name == "ETwo"
|
||||||
assert m.EOne.name == "EOne"
|
assert m.UnscopedEnum.ETwo.value == 2
|
||||||
# name readonly
|
assert m.EOne is m.UnscopedEnum.EOne
|
||||||
|
# name, value readonly
|
||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
m.UnscopedEnum.EOne.name = ""
|
m.UnscopedEnum.EOne.name = ""
|
||||||
# name returns a copy
|
with pytest.raises(AttributeError):
|
||||||
foo = m.UnscopedEnum.EOne.name
|
m.UnscopedEnum.EOne.value = 10
|
||||||
foo = "bar"
|
# name, value returns a copy
|
||||||
|
# TODO: Neither the name nor value tests actually check against aliasing.
|
||||||
|
# Use a mutable type that has reference semantics.
|
||||||
|
nonaliased_name = m.UnscopedEnum.EOne.name
|
||||||
|
nonaliased_name = "bar" # noqa: F841
|
||||||
assert m.UnscopedEnum.EOne.name == "EOne"
|
assert m.UnscopedEnum.EOne.name == "EOne"
|
||||||
|
nonaliased_value = m.UnscopedEnum.EOne.value
|
||||||
|
nonaliased_value = 10 # noqa: F841
|
||||||
|
assert m.UnscopedEnum.EOne.value == 1
|
||||||
|
|
||||||
# __members__ property
|
# __members__ property
|
||||||
assert m.UnscopedEnum.__members__ == {
|
assert m.UnscopedEnum.__members__ == {
|
||||||
@ -33,8 +42,8 @@ def test_unscoped_enum():
|
|||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
m.UnscopedEnum.__members__ = {}
|
m.UnscopedEnum.__members__ = {}
|
||||||
# __members__ returns a copy
|
# __members__ returns a copy
|
||||||
foo = m.UnscopedEnum.__members__
|
nonaliased_members = m.UnscopedEnum.__members__
|
||||||
foo["bar"] = "baz"
|
nonaliased_members["bar"] = "baz"
|
||||||
assert m.UnscopedEnum.__members__ == {
|
assert m.UnscopedEnum.__members__ == {
|
||||||
"EOne": m.UnscopedEnum.EOne,
|
"EOne": m.UnscopedEnum.EOne,
|
||||||
"ETwo": m.UnscopedEnum.ETwo,
|
"ETwo": m.UnscopedEnum.ETwo,
|
||||||
|
Loading…
Reference in New Issue
Block a user