Skip to content

Commit f39efb8

Browse files
jakobandersenrwgk
authored andcommitted
SH, attribute and property tests
1 parent fa5ffc3 commit f39efb8

2 files changed

+53
-0
lines changed

tests/test_class_sh_shared_ptr_copy_move.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ struct Foo {
3131
using FooShPtr = Foo<0>;
3232
using FooSmHld = Foo<1>;
3333

34+
struct Outer {
35+
std::shared_ptr<FooShPtr> ShPtr;
36+
std::shared_ptr<FooSmHld> SmHld;
37+
Outer()
38+
: ShPtr(std::make_shared<FooShPtr>("Outer")), SmHld(std::make_shared<FooSmHld>("Outer")) {}
39+
std::shared_ptr<FooShPtr> getShPtr() const { return ShPtr; }
40+
std::shared_ptr<FooSmHld> getSmHld() const { return SmHld; }
41+
};
42+
3443
} // namespace
3544
} // namespace pybind11_tests
3645

@@ -48,6 +57,32 @@ TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
4857
.def("get_history", &FooShPtr::get_history);
4958
py::classh<FooSmHld>(m, "FooSmHld").def("get_history", &FooSmHld::get_history);
5059

60+
auto outer = py::class_<Outer>(m, "Outer").def(py::init());
61+
#define MAKE_PROP(PropTyp) \
62+
MAKE_PROP_FOO(ShPtr, PropTyp) \
63+
MAKE_PROP_FOO(SmHld, PropTyp)
64+
65+
#define MAKE_PROP_FOO(FooTyp, PropTyp) \
66+
.def_##PropTyp(#FooTyp "_" #PropTyp "_default", &Outer::FooTyp) \
67+
.def_##PropTyp( \
68+
#FooTyp "_" #PropTyp "_copy", &Outer::FooTyp, py::return_value_policy::copy) \
69+
.def_##PropTyp( \
70+
#FooTyp "_" #PropTyp "_move", &Outer::FooTyp, py::return_value_policy::move)
71+
outer MAKE_PROP(readonly) MAKE_PROP(readwrite);
72+
#undef MAKE_PROP_FOO
73+
74+
#define MAKE_PROP_FOO(FooTyp, PropTyp) \
75+
.def_##PropTyp(#FooTyp "_property_" #PropTyp "_default", &Outer::FooTyp) \
76+
.def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_copy", \
77+
&Outer::get##FooTyp, \
78+
py::return_value_policy::copy) \
79+
.def_property_##PropTyp(#FooTyp "_property_" #PropTyp "_move", \
80+
&Outer::get##FooTyp, \
81+
py::return_value_policy::move)
82+
outer MAKE_PROP(readonly);
83+
#undef MAKE_PROP_FOO
84+
#undef MAKE_PROP
85+
5186
m.def("test_ShPtr_copy", []() {
5287
auto o = std::make_shared<FooShPtr>("copy");
5388
auto l = py::list();

tests/test_class_sh_shared_ptr_copy_move.py

+18
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,21 @@ def test_shptr_move():
2121
def test_smhld_move():
2222
txt = m.test_SmHld_move()[0].get_history()
2323
assert txt == "FooSmHld_move"
24+
25+
26+
def _check_property(foo_typ: str, prop_typ: str, policy: str):
27+
o = m.Outer()
28+
name = "{}_{}_{}".format(foo_typ, prop_typ, policy)
29+
history = "Foo{}_Outer".format(foo_typ)
30+
f = getattr(o, name)
31+
assert f.get_history() == history
32+
# and try again to check that o did not get changed
33+
f = getattr(o, name)
34+
assert f.get_history() == history
35+
36+
37+
def test_properties():
38+
for prop_typ in ("readonly", "readwrite", "property_readonly"):
39+
for foo_typ in ("ShPtr", "SmHld"):
40+
for policy in ("default", "copy", "move"):
41+
_check_property(foo_typ, prop_typ, policy)

0 commit comments

Comments
 (0)