blob: 08678cfc58803e913927db0e5c2a1869e01e3be3 [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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020035def test_iterator_passthrough():
36 """#181: iterator passthrough did not compile"""
37 from pybind11_tests.issues import iterator_passthrough
38
39 assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
40
41
42def test_shared_ptr_gc():
43 """// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
44 from pybind11_tests.issues import ElementList, ElementA
45
46 el = ElementList()
47 for i in range(10):
48 el.add(ElementA(i))
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +010049 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020050 for i, v in enumerate(el.get()):
51 assert i == v.value()
52
53
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +010054def test_no_id(msg):
Dean Moldovan99dbdc12016-08-19 13:45:36 +020055 from pybind11_tests.issues import get_element, expect_float, expect_int
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020056
57 with pytest.raises(TypeError) as excinfo:
Dean Moldovan99dbdc12016-08-19 13:45:36 +020058 get_element(None)
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020059 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090060 get_element(): incompatible function arguments. The following argument types are supported:
Dean Moldovan99dbdc12016-08-19 13:45:36 +020061 1. (arg0: m.issues.ElementA) -> int
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090062
63 Invoked with: None
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020064 """
65
66 with pytest.raises(TypeError) as excinfo:
67 expect_int(5.2)
68 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090069 expect_int(): incompatible function arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020070 1. (arg0: int) -> int
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090071
72 Invoked with: 5.2
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020073 """
74 assert expect_float(12) == 12
75
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020076
Dean Moldovan99dbdc12016-08-19 13:45:36 +020077def test_str_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020078 """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
79 from pybind11_tests.issues import StrIssue
80
Dean Moldovan99dbdc12016-08-19 13:45:36 +020081 assert str(StrIssue(3)) == "StrIssue[3]"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020082
83 with pytest.raises(TypeError) as excinfo:
84 str(StrIssue("no", "such", "constructor"))
85 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090086 __init__(): incompatible constructor arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020087 1. m.issues.StrIssue(arg0: int)
88 2. m.issues.StrIssue()
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090089
Jason Rhinelander7146d622016-11-22 05:28:40 -050090 Invoked with: 'no', 'such', 'constructor'
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020091 """
92
93
Dean Moldovan665e8802016-08-12 22:28:31 +020094def test_nested():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020095 """ #328: first member in a class can't be used in operators"""
Dean Moldovan665e8802016-08-12 22:28:31 +020096 from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020097
98 a = NestA()
99 b = NestB()
100 c = NestC()
101
102 a += 10
Dean Moldovan665e8802016-08-12 22:28:31 +0200103 assert get_NestA(a) == 13
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200104 b.a += 100
Dean Moldovan665e8802016-08-12 22:28:31 +0200105 assert get_NestA(b.a) == 103
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200106 c.b.a += 1000
Dean Moldovan665e8802016-08-12 22:28:31 +0200107 assert get_NestA(c.b.a) == 1003
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200108 b -= 1
Dean Moldovan665e8802016-08-12 22:28:31 +0200109 assert get_NestB(b) == 3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200110 c.b -= 3
Dean Moldovan665e8802016-08-12 22:28:31 +0200111 assert get_NestB(c.b) == 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200112 c *= 7
Dean Moldovan665e8802016-08-12 22:28:31 +0200113 assert get_NestC(c) == 35
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200114
115 abase = a.as_base()
116 assert abase.value == -2
117 a.as_base().value += 44
118 assert abase.value == 42
119 assert c.b.a.as_base().value == -2
120 c.b.a.as_base().value += 44
121 assert c.b.a.as_base().value == 42
122
123 del c
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100124 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200125 del a # Should't delete while abase is still alive
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100126 pytest.gc_collect()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200127
128 assert abase.value == 42
129 del abase, b
Wenzel Jakob1d1f81b2016-12-16 15:00:46 +0100130 pytest.gc_collect()
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900131
132
133def test_move_fallback():
134 from pybind11_tests.issues import get_moveissue1, get_moveissue2
135 m2 = get_moveissue2(2)
136 assert m2.value == 2
137 m1 = get_moveissue1(1)
138 assert m1.value == 1
Jason Rhinelander56f71772016-09-07 13:32:49 -0400139
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100140
Jason Rhinelander56f71772016-09-07 13:32:49 -0400141def test_override_ref():
142 from pybind11_tests.issues import OverrideTest
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400143 o = OverrideTest("asdf")
Jason Rhinelander56f71772016-09-07 13:32:49 -0400144
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400145 # Not allowed (see associated .cpp comment)
Dean Moldovanbad17402016-11-20 21:21:54 +0100146 # i = o.str_ref()
147 # assert o.str_ref() == "asdf"
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400148 assert o.str_value() == "asdf"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400149
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400150 assert o.A_value().value == "hi"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400151 a = o.A_ref()
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400152 assert a.value == "hi"
153 a.value = "bye"
154 assert a.value == "bye"
Wenzel Jakob382484a2016-09-10 15:28:37 +0900155
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100156
Wenzel Jakob382484a2016-09-10 15:28:37 +0900157def test_operators_notimplemented(capture):
158 from pybind11_tests.issues import OpTest1, OpTest2
159 with capture:
Dean Moldovanbad17402016-11-20 21:21:54 +0100160 c1, c2 = OpTest1(), OpTest2()
161 c1 + c1
162 c2 + c2
163 c2 + c1
164 c1 + c2
165 assert capture == """
166 Add OpTest1 with OpTest1
167 Add OpTest2 with OpTest2
168 Add OpTest2 with OpTest1
169 Add OpTest2 with OpTest1
170 """
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900171
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100172
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900173def test_iterator_rvpolicy():
174 """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
175 from pybind11_tests.issues import make_iterator_1
176 from pybind11_tests.issues import make_iterator_2
177
178 assert list(make_iterator_1()) == [1, 2, 3]
179 assert list(make_iterator_2()) == [1, 2, 3]
Dean Moldovanbad17402016-11-20 21:21:54 +0100180 assert not isinstance(make_iterator_1(), type(make_iterator_2()))
Jason Rhinelander6873c202016-10-24 21:58:22 -0400181
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100182
Jason Rhinelander6873c202016-10-24 21:58:22 -0400183def test_dupe_assignment():
184 """ Issue 461: overwriting a class with a function """
185 from pybind11_tests.issues import dupe_exception_failures
186 assert dupe_exception_failures() == []
Wenzel Jakobbd560ac2016-11-03 11:53:35 +0100187
188
189def test_enable_shared_from_this_with_reference_rvp():
190 """ Issue #471: shared pointer instance not dellocated """
191 from pybind11_tests import SharedParent, SharedChild
192
193 parent = SharedParent()
194 child = parent.get_child()
195
196 cstats = ConstructorStats.get(SharedChild)
197 assert cstats.alive() == 1
198 del child, parent
199 assert cstats.alive() == 0
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500200
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100201
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500202def test_non_destructed_holders():
203 """ Issue #478: unique ptrs constructed and freed without destruction """
204 from pybind11_tests import SpecialHolderObj
205
206 a = SpecialHolderObj(123)
207 b = a.child()
208
209 assert a.val == 123
210 assert b.val == 124
211
212 cstats = SpecialHolderObj.holder_cstats()
213 assert cstats.alive() == 1
214 del b
215 assert cstats.alive() == 1
216 del a
217 assert cstats.alive() == 0
Wenzel Jakobfe40dfe2016-11-07 15:59:01 +0100218
219
220def test_complex_cast(capture):
221 """ Issue #484: number conversion generates unhandled exceptions """
222 from pybind11_tests.issues import test_complex
223
224 with capture:
225 test_complex(1)
226 test_complex(2j)
227
228 assert capture == """
Dean Moldovanbad17402016-11-20 21:21:54 +0100229 1.0
230 (0.0, 2.0)
231 """
Wenzel Jakob7c2461e2016-11-20 05:26:02 +0100232
233
234def test_inheritance_override_def_static():
235 from pybind11_tests.issues import MyBase, MyDerived
236
237 b = MyBase.make()
238 d1 = MyDerived.make2()
239 d2 = MyDerived.make()
240
241 assert isinstance(b, MyBase)
242 assert isinstance(d1, MyDerived)
243 assert isinstance(d2, MyDerived)