test_enum.py 8.92 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
import pytest
3

4
import env
5
from pybind11_tests import enums as m
6
7
8


def test_unscoped_enum():
9
10
11
    assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
    assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
    assert str(m.EOne) == "UnscopedEnum.EOne"
David Vo's avatar
David Vo committed
12
13
14
    assert repr(m.UnscopedEnum.EOne) == "<UnscopedEnum.EOne: 1>"
    assert repr(m.UnscopedEnum.ETwo) == "<UnscopedEnum.ETwo: 2>"
    assert repr(m.EOne) == "<UnscopedEnum.EOne: 1>"
15
16
17

    # name property
    assert m.UnscopedEnum.EOne.name == "EOne"
18
    assert m.UnscopedEnum.EOne.value == 1
19
    assert m.UnscopedEnum.ETwo.name == "ETwo"
20
21
22
    assert m.UnscopedEnum.ETwo.value == 2
    assert m.EOne is m.UnscopedEnum.EOne
    # name, value readonly
23
24
    with pytest.raises(AttributeError):
        m.UnscopedEnum.EOne.name = ""
25
26
27
28
29
30
31
    with pytest.raises(AttributeError):
        m.UnscopedEnum.EOne.value = 10
    # 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
32
    assert m.UnscopedEnum.EOne.name == "EOne"
33
34
35
    nonaliased_value = m.UnscopedEnum.EOne.value
    nonaliased_value = 10  # noqa: F841
    assert m.UnscopedEnum.EOne.value == 1
36

37
    # __members__ property
38
39
40
41
42
    assert m.UnscopedEnum.__members__ == {
        "EOne": m.UnscopedEnum.EOne,
        "ETwo": m.UnscopedEnum.ETwo,
        "EThree": m.UnscopedEnum.EThree,
    }
43
44
    # __members__ readonly
    with pytest.raises(AttributeError):
45
        m.UnscopedEnum.__members__ = {}
46
    # __members__ returns a copy
47
48
    nonaliased_members = m.UnscopedEnum.__members__
    nonaliased_members["bar"] = "baz"
49
50
51
52
53
    assert m.UnscopedEnum.__members__ == {
        "EOne": m.UnscopedEnum.EOne,
        "ETwo": m.UnscopedEnum.ETwo,
        "EThree": m.UnscopedEnum.EThree,
    }
54

55
    for docstring_line in """An unscoped enumeration
56
57
58
59
60
61
62

Members:

  EOne : Docstring for EOne

  ETwo : Docstring for ETwo

63
64
65
  EThree : Docstring for EThree""".split(
        "\n"
    ):
66
        assert docstring_line in m.UnscopedEnum.__doc__
67

68
    # Unscoped enums will accept ==/!= int comparisons
69
    y = m.UnscopedEnum.ETwo
70
    assert y == 2
71
    assert 2 == y
72
    assert y != 3
73
    assert 3 != y
74
    # Compare with None
75
    assert y != None  # noqa: E711
76
77
    assert not (y == None)  # noqa: E711
    # Compare with an object
78
    assert y != object()
79
80
81
82
83
84
85
86
    assert not (y == object())
    # Compare with string
    assert y != "2"
    assert "2" != y
    assert not ("2" == y)
    assert not (y == "2")

    with pytest.raises(TypeError):
87
        y < object()  # noqa: B015
88
89

    with pytest.raises(TypeError):
90
        y <= object()  # noqa: B015
91
92

    with pytest.raises(TypeError):
93
        y > object()  # noqa: B015
94
95

    with pytest.raises(TypeError):
96
        y >= object()  # noqa: B015
97
98

    with pytest.raises(TypeError):
99
        y | object()
100
101

    with pytest.raises(TypeError):
102
        y & object()
103
104

    with pytest.raises(TypeError):
105
        y ^ object()
106

107
108
    assert int(m.UnscopedEnum.ETwo) == 2
    assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo"
109

Pim Schellart's avatar
Pim Schellart committed
110
    # order
111
112
113
114
115
116
117
118
119
120
121
122
    assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
    assert m.UnscopedEnum.EOne < 2
    assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
    assert m.UnscopedEnum.ETwo > 1
    assert m.UnscopedEnum.ETwo <= 2
    assert m.UnscopedEnum.ETwo >= 2
    assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
    assert m.UnscopedEnum.EOne <= 2
    assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
    assert m.UnscopedEnum.ETwo >= 1
    assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
    assert not (2 < m.UnscopedEnum.EOne)
Pim Schellart's avatar
Pim Schellart committed
123

124
125
126
127
128
    # arithmetic
    assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne
    assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree
    assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo

129
130

def test_scoped_enum():
131
132
133
    assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three"
    z = m.ScopedEnum.Two
    assert m.test_scoped_enum(z) == "ScopedEnum::Two"
134

135
136
137
138
139
    # Scoped enums will *NOT* accept ==/!= int comparisons (Will always return False)
    assert not z == 3
    assert not 3 == z
    assert z != 3
    assert 3 != z
140
    # Compare with None
141
    assert z != None  # noqa: E711
142
143
    assert not (z == None)  # noqa: E711
    # Compare with an object
144
    assert z != object()
145
    assert not (z == object())
146
    # Scoped enums will *NOT* accept >, <, >= and <= int comparisons (Will throw exceptions)
147
    with pytest.raises(TypeError):
148
        z > 3  # noqa: B015
149
    with pytest.raises(TypeError):
