blob: e60b5ca907e44b86317b89961805615553b704e3 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
Wenzel Jakobbd560ac2016-11-03 11:53:35 +01002from pybind11_tests import ConstructorStats
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02003
4
Dean Moldovan665e8802016-08-12 22:28:31 +02005def test_regressions():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02006 from pybind11_tests.issues import print_cchar, print_char
7
Dean Moldovan665e8802016-08-12 22:28:31 +02008 # #137: const char* isn't handled properly
9 assert print_cchar("const char *") == "const char *"
10 # #150: char bindings broken
11 assert print_char("c") == "c"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020012
13
Dean Moldovan665e8802016-08-12 22:28:31 +020014def test_dispatch_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020015 """#159: virtual function dispatch has problems with similar-named functions"""
16 from pybind11_tests.issues import DispatchIssue, dispatch_issue_go
17
18 class PyClass1(DispatchIssue):
19 def dispatch(self):
Dean Moldovan665e8802016-08-12 22:28:31 +020020 return "Yay.."
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020021
22 class PyClass2(DispatchIssue):
23 def dispatch(self):
24 with pytest.raises(RuntimeError) as excinfo:
25 super(PyClass2, self).dispatch()
26 assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'
27
28 p = PyClass1()
Dean Moldovan665e8802016-08-12 22:28:31 +020029 return dispatch_issue_go(p)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020030
31 b = PyClass2()
Dean Moldovan665e8802016-08-12 22:28:31 +020032 assert dispatch_issue_go(b) == "Yay.."
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020033
34
35def test_reference_wrapper():
36 """#171: Can't return reference wrappers (or STL data structures containing them)"""
37 from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper
38
39 assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
40 "[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"
41
42
43def test_iterator_passthrough():
44 """#181: iterator passthrough did not compile"""
45 from pybind11_tests.issues import iterator_passthrough
46
47 assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
48
49
50def test_shared_ptr_gc():
51 """// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
52 from pybind11_tests.issues import ElementList, ElementA
53
54 el = ElementList()
55 for i in range(10):
56 el.add(ElementA(i))
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010057 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020058 for i, v in enumerate(el.get()):
59 assert i == v.value()
60
61
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +010062def test_no_id(msg):
Dean Moldovan99dbdc12016-08-19 13:45:36 +020063 from pybind11_tests.issues import get_element, expect_float, expect_int
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020064
65 with pytest.raises(TypeError) as excinfo:
Dean Moldovan99dbdc12016-08-19 13:45:36 +020066 get_element(None)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020067 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090068 get_element(): incompatible function arguments. The following argument types are supported:
Dean Moldovan99dbdc12016-08-19 13:45:36 +020069 1. (arg0: m.issues.ElementA) -> int
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090070
71 Invoked with: None
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020072 """
73
74 with pytest.raises(TypeError) as excinfo:
75 expect_int(5.2)
76 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090077 expect_int(): incompatible function arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020078 1. (arg0: int) -> int
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090079
80 Invoked with: 5.2
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020081 """
82 assert expect_float(12) == 12
83
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020084
Dean Moldovan99dbdc12016-08-19 13:45:36 +020085def test_str_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020086 """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
87 from pybind11_tests.issues import StrIssue
88
Dean Moldovan99dbdc12016-08-19 13:45:36 +020089 assert str(StrIssue(3)) == "StrIssue[3]"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020090
91 with pytest.raises(TypeError) as excinfo:
92 str(StrIssue("no", "such", "constructor"))
93 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090094 __init__(): incompatible constructor arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020095 1. m.issues.StrIssue(arg0: int)
96 2. m.issues.StrIssue()
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090097
Jason Rhinelander7146d622016-11-22 05:28:40 -050098 Invoked with: 'no', 'such', 'constructor'
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020099 """
100
101
Dean Moldovan665e8802016-08-12 22:28:31 +0200102def test_nested():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200103 """ #328: first member in a class can't be used in operators"""
Dean Moldovan665e8802016-08-12 22:28:31 +0200104 from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200105
106 a = NestA()
107 b = NestB()
108 c = NestC()
109
110 a += 10
Dean Moldovan665e8802016-08-12 22:28:31 +0200111 assert get_NestA(a) == 13
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200112 b.a += 100
Dean Moldovan665e8802016-08-12 22:28:31 +0200113 assert get_NestA(b.a) == 103
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200114 c.b.a += 1000
Dean Moldovan665e8802016-08-12 22:28:31 +0200115 assert get_NestA(c.b.a) == 1003
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200116 b -= 1
Dean Moldovan665e8802016-08-12 22:28:31 +0200117 assert get_NestB(b) == 3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200118 c.b -= 3
Dean Moldovan665e8802016-08-12 22:28:31 +0200119 assert get_NestB(c.b) == 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200120 c *= 7
Dean Moldovan665e8802016-08-12 22:28:31 +0200121 assert get_NestC(c) == 35
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200122
123 abase = a.as_base()
124 assert abase.value == -2
125 a.as_base().value += 44
126 assert abase.value == 42
127 assert c.b.a.as_base().value == -2
128 c.b.a.as_base().value += 44
129 assert c.b.a.as_base().value == 42
130
131 del c
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100132 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200133 del a # Should't delete while abase is still alive
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100134 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200135
136 assert abase.value == 42
137 del abase, b
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100138 pytest.gc_collect()
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900139
140
141def test_move_fallback():
142 from pybind11_tests.issues import get_moveissue1, get_moveissue2
143 m2 = get_moveissue2(2)
144 assert m2.value == 2
145 m1 = get_moveissue1(1)
146 assert m1.value == 1
Jason Rhinelander56f71772016-09-07 13:32:49 -0400147
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100148
Jason Rhinelander56f71772016-09-07 13:32:49 -0400149def test_override_ref():
150 from pybind11_tests.issues import OverrideTest
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400151 o = OverrideTest("asdf")
Jason Rhinelander56f71772016-09-07 13:32:49 -0400152
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400153 # Not allowed (see associated .cpp comment)
Dean Moldovanbad17402016-11-20 21:21:54 +0100154 # i = o.str_ref()
155 # assert o.str_ref() == "asdf"
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400156 assert o.str_value() == "asdf"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400157
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400158 assert o.A_value().value == "hi"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400159 a = o.A_ref()
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400160 assert a.value == "hi"
161 a.value = "bye"
162 assert a.value == "bye"
Wenzel Jakob382484a2016-09-10 15:28:37 +0900163
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100164
Wenzel Jakob382484a2016-09-10 15:28:37 +0900165def test_operators_notimplemented(capture):
166 from pybind11_tests.issues import OpTest1, OpTest2
167 with capture:
Dean Moldovanbad17402016-11-20 21:21:54 +0100168 c1, c2 = OpTest1(), OpTest2()
169 c1 + c1
170 c2 + c2
171 c2 + c1
172 c1 + c2
173 assert capture == """
174 Add OpTest1 with OpTest1
175 Add OpTest2 with OpTest2
176 Add OpTest2 with OpTest1
177 Add OpTest2 with OpTest1
178 """
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900179
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100180
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900181def test_iterator_rvpolicy():
182 """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
183 from pybind11_tests.issues import make_iterator_1
184 from pybind11_tests.issues import make_iterator_2
185
186 assert list(make_iterator_1()) == [1, 2, 3]
187 assert list(make_iterator_2()) == [1, 2, 3]
Dean Moldovanbad17402016-11-20 21:21:54 +0100188 assert not isinstance(make_iterator_1(), type(make_iterator_2()))
Jason Rhinelander6873c202016-10-24 21:58:22 -0400189
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100190
Jason Rhinelander6873c202016-10-24 21:58:22 -0400191def test_dupe_assignment():
192 """ Issue 461: overwriting a class with a function """
193 from pybind11_tests.issues import dupe_exception_failures
194 assert dupe_exception_failures() == []
Wenzel Jakobbd560ac2016-11-03 11:53:35 +0100195
196
197def test_enable_shared_from_this_with_reference_rvp():
198 """ Issue #471: shared pointer instance not dellocated """
199 from pybind11_tests import SharedParent, SharedChild
200
201 parent = SharedParent()
202 child = parent.get_child()
203
204 cstats = ConstructorStats.get(SharedChild)
205 assert cstats.alive() == 1
206 del child, parent
207 assert cstats.alive() == 0
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500208
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100209
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500210def test_non_destructed_holders():
211 """ Issue #478: unique ptrs constructed and freed without destruction """
212 from pybind11_tests import SpecialHolderObj
213
214 a = SpecialHolderObj(123)
215 b = a.child()
216
217 assert a.val == 123
218 assert b.val == 124
219
220 cstats = SpecialHolderObj.holder_cstats()
221 assert cstats.alive() == 1
222 del b
223 assert cstats.alive() == 1
224 del a
225 assert cstats.alive() == 0
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100226
227
228def test_complex_cast(capture):
229 """ Issue #484: number conversion generates unhandled exceptions """
230 from pybind11_tests.issues import test_complex
231
232 with capture:
233 test_complex(1)
234 test_complex(2j)
235
236 assert capture == """
Dean Moldovanbad17402016-11-20 21:21:54 +0100237 1.0
238 (0.0, 2.0)
239 """
Wenzel Jakob7c2461e2016-11-20 05:26:02 +0100240
241
242def test_inheritance_override_def_static():
243 from pybind11_tests.issues import MyBase, MyDerived
244
245 b = MyBase.make()
246 d1 = MyDerived.make2()
247 d2 = MyDerived.make()
248
249 assert isinstance(b, MyBase)
250 assert isinstance(d1, MyDerived)
251 assert isinstance(d2, MyDerived)