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.

156 lines
5.0 KiB

  1. import pytest
  2. from pybind11_tests import stl as m
  3. from pybind11_tests import UserType
  4. def test_vector(doc):
  5. """std::vector <-> list"""
  6. l = m.cast_vector()
  7. assert l == [1]
  8. l.append(2)
  9. assert m.load_vector(l)
  10. assert m.load_vector(tuple(l))
  11. assert doc(m.cast_vector) == "cast_vector() -> List[int]"
  12. assert doc(m.load_vector) == "load_vector(arg0: List[int]) -> bool"
  13. def test_array(doc):
  14. """std::array <-> list"""
  15. l = m.cast_array()
  16. assert l == [1, 2]
  17. assert m.load_array(l)
  18. assert doc(m.cast_array) == "cast_array() -> List[int[2]]"
  19. assert doc(m.load_array) == "load_array(arg0: List[int[2]]) -> bool"
  20. def test_valarray(doc):
  21. """std::valarray <-> list"""
  22. l = m.cast_valarray()
  23. assert l == [1, 4, 9]
  24. assert m.load_valarray(l)
  25. assert doc(m.cast_valarray) == "cast_valarray() -> List[int]"
  26. assert doc(m.load_valarray) == "load_valarray(arg0: List[int]) -> bool"
  27. def test_map(doc):
  28. """std::map <-> dict"""
  29. d = m.cast_map()
  30. assert d == {"key": "value"}
  31. d["key2"] = "value2"
  32. assert m.load_map(d)
  33. assert doc(m.cast_map) == "cast_map() -> Dict[str, str]"
  34. assert doc(m.load_map) == "load_map(arg0: Dict[str, str]) -> bool"
  35. def test_set(doc):
  36. """std::set <-> set"""
  37. s = m.cast_set()
  38. assert s == {"key1", "key2"}
  39. s.add("key3")
  40. assert m.load_set(s)
  41. assert doc(m.cast_set) == "cast_set() -> Set[str]"
  42. assert doc(m.load_set) == "load_set(arg0: Set[str]) -> bool"
  43. def test_move_out_container():
  44. """Properties use the `reference_internal` policy by default. If the underlying function
  45. returns an rvalue, the policy is automatically changed to `move` to avoid referencing
  46. a temporary. In case the return value is a container of user-defined types, the policy
  47. also needs to be applied to the elements, not just the container."""
  48. c = m.MoveOutContainer()
  49. moved_out_list = c.move_list
  50. assert [x.value for x in moved_out_list] == [0, 1, 2]
  51. @pytest.mark.skipif(not hasattr(m, "has_optional"), reason='no <optional>')
  52. def test_optional():
  53. assert m.double_or_zero(None) == 0
  54. assert m.double_or_zero(42) == 84
  55. pytest.raises(TypeError, m.double_or_zero, 'foo')
  56. assert m.half_or_none(0) is None
  57. assert m.half_or_none(42) == 21
  58. pytest.raises(TypeError, m.half_or_none, 'foo')
  59. assert m.test_nullopt() == 42
  60. assert m.test_nullopt(None) == 42
  61. assert m.test_nullopt(42) == 42
  62. assert m.test_nullopt(43) == 43
  63. assert m.test_no_assign() == 42
  64. assert m.test_no_assign(None) == 42
  65. assert m.test_no_assign(m.NoAssign(43)) == 43
  66. pytest.raises(TypeError, m.test_no_assign, 43)
  67. assert m.nodefer_none_optional(None)
  68. @pytest.mark.skipif(not hasattr(m, "has_exp_optional"), reason='no <experimental/optional>')
  69. def test_exp_optional():
  70. assert m.double_or_zero_exp(None) == 0
  71. assert m.double_or_zero_exp(42) == 84
  72. pytest.raises(TypeError, m.double_or_zero_exp, 'foo')
  73. assert m.half_or_none_exp(0) is None
  74. assert m.half_or_none_exp(42) == 21
  75. pytest.raises(TypeError, m.half_or_none_exp, 'foo')
  76. assert m.test_nullopt_exp() == 42
  77. assert m.test_nullopt_exp(None) == 42
  78. assert m.test_nullopt_exp(42) == 42
  79. assert m.test_nullopt_exp(43) == 43
  80. assert m.test_no_assign_exp() == 42
  81. assert m.test_no_assign_exp(None) == 42
  82. assert m.test_no_assign_exp(m.NoAssign(43)) == 43
  83. pytest.raises(TypeError, m.test_no_assign_exp, 43)
  84. @pytest.mark.skipif(not hasattr(m, "load_variant"), reason='no <variant>')
  85. def test_variant(doc):
  86. assert m.load_variant(1) == "int"
  87. assert m.load_variant("1") == "std::string"
  88. assert m.load_variant(1.0) == "double"
  89. assert m.load_variant(None) == "std::nullptr_t"
  90. assert m.load_variant_2pass(1) == "int"
  91. assert m.load_variant_2pass(1.0) == "double"
  92. assert m.cast_variant() == (5, "Hello")
  93. assert doc(m.load_variant) == "load_variant(arg0: Union[int, str, float, None]) -> str"
  94. def test_vec_of_reference_wrapper():
  95. """#171: Can't return reference wrappers (or STL structures containing them)"""
  96. assert str(m.return_vec_of_reference_wrapper(UserType(4))) == \
  97. "[UserType(1), UserType(2), UserType(3), UserType(4)]"
  98. def test_stl_pass_by_pointer(msg):
  99. """Passing nullptr or None to an STL container pointer is not expected to work"""
  100. with pytest.raises(TypeError) as excinfo:
  101. m.stl_pass_by_pointer() # default value is `nullptr`
  102. assert msg(excinfo.value) == """
  103. stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
  104. 1. (v: List[int]=None) -> List[int]
  105. Invoked with:
  106. """ # noqa: E501 line too long
  107. with pytest.raises(TypeError) as excinfo:
  108. m.stl_pass_by_pointer(None)
  109. assert msg(excinfo.value) == """
  110. stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
  111. 1. (v: List[int]=None) -> List[int]
  112. Invoked with: None
  113. """ # noqa: E501 line too long
  114. assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]