ยปCore Development>Code coverage>Lib/test/test_property.py

Python code coverage for Lib/test/test_property.py

#countcontent
1n/a# Test case for property
2n/a# more tests are in test_descr
3n/a
4n/aimport sys
5n/aimport unittest
6n/a
7n/aclass PropertyBase(Exception):
8n/a pass
9n/a
10n/aclass PropertyGet(PropertyBase):
11n/a pass
12n/a
13n/aclass PropertySet(PropertyBase):
14n/a pass
15n/a
16n/aclass PropertyDel(PropertyBase):
17n/a pass
18n/a
19n/aclass BaseClass(object):
20n/a def __init__(self):
21n/a self._spam = 5
22n/a
23n/a @property
24n/a def spam(self):
25n/a """BaseClass.getter"""
26n/a return self._spam
27n/a
28n/a @spam.setter
29n/a def spam(self, value):
30n/a self._spam = value
31n/a
32n/a @spam.deleter
33n/a def spam(self):
34n/a del self._spam
35n/a
36n/aclass SubClass(BaseClass):
37n/a
38n/a @BaseClass.spam.getter
39n/a def spam(self):
40n/a """SubClass.getter"""
41n/a raise PropertyGet(self._spam)
42n/a
43n/a @spam.setter
44n/a def spam(self, value):
45n/a raise PropertySet(self._spam)
46n/a
47n/a @spam.deleter
48n/a def spam(self):
49n/a raise PropertyDel(self._spam)
50n/a
51n/aclass PropertyDocBase(object):
52n/a _spam = 1
53n/a def _get_spam(self):
54n/a return self._spam
55n/a spam = property(_get_spam, doc="spam spam spam")
56n/a
57n/aclass PropertyDocSub(PropertyDocBase):
58n/a @PropertyDocBase.spam.getter
59n/a def spam(self):
60n/a """The decorator does not use this doc string"""
61n/a return self._spam
62n/a
63n/aclass PropertySubNewGetter(BaseClass):
64n/a @BaseClass.spam.getter
65n/a def spam(self):
66n/a """new docstring"""
67n/a return 5
68n/a
69n/aclass PropertyNewGetter(object):
70n/a @property
71n/a def spam(self):
72n/a """original docstring"""
73n/a return 1
74n/a @spam.getter
75n/a def spam(self):
76n/a """new docstring"""
77n/a return 8
78n/a
79n/aclass PropertyTests(unittest.TestCase):
80n/a def test_property_decorator_baseclass(self):
81n/a # see #1620
82n/a base = BaseClass()
83n/a self.assertEqual(base.spam, 5)
84n/a self.assertEqual(base._spam, 5)
85n/a base.spam = 10
86n/a self.assertEqual(base.spam, 10)
87n/a self.assertEqual(base._spam, 10)
88n/a delattr(base, "spam")
89n/a self.assertTrue(not hasattr(base, "spam"))
90n/a self.assertTrue(not hasattr(base, "_spam"))
91n/a base.spam = 20
92n/a self.assertEqual(base.spam, 20)
93n/a self.assertEqual(base._spam, 20)
94n/a
95n/a def test_property_decorator_subclass(self):
96n/a # see #1620
97n/a sub = SubClass()
98n/a self.assertRaises(PropertyGet, getattr, sub, "spam")
99n/a self.assertRaises(PropertySet, setattr, sub, "spam", None)
100n/a self.assertRaises(PropertyDel, delattr, sub, "spam")
101n/a
102n/a @unittest.skipIf(sys.flags.optimize >= 2,
103n/a "Docstrings are omitted with -O2 and above")
104n/a def test_property_decorator_subclass_doc(self):
105n/a sub = SubClass()
106n/a self.assertEqual(sub.__class__.spam.__doc__, "SubClass.getter")
107n/a
108n/a @unittest.skipIf(sys.flags.optimize >= 2,
109n/a "Docstrings are omitted with -O2 and above")
110n/a def test_property_decorator_baseclass_doc(self):
111n/a base = BaseClass()
112n/a self.assertEqual(base.__class__.spam.__doc__, "BaseClass.getter")
113n/a
114n/a def test_property_decorator_doc(self):
115n/a base = PropertyDocBase()
116n/a sub = PropertyDocSub()
117n/a self.assertEqual(base.__class__.spam.__doc__, "spam spam spam")
118n/a self.assertEqual(sub.__class__.spam.__doc__, "spam spam spam")
119n/a
120n/a @unittest.skipIf(sys.flags.optimize >= 2,
121n/a "Docstrings are omitted with -O2 and above")
122n/a def test_property_getter_doc_override(self):
123n/a newgettersub = PropertySubNewGetter()
124n/a self.assertEqual(newgettersub.spam, 5)
125n/a self.assertEqual(newgettersub.__class__.spam.__doc__, "new docstring")
126n/a newgetter = PropertyNewGetter()
127n/a self.assertEqual(newgetter.spam, 8)
128n/a self.assertEqual(newgetter.__class__.spam.__doc__, "new docstring")
129n/a
130n/a def test_property___isabstractmethod__descriptor(self):
131n/a for val in (True, False, [], [1], '', '1'):
132n/a class C(object):
133n/a def foo(self):
134n/a pass
135n/a foo.__isabstractmethod__ = val
136n/a foo = property(foo)
137n/a self.assertIs(C.foo.__isabstractmethod__, bool(val))
138n/a
139n/a # check that the property's __isabstractmethod__ descriptor does the
140n/a # right thing when presented with a value that fails truth testing:
141n/a class NotBool(object):
142n/a def __bool__(self):
143n/a raise ValueError()
144n/a __len__ = __bool__
145n/a with self.assertRaises(ValueError):
146n/a class C(object):
147n/a def foo(self):
148n/a pass
149n/a foo.__isabstractmethod__ = NotBool()
150n/a foo = property(foo)
151n/a C.foo.__isabstractmethod__
152n/a
153n/a @unittest.skipIf(sys.flags.optimize >= 2,
154n/a "Docstrings are omitted with -O2 and above")
155n/a def test_property_builtin_doc_writable(self):
156n/a p = property(doc='basic')
157n/a self.assertEqual(p.__doc__, 'basic')
158n/a p.__doc__ = 'extended'
159n/a self.assertEqual(p.__doc__, 'extended')
160n/a
161n/a @unittest.skipIf(sys.flags.optimize >= 2,
162n/a "Docstrings are omitted with -O2 and above")
163n/a def test_property_decorator_doc_writable(self):
164n/a class PropertyWritableDoc(object):
165n/a
166n/a @property
167n/a def spam(self):
168n/a """Eggs"""
169n/a return "eggs"
170n/a
171n/a sub = PropertyWritableDoc()
172n/a self.assertEqual(sub.__class__.spam.__doc__, 'Eggs')
173n/a sub.__class__.spam.__doc__ = 'Spam'
174n/a self.assertEqual(sub.__class__.spam.__doc__, 'Spam')
175n/a
176n/a# Issue 5890: subclasses of property do not preserve method __doc__ strings
177n/aclass PropertySub(property):
178n/a """This is a subclass of property"""
179n/a
180n/aclass PropertySubSlots(property):
181n/a """This is a subclass of property that defines __slots__"""
182n/a __slots__ = ()
183n/a
184n/aclass PropertySubclassTests(unittest.TestCase):
185n/a
186n/a def test_slots_docstring_copy_exception(self):
187n/a try:
188n/a class Foo(object):
189n/a @PropertySubSlots
190n/a def spam(self):
191n/a """Trying to copy this docstring will raise an exception"""
192n/a return 1
193n/a except AttributeError:
194n/a pass
195n/a else:
196n/a raise Exception("AttributeError not raised")
197n/a
198n/a @unittest.skipIf(sys.flags.optimize >= 2,
199n/a "Docstrings are omitted with -O2 and above")
200n/a def test_docstring_copy(self):
201n/a class Foo(object):
202n/a @PropertySub
203n/a def spam(self):
204n/a """spam wrapped in property subclass"""
205n/a return 1
206n/a self.assertEqual(
207n/a Foo.spam.__doc__,
208n/a "spam wrapped in property subclass")
209n/a
210n/a @unittest.skipIf(sys.flags.optimize >= 2,
211n/a "Docstrings are omitted with -O2 and above")
212n/a def test_property_setter_copies_getter_docstring(self):
213n/a class Foo(object):
214n/a def __init__(self): self._spam = 1
215n/a @PropertySub
216n/a def spam(self):
217n/a """spam wrapped in property subclass"""
218n/a return self._spam
219n/a @spam.setter
220n/a def spam(self, value):
221n/a """this docstring is ignored"""
222n/a self._spam = value
223n/a foo = Foo()
224n/a self.assertEqual(foo.spam, 1)
225n/a foo.spam = 2
226n/a self.assertEqual(foo.spam, 2)
227n/a self.assertEqual(
228n/a Foo.spam.__doc__,
229n/a "spam wrapped in property subclass")
230n/a class FooSub(Foo):
231n/a @Foo.spam.setter
232n/a def spam(self, value):
233n/a """another ignored docstring"""
234n/a self._spam = 'eggs'
235n/a foosub = FooSub()
236n/a self.assertEqual(foosub.spam, 1)
237n/a foosub.spam = 7
238n/a self.assertEqual(foosub.spam, 'eggs')
239n/a self.assertEqual(
240n/a FooSub.spam.__doc__,
241n/a "spam wrapped in property subclass")
242n/a
243n/a @unittest.skipIf(sys.flags.optimize >= 2,
244n/a "Docstrings are omitted with -O2 and above")
245n/a def test_property_new_getter_new_docstring(self):
246n/a
247n/a class Foo(object):
248n/a @PropertySub
249n/a def spam(self):
250n/a """a docstring"""
251n/a return 1
252n/a @spam.getter
253n/a def spam(self):
254n/a """a new docstring"""
255n/a return 2
256n/a self.assertEqual(Foo.spam.__doc__, "a new docstring")
257n/a class FooBase(object):
258n/a @PropertySub
259n/a def spam(self):
260n/a """a docstring"""
261n/a return 1
262n/a class Foo2(FooBase):
263n/a @FooBase.spam.getter
264n/a def spam(self):
265n/a """a new docstring"""
266n/a return 2
267n/a self.assertEqual(Foo.spam.__doc__, "a new docstring")
268n/a
269n/a
270n/a
271n/aif __name__ == '__main__':
272n/a unittest.main()