Skip to content

Commit 74e6f44

Browse files
committed
add unit test
1 parent 35f9d97 commit 74e6f44

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

tests/test_class.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ TEST_SUBMODULE(class_, m) {
104104
~NoConstructorNew() { print_destroyed(this); }
105105
};
106106

107+
struct DynamicAttr {
108+
DynamicAttr() = default;
109+
};
110+
107111
py::class_<NoConstructor>(m, "NoConstructor")
108112
.def_static("new_instance", &NoConstructor::new_instance, "Return an instance");
109113

@@ -112,6 +116,9 @@ TEST_SUBMODULE(class_, m) {
112116
.def_static("__new__",
113117
[](const py::object &) { return NoConstructorNew::new_instance(); });
114118

119+
py::class_<DynamicAttr>(m, "DynamicAttr", py::dynamic_attr())
120+
.def(py::init<>());
121+
115122
// test_pass_unique_ptr
116123
struct ToBeHeldByUniquePtr {};
117124
py::class_<ToBeHeldByUniquePtr, std::unique_ptr<ToBeHeldByUniquePtr>>(m, "ToBeHeldByUniquePtr")

tests/test_class.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import gc
34
import sys
45
from unittest import mock
56

@@ -45,6 +46,12 @@ def test_instance(msg):
4546
assert cstats.alive() == 0
4647

4748

49+
def test_get_referrers():
50+
instance = m.DynamicAttr()
51+
instance.a = "test"
52+
assert instance in gc.get_referrers(instance.__dict__)
53+
54+
4855
def test_instance_new():
4956
instance = m.NoConstructorNew() # .__new__(m.NoConstructor.__class__)
5057

0 commit comments

Comments
 (0)