blob: c2668aa9534ac9aa3ff0904124caf20764d529f6 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2
3
Dean Moldovan665e8802016-08-12 22:28:31 +02004def test_callbacks():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02005 from functools import partial
6 from pybind11_tests import (test_callback1, test_callback2, test_callback3,
7 test_callback4, test_callback5)
8
9 def func1():
Dean Moldovan665e8802016-08-12 22:28:31 +020010 return "func1"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020011
12 def func2(a, b, c, d):
Dean Moldovan665e8802016-08-12 22:28:31 +020013 return "func2", a, b, c, d
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020014
15 def func3(a):
Dean Moldovan665e8802016-08-12 22:28:31 +020016 return "func3({})".format(a)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020017
Dean Moldovan665e8802016-08-12 22:28:31 +020018 assert test_callback1(func1) == "func1"
19 assert test_callback2(func2) == ("func2", "Hello", "x", True, 5)
20 assert test_callback1(partial(func2, 1, 2, 3, 4)) == ("func2", 1, 2, 3, 4)
21 assert test_callback1(partial(func3, "partial")) == "func3(partial)"
22 assert test_callback3(lambda i: i + 1) == "func(43) = 44"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020023
24 f = test_callback4()
25 assert f(43) == 44
26 f = test_callback5()
27 assert f(number=43) == 44
28
29
Dean Moldovanc743e1b2016-08-29 03:05:42 +020030def test_keyword_args_and_generalized_unpacking():
31 from pybind11_tests import (test_tuple_unpacking, test_dict_unpacking, test_keyword_args,
32 test_unpacking_and_keywords1, test_unpacking_and_keywords2,
33 test_unpacking_error1, test_unpacking_error2,
34 test_arg_conversion_error1, test_arg_conversion_error2)
35
36 def f(*args, **kwargs):
37 return args, kwargs
38
39 assert test_tuple_unpacking(f) == (("positional", 1, 2, 3, 4, 5, 6), {})
40 assert test_dict_unpacking(f) == (("positional", 1), {"key": "value", "a": 1, "b": 2})
41 assert test_keyword_args(f) == ((), {"x": 10, "y": 20})
42 assert test_unpacking_and_keywords1(f) == ((1, 2), {"c": 3, "d": 4})
43 assert test_unpacking_and_keywords2(f) == (
44 ("positional", 1, 2, 3, 4, 5),
45 {"key": "value", "a": 1, "b": 2, "c": 3, "d": 4, "e": 5}
46 )
47
48 with pytest.raises(TypeError) as excinfo:
49 test_unpacking_error1(f)
50 assert "Got multiple values for keyword argument" in str(excinfo.value)
51
52 with pytest.raises(TypeError) as excinfo:
53 test_unpacking_error2(f)
54 assert "Got multiple values for keyword argument" in str(excinfo.value)
55
56 with pytest.raises(RuntimeError) as excinfo:
57 test_arg_conversion_error1(f)
58 assert "Unable to convert call argument" in str(excinfo.value)
59
60 with pytest.raises(RuntimeError) as excinfo:
61 test_arg_conversion_error2(f)
62 assert "Unable to convert call argument" in str(excinfo.value)
63
64
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020065def test_lambda_closure_cleanup():
66 from pybind11_tests import test_cleanup, payload_cstats
67
68 test_cleanup()
69 cstats = payload_cstats()
70 assert cstats.alive() == 0
71 assert cstats.copy_constructions == 1
72 assert cstats.move_constructions >= 1
73
74
Dean Moldovan665e8802016-08-12 22:28:31 +020075def test_cpp_function_roundtrip():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020076 """Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
77 from pybind11_tests import dummy_function, dummy_function2, test_dummy_function, roundtrip
78
Dean Moldovan665e8802016-08-12 22:28:31 +020079 assert test_dummy_function(dummy_function) == "matches dummy_function: eval(1) = 2"
80 assert test_dummy_function(roundtrip(dummy_function)) == "matches dummy_function: eval(1) = 2"
81 assert roundtrip(None, expect_none=True) is None
82 assert test_dummy_function(lambda x: x + 2) == "can't convert to function pointer: eval(1) = 3"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020083
Dean Moldovan665e8802016-08-12 22:28:31 +020084 with pytest.raises(TypeError) as excinfo:
85 test_dummy_function(dummy_function2)
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090086 assert "incompatible function arguments" in str(excinfo.value)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020087
Dean Moldovan665e8802016-08-12 22:28:31 +020088 with pytest.raises(TypeError) as excinfo:
89 test_dummy_function(lambda x, y: x + y)
90 assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
91 "takes exactly 2 arguments"))
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020092
93
94def test_function_signatures(doc):
95 from pybind11_tests import test_callback3, test_callback4
96
Dean Moldovan665e8802016-08-12 22:28:31 +020097 assert doc(test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> str"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020098 assert doc(test_callback4) == "test_callback4() -> Callable[[int], int]"