blob: 5ed7d09290a7c8d98b48faa1720020f4a8e31a44 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2import gc
Wenzel Jakobbd560ac2016-11-03 11:53:35 +01003from pybind11_tests import ConstructorStats
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02004
5
Dean Moldovan665e8802016-08-12 22:28:31 +02006def test_regressions():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02007 from pybind11_tests.issues import print_cchar, print_char
8
Dean Moldovan665e8802016-08-12 22:28:31 +02009 # #137: const char* isn't handled properly
10 assert print_cchar("const char *") == "const char *"
11 # #150: char bindings broken
12 assert print_char("c") == "c"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020013
14
Dean Moldovan665e8802016-08-12 22:28:31 +020015def test_dispatch_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020016 """#159: virtual function dispatch has problems with similar-named functions"""
17 from pybind11_tests.issues import DispatchIssue, dispatch_issue_go
18
19 class PyClass1(DispatchIssue):
20 def dispatch(self):
Dean Moldovan665e8802016-08-12 22:28:31 +020021 return "Yay.."
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020022
23 class PyClass2(DispatchIssue):
24 def dispatch(self):
25 with pytest.raises(RuntimeError) as excinfo:
26 super(PyClass2, self).dispatch()
27 assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'
28
29 p = PyClass1()
Dean Moldovan665e8802016-08-12 22:28:31 +020030 return dispatch_issue_go(p)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020031
32 b = PyClass2()
Dean Moldovan665e8802016-08-12 22:28:31 +020033 assert dispatch_issue_go(b) == "Yay.."
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020034
35
36def test_reference_wrapper():
37 """#171: Can't return reference wrappers (or STL data structures containing them)"""
38 from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper
39
40 assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
41 "[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"
42
43
44def test_iterator_passthrough():
45 """#181: iterator passthrough did not compile"""
46 from pybind11_tests.issues import iterator_passthrough
47
48 assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
49
50
51def test_shared_ptr_gc():
52 """// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
53 from pybind11_tests.issues import ElementList, ElementA
54
55 el = ElementList()
56 for i in range(10):
57 el.add(ElementA(i))
58 gc.collect()
59 for i, v in enumerate(el.get()):
60 assert i == v.value()
61
62
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +010063def test_no_id(msg):
Dean Moldovan99dbdc12016-08-19 13:45:36 +020064 from pybind11_tests.issues import get_element, expect_float, expect_int
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020065
66 with pytest.raises(TypeError) as excinfo:
Dean Moldovan99dbdc12016-08-19 13:45:36 +020067 get_element(None)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020068 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090069 get_element(): incompatible function arguments. The following argument types are supported:
Dean Moldovan99dbdc12016-08-19 13:45:36 +020070 1. (arg0: m.issues.ElementA) -> int
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090071
72 Invoked with: None
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020073 """
74
75 with pytest.raises(TypeError) as excinfo:
76 expect_int(5.2)
77 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090078 expect_int(): incompatible function arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020079 1. (arg0: int) -> int
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090080
81 Invoked with: 5.2
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020082 """
83 assert expect_float(12) == 12
84
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020085
Dean Moldovan99dbdc12016-08-19 13:45:36 +020086def test_str_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020087 """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
88 from pybind11_tests.issues import StrIssue
89
Dean Moldovan99dbdc12016-08-19 13:45:36 +020090 assert str(StrIssue(3)) == "StrIssue[3]"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020091
92 with pytest.raises(TypeError) as excinfo:
93 str(StrIssue("no", "such", "constructor"))
94 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090095 __init__(): incompatible constructor arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020096 1. m.issues.StrIssue(arg0: int)
97 2. m.issues.StrIssue()
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090098
99 Invoked with: no, such, constructor
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200100 """
101
102
Dean Moldovan665e8802016-08-12 22:28:31 +0200103def test_nested():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200104 """ #328: first member in a class can't be used in operators"""
Dean Moldovan665e8802016-08-12 22:28:31 +0200105 from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200106
107 a = NestA()
108 b = NestB()
109 c = NestC()
110
111 a += 10
Dean Moldovan665e8802016-08-12 22:28:31 +0200112 assert get_NestA(a) == 13
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200113 b.a += 100
Dean Moldovan665e8802016-08-12 22:28:31 +0200114 assert get_NestA(b.a) == 103
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200115 c.b.a += 1000
Dean Moldovan665e8802016-08-12 22:28:31 +0200116 assert get_NestA(c.b.a) == 1003
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200117 b -= 1
Dean Moldovan665e8802016-08-12 22:28:31 +0200118 assert get_NestB(b) == 3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200119 c.b -= 3
Dean Moldovan665e8802016-08-12 22:28:31 +0200120 assert get_NestB(c.b) == 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200121 c *= 7
Dean Moldovan665e8802016-08-12 22:28:31 +0200122 assert get_NestC(c) == 35
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200123
124 abase = a.as_base()
125 assert abase.value == -2
126 a.as_base().value += 44
127 assert abase.value == 42
128 assert c.b.a.as_base().value == -2
129 c.b.a.as_base().value += 44
130 assert c.b.a.as_base().value == 42
131
132 del c
133 gc.collect()
134 del a # Should't delete while abase is still alive
135 gc.collect()
136
137 assert abase.value == 42
138 del abase, b
139 gc.collect()
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900140
141
142def test_move_fallback():
143 from pybind11_tests.issues import get_moveissue1, get_moveissue2
144 m2 = get_moveissue2(2)
145 assert m2.value == 2
146 m1 = get_moveissue1(1)
147 assert m1.value == 1
Jason Rhinelander56f71772016-09-07 13:32:49 -0400148
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100149
Jason Rhinelander56f71772016-09-07 13:32:49 -0400150def test_override_ref():
151 from pybind11_tests.issues import OverrideTest
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400152 o = OverrideTest("asdf")
Jason Rhinelander56f71772016-09-07 13:32:49 -0400153
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400154 # Not allowed (see associated .cpp comment)
Dean Moldovanbad17402016-11-20 21:21:54 +0100155 # i = o.str_ref()
156 # assert o.str_ref() == "asdf"
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400157 assert o.str_value() == "asdf"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400158
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400159 assert o.A_value().value == "hi"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400160 a = o.A_ref()
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400161 assert a.value == "hi"
162 a.value = "bye"
163 assert a.value == "bye"
Wenzel Jakob382484a2016-09-10 15:28:37 +0900164
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100165
Wenzel Jakob382484a2016-09-10 15:28:37 +0900166def test_operators_notimplemented(capture):
167 from pybind11_tests.issues import OpTest1, OpTest2
168 with capture:
Dean Moldovanbad17402016-11-20 21:21:54 +0100169 c1, c2 = OpTest1(), OpTest2()
170 c1 + c1
171 c2 + c2
172 c2 + c1
173 c1 + c2
174 assert capture == """
175 Add OpTest1 with OpTest1
176 Add OpTest2 with OpTest2
177 Add OpTest2 with OpTest1
178 Add OpTest2 with OpTest1
179 """
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900180
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100181
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900182def test_iterator_rvpolicy():
183 """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
184 from pybind11_tests.issues import make_iterator_1
185 from pybind11_tests.issues import make_iterator_2
186
187 assert list(make_iterator_1()) == [1, 2, 3]
188 assert list(make_iterator_2()) == [1, 2, 3]
Dean Moldovanbad17402016-11-20 21:21:54 +0100189 assert not isinstance(make_iterator_1(), type(make_iterator_2()))
Jason Rhinelander6873c202016-10-24 21:58:22 -0400190
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100191
Jason Rhinelander6873c202016-10-24 21:58:22 -0400192def test_dupe_assignment():
193 """ Issue 461: overwriting a class with a function """
194 from pybind11_tests.issues import dupe_exception_failures
195 assert dupe_exception_failures() == []
Wenzel Jakobbd560ac2016-11-03 11:53:35 +0100196
197
198def test_enable_shared_from_this_with_reference_rvp():
199 """ Issue #471: shared pointer instance not dellocated """
200 from pybind11_tests import SharedParent, SharedChild
201
202 parent = SharedParent()
203 child = parent.get_child()
204
205 cstats = ConstructorStats.get(SharedChild)
206 assert cstats.alive() == 1
207 del child, parent
208 assert cstats.alive() == 0
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500209
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100210
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500211def test_non_destructed_holders():
212 """ Issue #478: unique ptrs constructed and freed without destruction """
213 from pybind11_tests import SpecialHolderObj
214
215 a = SpecialHolderObj(123)
216 b = a.child()
217
218 assert a.val == 123
219 assert b.val == 124
220
221 cstats = SpecialHolderObj.holder_cstats()
222 assert cstats.alive() == 1
223 del b
224 assert cstats.alive() == 1
225 del a
226 assert cstats.alive() == 0
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100227
228
229def test_complex_cast(capture):
230 """ Issue #484: number conversion generates unhandled exceptions """
231 from pybind11_tests.issues import test_complex
232
233 with capture:
234 test_complex(1)
235 test_complex(2j)
236
237 assert capture == """
Dean Moldovanbad17402016-11-20 21:21:54 +0100238 1.0
239 (0.0, 2.0)
240 """
Wenzel Jakob7c2461e2016-11-20 05:26:02 +0100241
242
243def test_inheritance_override_def_static():
244 from pybind11_tests.issues import MyBase, MyDerived
245
246 b = MyBase.make()
247 d1 = MyDerived.make2()
248 d2 = MyDerived.make()
249
250 assert isinstance(b, MyBase)
251 assert isinstance(d1, MyDerived)
252 assert isinstance(d2, MyDerived)