better test for const only smart ptr (#5727)

This commit is contained in:
Rosdf
2025-06-14 19:38:09 +04:00
committed by GitHub
parent d218b160a6
commit f3bb00732d
2 changed files with 21 additions and 10 deletions

View File

@@ -193,6 +193,20 @@ public:
int value;
};
// test const_only_shared_ptr
class MyObject6 {
public:
static const_only_shared_ptr<MyObject6> createObject(std::string value) {
return const_only_shared_ptr<MyObject6>(new MyObject6(std::move(value)));
}
const std::string &value() const { return value_; }
private:
explicit MyObject6(std::string &&value) : value_{std::move(value)} {}
std::string value_;
};
// test_shared_ptr_and_references
struct SharedPtrRef {
struct A {
@@ -412,11 +426,6 @@ TEST_SUBMODULE(smart_ptr, m) {
m.def("print_myobject2_4",
[](const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); });
m.def("make_myobject2_3",
[](int val) { return const_only_shared_ptr<MyObject2>(new MyObject2(val)); });
m.def("print_myobject2_5",
[](const const_only_shared_ptr<MyObject2> &obj) { py::print(obj.get()->toString()); });
py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3").def(py::init<int>());
m.def("make_myobject3_1", []() { return new MyObject3(8); });
m.def("make_myobject3_2", []() { return std::make_shared<MyObject3>(9); });
@@ -459,6 +468,10 @@ TEST_SUBMODULE(smart_ptr, m) {
.def(py::init<int>())
.def_readwrite("value", &MyObject5::value);
py::class_<MyObject6, const_only_shared_ptr<MyObject6>>(m, "MyObject6")
.def(py::init([](const std::string &value) { return MyObject6::createObject(value); }))
.def_property_readonly("value", &MyObject6::value);
// test_shared_ptr_and_references
using A = SharedPtrRef::A;
py::class_<A, std::shared_ptr<A>>(m, "A");

View File

@@ -352,8 +352,6 @@ def test_move_only_holder_caster_shared_ptr_with_smart_holder_support_enabled():
)
def test_const_only_holder(capture):
o = m.make_myobject2_3(4)
with capture:
m.print_myobject2_5(o)
assert capture == "MyObject2[4]\n"
def test_const_only_holder():
o = m.MyObject6("my_data")
assert o.value == "my_data"