Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | |
| 3 | from pybind11_tests import stl as m |
| 4 | from pybind11_tests import UserType |
| 5 | |
| 6 | |
| 7 | def test_vector(doc): |
| 8 | """std::vector <-> list""" |
Jason Rhinelander | 5c7a290 | 2017-10-24 20:39:46 -0300 | [diff] [blame] | 9 | lst = m.cast_vector() |
| 10 | assert lst == [1] |
| 11 | lst.append(2) |
| 12 | assert m.load_vector(lst) |
| 13 | assert m.load_vector(tuple(lst)) |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 14 | |
Dean Moldovan | 3c4933c | 2017-09-01 21:42:20 +0200 | [diff] [blame] | 15 | assert m.cast_bool_vector() == [True, False] |
| 16 | assert m.load_bool_vector([True, False]) |
| 17 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 18 | assert doc(m.cast_vector) == "cast_vector() -> List[int]" |
| 19 | assert doc(m.load_vector) == "load_vector(arg0: List[int]) -> bool" |
| 20 | |
Jason Rhinelander | 67a0cc4 | 2017-07-06 20:41:52 -0400 | [diff] [blame] | 21 | # Test regression caused by 936: pointers to stl containers weren't castable |
| 22 | assert m.cast_ptr_vector() == ["lvalue", "lvalue"] |
| 23 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 24 | |
| 25 | def test_array(doc): |
| 26 | """std::array <-> list""" |
Jason Rhinelander | 5c7a290 | 2017-10-24 20:39:46 -0300 | [diff] [blame] | 27 | lst = m.cast_array() |
| 28 | assert lst == [1, 2] |
| 29 | assert m.load_array(lst) |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 30 | |
| 31 | assert doc(m.cast_array) == "cast_array() -> List[int[2]]" |
| 32 | assert doc(m.load_array) == "load_array(arg0: List[int[2]]) -> bool" |
| 33 | |
| 34 | |
| 35 | def test_valarray(doc): |
| 36 | """std::valarray <-> list""" |
Jason Rhinelander | 5c7a290 | 2017-10-24 20:39:46 -0300 | [diff] [blame] | 37 | lst = m.cast_valarray() |
| 38 | assert lst == [1, 4, 9] |
| 39 | assert m.load_valarray(lst) |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 40 | |
| 41 | assert doc(m.cast_valarray) == "cast_valarray() -> List[int]" |
| 42 | assert doc(m.load_valarray) == "load_valarray(arg0: List[int]) -> bool" |
| 43 | |
| 44 | |
| 45 | def test_map(doc): |
| 46 | """std::map <-> dict""" |
| 47 | d = m.cast_map() |
| 48 | assert d == {"key": "value"} |
| 49 | d["key2"] = "value2" |
| 50 | assert m.load_map(d) |
| 51 | |
| 52 | assert doc(m.cast_map) == "cast_map() -> Dict[str, str]" |
| 53 | assert doc(m.load_map) == "load_map(arg0: Dict[str, str]) -> bool" |
| 54 | |
| 55 | |
| 56 | def test_set(doc): |
| 57 | """std::set <-> set""" |
| 58 | s = m.cast_set() |
| 59 | assert s == {"key1", "key2"} |
| 60 | s.add("key3") |
| 61 | assert m.load_set(s) |
| 62 | |
| 63 | assert doc(m.cast_set) == "cast_set() -> Set[str]" |
| 64 | assert doc(m.load_set) == "load_set(arg0: Set[str]) -> bool" |
| 65 | |
| 66 | |
Jason Rhinelander | b57281b | 2017-07-03 19:12:09 -0400 | [diff] [blame] | 67 | def test_recursive_casting(): |
| 68 | """Tests that stl casters preserve lvalue/rvalue context for container values""" |
| 69 | assert m.cast_rv_vector() == ["rvalue", "rvalue"] |
| 70 | assert m.cast_lv_vector() == ["lvalue", "lvalue"] |
| 71 | assert m.cast_rv_array() == ["rvalue", "rvalue", "rvalue"] |
| 72 | assert m.cast_lv_array() == ["lvalue", "lvalue"] |
| 73 | assert m.cast_rv_map() == {"a": "rvalue"} |
| 74 | assert m.cast_lv_map() == {"a": "lvalue", "b": "lvalue"} |
| 75 | assert m.cast_rv_nested() == [[[{"b": "rvalue", "c": "rvalue"}], [{"a": "rvalue"}]]] |
| 76 | assert m.cast_lv_nested() == { |
| 77 | "a": [[["lvalue", "lvalue"]], [["lvalue", "lvalue"]]], |
| 78 | "b": [[["lvalue", "lvalue"], ["lvalue", "lvalue"]]] |
| 79 | } |
| 80 | |
| 81 | # Issue #853 test case: |
| 82 | z = m.cast_unique_ptr_vector() |
| 83 | assert z[0].value == 7 and z[1].value == 42 |
| 84 | |
| 85 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 86 | def test_move_out_container(): |
| 87 | """Properties use the `reference_internal` policy by default. If the underlying function |
| 88 | returns an rvalue, the policy is automatically changed to `move` to avoid referencing |
| 89 | a temporary. In case the return value is a container of user-defined types, the policy |
| 90 | also needs to be applied to the elements, not just the container.""" |
| 91 | c = m.MoveOutContainer() |
| 92 | moved_out_list = c.move_list |
| 93 | assert [x.value for x in moved_out_list] == [0, 1, 2] |
| 94 | |
| 95 | |
| 96 | @pytest.mark.skipif(not hasattr(m, "has_optional"), reason='no <optional>') |
| 97 | def test_optional(): |
| 98 | assert m.double_or_zero(None) == 0 |
| 99 | assert m.double_or_zero(42) == 84 |
| 100 | pytest.raises(TypeError, m.double_or_zero, 'foo') |
| 101 | |
| 102 | assert m.half_or_none(0) is None |
| 103 | assert m.half_or_none(42) == 21 |
| 104 | pytest.raises(TypeError, m.half_or_none, 'foo') |
| 105 | |
| 106 | assert m.test_nullopt() == 42 |
| 107 | assert m.test_nullopt(None) == 42 |
| 108 | assert m.test_nullopt(42) == 42 |
| 109 | assert m.test_nullopt(43) == 43 |
| 110 | |
| 111 | assert m.test_no_assign() == 42 |
| 112 | assert m.test_no_assign(None) == 42 |
| 113 | assert m.test_no_assign(m.NoAssign(43)) == 43 |
| 114 | pytest.raises(TypeError, m.test_no_assign, 43) |
| 115 | |
| 116 | assert m.nodefer_none_optional(None) |
| 117 | |
| 118 | |
| 119 | @pytest.mark.skipif(not hasattr(m, "has_exp_optional"), reason='no <experimental/optional>') |
| 120 | def test_exp_optional(): |
| 121 | assert m.double_or_zero_exp(None) == 0 |
| 122 | assert m.double_or_zero_exp(42) == 84 |
| 123 | pytest.raises(TypeError, m.double_or_zero_exp, 'foo') |
| 124 | |
| 125 | assert m.half_or_none_exp(0) is None |
| 126 | assert m.half_or_none_exp(42) == 21 |
| 127 | pytest.raises(TypeError, m.half_or_none_exp, 'foo') |
| 128 | |
| 129 | assert m.test_nullopt_exp() == 42 |
| 130 | assert m.test_nullopt_exp(None) == 42 |
| 131 | assert m.test_nullopt_exp(42) == 42 |
| 132 | assert m.test_nullopt_exp(43) == 43 |
| 133 | |
| 134 | assert m.test_no_assign_exp() == 42 |
| 135 | assert m.test_no_assign_exp(None) == 42 |
| 136 | assert m.test_no_assign_exp(m.NoAssign(43)) == 43 |
| 137 | pytest.raises(TypeError, m.test_no_assign_exp, 43) |
| 138 | |
| 139 | |
| 140 | @pytest.mark.skipif(not hasattr(m, "load_variant"), reason='no <variant>') |
| 141 | def test_variant(doc): |
| 142 | assert m.load_variant(1) == "int" |
| 143 | assert m.load_variant("1") == "std::string" |
| 144 | assert m.load_variant(1.0) == "double" |
| 145 | assert m.load_variant(None) == "std::nullptr_t" |
| 146 | |
| 147 | assert m.load_variant_2pass(1) == "int" |
| 148 | assert m.load_variant_2pass(1.0) == "double" |
| 149 | |
| 150 | assert m.cast_variant() == (5, "Hello") |
| 151 | |
| 152 | assert doc(m.load_variant) == "load_variant(arg0: Union[int, str, float, None]) -> str" |
| 153 | |
| 154 | |
| 155 | def test_vec_of_reference_wrapper(): |
| 156 | """#171: Can't return reference wrappers (or STL structures containing them)""" |
| 157 | assert str(m.return_vec_of_reference_wrapper(UserType(4))) == \ |
| 158 | "[UserType(1), UserType(2), UserType(3), UserType(4)]" |
Andreas Bergmeier | 34b7b54 | 2017-05-09 15:01:22 +0200 | [diff] [blame] | 159 | |
| 160 | |
| 161 | def test_stl_pass_by_pointer(msg): |
| 162 | """Passing nullptr or None to an STL container pointer is not expected to work""" |
| 163 | with pytest.raises(TypeError) as excinfo: |
| 164 | m.stl_pass_by_pointer() # default value is `nullptr` |
| 165 | assert msg(excinfo.value) == """ |
| 166 | stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: |
Antony Lee | 0826b3c | 2017-11-26 20:00:35 -0800 | [diff] [blame] | 167 | 1. (v: List[int] = None) -> List[int] |
Andreas Bergmeier | 34b7b54 | 2017-05-09 15:01:22 +0200 | [diff] [blame] | 168 | |
| 169 | Invoked with: |
| 170 | """ # noqa: E501 line too long |
| 171 | |
| 172 | with pytest.raises(TypeError) as excinfo: |
| 173 | m.stl_pass_by_pointer(None) |
| 174 | assert msg(excinfo.value) == """ |
| 175 | stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: |
Antony Lee | 0826b3c | 2017-11-26 20:00:35 -0800 | [diff] [blame] | 176 | 1. (v: List[int] = None) -> List[int] |
Andreas Bergmeier | 34b7b54 | 2017-05-09 15:01:22 +0200 | [diff] [blame] | 177 | |
| 178 | Invoked with: None |
| 179 | """ # noqa: E501 line too long |
| 180 | |
| 181 | assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3] |
Dean Moldovan | 2b4477e | 2017-09-09 20:21:34 +0200 | [diff] [blame] | 182 | |
| 183 | |
| 184 | def test_missing_header_message(): |
| 185 | """Trying convert `list` to a `std::vector`, or vice versa, without including |
| 186 | <pybind11/stl.h> should result in a helpful suggestion in the error message""" |
| 187 | import pybind11_cross_module_tests as cm |
| 188 | |
| 189 | expected_message = ("Did you forget to `#include <pybind11/stl.h>`? Or <pybind11/complex.h>,\n" |
| 190 | "<pybind11/functional.h>, <pybind11/chrono.h>, etc. Some automatic\n" |
| 191 | "conversions are optional and require extra headers to be included\n" |
| 192 | "when compiling your pybind11 module.") |
| 193 | |
| 194 | with pytest.raises(TypeError) as excinfo: |
| 195 | cm.missing_header_arg([1.0, 2.0, 3.0]) |
| 196 | assert expected_message in str(excinfo.value) |
| 197 | |
| 198 | with pytest.raises(TypeError) as excinfo: |
| 199 | cm.missing_header_return() |
| 200 | assert expected_message in str(excinfo.value) |