blob: f97dd4e7e23b2ba789d82ac033a9ee2c7f485546 [file] [log] [blame]
Dean Moldovan6fccf692016-10-11 01:12:48 +02001import pytest
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002from pybind11_tests import ExampleMandA, ConstructorStats
3
4
5def test_methods_and_attributes():
6 instance1 = ExampleMandA()
7 instance2 = ExampleMandA(32)
8
9 instance1.add1(instance2)
10 instance1.add2(instance2)
11 instance1.add3(instance2)
12 instance1.add4(instance2)
13 instance1.add5(instance2)
14 instance1.add6(32)
15 instance1.add7(32)
16 instance1.add8(32)
17 instance1.add9(32)
18 instance1.add10(32)
19
20 assert str(instance1) == "ExampleMandA[value=320]"
21 assert str(instance2) == "ExampleMandA[value=32]"
22 assert str(instance1.self1()) == "ExampleMandA[value=320]"
23 assert str(instance1.self2()) == "ExampleMandA[value=320]"
24 assert str(instance1.self3()) == "ExampleMandA[value=320]"
25 assert str(instance1.self4()) == "ExampleMandA[value=320]"
26 assert str(instance1.self5()) == "ExampleMandA[value=320]"
27
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.value == 320
35 instance1.value = 100
36 assert str(instance1) == "ExampleMandA[value=100]"
37
38 cstats = ConstructorStats.get(ExampleMandA)
39 assert cstats.alive() == 2
40 del instance1, instance2
41 assert cstats.alive() == 0
42 assert cstats.values() == ["32"]
43 assert cstats.default_constructions == 1
44 assert cstats.copy_constructions == 3
45 assert cstats.move_constructions >= 1
46 assert cstats.copy_assignments == 0
47 assert cstats.move_assignments == 0
Dean Moldovan6fccf692016-10-11 01:12:48 +020048
49
Dean Moldovan5b7e1902016-10-21 18:51:14 +020050def test_properties():
51 from pybind11_tests import TestProperties
52
53 instance = TestProperties()
54
55 assert instance.def_readonly == 1
56 with pytest.raises(AttributeError):
57 instance.def_readonly = 2
58
59 instance.def_readwrite = 2
60 assert instance.def_readwrite == 2
61
62 assert instance.def_property_readonly == 2
63 with pytest.raises(AttributeError):
64 instance.def_property_readonly = 3
65
66 instance.def_property = 3
67 assert instance.def_property == 3
68
69
70def test_static_properties():
71 from pybind11_tests import TestProperties as Type
72
73 assert Type.def_readonly_static == 1
74 with pytest.raises(AttributeError):
75 Type.def_readonly_static = 2
76
77 Type.def_readwrite_static = 2
78 assert Type.def_readwrite_static == 2
79
80 assert Type.def_property_readonly_static == 2
81 with pytest.raises(AttributeError):
82 Type.def_property_readonly_static = 3
83
84 Type.def_property_static = 3
85 assert Type.def_property_static == 3
86
87
Dean Moldovan03f627e2016-11-01 11:44:57 +010088@pytest.mark.parametrize("access", ["ro", "rw", "static_ro", "static_rw"])
89def test_property_return_value_policies(access):
90 from pybind11_tests import TestPropRVP
91
92 if not access.startswith("static"):
93 obj = TestPropRVP()
94 else:
95 obj = TestPropRVP
96
97 ref = getattr(obj, access + "_ref")
98 assert ref.value == 1
99 ref.value = 2
100 assert getattr(obj, access + "_ref").value == 2
101 ref.value = 1 # restore original value for static properties
102
103 copy = getattr(obj, access + "_copy")
104 assert copy.value == 1
105 copy.value = 2
106 assert getattr(obj, access + "_copy").value == 1
107
108 copy = getattr(obj, access + "_func")
109 assert copy.value == 1
110 copy.value = 2
111 assert getattr(obj, access + "_func").value == 1
112
113
114def test_property_rvalue_policy():
115 """When returning an rvalue, the return value policy is automatically changed from
116 `reference(_internal)` to `move`. The following would not work otherwise."""
117 from pybind11_tests import TestPropRVP
118
119 instance = TestPropRVP()
120 o = instance.rvalue
121 assert o.value == 1
122 o = TestPropRVP.static_rvalue
123 assert o.value == 1
124
125
Dean Moldovan6fccf692016-10-11 01:12:48 +0200126def test_dynamic_attributes():
Dean Moldovanb8cb5ca2016-10-14 18:01:17 +0200127 from pybind11_tests import DynamicClass, CppDerivedDynamicClass
Dean Moldovan6fccf692016-10-11 01:12:48 +0200128
129 instance = DynamicClass()
130 assert not hasattr(instance, "foo")
131 assert "foo" not in dir(instance)
132
133 # Dynamically add attribute
134 instance.foo = 42
135 assert hasattr(instance, "foo")
136 assert instance.foo == 42
137 assert "foo" in dir(instance)
138
139 # __dict__ should be accessible and replaceable
140 assert "foo" in instance.__dict__
141 instance.__dict__ = {"bar": True}
142 assert not hasattr(instance, "foo")
143 assert hasattr(instance, "bar")
144
145 with pytest.raises(TypeError) as excinfo:
146 instance.__dict__ = []
147 assert str(excinfo.value) == "__dict__ must be set to a dictionary, not a 'list'"
148
149 cstats = ConstructorStats.get(DynamicClass)
150 assert cstats.alive() == 1
151 del instance
152 assert cstats.alive() == 0
153
154 # Derived classes should work as well
Dean Moldovanb8cb5ca2016-10-14 18:01:17 +0200155 class PythonDerivedDynamicClass(DynamicClass):
Dean Moldovan6fccf692016-10-11 01:12:48 +0200156 pass
157
Dean Moldovanb8cb5ca2016-10-14 18:01:17 +0200158 for cls in CppDerivedDynamicClass, PythonDerivedDynamicClass:
159 derived = cls()
160 derived.foobar = 100
161 assert derived.foobar == 100
Dean Moldovan6fccf692016-10-11 01:12:48 +0200162
Dean Moldovanb8cb5ca2016-10-14 18:01:17 +0200163 assert cstats.alive() == 1
164 del derived
165 assert cstats.alive() == 0
Dean Moldovan6fccf692016-10-11 01:12:48 +0200166
167
168def test_cyclic_gc():
169 from pybind11_tests import DynamicClass
170
171 # One object references itself
172 instance = DynamicClass()
173 instance.circular_reference = instance
174
175 cstats = ConstructorStats.get(DynamicClass)
176 assert cstats.alive() == 1
177 del instance
178 assert cstats.alive() == 0
179
180 # Two object reference each other
181 i1 = DynamicClass()
182 i2 = DynamicClass()
183 i1.cycle = i2
184 i2.cycle = i1
185
186 assert cstats.alive() == 2
187 del i1, i2
188 assert cstats.alive() == 0