1 # Test case for property
2 # more tests are in test_descr
5 from test
.test_support
import run_unittest
7 class PropertyBase(Exception):
10 class PropertyGet(PropertyBase
):
13 class PropertySet(PropertyBase
):
16 class PropertyDel(PropertyBase
):
19 class BaseClass(object):
25 """BaseClass.getter"""
29 def spam(self
, value
):
36 class SubClass(BaseClass
):
38 @BaseClass.spam
.getter
41 raise PropertyGet(self
._spam
)
44 def spam(self
, value
):
45 raise PropertySet(self
._spam
)
49 raise PropertyDel(self
._spam
)
51 class PropertyDocBase(object):
55 spam
= property(_get_spam
, doc
="spam spam spam")
57 class PropertyDocSub(PropertyDocBase
):
58 @PropertyDocBase.spam
.getter
60 """The decorator does not use this doc string"""
63 class PropertyTests(unittest
.TestCase
):
64 def test_property_decorator_baseclass(self
):
67 self
.assertEqual(base
.spam
, 5)
68 self
.assertEqual(base
._spam
, 5)
70 self
.assertEqual(base
.spam
, 10)
71 self
.assertEqual(base
._spam
, 10)
73 self
.assert_(not hasattr(base
, "spam"))
74 self
.assert_(not hasattr(base
, "_spam"))
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
):
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")
95 run_unittest(PropertyTests
)
97 if __name__
== '__main__':