Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 1 | import pytest |
| 2 | import gc |
| 3 | |
| 4 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 5 | def test_regressions(): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 6 | from pybind11_tests.issues import print_cchar, print_char |
| 7 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 8 | # #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 Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 12 | |
| 13 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 14 | def test_dispatch_issue(msg): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 15 | """#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 Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 20 | return "Yay.." |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 21 | |
| 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 Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 29 | return dispatch_issue_go(p) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 30 | |
| 31 | b = PyClass2() |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 32 | assert dispatch_issue_go(b) == "Yay.." |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def 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 | |
| 43 | def 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 | |
| 50 | def 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 | |
| 62 | def test_no_id(capture, msg): |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 63 | from pybind11_tests.issues import get_element, expect_float, expect_int |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 64 | |
| 65 | with pytest.raises(TypeError) as excinfo: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 66 | get_element(None) |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 67 | assert msg(excinfo.value) == """ |
Wenzel Jakob | e99ebae | 2016-09-12 11:44:37 +0900 | [diff] [blame] | 68 | get_element(): incompatible function arguments. The following argument types are supported: |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 69 | 1. (arg0: m.issues.ElementA) -> int |
Wenzel Jakob | e99ebae | 2016-09-12 11:44:37 +0900 | [diff] [blame] | 70 | |
| 71 | Invoked with: None |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 72 | """ |
| 73 | |
| 74 | with pytest.raises(TypeError) as excinfo: |
| 75 | expect_int(5.2) |
| 76 | assert msg(excinfo.value) == """ |
Wenzel Jakob | e99ebae | 2016-09-12 11:44:37 +0900 | [diff] [blame] | 77 | expect_int(): incompatible function arguments. The following argument types are supported: |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 78 | 1. (arg0: int) -> int |
Wenzel Jakob | e99ebae | 2016-09-12 11:44:37 +0900 | [diff] [blame] | 79 | |
| 80 | Invoked with: 5.2 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 81 | """ |
| 82 | assert expect_float(12) == 12 |
| 83 | |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 84 | |
| 85 | |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 86 | def test_str_issue(msg): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 87 | """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid""" |
| 88 | from pybind11_tests.issues import StrIssue |
| 89 | |
Dean Moldovan | 99dbdc1 | 2016-08-19 13:45:36 +0200 | [diff] [blame] | 90 | assert str(StrIssue(3)) == "StrIssue[3]" |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 91 | |
| 92 | with pytest.raises(TypeError) as excinfo: |
| 93 | str(StrIssue("no", "such", "constructor")) |
| 94 | assert msg(excinfo.value) == """ |
Wenzel Jakob | e99ebae | 2016-09-12 11:44:37 +0900 | [diff] [blame] | 95 | __init__(): incompatible constructor arguments. The following argument types are supported: |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 96 | 1. m.issues.StrIssue(arg0: int) |
| 97 | 2. m.issues.StrIssue() |
Wenzel Jakob | e99ebae | 2016-09-12 11:44:37 +0900 | [diff] [blame] | 98 | |
| 99 | Invoked with: no, such, constructor |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 100 | """ |
| 101 | |
| 102 | |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 103 | def test_nested(): |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 104 | """ #328: first member in a class can't be used in operators""" |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 105 | from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 106 | |
| 107 | a = NestA() |
| 108 | b = NestB() |
| 109 | c = NestC() |
| 110 | |
| 111 | a += 10 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 112 | assert get_NestA(a) == 13 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 113 | b.a += 100 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 114 | assert get_NestA(b.a) == 103 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 115 | c.b.a += 1000 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 116 | assert get_NestA(c.b.a) == 1003 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 117 | b -= 1 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 118 | assert get_NestB(b) == 3 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 119 | c.b -= 3 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 120 | assert get_NestB(c.b) == 1 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 121 | c *= 7 |
Dean Moldovan | 665e880 | 2016-08-12 22:28:31 +0200 | [diff] [blame] | 122 | assert get_NestC(c) == 35 |
Dean Moldovan | a0c1ccf | 2016-08-12 13:50:00 +0200 | [diff] [blame] | 123 | |
| 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 Jakob | c84b37b | 2016-09-07 00:47:17 +0900 | [diff] [blame] | 140 | |
| 141 | |
| 142 | def 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 Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 148 | |
| 149 | def test_override_ref(): |
| 150 | from pybind11_tests.issues import OverrideTest |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 151 | o = OverrideTest("asdf") |
Jason Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 152 | |
Jason Rhinelander | c03db9b | 2016-09-07 13:38:32 -0400 | [diff] [blame] | 153 | # Not allowed (see associated .cpp comment) |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 154 | #i = o.str_ref() |
| 155 | #assert o.str_ref() == "asdf" |
| 156 | assert o.str_value() == "asdf" |
Jason Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 157 | |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 158 | assert o.A_value().value == "hi" |
Jason Rhinelander | 56f7177 | 2016-09-07 13:32:49 -0400 | [diff] [blame] | 159 | a = o.A_ref() |
Jason Rhinelander | 9c6859e | 2016-09-08 11:03:08 -0400 | [diff] [blame] | 160 | assert a.value == "hi" |
| 161 | a.value = "bye" |
| 162 | assert a.value == "bye" |
Wenzel Jakob | 382484a | 2016-09-10 15:28:37 +0900 | [diff] [blame] | 163 | |
| 164 | def 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 |
| 173 | Add OpTest2 with OpTest2 |
| 174 | Add OpTest2 with OpTest1 |
| 175 | Add OpTest2 with OpTest1""" |
Wenzel Jakob | b212f6c | 2016-09-10 16:00:50 +0900 | [diff] [blame] | 176 | |
| 177 | def 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())) |
Jason Rhinelander | 6873c20 | 2016-10-24 21:58:22 -0400 | [diff] [blame^] | 185 | |
| 186 | def test_dupe_assignment(): |
| 187 | """ Issue 461: overwriting a class with a function """ |
| 188 | from pybind11_tests.issues import dupe_exception_failures |
| 189 | assert dupe_exception_failures() == [] |