test_enum.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import pytest
  2. from pybind11_tests import enums as m
  3. def test_unscoped_enum():
  4. assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
  5. assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
  6. assert str(m.EOne) == "UnscopedEnum.EOne"
  7. # __members__ property
  8. assert m.UnscopedEnum.__members__ == \
  9. {"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo}
  10. # __members__ readonly
  11. with pytest.raises(AttributeError):
  12. m.UnscopedEnum.__members__ = {}
  13. # __members__ returns a copy
  14. foo = m.UnscopedEnum.__members__
  15. foo["bar"] = "baz"
  16. assert m.UnscopedEnum.__members__ == \
  17. {"EOne": m.UnscopedEnum.EOne, "ETwo": m.UnscopedEnum.ETwo}
  18. # no TypeError exception for unscoped enum ==/!= int comparisons
  19. y = m.UnscopedEnum.ETwo
  20. assert y == 2
  21. assert y != 3
  22. assert int(m.UnscopedEnum.ETwo) == 2
  23. assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo"
  24. # order
  25. assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
  26. assert m.UnscopedEnum.EOne < 2
  27. assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
  28. assert m.UnscopedEnum.ETwo > 1
  29. assert m.UnscopedEnum.ETwo <= 2
  30. assert m.UnscopedEnum.ETwo >= 2
  31. assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
  32. assert m.UnscopedEnum.EOne <= 2
  33. assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
  34. assert m.UnscopedEnum.ETwo >= 1
  35. assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
  36. assert not (2 < m.UnscopedEnum.EOne)
  37. def test_scoped_enum():
  38. assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three"
  39. z = m.ScopedEnum.Two
  40. assert m.test_scoped_enum(z) == "ScopedEnum::Two"
  41. # expected TypeError exceptions for scoped enum ==/!= int comparisons
  42. with pytest.raises(TypeError):
  43. assert z == 2
  44. with pytest.raises(TypeError):
  45. assert z != 3
  46. # order
  47. assert m.ScopedEnum.Two < m.ScopedEnum.Three
  48. assert m.ScopedEnum.Three > m.ScopedEnum.Two
  49. assert m.ScopedEnum.Two <= m.ScopedEnum.Three
  50. assert m.ScopedEnum.Two <= m.ScopedEnum.Two
  51. assert m.ScopedEnum.Two >= m.ScopedEnum.Two
  52. assert m.ScopedEnum.Three >= m.ScopedEnum.Two
  53. def test_implicit_conversion():
  54. assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
  55. assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
  56. f = m.ClassWithUnscopedEnum.test_function
  57. first = m.ClassWithUnscopedEnum.EFirstMode
  58. second = m.ClassWithUnscopedEnum.ESecondMode
  59. assert f(first) == 1
  60. assert f(first) == f(first)
  61. assert not f(first) != f(first)
  62. assert f(first) != f(second)
  63. assert not f(first) == f(second)
  64. assert f(first) == int(f(first))
  65. assert not f(first) != int(f(first))
  66. assert f(first) != int(f(second))
  67. assert not f(first) == int(f(second))
  68. # noinspection PyDictCreation
  69. x = {f(first): 1, f(second): 2}
  70. x[f(first)] = 3
  71. x[f(second)] = 4
  72. # Hashing test
  73. assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}"
  74. def test_binary_operators():
  75. assert int(m.Flags.Read) == 4
  76. assert int(m.Flags.Write) == 2
  77. assert int(m.Flags.Execute) == 1
  78. assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
  79. assert int(m.Flags.Read | m.Flags.Write) == 6
  80. assert int(m.Flags.Read | m.Flags.Execute) == 5
  81. assert int(m.Flags.Write | m.Flags.Execute) == 3
  82. assert int(m.Flags.Write | 1) == 3
  83. state = m.Flags.Read | m.Flags.Write
  84. assert (state & m.Flags.Read) != 0
  85. assert (state & m.Flags.Write) != 0
  86. assert (state & m.Flags.Execute) == 0
  87. assert (state & 1) == 0
  88. state2 = ~state
  89. assert state2 == -7
  90. assert int(state ^ state2) == -1
  91. def test_enum_to_int():
  92. m.test_enum_to_int(m.Flags.Read)
  93. m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
  94. m.test_enum_to_uint(m.Flags.Read)
  95. m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
  96. m.test_enum_to_long_long(m.Flags.Read)
  97. m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)