home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 July / maximum-cd-2011-07.iso / DiscContents / LibO_3.3.2_Win_x86_install_multi.exe / libreoffice1.cab / test_property.py < prev    next >
Encoding:
Python Source  |  2011-03-15  |  2.5 KB  |  99 lines

  1. # Test case for property
  2. # more tests are in test_descr
  3.  
  4. import unittest
  5. from test.test_support import run_unittest
  6.  
  7. class PropertyBase(Exception):
  8.     pass
  9.  
  10. class PropertyGet(PropertyBase):
  11.     pass
  12.  
  13. class PropertySet(PropertyBase):
  14.     pass
  15.  
  16. class PropertyDel(PropertyBase):
  17.     pass
  18.  
  19. class BaseClass(object):
  20.     def __init__(self):
  21.         self._spam = 5
  22.  
  23.     @property
  24.     def spam(self):
  25.         """BaseClass.getter"""
  26.         return self._spam
  27.  
  28.     @spam.setter
  29.     def spam(self, value):
  30.         self._spam = value
  31.  
  32.     @spam.deleter
  33.     def spam(self):
  34.         del self._spam
  35.  
  36. class SubClass(BaseClass):
  37.  
  38.     @BaseClass.spam.getter
  39.     def spam(self):
  40.         """SubClass.getter"""
  41.         raise PropertyGet(self._spam)
  42.  
  43.     @spam.setter
  44.     def spam(self, value):
  45.         raise PropertySet(self._spam)
  46.  
  47.     @spam.deleter
  48.     def spam(self):
  49.         raise PropertyDel(self._spam)
  50.  
  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")
  56.  
  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
  62.  
  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")
  79.  
  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")
  87.  
  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")
  93.  
  94. def test_main():
  95.     run_unittest(PropertyTests)
  96.  
  97. if __name__ == '__main__':
  98.     test_main()
  99.