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