MCPcopy Create free account
hub / github.com/pybind/pybind11 / test_unscoped_enum

Function test_unscoped_enum

tests/test_enum.py:15–130  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13 env.GRAALPY and env.GRAALPY_VERSION < (24, 2), reason="Fixed in GraalPy 24.2"
14)
15def test_unscoped_enum():
16 assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
17 assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
18 assert str(m.EOne) == "UnscopedEnum.EOne"
19 assert repr(m.UnscopedEnum.EOne) == "<UnscopedEnum.EOne: 1>"
20 assert repr(m.UnscopedEnum.ETwo) == "<UnscopedEnum.ETwo: 2>"
21 assert repr(m.EOne) == "<UnscopedEnum.EOne: 1>"
22
23 # name property
24 assert m.UnscopedEnum.EOne.name == "EOne"
25 assert m.UnscopedEnum.EOne.value == 1
26 assert m.UnscopedEnum.ETwo.name == "ETwo"
27 assert m.UnscopedEnum.ETwo.value == 2
28 assert m.EOne is m.UnscopedEnum.EOne
29 # name, value readonly
30 with pytest.raises(AttributeError):
31 m.UnscopedEnum.EOne.name = ""
32 with pytest.raises(AttributeError):
33 m.UnscopedEnum.EOne.value = 10
34 # name, value returns a copy
35 # TODO: Neither the name nor value tests actually check against aliasing.
36 # Use a mutable type that has reference semantics.
37 nonaliased_name = m.UnscopedEnum.EOne.name
38 nonaliased_name = "bar" # noqa: F841
39 assert m.UnscopedEnum.EOne.name == "EOne"
40 nonaliased_value = m.UnscopedEnum.EOne.value
41 nonaliased_value = 10 # noqa: F841
42 assert m.UnscopedEnum.EOne.value == 1
43
44 # __members__ property
45 assert m.UnscopedEnum.__members__ == {
46 "EOne": m.UnscopedEnum.EOne,
47 "ETwo": m.UnscopedEnum.ETwo,
48 "EThree": m.UnscopedEnum.EThree,
49 }
50 # __members__ readonly
51 with pytest.raises(AttributeError):
52 m.UnscopedEnum.__members__ = {}
53 # __members__ returns a copy
54 nonaliased_members = m.UnscopedEnum.__members__
55 nonaliased_members["bar"] = "baz"
56 assert m.UnscopedEnum.__members__ == {
57 "EOne": m.UnscopedEnum.EOne,
58 "ETwo": m.UnscopedEnum.ETwo,
59 "EThree": m.UnscopedEnum.EThree,
60 }
61
62 for docstring_line in [
63 "An unscoped enumeration",
64 "Members:",
65 " EOne : Docstring for EOne",
66 " ETwo : Docstring for ETwo",
67 " EThree : Docstring for EThree",
68 ]:
69 assert docstring_line in m.UnscopedEnum.__doc__
70
71 # Unscoped enums will accept ==/!= int comparisons
72 y = m.UnscopedEnum.ETwo

Callers

nothing calls this directly

Calls 3

strClass · 0.85
reprFunction · 0.85
objectFunction · 0.50

Tested by

no test coverage detected