150
        z < 3  # noqa: B015
151
    with pytest.raises(TypeError):
152
        z >= 3  # noqa: B015
153
    with pytest.raises(TypeError):
154
        z <= 3  # noqa: B015
155

Pim Schellart's avatar
Pim Schellart committed
156
    # order
157
158
159
160
161
162
    assert m.ScopedEnum.Two < m.ScopedEnum.Three
    assert m.ScopedEnum.Three > m.ScopedEnum.Two
    assert m.ScopedEnum.Two <= m.ScopedEnum.Three
    assert m.ScopedEnum.Two <= m.ScopedEnum.Two
    assert m.ScopedEnum.Two >= m.ScopedEnum.Two
    assert m.ScopedEnum.Three >= m.ScopedEnum.Two
163

164

165
def test_implicit_conversion():
166
167
    assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
    assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
David Vo's avatar
David Vo committed
168
169
    assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "<EMode.EFirstMode: 1>"
    assert repr(m.ClassWithUnscopedEnum.EFirstMode) == "<EMode.EFirstMode: 1>"
170

171
172
173
    f = m.ClassWithUnscopedEnum.test_function
    first = m.ClassWithUnscopedEnum.EFirstMode
    second = m.ClassWithUnscopedEnum.ESecondMode
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

    assert f(first) == 1

    assert f(first) == f(first)
    assert not f(first) != f(first)

    assert f(first) != f(second)
    assert not f(first) == f(second)

    assert f(first) == int(f(first))
    assert not f(first) != int(f(first))

    assert f(first) != int(f(second))
    assert not f(first) == int(f(second))

    # noinspection PyDictCreation
    x = {f(first): 1, f(second): 2}
    x[f(first)] = 3
    x[f(second)] = 4
    # Hashing test
David Vo's avatar
David Vo committed
194
    assert repr(x) == "{<EMode.EFirstMode: 1>: 3, <EMode.ESecondMode: 2>: 4}"
Pim Schellart's avatar
Pim Schellart committed
195

196

Pim Schellart's avatar
Pim Schellart committed
197
def test_binary_operators():
198
199
200
201
202
203
204
205
    assert int(m.Flags.Read) == 4
    assert int(m.Flags.Write) == 2
    assert int(m.Flags.Execute) == 1
    assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
    assert int(m.Flags.Read | m.Flags.Write) == 6
    assert int(m.Flags.Read | m.Flags.Execute) == 5
    assert int(m.Flags.Write | m.Flags.Execute) == 3
    assert int(m.Flags.Write | 1) == 3
206
    assert ~m.Flags.Write == -3
207
208
209
210
211

    state = m.Flags.Read | m.Flags.Write
    assert (state & m.Flags.Read) != 0
    assert (state & m.Flags.Write) != 0
    assert (state & m.Flags.Execute) == 0
Pim Schellart's avatar
Pim Schellart committed
212
213
214
215
216
    assert (state & 1) == 0

    state2 = ~state
    assert state2 == -7
    assert int(state ^ state2) == -1
217

218

219
def test_enum_to_int():
220
221
    m.test_enum_to_int(m.Flags.Read)
    m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
222
223
    m.test_enum_to_int(m.ScopedCharEnum.Positive)
    m.test_enum_to_int(m.ScopedBoolEnum.TRUE)
224
225
    m.test_enum_to_uint(m.Flags.Read)
    m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
226
227
    m.test_enum_to_uint(m.ScopedCharEnum.Positive)
    m.test_enum_to_uint(m.ScopedBoolEnum.TRUE)
228
229
    m.test_enum_to_long_long(m.Flags.Read)
    m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
230
231
    m.test_enum_to_long_long(m.ScopedCharEnum.Positive)
    m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)
232
233
234
235
236


def test_duplicate_enum_name():
    with pytest.raises(ValueError) as excinfo:
        m.register_bad_enum()
237
    assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!'
238
239


240
241
def test_char_underlying_enum():  # Issue #1331/PR #1334:
    assert type(m.ScopedCharEnum.Positive.__int__()) is int
242
    assert int(m.ScopedChar16Enum.Zero) == 0
243
    assert hash(m.ScopedChar32Enum.Positive) == 1
244
245
246
247
    if env.PY2:
        assert m.ScopedCharEnum.Positive.__getstate__() == 1  # long
    else:
        assert type(m.ScopedCharEnum.Positive.__getstate__()) is int
248
249
    assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
    with pytest.raises(TypeError):
250
251
        # Even if the underlying type is char, only an int can be used to construct the enum:
        m.ScopedCharEnum("0")
252
253
254
255
256
257


def test_bool_underlying_enum():
    assert type(m.ScopedBoolEnum.TRUE.__int__()) is int
    assert int(m.ScopedBoolEnum.FALSE) == 0
    assert hash(m.ScopedBoolEnum.TRUE) == 1
258
259
260
261
    if env.PY2:
        assert m.ScopedBoolEnum.TRUE.__getstate__() == 1  # long
    else:
        assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int
262
263
264
265
266
267
    assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
    # Enum could construct with a bool
    # (bool is a strict subclass of int, and False will be converted to 0)
    assert m.ScopedBoolEnum(False) == m.ScopedBoolEnum.FALSE


268
269
270
271
272
def test_docstring_signatures():
    for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
        for attr in enum_type.__dict__.values():
            # Issue #2623/PR #2637: Add argument names to enum_ methods
            assert "arg0" not in (attr.__doc__ or "")