Skip to content

Commit

Permalink
Add CppDrvd2, test_PPPCCC (needs more work).
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Nov 3, 2023
1 parent f3bb31e commit 90e8914
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/test_python_multiple_inheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ struct CppDrvd : CppBase {
int drvd_value;
};

struct CppDrvd2 : CppBase {
explicit CppDrvd2(int value) : CppBase(value), drvd2_value(value * 5) {}
int get_drvd2_value() const { return drvd2_value; }
void reset_drvd2_value(int new_value) { drvd2_value = new_value; }

int get_base_value_from_drvd2() const { return get_base_value(); }
void reset_base_value_from_drvd2(int new_value) { reset_base_value(new_value); }

private:
int drvd2_value;
};

} // namespace test_python_multiple_inheritance

TEST_SUBMODULE(python_multiple_inheritance, m) {
Expand All @@ -42,4 +54,11 @@ TEST_SUBMODULE(python_multiple_inheritance, m) {
.def("reset_drvd_value", &CppDrvd::reset_drvd_value)
.def("get_base_value_from_drvd", &CppDrvd::get_base_value_from_drvd)
.def("reset_base_value_from_drvd", &CppDrvd::reset_base_value_from_drvd);

py::class_<CppDrvd2, CppBase>(m, "CppDrvd2")
.def(py::init<int>())
.def("get_drvd2_value", &CppDrvd2::get_drvd2_value)
.def("reset_drvd2_value", &CppDrvd2::reset_drvd2_value)
.def("get_base_value_from_drvd2", &CppDrvd2::get_base_value_from_drvd2)
.def("reset_base_value_from_drvd2", &CppDrvd2::reset_base_value_from_drvd2);
}
14 changes: 14 additions & 0 deletions tests/test_python_multiple_inheritance.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Adapted from:
# https://github.com/google/clif/blob/5718e4d0807fd3b6a8187dde140069120b81ecef/clif/testing/python/python_multiple_inheritance_test.py

import pytest

from pybind11_tests import python_multiple_inheritance as m


Expand All @@ -12,6 +14,10 @@ class PPCC(PC, m.CppDrvd):
pass


class PPPCCC(PPCC, m.CppDrvd2):
pass


def test_PC():
d = PC(11)
assert d.get_base_value() == 11
Expand All @@ -33,3 +39,11 @@ def test_PPCC():
d.reset_base_value_from_drvd(30)
assert d.get_base_value() == 30
assert d.get_base_value_from_drvd() == 30


def test_PPPCCC():
with pytest.raises(
TypeError,
match=r"CppDrvd2\.__init__\(\) must be called when overriding __init__$",
):
PPPCCC(11)

0 comments on commit 90e8914

Please sign in to comment.