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,10 +1,9 @@
|
||||
import pytest
|
||||
from pybind11_tests import callbacks as m
|
||||
|
||||
|
||||
def test_callbacks():
|
||||
from functools import partial
|
||||
from pybind11_tests import (test_callback1, test_callback2, test_callback3,
|
||||
test_callback4, test_callback5)
|
||||
|
||||
def func1():
|
||||
return "func1"
|
||||
@@ -15,73 +14,65 @@ def test_callbacks():
|
||||
def func3(a):
|
||||
return "func3({})".format(a)
|
||||
|
||||
assert test_callback1(func1) == "func1"
|
||||
assert test_callback2(func2) == ("func2", "Hello", "x", True, 5)
|
||||
assert test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
|
||||
assert test_callback1(partial(func3, "partial")) == "func3(partial)"
|
||||
assert test_callback3(lambda i: i + 1) == "func(43) = 44"
|
||||
assert m.test_callback1(func1) == "func1"
|
||||
assert m.test_callback2(func2) == ("func2", "Hello", "x", True, 5)
|
||||
assert m.test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
|
||||
assert m.test_callback1(partial(func3, "partial")) == "func3(partial)"
|
||||
assert m.test_callback3(lambda i: i + 1) == "func(43) = 44"
|
||||
|
||||
f = test_callback4()
|
||||
f = m.test_callback4()
|
||||
assert f(43) == 44
|
||||
f = test_callback5()
|
||||
f = m.test_callback5()
|
||||
assert f(number=43) == 44
|
||||
|
||||
|
||||
def test_bound_method_callback():
|
||||
from pybind11_tests import test_callback3, CppBoundMethodTest
|
||||
|
||||
# Bound Python method:
|
||||
class MyClass:
|
||||
def double(self, val):
|
||||
return 2 * val
|
||||
|
||||
z = MyClass()
|
||||
assert test_callback3(z.double) == "func(43) = 86"
|
||||
assert m.test_callback3(z.double) == "func(43) = 86"
|
||||
|
||||
z = CppBoundMethodTest()
|
||||
assert test_callback3(z.triple) == "func(43) = 129"
|
||||
z = m.CppBoundMethodTest()
|
||||
assert m.test_callback3(z.triple) == "func(43) = 129"
|
||||
|
||||
|
||||
def test_keyword_args_and_generalized_unpacking():
|
||||
from pybind11_tests import (test_tuple_unpacking, test_dict_unpacking, test_keyword_args,
|
||||
test_unpacking_and_keywords1, test_unpacking_and_keywords2,
|
||||
test_unpacking_error1, test_unpacking_error2,
|
||||
test_arg_conversion_error1, test_arg_conversion_error2)
|
||||
|
||||
def f(*args, **kwargs):
|
||||
return args, kwargs
|
||||
|
||||
assert test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
|
||||
assert test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
|
||||
assert test_keyword_args(f) == ((), {"x": 10, "y": 20})
|
||||
assert test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
|
||||
assert test_unpacking_and_keywords2(f) == (
|
||||
assert m.test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
|
||||
assert m.test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
|
||||
assert m.test_keyword_args(f) == ((), {"x": 10, "y": 20})
|
||||
assert m.test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
|
||||
assert m.test_unpacking_and_keywords2(f) == (
|
||||
("positional", 1, 2, 3, 4, 5),
|
||||
{"key": "value", "a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
|
||||
)
|
||||
|
||||
with pytest.raises(TypeError) as excinfo:
|
||||
test_unpacking_error1(f)
|
||||
m.test_unpacking_error1(f)
|
||||
assert "Got multiple values for keyword argument" in str(excinfo.value)
|
||||
|
||||
with pytest.raises(TypeError) as excinfo:
|
||||
test_unpacking_error2(f)
|
||||
m.test_unpacking_error2(f)
|
||||
assert "Got multiple values for keyword argument" in str(excinfo.value)
|
||||
|
||||
with pytest.raises(RuntimeError) as excinfo:
|
||||
test_arg_conversion_error1(f)
|
||||
m.test_arg_conversion_error1(f)
|
||||
assert "Unable to convert call argument" in str(excinfo.value)
|
||||
|
||||
with pytest.raises(RuntimeError) as excinfo:
|
||||
test_arg_conversion_error2(f)
|
||||
m.test_arg_conversion_error2(f)
|
||||
assert "Unable to convert call argument" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_lambda_closure_cleanup():
|
||||
from pybind11_tests import test_cleanup, payload_cstats
|
||||
|
||||
test_cleanup()
|
||||
cstats = payload_cstats()
|
||||
m.test_cleanup()
|
||||
cstats = m.payload_cstats()
|
||||
assert cstats.alive() == 0
|
||||
assert cstats.copy_constructions == 1
|
||||
assert cstats.move_constructions >= 1
|
||||
@@ -89,31 +80,28 @@ def test_lambda_closure_cleanup():
|
||||
|
||||
def test_cpp_function_roundtrip():
|
||||
"""Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
|
||||
from pybind11_tests import dummy_function, dummy_function2, test_dummy_function, roundtrip
|
||||
|
||||
assert test_dummy_function(dummy_function) == "matches dummy_function: eval(1) = 2"
|
||||
assert test_dummy_function(roundtrip(dummy_function)) == "matches dummy_function: eval(1) = 2"
|
||||
assert roundtrip(None, expect_none=True) is None
|
||||
assert test_dummy_function(lambda x: x + 2) == "can't convert to function pointer: eval(1) = 3"
|
||||
assert m.test_dummy_function(m.dummy_function) == "matches dummy_function: eval(1) = 2"
|
||||
assert (m.test_dummy_function(m.roundtrip(m.dummy_function)) ==
|
||||
"matches dummy_function: eval(1) = 2")
|
||||
assert m.roundtrip(None, expect_none=True) is None
|
||||
assert (m.test_dummy_function(lambda x: x + 2) ==
|
||||
"can't convert to function pointer: eval(1) = 3")
|
||||
|
||||
with pytest.raises(TypeError) as excinfo:
|
||||
test_dummy_function(dummy_function2)
|
||||
m.test_dummy_function(m.dummy_function2)
|
||||
assert "incompatible function arguments" in str(excinfo.value)
|
||||
|
||||
with pytest.raises(TypeError) as excinfo:
|
||||
test_dummy_function(lambda x, y: x + y)
|
||||
m.test_dummy_function(lambda x, y: x + y)
|
||||
assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
|
||||
"takes exactly 2 arguments"))
|
||||
|
||||
|
||||
def test_function_signatures(doc):
|
||||
from pybind11_tests import test_callback3, test_callback4
|
||||
|
||||
assert doc(test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
|
||||
assert doc(test_callback4) == "test_callback4() -> Callable[[int], int]"
|
||||
assert doc(m.test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
|
||||
assert doc(m.test_callback4) == "test_callback4() -> Callable[[int], int]"
|
||||
|
||||
|
||||
def test_movable_object():
|
||||
from pybind11_tests import callback_with_movable
|
||||
|
||||
assert callback_with_movable(lambda _: None) is True
|
||||
assert m.callback_with_movable(lambda _: None) is True
|
||||
|
||||
Reference in New Issue
Block a user