blob: efabae7f7c15c6e0dc6bda5cf68a5f564ba10ac4 [file] [log] [blame]
Dean Moldovana9a37b42016-08-13 00:57:24 +02001import pytest
2
3
4def 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"
10
11 # no TypeError exception for unscoped enum ==/!= int comparisons
12 y = UnscopedEnum.ETwo
13 assert y == 2
14 assert y != 3
15
16 assert int(UnscopedEnum.ETwo) == 2
17 assert str(UnscopedEnum(2)) == "UnscopedEnum.ETwo"
18
19
20def test_scoped_enum():
21 from pybind11_tests import ScopedEnum, test_scoped_enum
22
23 assert test_scoped_enum(ScopedEnum.Three) == "ScopedEnum::Three"
24 z = ScopedEnum.Two
25 assert test_scoped_enum(z) == "ScopedEnum::Two"
26
27 # expected TypeError exceptions for scoped enum ==/!= int comparisons
28 with pytest.raises(TypeError):
29 assert z == 2
30 with pytest.raises(TypeError):
31 assert z != 3
32
33
34def test_implicit_conversion():
35 from pybind11_tests import ClassWithUnscopedEnum
36
37 assert str(ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
38 assert str(ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
39
40 f = ClassWithUnscopedEnum.test_function
41 first = ClassWithUnscopedEnum.EFirstMode
42 second = ClassWithUnscopedEnum.ESecondMode
43
44 assert f(first) == 1
45
46 assert f(first) == f(first)
47 assert not f(first) != f(first)
48
49 assert f(first) != f(second)
50 assert not f(first) == f(second)
51
52 assert f(first) == int(f(first))
53 assert not f(first) != int(f(first))
54
55 assert f(first) != int(f(second))
56 assert not f(first) == int(f(second))
57
58 # noinspection PyDictCreation
59 x = {f(first): 1, f(second): 2}
60 x[f(first)] = 3
61 x[f(second)] = 4
62 # Hashing test
63 assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}"