2 from __future__
import annotations
6 from pybind11_tests
import enums
as m
10 assert str(m.UnscopedEnum.EOne) ==
"UnscopedEnum.EOne"
11 assert str(m.UnscopedEnum.ETwo) ==
"UnscopedEnum.ETwo"
12 assert str(m.EOne) ==
"UnscopedEnum.EOne"
13 assert repr(m.UnscopedEnum.EOne) ==
"<UnscopedEnum.EOne: 1>"
14 assert repr(m.UnscopedEnum.ETwo) ==
"<UnscopedEnum.ETwo: 2>"
15 assert repr(m.EOne) ==
"<UnscopedEnum.EOne: 1>"
18 assert m.UnscopedEnum.EOne.name ==
"EOne"
19 assert m.UnscopedEnum.EOne.value == 1
20 assert m.UnscopedEnum.ETwo.name ==
"ETwo"
21 assert m.UnscopedEnum.ETwo.value == 2
22 assert m.EOne
is m.UnscopedEnum.EOne
24 with pytest.raises(AttributeError):
25 m.UnscopedEnum.EOne.name =
""
26 with pytest.raises(AttributeError):
27 m.UnscopedEnum.EOne.value = 10
31 nonaliased_name = m.UnscopedEnum.EOne.name
32 nonaliased_name =
"bar"
33 assert m.UnscopedEnum.EOne.name ==
"EOne"
34 nonaliased_value = m.UnscopedEnum.EOne.value
36 assert m.UnscopedEnum.EOne.value == 1
39 assert m.UnscopedEnum.__members__ == {
40 "EOne": m.UnscopedEnum.EOne,
41 "ETwo": m.UnscopedEnum.ETwo,
42 "EThree": m.UnscopedEnum.EThree,
45 with pytest.raises(AttributeError):
46 m.UnscopedEnum.__members__ = {}
48 nonaliased_members = m.UnscopedEnum.__members__
49 nonaliased_members[
"bar"] =
"baz"
50 assert m.UnscopedEnum.__members__ == {
51 "EOne": m.UnscopedEnum.EOne,
52 "ETwo": m.UnscopedEnum.ETwo,
53 "EThree": m.UnscopedEnum.EThree,
56 for docstring_line
in """An unscoped enumeration
60 EOne : Docstring for EOne
62 ETwo : Docstring for ETwo
64 EThree : Docstring for EThree""".
split(
"\n"):
65 assert docstring_line
in m.UnscopedEnum.__doc__
68 y = m.UnscopedEnum.ETwo
75 assert not (y ==
None)
85 with pytest.raises(TypeError):
88 with pytest.raises(TypeError):
91 with pytest.raises(TypeError):
94 with pytest.raises(TypeError):
97 with pytest.raises(TypeError):
100 with pytest.raises(TypeError):
103 with pytest.raises(TypeError):
106 assert int(m.UnscopedEnum.ETwo) == 2
107 assert str(m.UnscopedEnum(2)) ==
"UnscopedEnum.ETwo"
110 assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
111 assert m.UnscopedEnum.EOne < 2
112 assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
113 assert m.UnscopedEnum.ETwo > 1
114 assert m.UnscopedEnum.ETwo <= 2
115 assert m.UnscopedEnum.ETwo >= 2
116 assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
117 assert m.UnscopedEnum.EOne <= 2
118 assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
119 assert m.UnscopedEnum.ETwo >= 1
120 assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
121 assert not (2 < m.UnscopedEnum.EOne)
124 assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne
125 assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree
126 assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo
130 assert m.test_scoped_enum(m.ScopedEnum.Three) ==
"ScopedEnum::Three"
132 assert m.test_scoped_enum(z) ==
"ScopedEnum::Two"
141 assert not (z ==
None)
144 assert not (z ==
object())
146 with pytest.raises(TypeError):
148 with pytest.raises(TypeError):
150 with pytest.raises(TypeError):
152 with pytest.raises(TypeError):
156 assert m.ScopedEnum.Two < m.ScopedEnum.Three
157 assert m.ScopedEnum.Three > m.ScopedEnum.Two
158 assert m.ScopedEnum.Two <= m.ScopedEnum.Three
159 assert m.ScopedEnum.Two <= m.ScopedEnum.Two
160 assert m.ScopedEnum.Two >= m.ScopedEnum.Two
161 assert m.ScopedEnum.Three >= m.ScopedEnum.Two
165 assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) ==
"EMode.EFirstMode"
166 assert str(m.ClassWithUnscopedEnum.EFirstMode) ==
"EMode.EFirstMode"
167 assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) ==
"<EMode.EFirstMode: 1>"
168 assert repr(m.ClassWithUnscopedEnum.EFirstMode) ==
"<EMode.EFirstMode: 1>"
170 f = m.ClassWithUnscopedEnum.test_function
171 first = m.ClassWithUnscopedEnum.EFirstMode
172 second = m.ClassWithUnscopedEnum.ESecondMode
176 assert f(first) ==
f(first)
177 assert not f(first) !=
f(first)
179 assert f(first) !=
f(second)
180 assert not f(first) ==
f(second)
182 assert f(first) ==
int(
f(first))
183 assert not f(first) !=
int(
f(first))
185 assert f(first) !=
int(
f(second))
186 assert not f(first) ==
int(
f(second))
189 x = {
f(first): 1,
f(second): 2}
193 assert repr(x) ==
"{<EMode.EFirstMode: 1>: 3, <EMode.ESecondMode: 2>: 4}"
197 assert int(m.Flags.Read) == 4
198 assert int(m.Flags.Write) == 2
199 assert int(m.Flags.Execute) == 1
200 assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
201 assert int(m.Flags.Read | m.Flags.Write) == 6
202 assert int(m.Flags.Read | m.Flags.Execute) == 5
203 assert int(m.Flags.Write | m.Flags.Execute) == 3
204 assert int(m.Flags.Write | 1) == 3
205 assert ~m.Flags.Write == -3
207 state = m.Flags.Read | m.Flags.Write
208 assert (state & m.Flags.Read) != 0
209 assert (state & m.Flags.Write) != 0
210 assert (state & m.Flags.Execute) == 0
211 assert (state & 1) == 0
215 assert int(state ^ state2) == -1
219 m.test_enum_to_int(m.Flags.Read)
220 m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
221 m.test_enum_to_int(m.ScopedCharEnum.Positive)
222 m.test_enum_to_int(m.ScopedBoolEnum.TRUE)
223 m.test_enum_to_uint(m.Flags.Read)
224 m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
225 m.test_enum_to_uint(m.ScopedCharEnum.Positive)
226 m.test_enum_to_uint(m.ScopedBoolEnum.TRUE)
227 m.test_enum_to_long_long(m.Flags.Read)
228 m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
229 m.test_enum_to_long_long(m.ScopedCharEnum.Positive)
230 m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)
234 with pytest.raises(ValueError)
as excinfo:
235 m.register_bad_enum()
236 assert str(excinfo.value) ==
'SimpleEnum: element "ONE" already exists!'
240 assert type(m.ScopedCharEnum.Positive.__int__())
is int
241 assert int(m.ScopedChar16Enum.Zero) == 0
242 assert hash(m.ScopedChar32Enum.Positive) == 1
243 assert type(m.ScopedCharEnum.Positive.__getstate__())
is int
244 assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
245 with pytest.raises(TypeError):
247 m.ScopedCharEnum(
"0")
251 assert type(m.ScopedBoolEnum.TRUE.__int__())
is int
252 assert int(m.ScopedBoolEnum.FALSE) == 0
253 assert hash(m.ScopedBoolEnum.TRUE) == 1
254 assert type(m.ScopedBoolEnum.TRUE.__getstate__())
is int
255 assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
258 assert m.ScopedBoolEnum(
False) == m.ScopedBoolEnum.FALSE
262 for enum_type
in [m.ScopedEnum, m.UnscopedEnum]:
263 for attr
in enum_type.__dict__.values():
265 assert "arg0" not in (attr.__doc__
or "")
269 for enum_type
in [m.ScopedEnum, m.UnscopedEnum]:
270 assert enum_type.__str__.__doc__.startswith(
"__str__")