2020-07-20 17:35:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-08-12 22:57:24 +00:00
|
|
|
import pytest
|
2021-08-13 16:37:05 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
from pybind11_tests import enums as m
|
2016-08-12 22:57:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_unscoped_enum():
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
|
|
|
|
assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
|
|
|
|
assert str(m.EOne) == "UnscopedEnum.EOne"
|
2020-09-19 23:12:19 +00:00
|
|
|
assert repr(m.UnscopedEnum.EOne) == "<UnscopedEnum.EOne: 1>"
|
|
|
|
assert repr(m.UnscopedEnum.ETwo) == "<UnscopedEnum.ETwo: 2>"
|
|
|
|
assert repr(m.EOne) == "<UnscopedEnum.EOne: 1>"
|
2018-04-02 21:26:48 +00:00
|
|
|
|
|
|
|
# name property
|
|
|
|
assert m.UnscopedEnum.EOne.name == "EOne"
|
2020-12-31 16:08:15 +00:00
|
|
|
assert m.UnscopedEnum.EOne.value == 1
|
2018-04-02 21:26:48 +00:00
|
|
|
assert m.UnscopedEnum.ETwo.name == "ETwo"
|
2020-12-31 16:08:15 +00:00
|
|
|
assert m.UnscopedEnum.ETwo.value == 2
|
|
|
|
assert m.EOne is m.UnscopedEnum.EOne
|
|
|
|
# name, value readonly
|
2018-04-02 21:26:48 +00:00
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
m.UnscopedEnum.EOne.name = ""
|
2020-12-31 16:08:15 +00:00
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
m.UnscopedEnum.EOne.value = 10
|
|
|
|
# name, value returns a copy
|
|
|
|
# TODO: Neither the name nor value tests actually check against aliasing.
|
|
|
|
# Use a mutable type that has reference semantics.
|
|
|
|
nonaliased_name = m.UnscopedEnum.EOne.name
|
|
|
|
nonaliased_name = "bar" # noqa: F841
|
2018-04-02 21:26:48 +00:00
|
|
|
assert m.UnscopedEnum.EOne.name == "EOne"
|
2020-12-31 16:08:15 +00:00
|
|
|
nonaliased_value = m.UnscopedEnum.EOne.value
|
|
|
|
nonaliased_value = 10 # noqa: F841
|
|
|
|
assert m.UnscopedEnum.EOne.value == 1
|
2018-04-02 21:26:48 +00:00
|
|
|
|
2017-03-03 16:45:50 +00:00
|
|
|
# __members__ property
|
2020-10-16 20:38:13 +00:00
|
|
|
assert m.UnscopedEnum.__members__ == {
|
|
|
|
"EOne": m.UnscopedEnum.EOne,
|
|
|
|
"ETwo": m.UnscopedEnum.ETwo,
|
|
|
|
"EThree": m.UnscopedEnum.EThree,
|
|
|
|
}
|
2017-03-03 16:45:50 +00:00
|
|
|
# __members__ readonly
|
|
|
|
with pytest.raises(AttributeError):
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
m.UnscopedEnum.__members__ = {}
|
2017-03-03 16:45:50 +00:00
|
|
|
# __members__ returns a copy
|
2020-12-31 16:08:15 +00:00
|
|
|
nonaliased_members = m.UnscopedEnum.__members__
|
|
|
|
nonaliased_members["bar"] = "baz"
|
2020-10-16 20:38:13 +00:00
|
|
|
assert m.UnscopedEnum.__members__ == {
|
|
|
|
"EOne": m.UnscopedEnum.EOne,
|
|
|
|
"ETwo": m.UnscopedEnum.ETwo,
|
|
|
|
"EThree": m.UnscopedEnum.EThree,
|
|
|
|
}
|
2016-08-12 22:57:24 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
for docstring_line in """An unscoped enumeration
|
2017-11-16 21:24:36 +00:00
|
|
|
|
|
|
|
Members:
|
|
|
|
|
|
|
|
EOne : Docstring for EOne
|
|
|
|
|
|
|
|
ETwo : Docstring for ETwo
|
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
EThree : Docstring for EThree""".split(
|
|
|
|
"\n"
|
|
|
|
):
|
2019-09-19 16:23:27 +00:00
|
|
|
assert docstring_line in m.UnscopedEnum.__doc__
|
2017-11-16 21:24:36 +00:00
|
|
|
|
2018-10-24 09:18:58 +00:00
|
|
|
# Unscoped enums will accept ==/!= int comparisons
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
y = m.UnscopedEnum.ETwo
|
2016-08-12 22:57:24 +00:00
|
|
|
assert y == 2
|
2018-10-24 09:18:58 +00:00
|
|
|
assert 2 == y
|
2016-08-12 22:57:24 +00:00
|
|
|
assert y != 3
|
2018-10-24 09:18:58 +00:00
|
|
|
assert 3 != y
|
2019-09-19 16:23:27 +00:00
|
|
|
# Compare with None
|
2020-10-16 20:38:13 +00:00
|
|
|
assert y != None # noqa: E711
|
2019-09-19 16:23:27 +00:00
|
|
|
assert not (y == None) # noqa: E711
|
|
|
|
# Compare with an object
|
2020-10-16 20:38:13 +00:00
|
|
|
assert y != object()
|
2019-09-19 16:23:27 +00:00
|
|
|
assert not (y == object())
|
|
|
|
# Compare with string
|
|
|
|
assert y != "2"
|
|
|
|
assert "2" != y
|
|
|
|
assert not ("2" == y)
|
|
|
|
assert not (y == "2")
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
y < object() # noqa: B015
|
2019-09-19 16:23:27 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
y <= object() # noqa: B015
|
2019-09-19 16:23:27 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
y > object() # noqa: B015
|
2019-09-19 16:23:27 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
y >= object() # noqa: B015
|
2019-09-19 16:23:27 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
y | object() # noqa: B015
|
2019-09-19 16:23:27 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
y & object() # noqa: B015
|
2019-09-19 16:23:27 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
y ^ object() # noqa: B015
|
2016-08-12 22:57:24 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert int(m.UnscopedEnum.ETwo) == 2
|
|
|
|
assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo"
|
2016-08-12 22:57:24 +00:00
|
|
|
|
2016-11-16 16:28:11 +00:00
|
|
|
# order
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
|
|
|
|
assert m.UnscopedEnum.EOne < 2
|
|
|
|
assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
|
|
|
|
assert m.UnscopedEnum.ETwo > 1
|
|
|
|
assert m.UnscopedEnum.ETwo <= 2
|
|
|
|
assert m.UnscopedEnum.ETwo >= 2
|
|
|
|
assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
|
|
|
|
assert m.UnscopedEnum.EOne <= 2
|
|
|
|
assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
|
|
|
|
assert m.UnscopedEnum.ETwo >= 1
|
|
|
|
assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
|
|
|
|
assert not (2 < m.UnscopedEnum.EOne)
|
2016-11-16 16:28:11 +00:00
|
|
|
|
2019-09-19 16:23:27 +00:00
|
|
|
# arithmetic
|
|
|
|
assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne
|
|
|
|
assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree
|
|
|
|
assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo
|
|
|
|
|
2016-08-12 22:57:24 +00:00
|
|
|
|
|
|
|
def test_scoped_enum():
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three"
|
|
|
|
z = m.ScopedEnum.Two
|
|
|
|
assert m.test_scoped_enum(z) == "ScopedEnum::Two"
|
2016-08-12 22:57:24 +00:00
|
|
|
|
2018-10-24 09:18:58 +00:00
|
|
|
# Scoped enums will *NOT* accept ==/!= int comparisons (Will always return False)
|
|
|
|
assert not z == 3
|
|
|
|
assert not 3 == z
|
|
|
|
assert z != 3
|
|
|
|
assert 3 != z
|
2019-09-19 16:23:27 +00:00
|
|
|
# Compare with None
|
2020-10-16 20:38:13 +00:00
|
|
|
assert z != None # noqa: E711
|
2019-09-19 16:23:27 +00:00
|
|
|
assert not (z == None) # noqa: E711
|
|
|
|
# Compare with an object
|
2020-10-16 20:38:13 +00:00
|
|
|
assert z != object()
|
2019-09-19 16:23:27 +00:00
|
|
|
assert not (z == object())
|
2018-10-24 09:18:58 +00:00
|
|
|
# Scoped enums will *NOT* accept >, <, >= and <= int comparisons (Will throw exceptions)
|
2016-08-12 22:57:24 +00:00
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
z > 3 # noqa: B015
|
2016-08-12 22:57:24 +00:00
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
z < 3 # noqa: B015
|
2018-10-24 09:18:58 +00:00
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
z >= 3 # noqa: B015
|
2018-10-24 09:18:58 +00:00
|
|
|
with pytest.raises(TypeError):
|
2020-12-19 00:41:43 +00:00
|
|
|
z <= 3 # noqa: B015
|
2016-08-12 22:57:24 +00:00
|
|
|
|
2016-11-16 16:28:11 +00:00
|
|
|
# order
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert m.ScopedEnum.Two < m.ScopedEnum.Three
|
|
|
|
assert m.ScopedEnum.Three > m.ScopedEnum.Two
|
|
|
|
assert m.ScopedEnum.Two <= m.ScopedEnum.Three
|
|
|
|
assert m.ScopedEnum.Two <= m.ScopedEnum.Two
|
|
|
|
assert m.ScopedEnum.Two >= m.ScopedEnum.Two
|
|
|
|
assert m.ScopedEnum.Three >= m.ScopedEnum.Two
|
2016-08-12 22:57:24 +00:00
|
|
|
|
2016-11-20 20:21:54 +00:00
|
|
|
|
2016-08-12 22:57:24 +00:00
|
|
|
def test_implicit_conversion():
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
|
|
|
|
assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
|
2020-09-19 23:12:19 +00:00
|
|
|
assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "<EMode.EFirstMode: 1>"
|
|
|
|
assert repr(m.ClassWithUnscopedEnum.EFirstMode) == "<EMode.EFirstMode: 1>"
|
2016-08-12 22:57:24 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
f = m.ClassWithUnscopedEnum.test_function
|
|
|
|
first = m.ClassWithUnscopedEnum.EFirstMode
|
|
|
|
second = m.ClassWithUnscopedEnum.ESecondMode
|
2016-08-12 22:57:24 +00:00
|
|
|
|
|
|
|
assert f(first) == 1
|
|
|
|
|
|
|
|
assert f(first) == f(first)
|
|
|
|
assert not f(first) != f(first)
|
|
|
|
|
|
|
|
assert f(first) != f(second)
|
|
|
|
assert not f(first) == f(second)
|
|
|
|
|
|
|
|
assert f(first) == int(f(first))
|
|
|
|
assert not f(first) != int(f(first))
|
|
|
|
|
|
|
|
assert f(first) != int(f(second))
|
|
|
|
assert not f(first) == int(f(second))
|
|
|
|
|
|
|
|
# noinspection PyDictCreation
|
|
|
|
x = {f(first): 1, f(second): 2}
|
|
|
|
x[f(first)] = 3
|
|
|
|
x[f(second)] = 4
|
|
|
|
# Hashing test
|
2020-09-19 23:12:19 +00:00
|
|
|
assert repr(x) == "{<EMode.EFirstMode: 1>: 3, <EMode.ESecondMode: 2>: 4}"
|
2016-11-16 16:28:11 +00:00
|
|
|
|
2016-11-20 20:21:54 +00:00
|
|
|
|
2016-11-16 16:28:11 +00:00
|
|
|
def test_binary_operators():
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
assert int(m.Flags.Read) == 4
|
|
|
|
assert int(m.Flags.Write) == 2
|
|
|
|
assert int(m.Flags.Execute) == 1
|
|
|
|
assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
|
|
|
|
assert int(m.Flags.Read | m.Flags.Write) == 6
|
|
|
|
assert int(m.Flags.Read | m.Flags.Execute) == 5
|
|
|
|
assert int(m.Flags.Write | m.Flags.Execute) == 3
|
|
|
|
assert int(m.Flags.Write | 1) == 3
|
2019-09-04 20:16:21 +00:00
|
|
|
assert ~m.Flags.Write == -3
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
|
|
|
|
state = m.Flags.Read | m.Flags.Write
|
|
|
|
assert (state & m.Flags.Read) != 0
|
|
|
|
assert (state & m.Flags.Write) != 0
|
|
|
|
assert (state & m.Flags.Execute) == 0
|
2016-11-16 16:28:11 +00:00
|
|
|
assert (state & 1) == 0
|
|
|
|
|
|
|
|
state2 = ~state
|
|
|
|
assert state2 == -7
|
|
|
|
assert int(state ^ state2) == -1
|
2017-04-28 12:46:52 +00:00
|
|
|
|
2017-04-28 20:43:14 +00:00
|
|
|
|
2017-04-28 12:46:52 +00:00
|
|
|
def test_enum_to_int():
|
2019-09-20 09:06:10 +00:00
|
|
|
m.test_enum_to_int(m.Flags.Read)
|
|
|
|
m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
|
2021-08-26 21:34:24 +00:00
|
|
|
m.test_enum_to_int(m.ScopedCharEnum.Positive)
|
|
|
|
m.test_enum_to_int(m.ScopedBoolEnum.TRUE)
|
2019-09-20 09:06:10 +00:00
|
|
|
m.test_enum_to_uint(m.Flags.Read)
|
|
|
|
m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
|
2021-08-26 21:34:24 +00:00
|
|
|
m.test_enum_to_uint(m.ScopedCharEnum.Positive)
|
|
|
|
m.test_enum_to_uint(m.ScopedBoolEnum.TRUE)
|
2019-09-20 09:06:10 +00:00
|
|
|
m.test_enum_to_long_long(m.Flags.Read)
|
|
|
|
m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
|
2021-08-26 21:34:24 +00:00
|
|
|
m.test_enum_to_long_long(m.ScopedCharEnum.Positive)
|
|
|
|
m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)
|
2018-09-11 08:59:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_duplicate_enum_name():
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
m.register_bad_enum()
|
2018-08-31 23:20:24 +00:00
|
|
|
assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!'
|
2020-11-10 17:49:42 +00:00
|
|
|
|
|
|
|
|
2021-08-26 21:34:24 +00:00
|
|
|
def test_char_underlying_enum(): # Issue #1331/PR #1334:
|
|
|
|
assert type(m.ScopedCharEnum.Positive.__int__()) is int
|
|
|
|
assert int(m.ScopedChar16Enum.Zero) == 0 # int() call should successfully return
|
|
|
|
assert hash(m.ScopedChar32Enum.Positive) == 1
|
|
|
|
assert m.ScopedCharEnum.Positive.__getstate__() == 1 # return type is long in py2.x
|
|
|
|
assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
# Enum should construct with a int, even with char underlying type
|
|
|
|
m.ScopedWCharEnum("0")
|
|
|
|
|
|
|
|
|
|
|
|
def test_bool_underlying_enum():
|
|
|
|
assert type(m.ScopedBoolEnum.TRUE.__int__()) is int
|
|
|
|
assert int(m.ScopedBoolEnum.FALSE) == 0
|
|
|
|
assert hash(m.ScopedBoolEnum.TRUE) == 1
|
|
|
|
assert m.ScopedBoolEnum.TRUE.__getstate__() == 1
|
|
|
|
assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
|
|
|
|
# Enum could construct with a bool
|
|
|
|
# (bool is a strict subclass of int, and False will be converted to 0)
|
|
|
|
assert m.ScopedBoolEnum(False) == m.ScopedBoolEnum.FALSE
|
|
|
|
|
|
|
|
|
2020-11-10 17:49:42 +00:00
|
|
|
def test_docstring_signatures():
|
|
|
|
for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
|
|
|
|
for attr in enum_type.__dict__.values():
|
|
|
|
# Issue #2623/PR #2637: Add argument names to enum_ methods
|
|
|
|
assert "arg0" not in (attr.__doc__ or "")
|