Skip to content

Commit f3bb007

Browse files
authored
better test for const only smart ptr (#5727)
1 parent d218b16 commit f3bb007

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

tests/test_smart_ptr.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ class MyObject5 { // managed by huge_unique_ptr
193193
int value;
194194
};
195195

196+
// test const_only_shared_ptr
197+
class MyObject6 {
198+
public:
199+
static const_only_shared_ptr<MyObject6> createObject(std::string value) {
200+
return const_only_shared_ptr<MyObject6>(new MyObject6(std::move(value)));
201+
}
202+
203+
const std::string &value() const { return value_; }
204+
205+
private:
206+
explicit MyObject6(std::string &&value) : value_{std::move(value)} {}
207+
std::string value_;
208+
};
209+
196210
// test_shared_ptr_and_references
197211
struct SharedPtrRef {
198212
struct A {
@@ -412,11 +426,6 @@ TEST_SUBMODULE(smart_ptr, m) {
412426
m.def("print_myobject2_4",
413427
[](const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); });
414428

415-
m.def("make_myobject2_3",
416-
[](int val) { return const_only_shared_ptr<MyObject2>(new MyObject2(val)); });
417-
m.def("print_myobject2_5",
418-
[](const const_only_shared_ptr<MyObject2> &obj) { py::print(obj.get()->toString()); });
419-
420429
py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3").def(py::init<int>());
421430
m.def("make_myobject3_1", []() { return new MyObject3(8); });
422431
m.def("make_myobject3_2", []() { return std::make_shared<MyObject3>(9); });
@@ -459,6 +468,10 @@ TEST_SUBMODULE(smart_ptr, m) {
459468
.def(py::init<int>())
460469
.def_readwrite("value", &MyObject5::value);
461470

471+
py::class_<MyObject6, const_only_shared_ptr<MyObject6>>(m, "MyObject6")
472+
.def(py::init([](const std::string &value) { return MyObject6::createObject(value); }))
473+
.def_property_readonly("value", &MyObject6::value);
474+
462475
// test_shared_ptr_and_references
463476
using A = SharedPtrRef::A;
464477
py::class_<A, std::shared_ptr<A>>(m, "A");

tests/test_smart_ptr.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,6 @@ def test_move_only_holder_caster_shared_ptr_with_smart_holder_support_enabled():
352352
)
353353

354354

355-
def test_const_only_holder(capture):
356-
o = m.make_myobject2_3(4)
357-
with capture:
358-
m.print_myobject2_5(o)
359-
assert capture == "MyObject2[4]\n"
355+
def test_const_only_holder():
356+
o = m.MyObject6("my_data")
357+
assert o.value == "my_data"

0 commit comments

Comments
 (0)