blob: ad3d39d5174d805ac41c545c12b4a48cadf3b604 [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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020082
83
Dean Moldovan99dbdc12016-08-19 13:45:36 +020084def test_str_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020085 """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
86 from pybind11_tests.issues import StrIssue
87
Dean Moldovan99dbdc12016-08-19 13:45:36 +020088 assert str(StrIssue(3)) == "StrIssue[3]"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020089
90 with pytest.raises(TypeError) as excinfo:
91 str(StrIssue("no", "such", "constructor"))
92 assert msg(excinfo.value) == """
93 Incompatible constructor arguments. The following argument types are supported:
94 1. m.issues.StrIssue(arg0: int)
95 2. m.issues.StrIssue()
96 Invoked with: no, such, constructor
97 """
98
99
Dean Moldovan665e8802016-08-12 22:28:31 +0200100def test_nested():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200101 """ #328: first member in a class can't be used in operators"""
Dean Moldovan665e8802016-08-12 22:28:31 +0200102 from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200103
104 a = NestA()
105 b = NestB()
106 c = NestC()
107
108 a += 10
Dean Moldovan665e8802016-08-12 22:28:31 +0200109 assert get_NestA(a) == 13
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200110 b.a += 100
Dean Moldovan665e8802016-08-12 22:28:31 +0200111 assert get_NestA(b.a) == 103
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200112 c.b.a += 1000
Dean Moldovan665e8802016-08-12 22:28:31 +0200113 assert get_NestA(c.b.a) == 1003
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200114 b -= 1
Dean Moldovan665e8802016-08-12 22:28:31 +0200115 assert get_NestB(b) == 3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200116 c.b -= 3
Dean Moldovan665e8802016-08-12 22:28:31 +0200117 assert get_NestB(c.b) == 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200118 c *= 7
Dean Moldovan665e8802016-08-12 22:28:31 +0200119 assert get_NestC(c) == 35
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200120
121 abase = a.as_base()
122 assert abase.value == -2
123 a.as_base().value += 44
124 assert abase.value == 42
125 assert c.b.a.as_base().value == -2
126 c.b.a.as_base().value += 44
127 assert c.b.a.as_base().value == 42
128
129 del c
130 gc.collect()
131 del a # Should't delete while abase is still alive
132 gc.collect()
133
134 assert abase.value == 42
135 del abase, b
136 gc.collect()
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900137
138
139def test_move_fallback():
140 from pybind11_tests.issues import get_moveissue1, get_moveissue2
141 m2 = get_moveissue2(2)
142 assert m2.value == 2
143 m1 = get_moveissue1(1)
144 assert m1.value == 1
Jason Rhinelander56f71772016-09-07 13:32:49 -0400145
146def test_override_ref():
147 from pybind11_tests.issues import OverrideTest
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400148 o = OverrideTest("asdf")
Jason Rhinelander56f71772016-09-07 13:32:49 -0400149
Jason Rhinelanderc03db9b2016-09-07 13:38:32 -0400150 # Not allowed (see associated .cpp comment)
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400151 #i = o.str_ref()
152 #assert o.str_ref() == "asdf"
153 assert o.str_value() == "asdf"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400154
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400155 assert o.A_value().value == "hi"
Jason Rhinelander56f71772016-09-07 13:32:49 -0400156 a = o.A_ref()
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400157 assert a.value == "hi"
158 a.value = "bye"
159 assert a.value == "bye"
Wenzel Jakob382484a2016-09-10 15:28:37 +0900160
161def test_operators_notimplemented(capture):
162 from pybind11_tests.issues import OpTest1, OpTest2
163 with capture:
164 C1, C2 = OpTest1(), OpTest2()
165 C1 + C1
166 C2 + C2
167 C2 + C1
168 C1 + C2
169 assert capture == """Add OpTest1 with OpTest1
170Add OpTest2 with OpTest2
171Add OpTest2 with OpTest1
172Add OpTest2 with OpTest1"""
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900173
174def test_iterator_rvpolicy():
175 """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
176 from pybind11_tests.issues import make_iterator_1
177 from pybind11_tests.issues import make_iterator_2
178
179 assert list(make_iterator_1()) == [1, 2, 3]
180 assert list(make_iterator_2()) == [1, 2, 3]
181 assert(type(make_iterator_1()) != type(make_iterator_2()))