Deleted Added
sdiff udiff text old ( 11986:c12e4625ab56 ) new ( 12037:d28054ac6ec9 )
full compact
1import pytest
2from pybind11_tests import ExampleMandA, ConstructorStats
3
4
5def test_methods_and_attributes():
6 instance1 = ExampleMandA()
7 instance2 = ExampleMandA(32)
8

--- 19 unchanged lines hidden (view full) ---

28 assert instance1.internal1() == 320
29 assert instance1.internal2() == 320
30 assert instance1.internal3() == 320
31 assert instance1.internal4() == 320
32 assert instance1.internal5() == 320
33
34 assert instance1.overloaded(1, 1.0) == "(int, float)"
35 assert instance1.overloaded(2.0, 2) == "(float, int)"
36 assert instance1.overloaded_const(3, 3.0) == "(int, float) const"
37 assert instance1.overloaded_const(4.0, 4) == "(float, int) const"
38
39 assert instance1.value == 320
40 instance1.value = 100
41 assert str(instance1) == "ExampleMandA[value=100]"
42
43 cstats = ConstructorStats.get(ExampleMandA)
44 assert cstats.alive() == 2
45 del instance1, instance2

--- 25 unchanged lines hidden (view full) ---

71 instance.def_property = 3
72 assert instance.def_property == 3
73
74
75def test_static_properties():
76 from pybind11_tests import TestProperties as Type
77
78 assert Type.def_readonly_static == 1
79 with pytest.raises(AttributeError):
80 Type.def_readonly_static = 2
81
82 Type.def_readwrite_static = 2
83 assert Type.def_readwrite_static == 2
84
85 assert Type.def_property_readonly_static == 2
86 with pytest.raises(AttributeError):
87 Type.def_property_readonly_static = 3
88
89 Type.def_property_static = 3
90 assert Type.def_property_static == 3
91
92
93@pytest.mark.parametrize("access", ["ro", "rw", "static_ro", "static_rw"])
94def test_property_return_value_policies(access):
95 from pybind11_tests import TestPropRVP
96
97 if not access.startswith("static"):
98 obj = TestPropRVP()
99 else:
100 obj = TestPropRVP

--- 19 unchanged lines hidden (view full) ---

120 """When returning an rvalue, the return value policy is automatically changed from
121 `reference(_internal)` to `move`. The following would not work otherwise.
122 """
123 from pybind11_tests import TestPropRVP
124
125 instance = TestPropRVP()
126 o = instance.rvalue
127 assert o.value == 1
128 o = TestPropRVP.static_rvalue
129 assert o.value == 1
130
131
132def test_dynamic_attributes():
133 from pybind11_tests import DynamicClass, CppDerivedDynamicClass
134
135 instance = DynamicClass()
136 assert not hasattr(instance, "foo")
137 assert "foo" not in dir(instance)
138
139 # Dynamically add attribute

--- 26 unchanged lines hidden (view full) ---

166 derived.foobar = 100
167 assert derived.foobar == 100
168
169 assert cstats.alive() == 1
170 del derived
171 assert cstats.alive() == 0
172
173
174def test_cyclic_gc():
175 from pybind11_tests import DynamicClass
176
177 # One object references itself
178 instance = DynamicClass()
179 instance.circular_reference = instance
180
181 cstats = ConstructorStats.get(DynamicClass)

--- 5 unchanged lines hidden (view full) ---

187 i1 = DynamicClass()
188 i2 = DynamicClass()
189 i1.cycle = i2
190 i2.cycle = i1
191
192 assert cstats.alive() == 2
193 del i1, i2
194 assert cstats.alive() == 0