blob: fdbca0dafc200fdf94f70f506bec2109b7b2448f [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
61def test_move_out_container():
62 """Properties use the `reference_internal` policy by default. If the underlying function
63 returns an rvalue, the policy is automatically changed to `move` to avoid referencing
64 a temporary. In case the return value is a container of user-defined types, the policy
65 also needs to be applied to the elements, not just the container."""
66 c = m.MoveOutContainer()
67 moved_out_list = c.move_list
68 assert [x.value for x in moved_out_list] == [0, 1, 2]
69
70
71@pytest.mark.skipif(not hasattr(m, "has_optional"), reason='no <optional>')
72def test_optional():
73 assert m.double_or_zero(None) == 0
74 assert m.double_or_zero(42) == 84
75 pytest.raises(TypeError, m.double_or_zero, 'foo')
76
77 assert m.half_or_none(0) is None
78 assert m.half_or_none(42) == 21
79 pytest.raises(TypeError, m.half_or_none, 'foo')
80
81 assert m.test_nullopt() == 42
82 assert m.test_nullopt(None) == 42
83 assert m.test_nullopt(42) == 42
84 assert m.test_nullopt(43) == 43
85
86 assert m.test_no_assign() == 42
87 assert m.test_no_assign(None) == 42
88 assert m.test_no_assign(m.NoAssign(43)) == 43
89 pytest.raises(TypeError, m.test_no_assign, 43)
90
91 assert m.nodefer_none_optional(None)
92
93
94@pytest.mark.skipif(not hasattr(m, "has_exp_optional"), reason='no <experimental/optional>')
95def test_exp_optional():
96 assert m.double_or_zero_exp(None) == 0
97 assert m.double_or_zero_exp(42) == 84
98 pytest.raises(TypeError, m.double_or_zero_exp, 'foo')
99
100 assert m.half_or_none_exp(0) is None
101 assert m.half_or_none_exp(42) == 21
102 pytest.raises(TypeError, m.half_or_none_exp, 'foo')
103
104 assert m.test_nullopt_exp() == 42
105 assert m.test_nullopt_exp(None) == 42
106 assert m.test_nullopt_exp(42) == 42
107 assert m.test_nullopt_exp(43) == 43
108
109 assert m.test_no_assign_exp() == 42
110 assert m.test_no_assign_exp(None) == 42
111 assert m.test_no_assign_exp(m.NoAssign(43)) == 43
112 pytest.raises(TypeError, m.test_no_assign_exp, 43)
113
114
115@pytest.mark.skipif(not hasattr(m, "load_variant"), reason='no <variant>')
116def test_variant(doc):
117 assert m.load_variant(1) == "int"
118 assert m.load_variant("1") == "std::string"
119 assert m.load_variant(1.0) == "double"
120 assert m.load_variant(None) == "std::nullptr_t"
121
122 assert m.load_variant_2pass(1) == "int"
123 assert m.load_variant_2pass(1.0) == "double"
124
125 assert m.cast_variant() == (5, "Hello")
126
127 assert doc(m.load_variant) == "load_variant(arg0: Union[int, str, float, None]) -> str"
128
129
130def test_vec_of_reference_wrapper():
131 """#171: Can't return reference wrappers (or STL structures containing them)"""
132 assert str(m.return_vec_of_reference_wrapper(UserType(4))) == \
133 "[UserType(1), UserType(2), UserType(3), UserType(4)]"
Andreas Bergmeier34b7b542017-05-09 15:01:22 +0200134
135
136def test_stl_pass_by_pointer(msg):
137 """Passing nullptr or None to an STL container pointer is not expected to work"""
138 with pytest.raises(TypeError) as excinfo:
139 m.stl_pass_by_pointer() # default value is `nullptr`
140 assert msg(excinfo.value) == """
141 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
142 1. (v: List[int]=None) -> List[int]
143
144 Invoked with:
145 """ # noqa: E501 line too long
146
147 with pytest.raises(TypeError) as excinfo:
148 m.stl_pass_by_pointer(None)
149 assert msg(excinfo.value) == """
150 stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
151 1. (v: List[int]=None) -> List[int]
152
153 Invoked with: None
154 """ # noqa: E501 line too long
155
156 assert m.stl_pass_by_pointer([1, 2, 3]) == [1, 2, 3]