mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +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.
This commit is contained in:
@@ -1,32 +1,29 @@
|
||||
import pytest
|
||||
from pybind11_tests import has_optional
|
||||
from pybind11_tests import copy_move_policies as m
|
||||
|
||||
|
||||
def test_lacking_copy_ctor():
|
||||
from pybind11_tests import lacking_copy_ctor
|
||||
with pytest.raises(RuntimeError) as excinfo:
|
||||
lacking_copy_ctor.get_one()
|
||||
m.lacking_copy_ctor.get_one()
|
||||
assert "the object is non-copyable!" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_lacking_move_ctor():
|
||||
from pybind11_tests import lacking_move_ctor
|
||||
with pytest.raises(RuntimeError) as excinfo:
|
||||
lacking_move_ctor.get_one()
|
||||
m.lacking_move_ctor.get_one()
|
||||
assert "the object is neither movable nor copyable!" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_move_and_copy_casts():
|
||||
"""Cast some values in C++ via custom type casters and count the number of moves/copies."""
|
||||
from pybind11_tests import move_and_copy_casts, move_and_copy_cstats
|
||||
|
||||
cstats = move_and_copy_cstats()
|
||||
cstats = m.move_and_copy_cstats()
|
||||
c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
|
||||
|
||||
# The type move constructions/assignments below each get incremented: the move assignment comes
|
||||
# from the type_caster load; the move construction happens when extracting that via a cast or
|
||||
# loading into an argument.
|
||||
assert move_and_copy_casts(3) == 18
|
||||
assert m.move_and_copy_casts(3) == 18
|
||||
assert c_m.copy_assignments + c_m.copy_constructions == 0
|
||||
assert c_m.move_assignments == 2
|
||||
assert c_m.move_constructions >= 2
|
||||
@@ -43,21 +40,19 @@ def test_move_and_copy_casts():
|
||||
def test_move_and_copy_loads():
|
||||
"""Call some functions that load arguments via custom type casters and count the number of
|
||||
moves/copies."""
|
||||
from pybind11_tests import (move_and_copy_cstats, move_only, move_or_copy, copy_only,
|
||||
move_pair, move_tuple, copy_tuple, move_copy_nested)
|
||||
|
||||
cstats = move_and_copy_cstats()
|
||||
cstats = m.move_and_copy_cstats()
|
||||
c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
|
||||
|
||||
assert move_only(10) == 10 # 1 move, c_m
|
||||
assert move_or_copy(11) == 11 # 1 move, c_mc
|
||||
assert copy_only(12) == 12 # 1 copy, c_c
|
||||
assert move_pair((13, 14)) == 27 # 1 c_m move, 1 c_mc move
|
||||
assert move_tuple((15, 16, 17)) == 48 # 2 c_m moves, 1 c_mc move
|
||||
assert copy_tuple((18, 19)) == 37 # 2 c_c copies
|
||||
assert m.move_only(10) == 10 # 1 move, c_m
|
||||
assert m.move_or_copy(11) == 11 # 1 move, c_mc
|
||||
assert m.copy_only(12) == 12 # 1 copy, c_c
|
||||
assert m.move_pair((13, 14)) == 27 # 1 c_m move, 1 c_mc move
|
||||
assert m.move_tuple((15, 16, 17)) == 48 # 2 c_m moves, 1 c_mc move
|
||||
assert m.copy_tuple((18, 19)) == 37 # 2 c_c copies
|
||||
# Direct constructions: 2 c_m moves, 2 c_mc moves, 1 c_c copy
|
||||
# Extra moves/copies when moving pairs/tuples: 3 c_m, 3 c_mc, 2 c_c
|
||||
assert move_copy_nested((1, ((2, 3, (4,)), 5))) == 15
|
||||
assert m.move_copy_nested((1, ((2, 3, (4,)), 5))) == 15
|
||||
|
||||
assert c_m.copy_assignments + c_m.copy_constructions == 0
|
||||
assert c_m.move_assignments == 6
|
||||
@@ -70,24 +65,22 @@ def test_move_and_copy_loads():
|
||||
assert c_m.alive() + c_mc.alive() + c_c.alive() == 0
|
||||
|
||||
|
||||
@pytest.mark.skipif(not has_optional, reason='no <optional>')
|
||||
@pytest.mark.skipif(not m.has_optional, reason='no <optional>')
|
||||
def test_move_and_copy_load_optional():
|
||||
"""Tests move/copy loads of std::optional arguments"""
|
||||
from pybind11_tests import (move_and_copy_cstats, move_optional, move_or_copy_optional,
|
||||
copy_optional, move_optional_tuple)
|
||||
|
||||
cstats = move_and_copy_cstats()
|
||||
cstats = m.move_and_copy_cstats()
|
||||
c_m, c_mc, c_c = cstats["MoveOnlyInt"], cstats["MoveOrCopyInt"], cstats["CopyOnlyInt"]
|
||||
|
||||
# The extra move/copy constructions below come from the std::optional move (which has to move
|
||||
# its arguments):
|
||||
assert move_optional(10) == 10 # c_m: 1 move assign, 2 move construct
|
||||
assert move_or_copy_optional(11) == 11 # c_mc: 1 move assign, 2 move construct
|
||||
assert copy_optional(12) == 12 # c_c: 1 copy assign, 2 copy construct
|
||||
assert m.move_optional(10) == 10 # c_m: 1 move assign, 2 move construct
|
||||
assert m.move_or_copy_optional(11) == 11 # c_mc: 1 move assign, 2 move construct
|
||||
assert m.copy_optional(12) == 12 # c_c: 1 copy assign, 2 copy construct
|
||||
# 1 move assign + move construct moves each of c_m, c_mc, 1 c_c copy
|
||||
# +1 move/copy construct each from moving the tuple
|
||||
# +1 move/copy construct each from moving the optional (which moves the tuple again)
|
||||
assert move_optional_tuple((3, 4, 5)) == 12
|
||||
assert m.move_optional_tuple((3, 4, 5)) == 12
|
||||
|
||||
assert c_m.copy_assignments + c_m.copy_constructions == 0
|
||||
assert c_m.move_assignments == 2
|
||||
@@ -102,7 +95,6 @@ def test_move_and_copy_load_optional():
|
||||
|
||||
def test_private_op_new():
|
||||
"""An object with a private `operator new` cannot be returned by value"""
|
||||
import pybind11_tests as m
|
||||
|
||||
with pytest.raises(RuntimeError) as excinfo:
|
||||
m.private_op_new_value()
|
||||
@@ -113,9 +105,8 @@ def test_private_op_new():
|
||||
|
||||
def test_move_fallback():
|
||||
"""#389: rvp::move should fall-through to copy on non-movable objects"""
|
||||
from pybind11_tests import get_moveissue1, get_moveissue2
|
||||
|
||||
m2 = get_moveissue2(2)
|
||||
m2 = m.get_moveissue2(2)
|
||||
assert m2.value == 2
|
||||
m1 = get_moveissue1(1)
|
||||
m1 = m.get_moveissue1(1)
|
||||
assert m1.value == 1
|
||||
|
||||
Reference in New Issue
Block a user