blob: e2ab0b45c702bbb9cf0d351c88bdc44749af5459 [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) == """
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
85
Dean Moldovan99dbdc12016-08-19 13:45:36 +020086def test_str_issue(msg):
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020087 """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
88 from pybind11_tests.issues import StrIssue
89
Dean Moldovan99dbdc12016-08-19 13:45:36 +020090 assert str(StrIssue(3)) == "StrIssue[3]"
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020091
92 with pytest.raises(TypeError) as excinfo:
93 str(StrIssue("no", "such", "constructor"))
94 assert msg(excinfo.value) == """
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090095 __init__(): incompatible constructor arguments. The following argument types are supported:
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020096 1. m.issues.StrIssue(arg0: int)
97 2. m.issues.StrIssue()
Wenzel Jakobe99ebae2016-09-12 11:44:37 +090098
99 Invoked with: no, such, constructor
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200100 """
101
102
Dean Moldovan665e8802016-08-12 22:28:31 +0200103def test_nested():
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200104 """ #328: first member in a class can't be used in operators"""
Dean Moldovan665e8802016-08-12 22:28:31 +0200105 from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200106
107 a = NestA()
108 b = NestB()
109 c = NestC()
110
111 a += 10
Dean Moldovan665e8802016-08-12 22:28:31 +0200112 assert get_NestA(a) == 13
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200113 b.a += 100
Dean Moldovan665e8802016-08-12 22:28:31 +0200114 assert get_NestA(b.a) == 103
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200115 c.b.a += 1000
Dean Moldovan665e8802016-08-12 22:28:31 +0200116 assert get_NestA(c.b.a) == 1003
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200117 b -= 1
Dean Moldovan665e8802016-08-12 22:28:31 +0200118 assert get_NestB(b) == 3
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200119 c.b -= 3
Dean Moldovan665e8802016-08-12 22:28:31 +0200120 assert get_NestB(c.b) == 1
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200121 c *= 7
Dean Moldovan665e8802016-08-12 22:28:31 +0200122 assert get_NestC(c) == 35
Dean Moldovana0c1ccf2016-08-12 13:50:00 +0200123
124 abase = a.as_base()
125 assert abase.value == -2
126 a.as_base().value += 44
127 assert abase.value == 42
128 assert c.b.a.as_base().value == -2
129 c.b.a.as_base().value += 44
130 assert c.b.a.as_base().value == 42
131
132 del c
133 gc.collect()
134 del a # Should't delete while abase is still alive
135 gc.collect()
136
137 assert abase.value == 42
138 del abase, b
139 gc.collect()
Wenzel Jakobc84b37b2016-09-07 00:47:17 +0900140
141
142def test_move_fallback():
143 from pybind11_tests.issues import get_moveissue1, get_moveissue2
144 m2 = get_moveissue2(2)
145 assert m2.value == 2
146 m1 = get_moveissue1(1)
147 assert m1.value == 1
Jason Rhinelander56f71772016-09-07 13:32:49 -0400148
149def 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)
Jason Rhinelander9c6859e2016-09-08 11:03:08 -0400154 #i = o.str_ref()
155 #assert o.str_ref() == "asdf"
156 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
164def test_operators_notimplemented(capture):
165 from pybind11_tests.issues import OpTest1, OpTest2
166 with capture:
167 C1, C2 = OpTest1(), OpTest2()
168 C1 + C1
169 C2 + C2
170 C2 + C1
171 C1 + C2
172 assert capture == """Add OpTest1 with OpTest1
173Add OpTest2 with OpTest2
174Add OpTest2 with OpTest1
175Add OpTest2 with OpTest1"""
Wenzel Jakobb212f6c2016-09-10 16:00:50 +0900176
177def test_iterator_rvpolicy():
178 """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
179 from pybind11_tests.issues import make_iterator_1
180 from pybind11_tests.issues import make_iterator_2
181
182 assert list(make_iterator_1()) == [1, 2, 3]
183 assert list(make_iterator_2()) == [1, 2, 3]
184 assert(type(make_iterator_1()) != type(make_iterator_2()))