Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:53

0001 import pytest
0002 
0003 from pybind11_tests import enums as m
0004 
0005 
0006 def test_unscoped_enum():
0007     assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
0008     assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
0009     assert str(m.EOne) == "UnscopedEnum.EOne"
0010     assert repr(m.UnscopedEnum.EOne) == "<UnscopedEnum.EOne: 1>"
0011     assert repr(m.UnscopedEnum.ETwo) == "<UnscopedEnum.ETwo: 2>"
0012     assert repr(m.EOne) == "<UnscopedEnum.EOne: 1>"
0013 
0014     # name property
0015     assert m.UnscopedEnum.EOne.name == "EOne"
0016     assert m.UnscopedEnum.EOne.value == 1
0017     assert m.UnscopedEnum.ETwo.name == "ETwo"
0018     assert m.UnscopedEnum.ETwo.value == 2
0019     assert m.EOne is m.UnscopedEnum.EOne
0020     # name, value readonly
0021     with pytest.raises(AttributeError):
0022         m.UnscopedEnum.EOne.name = ""
0023     with pytest.raises(AttributeError):
0024         m.UnscopedEnum.EOne.value = 10
0025     # name, value returns a copy
0026     # TODO: Neither the name nor value tests actually check against aliasing.
0027     # Use a mutable type that has reference semantics.
0028     nonaliased_name = m.UnscopedEnum.EOne.name
0029     nonaliased_name = "bar"  # noqa: F841
0030     assert m.UnscopedEnum.EOne.name == "EOne"
0031     nonaliased_value = m.UnscopedEnum.EOne.value
0032     nonaliased_value = 10  # noqa: F841
0033     assert m.UnscopedEnum.EOne.value == 1
0034 
0035     # __members__ property
0036     assert m.UnscopedEnum.__members__ == {
0037         "EOne": m.UnscopedEnum.EOne,
0038         "ETwo": m.UnscopedEnum.ETwo,
0039         "EThree": m.UnscopedEnum.EThree,
0040     }
0041     # __members__ readonly
0042     with pytest.raises(AttributeError):
0043         m.UnscopedEnum.__members__ = {}
0044     # __members__ returns a copy
0045     nonaliased_members = m.UnscopedEnum.__members__
0046     nonaliased_members["bar"] = "baz"
0047     assert m.UnscopedEnum.__members__ == {
0048         "EOne": m.UnscopedEnum.EOne,
0049         "ETwo": m.UnscopedEnum.ETwo,
0050         "EThree": m.UnscopedEnum.EThree,
0051     }
0052 
0053     for docstring_line in """An unscoped enumeration
0054 
0055 Members:
0056 
0057   EOne : Docstring for EOne
0058 
0059   ETwo : Docstring for ETwo
0060 
0061   EThree : Docstring for EThree""".split(
0062         "\n"
0063     ):
0064         assert docstring_line in m.UnscopedEnum.__doc__
0065 
0066     # Unscoped enums will accept ==/!= int comparisons
0067     y = m.UnscopedEnum.ETwo
0068     assert y == 2
0069     assert 2 == y
0070     assert y != 3
0071     assert 3 != y
0072     # Compare with None
0073     assert y != None  # noqa: E711
0074     assert not (y == None)  # noqa: E711
0075     # Compare with an object
0076     assert y != object()
0077     assert not (y == object())
0078     # Compare with string
0079     assert y != "2"
0080     assert "2" != y
0081     assert not ("2" == y)
0082     assert not (y == "2")
0083 
0084     with pytest.raises(TypeError):
0085         y < object()  # noqa: B015
0086 
0087     with pytest.raises(TypeError):
0088         y <= object()  # noqa: B015
0089 
0090     with pytest.raises(TypeError):
0091         y > object()  # noqa: B015
0092 
0093     with pytest.raises(TypeError):
0094         y >= object()  # noqa: B015
0095 
0096     with pytest.raises(TypeError):
0097         y | object()
0098 
0099     with pytest.raises(TypeError):
0100         y & object()
0101 
0102     with pytest.raises(TypeError):
0103         y ^ object()
0104 
0105     assert int(m.UnscopedEnum.ETwo) == 2
0106     assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo"
0107 
0108     # order
0109     assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
0110     assert m.UnscopedEnum.EOne < 2
0111     assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
0112     assert m.UnscopedEnum.ETwo > 1
0113     assert m.UnscopedEnum.ETwo <= 2
0114     assert m.UnscopedEnum.ETwo >= 2
0115     assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
0116     assert m.UnscopedEnum.EOne <= 2
0117     assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
0118     assert m.UnscopedEnum.ETwo >= 1
0119     assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
0120     assert not (2 < m.UnscopedEnum.EOne)
0121 
0122     # arithmetic
0123     assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne
0124     assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree
0125     assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo
0126 
0127 
0128 def test_scoped_enum():
0129     assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three"
0130     z = m.ScopedEnum.Two
0131     assert m.test_scoped_enum(z) == "ScopedEnum::Two"
0132 
0133     # Scoped enums will *NOT* accept ==/!= int comparisons (Will always return False)
0134     assert not z == 3
0135     assert not 3 == z
0136     assert z != 3
0137     assert 3 != z
0138     # Compare with None
0139     assert z != None  # noqa: E711
0140     assert not (z == None)  # noqa: E711
0141     # Compare with an object
0142     assert z != object()
0143     assert not (z == object())
0144     # Scoped enums will *NOT* accept >, <, >= and <= int comparisons (Will throw exceptions)
0145     with pytest.raises(TypeError):
0146         z > 3  # noqa: B015
0147     with pytest.raises(TypeError):
0148         z < 3  # noqa: B015
0149     with pytest.raises(TypeError):
0150         z >= 3  # noqa: B015
0151     with pytest.raises(TypeError):
0152         z <= 3  # noqa: B015
0153 
0154     # order
0155     assert m.ScopedEnum.Two < m.ScopedEnum.Three
0156     assert m.ScopedEnum.Three > m.ScopedEnum.Two
0157     assert m.ScopedEnum.Two <= m.ScopedEnum.Three
0158     assert m.ScopedEnum.Two <= m.ScopedEnum.Two
0159     assert m.ScopedEnum.Two >= m.ScopedEnum.Two
0160     assert m.ScopedEnum.Three >= m.ScopedEnum.Two
0161 
0162 
0163 def test_implicit_conversion():
0164     assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
0165     assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
0166     assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "<EMode.EFirstMode: 1>"
0167     assert repr(m.ClassWithUnscopedEnum.EFirstMode) == "<EMode.EFirstMode: 1>"
0168 
0169     f = m.ClassWithUnscopedEnum.test_function
0170     first = m.ClassWithUnscopedEnum.EFirstMode
0171     second = m.ClassWithUnscopedEnum.ESecondMode
0172 
0173     assert f(first) == 1
0174 
0175     assert f(first) == f(first)
0176     assert not f(first) != f(first)
0177 
0178     assert f(first) != f(second)
0179     assert not f(first) == f(second)
0180 
0181     assert f(first) == int(f(first))
0182     assert not f(first) != int(f(first))
0183 
0184     assert f(first) != int(f(second))
0185     assert not f(first) == int(f(second))
0186 
0187     # noinspection PyDictCreation
0188     x = {f(first): 1, f(second): 2}
0189     x[f(first)] = 3
0190     x[f(second)] = 4
0191     # Hashing test
0192     assert repr(x) == "{<EMode.EFirstMode: 1>: 3, <EMode.ESecondMode: 2>: 4}"
0193 
0194 
0195 def test_binary_operators():
0196     assert int(m.Flags.Read) == 4
0197     assert int(m.Flags.Write) == 2
0198     assert int(m.Flags.Execute) == 1
0199     assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
0200     assert int(m.Flags.Read | m.Flags.Write) == 6
0201     assert int(m.Flags.Read | m.Flags.Execute) == 5
0202     assert int(m.Flags.Write | m.Flags.Execute) == 3
0203     assert int(m.Flags.Write | 1) == 3
0204     assert ~m.Flags.Write == -3
0205 
0206     state = m.Flags.Read | m.Flags.Write
0207     assert (state & m.Flags.Read) != 0
0208     assert (state & m.Flags.Write) != 0
0209     assert (state & m.Flags.Execute) == 0
0210     assert (state & 1) == 0
0211 
0212     state2 = ~state
0213     assert state2 == -7
0214     assert int(state ^ state2) == -1
0215 
0216 
0217 def test_enum_to_int():
0218     m.test_enum_to_int(m.Flags.Read)
0219     m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
0220     m.test_enum_to_int(m.ScopedCharEnum.Positive)
0221     m.test_enum_to_int(m.ScopedBoolEnum.TRUE)
0222     m.test_enum_to_uint(m.Flags.Read)
0223     m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
0224     m.test_enum_to_uint(m.ScopedCharEnum.Positive)
0225     m.test_enum_to_uint(m.ScopedBoolEnum.TRUE)
0226     m.test_enum_to_long_long(m.Flags.Read)
0227     m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
0228     m.test_enum_to_long_long(m.ScopedCharEnum.Positive)
0229     m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)
0230 
0231 
0232 def test_duplicate_enum_name():
0233     with pytest.raises(ValueError) as excinfo:
0234         m.register_bad_enum()
0235     assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!'
0236 
0237 
0238 def test_char_underlying_enum():  # Issue #1331/PR #1334:
0239     assert type(m.ScopedCharEnum.Positive.__int__()) is int
0240     assert int(m.ScopedChar16Enum.Zero) == 0
0241     assert hash(m.ScopedChar32Enum.Positive) == 1
0242     assert type(m.ScopedCharEnum.Positive.__getstate__()) is int
0243     assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
0244     with pytest.raises(TypeError):
0245         # Even if the underlying type is char, only an int can be used to construct the enum:
0246         m.ScopedCharEnum("0")
0247 
0248 
0249 def test_bool_underlying_enum():
0250     assert type(m.ScopedBoolEnum.TRUE.__int__()) is int
0251     assert int(m.ScopedBoolEnum.FALSE) == 0
0252     assert hash(m.ScopedBoolEnum.TRUE) == 1
0253     assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int
0254     assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
0255     # Enum could construct with a bool
0256     # (bool is a strict subclass of int, and False will be converted to 0)
0257     assert m.ScopedBoolEnum(False) == m.ScopedBoolEnum.FALSE
0258 
0259 
0260 def test_docstring_signatures():
0261     for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
0262         for attr in enum_type.__dict__.values():
0263             # Issue #2623/PR #2637: Add argument names to enum_ methods
0264             assert "arg0" not in (attr.__doc__ or "")