test_smart_ptr_base_derived.py 1.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
# -*- coding: utf-8 -*-
import pytest

from pybind11_tests import smart_ptr_base_derived as m

CBASE_GET_INT_RESULT = 90146438
CDERIVED_GET_INT_RESULT = 31607978
VDERIVED_GET_INT_RESULT = 29852452

Ralf W. Grosse-Kunstleve's avatar
Ralf W. Grosse-Kunstleve committed
10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def test_concrete():
    m.to_cout("")
    m.to_cout("")
    m.to_cout("make_shared_cderived")
    cd = m.make_shared_cderived()
    assert cd.get_int() == CDERIVED_GET_INT_RESULT
    m.pass_shared_cderived(cd)
    m.pass_shared_cbase(cd)
    cb = m.make_shared_cderived_up_cast()
    assert cb.get_int() == CBASE_GET_INT_RESULT
    m.pass_shared_cbase(cb)
    with pytest.raises(TypeError):
        m.pass_shared_cderived(cb)
    m.to_cout("")

Ralf W. Grosse-Kunstleve's avatar
Ralf W. Grosse-Kunstleve committed
26

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def test_virtual():
    m.to_cout("")
    m.to_cout("")
    m.to_cout("make_shared_vderived")
    vd = m.make_shared_vderived()
    assert vd.get_int() == VDERIVED_GET_INT_RESULT
    m.pass_shared_vderived(vd)
    m.pass_shared_vbase(vd)
    vd_uc = m.make_shared_vderived_up_cast()
    assert vd_uc.get_int() == VDERIVED_GET_INT_RESULT
    assert isinstance(vd_uc, m.vderived)  # pybind11 un-did upcast.
    m.pass_shared_vbase(vd_uc)
    m.pass_shared_vderived(vd_uc)
    with pytest.raises(TypeError):
        m.pass_shared_vrederived(vd_uc)
    m.to_cout("")