You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.5 KiB

8 years ago
8 years ago
  1. import pytest
  2. from pybind11_tests import (kw_func0, kw_func1, kw_func2, kw_func3, kw_func4, args_function,
  3. args_kwargs_function, kw_func_udl, kw_func_udl_z, KWClass)
  4. def test_function_signatures(doc):
  5. assert doc(kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
  6. assert doc(kw_func1) == "kw_func1(x: int, y: int) -> str"
  7. assert doc(kw_func2) == "kw_func2(x: int=100, y: int=200) -> str"
  8. assert doc(kw_func3) == "kw_func3(data: str='Hello world!') -> None"
  9. assert doc(kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> str"
  10. assert doc(kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> str"
  11. assert doc(kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> str"
  12. assert doc(args_function) == "args_function(*args) -> tuple"
  13. assert doc(args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
  14. assert doc(KWClass.foo0) == "foo0(self: m.KWClass, arg0: int, arg1: float) -> None"
  15. assert doc(KWClass.foo1) == "foo1(self: m.KWClass, x: int, y: float) -> None"
  16. def test_named_arguments(msg):
  17. assert kw_func0(5, 10) == "x=5, y=10"
  18. assert kw_func1(5, 10) == "x=5, y=10"
  19. assert kw_func1(5, y=10) == "x=5, y=10"
  20. assert kw_func1(y=10, x=5) == "x=5, y=10"
  21. assert kw_func2() == "x=100, y=200"
  22. assert kw_func2(5) == "x=5, y=200"
  23. assert kw_func2(x=5) == "x=5, y=200"
  24. assert kw_func2(y=10) == "x=100, y=10"
  25. assert kw_func2(5, 10) == "x=5, y=10"
  26. assert kw_func2(x=5, y=10) == "x=5, y=10"
  27. with pytest.raises(TypeError) as excinfo:
  28. # noinspection PyArgumentList
  29. kw_func2(x=5, y=10, z=12)
  30. assert excinfo.match(
  31. r'(?s)^kw_func2\(\): incompatible.*Invoked with: kwargs: ((x=5|y=10|z=12)(, |$))' + '{3}$')
  32. assert kw_func4() == "{13 17}"
  33. assert kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
  34. assert kw_func_udl(x=5, y=10) == "x=5, y=10"
  35. assert kw_func_udl_z(x=5) == "x=5, y=0"
  36. def test_arg_and_kwargs():
  37. args = 'arg1_value', 'arg2_value', 3
  38. assert args_function(*args) == args
  39. args = 'a1', 'a2'
  40. kwargs = dict(arg3='a3', arg4=4)
  41. assert args_kwargs_function(*args, **kwargs) == (args, kwargs)
  42. def test_mixed_args_and_kwargs(msg):
  43. from pybind11_tests import (mixed_plus_args, mixed_plus_kwargs, mixed_plus_args_kwargs,
  44. mixed_plus_args_kwargs_defaults)
  45. mpa = mixed_plus_args
  46. mpk = mixed_plus_kwargs
  47. mpak = mixed_plus_args_kwargs
  48. mpakd = mixed_plus_args_kwargs_defaults
  49. assert mpa(1, 2.5, 4, 99.5, None) == (1, 2.5, (4, 99.5, None))
  50. assert mpa(1, 2.5) == (1, 2.5, ())
  51. with pytest.raises(TypeError) as excinfo:
  52. assert mpa(1)
  53. assert msg(excinfo.value) == """
  54. mixed_plus_args(): incompatible function arguments. The following argument types are supported:
  55. 1. (arg0: int, arg1: float, *args) -> tuple
  56. Invoked with: 1
  57. """ # noqa: E501 line too long
  58. with pytest.raises(TypeError) as excinfo:
  59. assert mpa()
  60. assert msg(excinfo.value) == """
  61. mixed_plus_args(): incompatible function arguments. The following argument types are supported:
  62. 1. (arg0: int, arg1: float, *args) -> tuple
  63. Invoked with:
  64. """ # noqa: E501 line too long
  65. assert mpk(-2, 3.5, pi=3.14159, e=2.71828) == (-2, 3.5, {'e': 2.71828, 'pi': 3.14159})
  66. assert mpak(7, 7.7, 7.77, 7.777, 7.7777, minusseven=-7) == (
  67. 7, 7.7, (7.77, 7.777, 7.7777), {'minusseven': -7})
  68. assert mpakd() == (1, 3.14159, (), {})
  69. assert mpakd(3) == (3, 3.14159, (), {})
  70. assert mpakd(j=2.71828) == (1, 2.71828, (), {})
  71. assert mpakd(k=42) == (1, 3.14159, (), {'k': 42})
  72. assert mpakd(1, 1, 2, 3, 5, 8, then=13, followedby=21) == (
  73. 1, 1, (2, 3, 5, 8), {'then': 13, 'followedby': 21})
  74. # Arguments specified both positionally and via kwargs should fail:
  75. with pytest.raises(TypeError) as excinfo:
  76. assert mpakd(1, i=1)
  77. assert msg(excinfo.value) == """
  78. mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
  79. 1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
  80. Invoked with: 1; kwargs: i=1
  81. """ # noqa: E501 line too long
  82. with pytest.raises(TypeError) as excinfo:
  83. assert mpakd(1, 2, j=1)
  84. assert msg(excinfo.value) == """
  85. mixed_plus_args_kwargs_defaults(): incompatible function arguments. The following argument types are supported:
  86. 1. (i: int=1, j: float=3.14159, *args, **kwargs) -> tuple
  87. Invoked with: 1, 2; kwargs: j=1
  88. """ # noqa: E501 line too long