test_issues.py 5.27 KB
Newer Older
Dean Moldovan's avatar
Dean Moldovan committed
1
2
3
4
import pytest
import gc


5
def test_regressions():
Dean Moldovan's avatar
Dean Moldovan committed
6
7
    from pybind11_tests.issues import print_cchar, print_char

8
9
10
11
    # #137: const char* isn't handled properly
    assert print_cchar("const char *") == "const char *"
    # #150: char bindings broken
    assert print_char("c") == "c"
Dean Moldovan's avatar
Dean Moldovan committed
12
13


14
def test_dispatch_issue(msg):
Dean Moldovan's avatar
Dean Moldovan committed
15
16
17
18
19
    """#159: virtual function dispatch has problems with similar-named functions"""
    from pybind11_tests.issues import DispatchIssue, dispatch_issue_go

    class PyClass1(DispatchIssue):
        def dispatch(self):
20
            return "Yay.."
Dean Moldovan's avatar
Dean Moldovan committed
21
22
23
24
25
26
27
28

    class PyClass2(DispatchIssue):
        def dispatch(self):
            with pytest.raises(RuntimeError) as excinfo:
                super(PyClass2, self).dispatch()
            assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'

            p = PyClass1()
29
            return dispatch_issue_go(p)
Dean Moldovan's avatar
Dean Moldovan committed
30
31

    b = PyClass2()
32
    assert dispatch_issue_go(b) == "Yay.."
Dean Moldovan's avatar
Dean Moldovan committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62


def test_reference_wrapper():
    """#171: Can't return reference wrappers (or STL data structures containing them)"""
    from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper

    assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
        "[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"


def test_iterator_passthrough():
    """#181: iterator passthrough did not compile"""
    from pybind11_tests.issues import iterator_passthrough

    assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]


def test_shared_ptr_gc():
    """// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
    from pybind11_tests.issues import ElementList, ElementA

    el = ElementList()
    for i in range(10):
        el.add(ElementA(i))
    gc.collect()
    for i, v in enumerate(el.get()):
        assert i == v.value()


def test_no_id(capture, msg):
63
    from pybind11_tests.issues import get_element, expect_float, expect_int
Dean Moldovan's avatar
Dean Moldovan committed
64
65

    with pytest.raises(TypeError) as excinfo:
66
        get_element(None)
Dean Moldovan's avatar
Dean Moldovan committed
67
    assert msg(excinfo.value) == """
68
        get_element(): incompatible function arguments. The following argument types are supported:
69
            1. (arg0: m.issues.ElementA) -> int
70
71

        Invoked with: None
Dean Moldovan's avatar
Dean Moldovan committed
72
73
74
75
76
    """

    with pytest.raises(TypeError) as excinfo:
        expect_int(5.2)
    assert msg(excinfo.value) == """
77
        expect_int(): incompatible function arguments. The following argument types are supported:
Dean Moldovan's avatar
Dean Moldovan committed
78
            1. (arg0: int) -> int
79
80

        Invoked with: 5.2
Dean Moldovan's avatar
Dean Moldovan committed
81
82
83
84
85
    """
    assert expect_float(12) == 12



86
def test_str_issue(msg):
Dean Moldovan's avatar
Dean Moldovan committed
87
88
89
    """Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
    from pybind11_tests.issues import StrIssue

90
    assert str(StrIssue(3)) == "StrIssue[3]"
Dean Moldovan's avatar
Dean Moldovan committed
91
92
93
94

    with pytest.raises(TypeError) as excinfo:
        str(StrIssue("no", "such", "constructor"))
    assert msg(excinfo.value) == """
95
        __init__(): incompatible constructor arguments. The following argument types are supported:
Dean Moldovan's avatar
Dean Moldovan committed
96
97
            1. m.issues.StrIssue(arg0: int)
            2. m.issues.StrIssue()
98
99

        Invoked with: no, such, constructor
Dean Moldovan's avatar
Dean Moldovan committed
100
101
102
    """


103
def test_nested():
Dean Moldovan's avatar
Dean Moldovan committed
104
    """ #328: first member in a class can't be used in operators"""
105
    from pybind11_tests.issues import NestA, NestB, NestC, get_NestA, get_NestB, get_NestC
Dean Moldovan's avatar
Dean Moldovan committed
106
107
108
109
110
111

    a = NestA()
    b = NestB()
    c = NestC()

    a += 10
112
    assert get_NestA(a) == 13
Dean Moldovan's avatar
Dean Moldovan committed
113
    b.a += 100
114
    assert get_NestA(b.a) == 103
Dean Moldovan's avatar
Dean Moldovan committed
115
    c.b.a += 1000
116
    assert get_NestA(c.b.a) == 1003
Dean Moldovan's avatar
Dean Moldovan committed
117
    b -= 1
118
    assert get_NestB(b) == 3
Dean Moldovan's avatar
Dean Moldovan committed
119
    c.b -= 3
120
    assert get_NestB(c.b) == 1
Dean Moldovan's avatar
Dean Moldovan committed
121
    c *= 7
122
    assert get_NestC(c) == 35
Dean Moldovan's avatar
Dean Moldovan committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

    abase = a.as_base()
    assert abase.value == -2
    a.as_base().value += 44
    assert abase.value == 42
    assert c.b.a.as_base().value == -2
    c.b.a.as_base().value += 44
    assert c.b.a.as_base().value == 42

    del c
    gc.collect()
    del a  # Should't delete while abase is still alive
    gc.collect()

    assert abase.value == 42
    del abase, b
    gc.collect()
140
141
142
143
144
145
146
147


def test_move_fallback():
    from pybind11_tests.issues import get_moveissue1, get_moveissue2
    m2 = get_moveissue2(2)
    assert m2.value == 2
    m1 = get_moveissue1(1)
    assert m1.value == 1
148
149
150

def test_override_ref():
    from pybind11_tests.issues import OverrideTest
151
    o = OverrideTest("asdf")
152

153
    # Not allowed (see associated .cpp comment)
154
155
156
    #i = o.str_ref()
    #assert o.str_ref() == "asdf"
    assert o.str_value() == "asdf"
157

158
    assert o.A_value().value == "hi"
159
    a = o.A_ref()
160
161
162
    assert a.value == "hi"
    a.value = "bye"
    assert a.value == "bye"
163
164
165
166
167
168
169
170
171
172
173
174
175

def test_operators_notimplemented(capture):
    from pybind11_tests.issues import OpTest1, OpTest2
    with capture:
        C1, C2 = OpTest1(), OpTest2()
        C1 + C1
        C2 + C2
        C2 + C1
        C1 + C2
    assert capture == """Add OpTest1 with OpTest1
Add OpTest2 with OpTest2
Add OpTest2 with OpTest1
Add OpTest2 with OpTest1"""
176
177
178
179
180
181
182
183
184

def test_iterator_rvpolicy():
    """ Issue 388: Can't make iterators via make_iterator() with different r/v policies """
    from pybind11_tests.issues import make_iterator_1
    from pybind11_tests.issues import make_iterator_2

    assert list(make_iterator_1()) == [1, 2, 3]
    assert list(make_iterator_2()) == [1, 2, 3]
    assert(type(make_iterator_1()) != type(make_iterator_2()))