blob: c2fa7db1559546d3bfcb724db9338f07600a8e65 [file] [log] [blame]
Dean Moldovan83e328f2017-06-09 00:44:49 +02001import pytest
2
3from pybind11_tests import stl as m
4from pybind11_tests import UserType
5
6
7def test_vector(doc):
8 """std::vector <-> list"""
9 l = m.cast_vector()
10 assert l == [1]
11 l.append(2)
12 assert m.load_vector(l)
13 assert m.load_vector(tuple(l))
14
15 assert doc(m.cast_vector) == "cast_vector() -> List[int]"
16 assert doc(m.load_vector) == "load_vector(arg0: List[int]) -> bool"
17
Jason Rhinelander67a0cc42017-07-06 20:41:52 -040018 # Test regression caused by 936: pointers to stl containers weren't castable
19 assert m.cast_ptr_vector() == ["lvalue", "lvalue"]
20
Dean Moldovan83e328f2017-06-09 00:44:49 +020021
22def test_array(doc):
23 """std::array <-> list"""
24 l = m.cast_array()
25 assert l == [1, 2]
26 assert m.load_array(l)
27
28 assert doc(m.cast_array) == "cast_array() -> List[int[2]]"
29 assert doc(m.load_array) == "load_array(arg0: List[int[2]]) -> bool"
30
31
32def test_valarray(doc):
33 """std::valarray <-> list"""
34 l = m.cast_valarray()
35 assert l == [1, 4, 9]
36 assert m.load_valarray(l)
37
38 assert doc(m.cast_valarray) == "cast_valarray() -> List[int]"
39 assert doc(m.load_valarray) == "load_valarray(arg0: List[int]) -> bool"
40
41
42def test_map(doc):
43 """std::map <-> dict"""
44 d = m.cast_map()
45 assert d == {"key": "value"}
46 d["key2"] = "value2"
47 assert m.load_map(d)
48
49 assert doc(m.cast_map) == "cast_map() -> Dict[str, str]"
50 assert doc(m.load_map) == "load_map(arg0: Dict[str, str]) -> bool"
51
52
53def test_set(doc):
54 """std::set <-> set"""
55 s = m.cast_set()
56 assert s == {"key1", "key2"}
57 s.add("key3")
58 assert m.load_set(s)
59
60 assert doc(m.cast_set) == "cast_set() -> Set[str]"
61 assert doc(m.load_set) == "load_set(arg0: Set[str]) -> bool"
62
63
Jason Rhinelanderb57281b2017-07-03 19:12:09 -040064def test_recursive_casting():
65 """Tests that stl casters preserve lvalue/rvalue context for container values"""
66 assert m.cast_rv_vector() == ["rvalue", "rvalue"]
67 assert m.cast_lv_vector() == ["lvalue", "lvalue"]
68 assert m.cast_rv_array() == ["rvalue", "rvalue", "rvalue"]
69 assert m.cast_lv_array() == ["lvalue", "lvalue"]
70 assert m.cast_rv_map() == {"a": "rvalue"}
71 assert m.cast_lv_map() == {"a": "lvalue", "b": "lvalue"}
72 assert m.cast_rv_nested() == [[[{"b": "rvalue", "c": "rvalue"}], [{"a": "rvalue"}]]]
73 assert m.cast_lv_nested() == {
74 "a": [[["lvalue", "lvalue"]], [["lvalue", "lvalue"]]],
75 "b": [[["lvalue", "lvalue"], ["lvalue", "lvalue"]]]
76 }
77
78 # Issue #853 test case:
79 z = m.cast_unique_ptr_vector()
80 assert z[0].value == 7 and z[1].value == 42
81
82
Dean Moldovan83e328f2017-06-09 00:44:49 +020083def test_move_out_container():
84 """Properties use the `reference_internal` policy by default. If the underlying function
85 returns an rvalue, the policy is automatically changed to `move` to avoid referencing
86 a temporary. In case the return value is a container of user-defined types, the policy
87 also needs to be applied to the elements, not just the container."""
88 c = m.MoveOutContainer()
89 moved_out_list = c.move_list
90 assert [x.value for x in moved_out_list] == [0, 1, 2]
91
92
93@pytest.mark.skipif(not hasattr(m, "has_optional"), reason='no <optional>')
94def test_optional():
95 assert m.double_or_zero(None) == 0
96 assert m.double_or_zero(42) == 84
97 pytest.raises(TypeError, m.double_or_zero, 'foo')
98
99 assert m.half_or_none(0) is None
100 assert m.half_or_none(42) == 21
101 pytest.raises(TypeError, m.half_or_none, 'foo')
102
103 assert m.test_nullopt() == 42
104 assert m.test_nullopt(None) == 42
105 assert m.test_nullopt(42) == 42
106 assert m.test_nullopt(43) == 43
107
108 assert m.test_no_assign() == 42
109 assert m.test_no_assign(None) == 42
110 assert m.test_no_assign(m.NoAssign(43)) == 43
111 pytest.raises(TypeError, m.test_no_assign, 43)
112
113 assert m.nodefer_none_optional(None)
114
115
116@pytest.mark.skipif(not hasattr(m, "has_exp_optional"), reason='no <experimental/optional>')
117def test_exp_optional():
118 assert m.double_or_zero_exp(None) == 0
119 assert m.double_or_zero_exp(42) == 84
120 pytest.raises(TypeError, m.double_or_zero_exp, 'foo')
121
122 assert m.half_or_none_exp(0) is None
123 assert m.half_or_none_exp(42) == 21
124 pytest.raises(TypeError, m.half_or_none_exp, 'foo')
125
126 assert m.test_nullopt_exp() == 42
127 assert m.test_nullopt_exp(None) == 42
128 assert m.test_nullopt_exp(42) == 42
129 assert m.test_nullopt_exp(43) == 43
130
131 assert m.test_no_assign_exp() == 42
132 assert m.test_no_assign_exp(None) == 42
133 assert m.test_no_assign_exp(m.NoAssign(43)) == 43
134 pytest.raises(TypeError, m.test_no_assign_exp, 43)
135
136
137@pytest.mark.skipif(not hasattr(m, "load_variant"), reason='no <variant>')
138def test_variant(doc):
139 assert m.load_variant(1) == "int"
140 assert m.load_variant("1") == "std::string"
141 assert m.load_variant(1.0) == "double"
142 assert m.load_variant(None) == "std::nullptr_t"
143
144 assert m.load_variant_2pass(1) == "int"
145 assert m.load_variant_2pass(1.0) == "double"
146
147 assert m.cast_variant() == (5, "Hello")
148
149 assert doc(m.load_variant) == "load_variant(arg0: Union[int, str, float, None]) -> str"
150
151
152def test_vec_of_reference_wrapper():
153 """#171: Can't return reference wrappers (or STL structures containing them)"""
154 assert str(m.return_vec_of_reference_wrapper(UserType(4))) == \
155 "[UserType(1), UserType(2), UserType(3), UserType(4)]"
Andreas Bergmeier34b7b542017-05-09 15:01:22 +0200156
157
158def test_stl_pass_by_pointer(msg):
159 """Passing nullptr or None to an STL container pointer is not expected to work"""
160 with pytest.raises(TypeError) as excinfo:
161 m.stl_pass_by_pointer() # default value is `nullptr`
162 assert msg(excinfo.value) == """
163 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
164 1. (v: List[int]=None) -> List[int]
165
166 Invoked with:
167 """ # noqa: E501 line too long
168
169 with pytest.raises(TypeError) as excinfo:
170 m.stl_pass_by_pointer(None)
171 assert msg(excinfo.value) == """
172 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
173 1. (v: List[int]=None) -> List[int]
174
175 Invoked with: None
176 """ # noqa: E501 line too long
177
178 assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]