Skip to content

Commit

Permalink
[tests/python] test_polymorphic : test Python-derived class
Browse files Browse the repository at this point in the history
+ test class holding Python wrapper class
  • Loading branch information
ManifoldFR committed Jun 5, 2024
1 parent 9bc3639 commit 031b3fa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 12 additions & 0 deletions tests/python/polymorphic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ struct poly_use_base {
PolyX x;
};

struct X_wrap_store {
X_wrap_store(const PyX &x) : x(x) {}
PyX x;
};

struct poly_store {
poly_store(const PolyX &x) : x(x) {}
PolyX x;
Expand All @@ -53,6 +58,10 @@ PolyX getZ() { return PolyX(Z()); }

PolyX poly_passthrough(const PolyX &x) { return x; }

void call_method(const PolyX &x) {
std::cout << " - call_method: PolyX::name(): " << x->name() << std::endl;
}

static PolyX static_x = PolyX{Y()};

const PolyX &set_return(const PolyX &x) {
Expand Down Expand Up @@ -81,6 +90,9 @@ BOOST_PYTHON_MODULE(polymorphic_test) {
bp::def("set_return", &set_return,
bp::return_value_policy<bp::reference_existing_object>());

bp::class_<X_wrap_store>("X_wrap_store", bp::init<const PyX &>("x"_a))
.def_readwrite("x", &X_wrap_store::x);

bp::class_<poly_use_base>("poly_use_base", bp::no_init)
.def(bp::init<const Y &>(bp::args("self", "t")))
.def(bp::init<const Z &>(bp::args("self", "t")))
Expand Down
21 changes: 19 additions & 2 deletions tests/python/test_polymorphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Z,
getY,
getZ,
X_wrap_store,
poly_use_base,
poly_store,
create_vec_poly,
Expand All @@ -12,6 +13,12 @@
call_method,
)


class DerX(X):
def name(self):
return "My name is DerX and I'm from Python!"


assert isinstance(getY(), Y)
assert isinstance(getZ(), Z)
assert issubclass(Y, X)
Expand All @@ -23,10 +30,16 @@
z = Z()
print(z)
call_method(z)
d = DerX()
print(d)
call_method(d)

dstore = X_wrap_store(d)
print("dstore.x:", dstore.x)


def test_poly_base():
print("test_poly_base")
print("=== test_poly_base ===")
b = poly_use_base(y)
print(b.x)
assert isinstance(b.x, Y)
Expand All @@ -35,9 +48,13 @@ def test_poly_base():
print(b.x)
assert isinstance(b.x, Z)

b = poly_use_base(d)
print(b.x, b.x.name())
assert isinstance(b.x, DerX)


def test_poly_store():
print("test_poly_store")
print("=== test_poly_store ===")
b = poly_store(y)
print(b.x)
assert isinstance(b.x, X)
Expand Down

0 comments on commit 031b3fa

Please sign in to comment.