Dean Moldovan | a9a37b4 | 2016-08-13 00:57:24 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | |
| 4 | def test_unscoped_enum(): |
| 5 | from pybind11_tests import UnscopedEnum, EOne |
| 6 | |
| 7 | assert str(UnscopedEnum.EOne) == "UnscopedEnum.EOne" |
| 8 | assert str(UnscopedEnum.ETwo) == "UnscopedEnum.ETwo" |
| 9 | assert str(EOne) == "UnscopedEnum.EOne" |
Matthieu Bec | af936e1 | 2017-03-03 08:45:50 -0800 | [diff] [blame] | 10 | # __members__ property |
| 11 | assert UnscopedEnum.__members__ == {"EOne": UnscopedEnum.EOne, "ETwo": UnscopedEnum.ETwo} |
| 12 | # __members__ readonly |
| 13 | with pytest.raises(AttributeError): |
| 14 | UnscopedEnum.__members__ = {} |
| 15 | # __members__ returns a copy |
| 16 | foo = UnscopedEnum.__members__ |
| 17 | foo["bar"] = "baz" |
| 18 | assert UnscopedEnum.__members__ == {"EOne": UnscopedEnum.EOne, "ETwo": UnscopedEnum.ETwo} |
Dean Moldovan | a9a37b4 | 2016-08-13 00:57:24 +0200 | [diff] [blame] | 19 | |
| 20 | # no TypeError exception for unscoped enum ==/!= int comparisons |
| 21 | y = UnscopedEnum.ETwo |
| 22 | assert y == 2 |
| 23 | assert y != 3 |
| 24 | |
| 25 | assert int(UnscopedEnum.ETwo) == 2 |
| 26 | assert str(UnscopedEnum(2)) == "UnscopedEnum.ETwo" |
| 27 | |
Pim Schellart | 90d2780 | 2016-11-16 11:28:11 -0500 | [diff] [blame] | 28 | # order |
| 29 | assert UnscopedEnum.EOne < UnscopedEnum.ETwo |
| 30 | assert UnscopedEnum.EOne < 2 |
| 31 | assert UnscopedEnum.ETwo > UnscopedEnum.EOne |
| 32 | assert UnscopedEnum.ETwo > 1 |
| 33 | assert UnscopedEnum.ETwo <= 2 |
| 34 | assert UnscopedEnum.ETwo >= 2 |
| 35 | assert UnscopedEnum.EOne <= UnscopedEnum.ETwo |
| 36 | assert UnscopedEnum.EOne <= 2 |
| 37 | assert UnscopedEnum.ETwo >= UnscopedEnum.EOne |
| 38 | assert UnscopedEnum.ETwo >= 1 |
| 39 | assert not (UnscopedEnum.ETwo < UnscopedEnum.EOne) |
| 40 | assert not (2 < UnscopedEnum.EOne) |
| 41 | |
Dean Moldovan | a9a37b4 | 2016-08-13 00:57:24 +0200 | [diff] [blame] | 42 | |
| 43 | def test_scoped_enum(): |
| 44 | from pybind11_tests import ScopedEnum, test_scoped_enum |
| 45 | |
| 46 | assert test_scoped_enum(ScopedEnum.Three) == "ScopedEnum::Three" |
| 47 | z = ScopedEnum.Two |
| 48 | assert test_scoped_enum(z) == "ScopedEnum::Two" |
| 49 | |
| 50 | # expected TypeError exceptions for scoped enum ==/!= int comparisons |
| 51 | with pytest.raises(TypeError): |
| 52 | assert z == 2 |
| 53 | with pytest.raises(TypeError): |
| 54 | assert z != 3 |
| 55 | |
Pim Schellart | 90d2780 | 2016-11-16 11:28:11 -0500 | [diff] [blame] | 56 | # order |
| 57 | assert ScopedEnum.Two < ScopedEnum.Three |
| 58 | assert ScopedEnum.Three > ScopedEnum.Two |
| 59 | assert ScopedEnum.Two <= ScopedEnum.Three |
| 60 | assert ScopedEnum.Two <= ScopedEnum.Two |
| 61 | assert ScopedEnum.Two >= ScopedEnum.Two |
| 62 | assert ScopedEnum.Three >= ScopedEnum.Two |
Dean Moldovan | a9a37b4 | 2016-08-13 00:57:24 +0200 | [diff] [blame] | 63 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 64 | |
Dean Moldovan | a9a37b4 | 2016-08-13 00:57:24 +0200 | [diff] [blame] | 65 | def test_implicit_conversion(): |
| 66 | from pybind11_tests import ClassWithUnscopedEnum |
| 67 | |
| 68 | assert str(ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode" |
| 69 | assert str(ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode" |
| 70 | |
| 71 | f = ClassWithUnscopedEnum.test_function |
| 72 | first = ClassWithUnscopedEnum.EFirstMode |
| 73 | second = ClassWithUnscopedEnum.ESecondMode |
| 74 | |
| 75 | assert f(first) == 1 |
| 76 | |
| 77 | assert f(first) == f(first) |
| 78 | assert not f(first) != f(first) |
| 79 | |
| 80 | assert f(first) != f(second) |
| 81 | assert not f(first) == f(second) |
| 82 | |
| 83 | assert f(first) == int(f(first)) |
| 84 | assert not f(first) != int(f(first)) |
| 85 | |
| 86 | assert f(first) != int(f(second)) |
| 87 | assert not f(first) == int(f(second)) |
| 88 | |
| 89 | # noinspection PyDictCreation |
| 90 | x = {f(first): 1, f(second): 2} |
| 91 | x[f(first)] = 3 |
| 92 | x[f(second)] = 4 |
| 93 | # Hashing test |
| 94 | assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}" |
Pim Schellart | 90d2780 | 2016-11-16 11:28:11 -0500 | [diff] [blame] | 95 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 96 | |
Pim Schellart | 90d2780 | 2016-11-16 11:28:11 -0500 | [diff] [blame] | 97 | def test_binary_operators(): |
| 98 | from pybind11_tests import Flags |
| 99 | |
| 100 | assert int(Flags.Read) == 4 |
| 101 | assert int(Flags.Write) == 2 |
| 102 | assert int(Flags.Execute) == 1 |
| 103 | assert int(Flags.Read | Flags.Write | Flags.Execute) == 7 |
| 104 | assert int(Flags.Read | Flags.Write) == 6 |
| 105 | assert int(Flags.Read | Flags.Execute) == 5 |
| 106 | assert int(Flags.Write | Flags.Execute) == 3 |
| 107 | assert int(Flags.Write | 1) == 3 |
| 108 | |
| 109 | state = Flags.Read | Flags.Write |
| 110 | assert (state & Flags.Read) != 0 |
| 111 | assert (state & Flags.Write) != 0 |
| 112 | assert (state & Flags.Execute) == 0 |
| 113 | assert (state & 1) == 0 |
| 114 | |
| 115 | state2 = ~state |
| 116 | assert state2 == -7 |
| 117 | assert int(state ^ state2) == -1 |
Wenzel Jakob | e6fd2cd | 2017-04-28 14:46:52 +0200 | [diff] [blame] | 118 | |
Wenzel Jakob | 7653a11 | 2017-04-28 22:43:14 +0200 | [diff] [blame] | 119 | |
Wenzel Jakob | e6fd2cd | 2017-04-28 14:46:52 +0200 | [diff] [blame] | 120 | def test_enum_to_int(): |
| 121 | from pybind11_tests import Flags, ClassWithUnscopedEnum |
| 122 | from pybind11_tests import test_enum_to_int, test_enum_to_uint, test_enum_to_long_long |
| 123 | |
| 124 | test_enum_to_int(Flags.Read) |
| 125 | test_enum_to_int(ClassWithUnscopedEnum.EMode.EFirstMode) |
| 126 | test_enum_to_uint(Flags.Read) |
| 127 | test_enum_to_uint(ClassWithUnscopedEnum.EMode.EFirstMode) |
| 128 | test_enum_to_long_long(Flags.Read) |
| 129 | test_enum_to_long_long(ClassWithUnscopedEnum.EMode.EFirstMode) |