blob: 056737a1ae129e312c97473b7354030776723334 [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001import pytest
2import gc
3
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))
57 gc.collect()
58 for i, v in enumerate(el.get()):
59 assert i == v.value()
60
61
62def test_no_id(capture, 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) == """
68 Incompatible function arguments. The following argument types are supported:
Dean Moldovan99dbdc12016-08-19 13:45:36 +020069 1. (arg0: m.issues.ElementA) -> int
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020070 Invoked with: None
71 """
72
73 with pytest.raises(TypeError) as excinfo:
74 expect_int(5.2)
75 assert msg(excinfo.value) == """
76 Incompatible function arguments. The following argument types are supported:
77 1. (arg0: int) -> int
78 Invoked with: 5.2
79 """
80 assert expect_float(12) == 12
81
82 from pybind11_tests.issues import A, call_f
83
84 class B(A):
85 def __init__(self):
86 super(B, self).__init__()
87
88 def f(self):
89 print("In python f()")
90
91 # C++ version
92 with capture:
93 a = A()
94 call_f(a)
95 assert capture == "A.f()"
96
97 # Python version
98 with capture:
99 b = B()
100 call_f(b)
101 assert capture == """
102 PyA.PyA()
103 PyA.f()
104 In python f()
105 """
106
107
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200108def test_str_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200109 """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
110 from pybind11_tests.issues import StrIssue
111
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200112 assert str(StrIssue(3)) == "StrIssue[3]"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200113
114 with pytest.raises(TypeError) as excinfo:
115 str(StrIssue("no", "such", "constructor"))
116 assert msg(excinfo.value) == """
117 Incompatible constructor arguments. The following argument types are supported:
118 1. m.issues.StrIssue(arg0: int)
119 2. m.issues.StrIssue()
120 Invoked with: no, such, constructor
121 """
122
123
Dean Moldovan665e8802016-08-12 22:28:31 +0200124def test_nested():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200125 """ #328: first member in a class can't be used in operators"""
Dean Moldovan665e8802016-08-12 22:28:31 +0200126 from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200127
128 a = NestA()
129 b = NestB()
130 c = NestC()
131
132 a += 10
Dean Moldovan665e8802016-08-12 22:28:31 +0200133 assert get_NestA(a) == 13
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200134 b.a += 100
Dean Moldovan665e8802016-08-12 22:28:31 +0200135 assert get_NestA(b.a) == 103
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200136 c.b.a += 1000
Dean Moldovan665e8802016-08-12 22:28:31 +0200137 assert get_NestA(c.b.a) == 1003
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200138 b -= 1
Dean Moldovan665e8802016-08-12 22:28:31 +0200139 assert get_NestB(b) == 3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200140 c.b -= 3
Dean Moldovan665e8802016-08-12 22:28:31 +0200141 assert get_NestB(c.b) == 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200142 c *= 7
Dean Moldovan665e8802016-08-12 22:28:31 +0200143 assert get_NestC(c) == 35
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200144
145 abase = a.as_base()
146 assert abase.value == -2
147 a.as_base().value += 44
148 assert abase.value == 42
149 assert c.b.a.as_base().value == -2
150 c.b.a.as_base().value += 44
151 assert c.b.a.as_base().value == 42
152
153 del c
154 gc.collect()
155 del a # Should't delete while abase is still alive
156 gc.collect()
157
158 assert abase.value == 42
159 del abase, b
160 gc.collect()
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900161
162
163def test_move_fallback():
164 from pybind11_tests.issues import get_moveissue1, get_moveissue2
165 m2 = get_moveissue2(2)
166 assert m2.value == 2
167 m1 = get_moveissue1(1)
168 assert m1.value == 1
Jason Rhinelander56f71772016-09-07 13:32:49 -0400169
170def test_override_ref():
171 from pybind11_tests.issues import OverrideTest
172 o = OverrideTest(42)
173
174 i = o.int_ref()
175 assert o.int_value() == 42
176 assert o.int_ref() == 42
177
178 assert o.A_value().value == 99
179 a = o.A_ref()
180 assert a.value == 99
181 a.value = 7
182 assert a.value == 7