1import pytest 2from pybind11_tests import enums as m 3 4 5def test_unscoped_enum(): 6 assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne" 7 assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo" 8 assert str(m.EOne) == "UnscopedEnum.EOne" 9 10 # name property 11 assert m.UnscopedEnum.EOne.name == "EOne" 12 assert m.UnscopedEnum.ETwo.name == "ETwo" 13 assert m.EOne.name == "EOne" 14 # name readonly 15 with pytest.raises(AttributeError): 16 m.UnscopedEnum.EOne.name = "" 17 # name returns a copy 18 foo = m.UnscopedEnum.EOne.name 19 foo = "bar" 20 assert m.UnscopedEnum.EOne.name == "EOne" 21 22 # __members__ property 23 assert m.UnscopedEnum.__members__ == \ 24 {"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo, "EThree": m.UnscopedEnum.EThree} 25 # __members__ readonly 26 with pytest.raises(AttributeError): 27 m.UnscopedEnum.__members__ = {} 28 # __members__ returns a copy 29 foo = m.UnscopedEnum.__members__ 30 foo["bar"] = "baz" 31 assert m.UnscopedEnum.__members__ == \ 32 {"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo, "EThree": m.UnscopedEnum.EThree} 33 34 for docstring_line in '''An unscoped enumeration 35 36Members: 37 38 EOne : Docstring for EOne 39 40 ETwo : Docstring for ETwo 41 42 EThree : Docstring for EThree'''.split('\n'): 43 assert docstring_line in m.UnscopedEnum.__doc__ 44 45 # Unscoped enums will accept ==/!= int comparisons 46 y = m.UnscopedEnum.ETwo 47 assert y == 2 48 assert 2 == y 49 assert y != 3 50 assert 3 != y 51 # Compare with None 52 assert (y != None) # noqa: E711 53 assert not (y == None) # noqa: E711 54 # Compare with an object 55 assert (y != object()) 56 assert not (y == object()) 57 # Compare with string 58 assert y != "2" 59 assert "2" != y 60 assert not ("2" == y) 61 assert not (y == "2") 62 63 with pytest.raises(TypeError): 64 y < object() 65 66 with pytest.raises(TypeError): 67 y <= object() 68 69 with pytest.raises(TypeError): 70 y > object() 71 72 with pytest.raises(TypeError): 73 y >= object() 74 75 with pytest.raises(TypeError): 76 y | object() 77 78 with pytest.raises(TypeError): 79 y & object() 80 81 with pytest.raises(TypeError): 82 y ^ object() 83 84 assert int(m.UnscopedEnum.ETwo) == 2 85 assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo" 86 87 # order 88 assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo 89 assert m.UnscopedEnum.EOne < 2 90 assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne 91 assert m.UnscopedEnum.ETwo > 1 92 assert m.UnscopedEnum.ETwo <= 2 93 assert m.UnscopedEnum.ETwo >= 2 94 assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo 95 assert m.UnscopedEnum.EOne <= 2 96 assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne 97 assert m.UnscopedEnum.ETwo >= 1 98 assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne) 99 assert not (2 < m.UnscopedEnum.EOne) 100 101 # arithmetic 102 assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne 103 assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree 104 assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo 105 106 107def test_scoped_enum(): 108 assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three" 109 z = m.ScopedEnum.Two 110 assert m.test_scoped_enum(z) == "ScopedEnum::Two" 111 112 # Scoped enums will *NOT* accept ==/!= int comparisons (Will always return False) 113 assert not z == 3 114 assert not 3 == z 115 assert z != 3 116 assert 3 != z 117 # Compare with None 118 assert (z != None) # noqa: E711 119 assert not (z == None) # noqa: E711 120 # Compare with an object 121 assert (z != object()) 122 assert not (z == object()) 123 # Scoped enums will *NOT* accept >, <, >= and <= int comparisons (Will throw exceptions) 124 with pytest.raises(TypeError): 125 z > 3 126 with pytest.raises(TypeError): 127 z < 3 128 with pytest.raises(TypeError): 129 z >= 3 130 with pytest.raises(TypeError): 131 z <= 3 132 133 # order 134 assert m.ScopedEnum.Two < m.ScopedEnum.Three 135 assert m.ScopedEnum.Three > m.ScopedEnum.Two 136 assert m.ScopedEnum.Two <= m.ScopedEnum.Three 137 assert m.ScopedEnum.Two <= m.ScopedEnum.Two 138 assert m.ScopedEnum.Two >= m.ScopedEnum.Two 139 assert m.ScopedEnum.Three >= m.ScopedEnum.Two 140 141 142def test_implicit_conversion(): 143 assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode" 144 assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode" 145 146 f = m.ClassWithUnscopedEnum.test_function 147 first = m.ClassWithUnscopedEnum.EFirstMode 148 second = m.ClassWithUnscopedEnum.ESecondMode 149 150 assert f(first) == 1 151 152 assert f(first) == f(first) 153 assert not f(first) != f(first) 154 155 assert f(first) != f(second) 156 assert not f(first) == f(second) 157 158 assert f(first) == int(f(first)) 159 assert not f(first) != int(f(first)) 160 161 assert f(first) != int(f(second)) 162 assert not f(first) == int(f(second)) 163 164 # noinspection PyDictCreation 165 x = {f(first): 1, f(second): 2} 166 x[f(first)] = 3 167 x[f(second)] = 4 168 # Hashing test 169 assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}" 170 171 172def test_binary_operators(): 173 assert int(m.Flags.Read) == 4 174 assert int(m.Flags.Write) == 2 175 assert int(m.Flags.Execute) == 1 176 assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7 177 assert int(m.Flags.Read | m.Flags.Write) == 6 178 assert int(m.Flags.Read | m.Flags.Execute) == 5 179 assert int(m.Flags.Write | m.Flags.Execute) == 3 180 assert int(m.Flags.Write | 1) == 3 181 assert ~m.Flags.Write == -3 182 183 state = m.Flags.Read | m.Flags.Write 184 assert (state & m.Flags.Read) != 0 185 assert (state & m.Flags.Write) != 0 186 assert (state & m.Flags.Execute) == 0 187 assert (state & 1) == 0 188 189 state2 = ~state 190 assert state2 == -7 191 assert int(state ^ state2) == -1 192 193 194def test_enum_to_int(): 195 m.test_enum_to_int(m.Flags.Read) 196 m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode) 197 m.test_enum_to_uint(m.Flags.Read) 198 m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode) 199 m.test_enum_to_long_long(m.Flags.Read) 200 m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode) 201 202 203def test_duplicate_enum_name(): 204 with pytest.raises(ValueError) as excinfo: 205 m.register_bad_enum() 206 assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!' 207