3 from pybind11_tests
import enums
as m
7 assert str(m.UnscopedEnum.EOne) ==
"UnscopedEnum.EOne" 8 assert str(m.UnscopedEnum.ETwo) ==
"UnscopedEnum.ETwo" 9 assert str(m.EOne) ==
"UnscopedEnum.EOne" 10 assert repr(m.UnscopedEnum.EOne) ==
"<UnscopedEnum.EOne: 1>" 11 assert repr(m.UnscopedEnum.ETwo) ==
"<UnscopedEnum.ETwo: 2>" 12 assert repr(m.EOne) ==
"<UnscopedEnum.EOne: 1>" 15 assert m.UnscopedEnum.EOne.name ==
"EOne" 16 assert m.UnscopedEnum.EOne.value == 1
17 assert m.UnscopedEnum.ETwo.name ==
"ETwo" 18 assert m.UnscopedEnum.ETwo.value == 2
19 assert m.EOne
is m.UnscopedEnum.EOne
21 with pytest.raises(AttributeError):
22 m.UnscopedEnum.EOne.name =
"" 23 with pytest.raises(AttributeError):
24 m.UnscopedEnum.EOne.value = 10
28 nonaliased_name = m.UnscopedEnum.EOne.name
29 nonaliased_name =
"bar" 30 assert m.UnscopedEnum.EOne.name ==
"EOne" 31 nonaliased_value = m.UnscopedEnum.EOne.value
33 assert m.UnscopedEnum.EOne.value == 1
36 assert m.UnscopedEnum.__members__ == {
37 "EOne": m.UnscopedEnum.EOne,
38 "ETwo": m.UnscopedEnum.ETwo,
39 "EThree": m.UnscopedEnum.EThree,
42 with pytest.raises(AttributeError):
43 m.UnscopedEnum.__members__ = {}
45 nonaliased_members = m.UnscopedEnum.__members__
46 nonaliased_members[
"bar"] =
"baz" 47 assert m.UnscopedEnum.__members__ == {
48 "EOne": m.UnscopedEnum.EOne,
49 "ETwo": m.UnscopedEnum.ETwo,
50 "EThree": m.UnscopedEnum.EThree,
53 for docstring_line
in """An unscoped enumeration 57 EOne : Docstring for EOne 59 ETwo : Docstring for ETwo 61 EThree : Docstring for EThree""".
split(
64 assert docstring_line
in m.UnscopedEnum.__doc__
67 y = m.UnscopedEnum.ETwo
74 assert not (y ==
None)
84 with pytest.raises(TypeError):
87 with pytest.raises(TypeError):
90 with pytest.raises(TypeError):
93 with pytest.raises(TypeError):
96 with pytest.raises(TypeError):
99 with pytest.raises(TypeError):
102 with pytest.raises(TypeError):
105 assert int(m.UnscopedEnum.ETwo) == 2
106 assert str(m.UnscopedEnum(2)) ==
"UnscopedEnum.ETwo" 109 assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
110 assert m.UnscopedEnum.EOne < 2
111 assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
112 assert m.UnscopedEnum.ETwo > 1
113 assert m.UnscopedEnum.ETwo <= 2
114 assert m.UnscopedEnum.ETwo >= 2
115 assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
116 assert m.UnscopedEnum.EOne <= 2
117 assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
118 assert m.UnscopedEnum.ETwo >= 1
119 assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
120 assert not (2 < m.UnscopedEnum.EOne)
123 assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne
124 assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree
125 assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo
129 assert m.test_scoped_enum(m.ScopedEnum.Three) ==
"ScopedEnum::Three" 131 assert m.test_scoped_enum(z) ==
"ScopedEnum::Two" 140 assert not (z ==
None)
143 assert not (z ==
object())
145 with pytest.raises(TypeError):
147 with pytest.raises(TypeError):
149 with pytest.raises(TypeError):
151 with pytest.raises(TypeError):
155 assert m.ScopedEnum.Two < m.ScopedEnum.Three
156 assert m.ScopedEnum.Three > m.ScopedEnum.Two
157 assert m.ScopedEnum.Two <= m.ScopedEnum.Three
158 assert m.ScopedEnum.Two <= m.ScopedEnum.Two
159 assert m.ScopedEnum.Two >= m.ScopedEnum.Two
160 assert m.ScopedEnum.Three >= m.ScopedEnum.Two
164 assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) ==
"EMode.EFirstMode" 165 assert str(m.ClassWithUnscopedEnum.EFirstMode) ==
"EMode.EFirstMode" 166 assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) ==
"<EMode.EFirstMode: 1>" 167 assert repr(m.ClassWithUnscopedEnum.EFirstMode) ==
"<EMode.EFirstMode: 1>" 169 f = m.ClassWithUnscopedEnum.test_function
170 first = m.ClassWithUnscopedEnum.EFirstMode
171 second = m.ClassWithUnscopedEnum.ESecondMode
175 assert f(first) ==
f(first)
176 assert not f(first) !=
f(first)
178 assert f(first) !=
f(second)
179 assert not f(first) ==
f(second)
181 assert f(first) ==
int(
f(first))
182 assert not f(first) !=
int(
f(first))
184 assert f(first) !=
int(
f(second))
185 assert not f(first) ==
int(
f(second))
188 x = {
f(first): 1,
f(second): 2}
192 assert repr(x) ==
"{<EMode.EFirstMode: 1>: 3, <EMode.ESecondMode: 2>: 4}" 196 assert int(m.Flags.Read) == 4
197 assert int(m.Flags.Write) == 2
198 assert int(m.Flags.Execute) == 1
199 assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
200 assert int(m.Flags.Read | m.Flags.Write) == 6
201 assert int(m.Flags.Read | m.Flags.Execute) == 5
202 assert int(m.Flags.Write | m.Flags.Execute) == 3
203 assert int(m.Flags.Write | 1) == 3
204 assert ~m.Flags.Write == -3
206 state = m.Flags.Read | m.Flags.Write
207 assert (state & m.Flags.Read) != 0
208 assert (state & m.Flags.Write) != 0
209 assert (state & m.Flags.Execute) == 0
210 assert (state & 1) == 0
214 assert int(state ^ state2) == -1
218 m.test_enum_to_int(m.Flags.Read)
219 m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
220 m.test_enum_to_int(m.ScopedCharEnum.Positive)
221 m.test_enum_to_int(m.ScopedBoolEnum.TRUE)
222 m.test_enum_to_uint(m.Flags.Read)
223 m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
224 m.test_enum_to_uint(m.ScopedCharEnum.Positive)
225 m.test_enum_to_uint(m.ScopedBoolEnum.TRUE)
226 m.test_enum_to_long_long(m.Flags.Read)
227 m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
228 m.test_enum_to_long_long(m.ScopedCharEnum.Positive)
229 m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)
233 with pytest.raises(ValueError)
as excinfo:
234 m.register_bad_enum()
235 assert str(excinfo.value) ==
'SimpleEnum: element "ONE" already exists!' 239 assert type(m.ScopedCharEnum.Positive.__int__())
is int
240 assert int(m.ScopedChar16Enum.Zero) == 0
241 assert hash(m.ScopedChar32Enum.Positive) == 1
242 assert type(m.ScopedCharEnum.Positive.__getstate__())
is int
243 assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
244 with pytest.raises(TypeError):
246 m.ScopedCharEnum(
"0")
250 assert type(m.ScopedBoolEnum.TRUE.__int__())
is int
251 assert int(m.ScopedBoolEnum.FALSE) == 0
252 assert hash(m.ScopedBoolEnum.TRUE) == 1
253 assert type(m.ScopedBoolEnum.TRUE.__getstate__())
is int
254 assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
257 assert m.ScopedBoolEnum(
False) == m.ScopedBoolEnum.FALSE
261 for enum_type
in [m.ScopedEnum, m.UnscopedEnum]:
262 for attr
in enum_type.__dict__.values():
264 assert "arg0" not in (attr.__doc__
or "")
def test_char_underlying_enum()
def test_bool_underlying_enum()
def test_binary_operators()
void split(const G &g, const PredecessorMap< KEY > &tree, G &Ab1, G &Ab2)
def test_docstring_signatures()
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
def test_duplicate_enum_name()
def test_implicit_conversion()