Issue #3389: Allow resolving dotted names for handlers in logging configuration files...
[python.git] / Lib / test / test_property.py
blob4b6e20caa3925848130612282f85a5c1177dd140
1 # Test case for property
2 # more tests are in test_descr
4 import unittest
5 from test.test_support import run_unittest
7 class PropertyBase(Exception):
8 pass
10 class PropertyGet(PropertyBase):
11 pass
13 class PropertySet(PropertyBase):
14 pass
16 class PropertyDel(PropertyBase):
17 pass
19 class BaseClass(object):
20 def __init__(self):
21 self._spam = 5
23 @property
24 def spam(self):
25 """BaseClass.getter"""
26 return self._spam
28 @spam.setter
29 def spam(self, value):
30 self._spam = value
32 @spam.deleter
33 def spam(self):
34 del self._spam
36 class SubClass(BaseClass):
38 @BaseClass.spam.getter
39 def spam(self):
40 """SubClass.getter"""
41 raise PropertyGet(self._spam)
43 @spam.setter
44 def spam(self, value):
45 raise PropertySet(self._spam)
47 @spam.deleter
48 def spam(self):
49 raise PropertyDel(self._spam)
51 class PropertyDocBase(object):
52 _spam = 1
53 def _get_spam(self):
54 return self._spam
55 spam = property(_get_spam, doc="spam spam spam")
57 class PropertyDocSub(PropertyDocBase):
58 @PropertyDocBase.spam.getter
59 def spam(self):
60 """The decorator does not use this doc string"""
61 return self._spam
63 class PropertyTests(unittest.TestCase):
64 def test_property_decorator_baseclass(self):
65 # see #1620
66 base = BaseClass()
67 self.assertEqual(base.spam, 5)
68 self.assertEqual(base._spam, 5)
69 base.spam = 10
70 self.assertEqual(base.spam, 10)
71 self.assertEqual(base._spam, 10)
72 delattr(base, "spam")
73 self.assert_(not hasattr(base, "spam"))
74 self.assert_(not hasattr(base, "_spam"))
75 base.spam = 20
76 self.assertEqual(base.spam, 20)
77 self.assertEqual(base._spam, 20)
78 self.assertEqual(base.__class__.spam.__doc__, "BaseClass.getter")
80 def test_property_decorator_subclass(self):
81 # see #1620
82 sub = SubClass()
83 self.assertRaises(PropertyGet, getattr, sub, "spam")
84 self.assertRaises(PropertySet, setattr, sub, "spam", None)
85 self.assertRaises(PropertyDel, delattr, sub, "spam")
86 self.assertEqual(sub.__class__.spam.__doc__, "SubClass.getter")
88 def test_property_decorator_doc(self):
89 base = PropertyDocBase()
90 sub = PropertyDocSub()
91 self.assertEqual(base.__class__.spam.__doc__, "spam spam spam")
92 self.assertEqual(sub.__class__.spam.__doc__, "spam spam spam")
94 def test_main():
95 run_unittest(PropertyTests)
97 if __name__ == '__main__':
98 test_main()