blob: 6f84f777b483eab968652ea090708a3ac9eb7bf8 [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
63def test_no_id(capture, 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
86
Dean Moldovan99dbdc12016-08-19 13:45:36 +020087def test_str_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020088 """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
89 from pybind11_tests.issues import StrIssue
90
Dean Moldovan99dbdc12016-08-19 13:45:36 +020091 assert str(StrIssue(3)) == "StrIssue[3]"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020092
93 with pytest.raises(TypeError) as excinfo:
94 str(StrIssue("no", "such", "constructor"))
95 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090096 __init__(): incompatible constructor arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020097 1. m.issues.StrIssue(arg0: int)
98 2. m.issues.StrIssue()
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090099
100 Invoked with: no, such, constructor
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200101 """
102
103
Dean Moldovan665e8802016-08-12 22:28:31 +0200104def test_nested():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200105 """ #328: first member in a class can't be used in operators"""
Dean Moldovan665e8802016-08-12 22:28:31 +0200106 from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200107
108 a = NestA()
109 b = NestB()
110 c = NestC()
111
112 a += 10
Dean Moldovan665e8802016-08-12 22:28:31 +0200113 assert get_NestA(a) == 13
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200114 b.a += 100
Dean Moldovan665e8802016-08-12 22:28:31 +0200115 assert get_NestA(b.a) == 103
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200116 c.b.a += 1000
Dean Moldovan665e8802016-08-12 22:28:31 +0200117 assert get_NestA(c.b.a) == 1003
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200118 b -= 1
Dean Moldovan665e8802016-08-12 22:28:31 +0200119 assert get_NestB(b) == 3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200120 c.b -= 3
Dean Moldovan665e8802016-08-12 22:28:31 +0200121 assert get_NestB(c.b) == 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200122 c *= 7
Dean Moldovan665e8802016-08-12 22:28:31 +0200123 assert get_NestC(c) == 35
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200124
125 abase = a.as_base()
126 assert abase.value == -2
127 a.as_base().value += 44
128 assert abase.value == 42
129 assert c.b.a.as_base().value == -2
130 c.b.a.as_base().value += 44
131 assert c.b.a.as_base().value == 42
132
133 del c
134 gc.collect()
135 del a # Should't delete while abase is still alive
136 gc.collect()
137
138 assert abase.value == 42
139 del abase, b
140 gc.collect()
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900141
142
143def test_move_fallback():
144 from pybind11_tests.issues import get_moveissue1, get_moveissue2
145 m2 = get_moveissue2(2)
146 assert m2.value == 2
147 m1 = get_moveissue1(1)
148 assert m1.value == 1
Jason Rhinelander56f71772016-09-07 13:32:49 -0400149
150def 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)
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400155 #i = o.str_ref()
156 #assert o.str_ref() == "asdf"
157 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
165def test_operators_notimplemented(capture):
166 from pybind11_tests.issues import OpTest1, OpTest2
167 with capture:
168 C1, C2 = OpTest1(), OpTest2()
169 C1 + C1
170 C2 + C2
171 C2 + C1
172 C1 + C2
173 assert capture == """Add OpTest1 with OpTest1
174Add OpTest2 with OpTest2
175Add OpTest2 with OpTest1
176Add OpTest2 with OpTest1"""
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900177
178def test_iterator_rvpolicy():
179 """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
180 from pybind11_tests.issues import make_iterator_1
181 from pybind11_tests.issues import make_iterator_2
182
183 assert list(make_iterator_1()) == [1, 2, 3]
184 assert list(make_iterator_2()) == [1, 2, 3]
185 assert(type(make_iterator_1()) != type(make_iterator_2()))
Jason Rhinelander6873c202016-10-24 21:58:22 -0400186
187def test_dupe_assignment():
188 """ Issue 461: overwriting a class with a function """
189 from pybind11_tests.issues import dupe_exception_failures
190 assert dupe_exception_failures() == []
Wenzel Jakobbd560ac2016-11-03 11:53:35 +0100191
192
193def test_enable_shared_from_this_with_reference_rvp():
194 """ Issue #471: shared pointer instance not dellocated """
195 from pybind11_tests import SharedParent, SharedChild
196
197 parent = SharedParent()
198 child = parent.get_child()
199
200 cstats = ConstructorStats.get(SharedChild)
201 assert cstats.alive() == 1
202 del child, parent
203 assert cstats.alive() == 0
Jason Rhinelanderc07ec312016-11-06 13:12:48 -0500204
205def test_non_destructed_holders():
206 """ Issue #478: unique ptrs constructed and freed without destruction """
207 from pybind11_tests import SpecialHolderObj
208
209 a = SpecialHolderObj(123)
210 b = a.child()
211
212 assert a.val == 123
213 assert b.val == 124
214
215 cstats = SpecialHolderObj.holder_cstats()
216 assert cstats.alive() == 1
217 del b
218 assert cstats.alive() == 1
219 del a
220 assert cstats.alive() == 0