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