diff options
author | Christian Heimes <christian@cheimes.de> | 2007-12-15 01:27:15 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2007-12-15 01:27:15 (GMT) |
commit | 0449f63f53fa0c2fcf779703d2db77d8a658cf7d (patch) | |
tree | f7e541fe54279a4b5954b79f529d652e659e6bd9 /Lib/test | |
parent | 54cc54c1fe26328d62c70fa55350ad89868c1d61 (diff) | |
download | cpython-0449f63f53fa0c2fcf779703d2db77d8a658cf7d.zip cpython-0449f63f53fa0c2fcf779703d2db77d8a658cf7d.tar.gz cpython-0449f63f53fa0c2fcf779703d2db77d8a658cf7d.tar.bz2 |
Merged revisions 59488-59511 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59489 | christian.heimes | 2007-12-14 03:33:57 +0100 (Fri, 14 Dec 2007) | 1 line
Silence a warning about an unsed variable in debug builds
........
r59490 | christian.heimes | 2007-12-14 03:35:23 +0100 (Fri, 14 Dec 2007) | 2 lines
Fixed bug #1620: New @spam.getter property syntax modifies the property in place.
I added also the feature that a @prop.getter decorator does not overwrite the doc string of the property if it was given as an argument to property().
........
r59491 | raymond.hettinger | 2007-12-14 03:49:47 +0100 (Fri, 14 Dec 2007) | 1 line
Cleaner method naming convention
........
r59492 | christian.heimes | 2007-12-14 04:02:34 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed a warning in _codecs_iso2022.c and some non C89 conform // comments.
........
r59493 | christian.heimes | 2007-12-14 05:38:13 +0100 (Fri, 14 Dec 2007) | 1 line
Fixed warning in ssl module
........
r59500 | raymond.hettinger | 2007-12-14 19:08:20 +0100 (Fri, 14 Dec 2007) | 1 line
Add line spacing for readability
........
r59501 | raymond.hettinger | 2007-12-14 19:12:21 +0100 (Fri, 14 Dec 2007) | 3 lines
Update method names for named tuples.
........
r59503 | georg.brandl | 2007-12-14 20:03:36 +0100 (Fri, 14 Dec 2007) | 3 lines
Add a section about nested listcomps to the tutorial.
Thanks to Ian Bruntlett and Robert Lehmann.
........
r59504 | raymond.hettinger | 2007-12-14 20:19:59 +0100 (Fri, 14 Dec 2007) | 1 line
Faster and simpler _replace() method
........
r59505 | raymond.hettinger | 2007-12-14 22:51:50 +0100 (Fri, 14 Dec 2007) | 1 line
Add usage note
........
r59507 | andrew.kuchling | 2007-12-14 23:41:18 +0100 (Fri, 14 Dec 2007) | 1 line
Remove warning about URL
........
r59510 | andrew.kuchling | 2007-12-14 23:52:36 +0100 (Fri, 14 Dec 2007) | 1 line
Bump the version number, and make a few small edits
........
r59511 | christian.heimes | 2007-12-15 00:42:36 +0100 (Sat, 15 Dec 2007) | 2 lines
Fixed bug #1628
The detection now works on Unix with Makefile, Makefile with VPATH and on Windows.
........
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_collections.py | 16 | ||||
-rw-r--r-- | Lib/test/test_property.py | 98 |
2 files changed, 106 insertions, 8 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 45b4082..5a4029d 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -28,11 +28,11 @@ class TestNamedTuple(unittest.TestCase): self.assertRaises(ValueError, namedtuple, 'abc', 'efg g%hi') # field with non-alpha char self.assertRaises(ValueError, namedtuple, 'abc', 'abc class') # field has keyword self.assertRaises(ValueError, namedtuple, 'abc', '8efg 9ghi') # field starts with digit - self.assertRaises(ValueError, namedtuple, 'abc', '__efg__ ghi') # field with double underscores + self.assertRaises(ValueError, namedtuple, 'abc', '_efg ghi') # field with leading underscore self.assertRaises(ValueError, namedtuple, 'abc', 'efg efg ghi') # duplicate field namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names - namedtuple('_', '_ __ ___') # Verify that underscores are allowed + namedtuple('_', 'a b c') # Test leading underscores in a typename def test_instance(self): Point = namedtuple('Point', 'x y') @@ -49,17 +49,17 @@ class TestNamedTuple(unittest.TestCase): self.assertEqual(repr(p), 'Point(x=11, y=22)') self.assert_('__dict__' not in dir(p)) # verify instance has no dict self.assert_('__weakref__' not in dir(p)) - self.assertEqual(p.__fields__, ('x', 'y')) # test __fields__ attribute - self.assertEqual(p.__replace__(x=1), (1, 22)) # test __replace__ method - self.assertEqual(p.__asdict__(), dict(x=11, y=22)) # test __dict__ method + self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute + self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method + self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method - # Verify that __fields__ is read-only + # Verify that _fields is read-only try: - p.__fields__ = ('F1' ,'F2') + p._fields = ('F1' ,'F2') except AttributeError: pass else: - self.fail('The __fields__ attribute needs to be read-only') + self.fail('The _fields attribute needs to be read-only') # verify that field string can have commas Point = namedtuple('Point', 'x, y') diff --git a/Lib/test/test_property.py b/Lib/test/test_property.py new file mode 100644 index 0000000..4b6e20c --- /dev/null +++ b/Lib/test/test_property.py @@ -0,0 +1,98 @@ +# Test case for property +# more tests are in test_descr + +import unittest +from test.test_support import run_unittest + +class PropertyBase(Exception): + pass + +class PropertyGet(PropertyBase): + pass + +class PropertySet(PropertyBase): + pass + +class PropertyDel(PropertyBase): + pass + +class BaseClass(object): + def __init__(self): + self._spam = 5 + + @property + def spam(self): + """BaseClass.getter""" + return self._spam + + @spam.setter + def spam(self, value): + self._spam = value + + @spam.deleter + def spam(self): + del self._spam + +class SubClass(BaseClass): + + @BaseClass.spam.getter + def spam(self): + """SubClass.getter""" + raise PropertyGet(self._spam) + + @spam.setter + def spam(self, value): + raise PropertySet(self._spam) + + @spam.deleter + def spam(self): + raise PropertyDel(self._spam) + +class PropertyDocBase(object): + _spam = 1 + def _get_spam(self): + return self._spam + spam = property(_get_spam, doc="spam spam spam") + +class PropertyDocSub(PropertyDocBase): + @PropertyDocBase.spam.getter + def spam(self): + """The decorator does not use this doc string""" + return self._spam + +class PropertyTests(unittest.TestCase): + def test_property_decorator_baseclass(self): + # see #1620 + base = BaseClass() + self.assertEqual(base.spam, 5) + self.assertEqual(base._spam, 5) + base.spam = 10 + self.assertEqual(base.spam, 10) + self.assertEqual(base._spam, 10) + delattr(base, "spam") + self.assert_(not hasattr(base, "spam")) + self.assert_(not hasattr(base, "_spam")) + base.spam = 20 + self.assertEqual(base.spam, 20) + self.assertEqual(base._spam, 20) + self.assertEqual(base.__class__.spam.__doc__, "BaseClass.getter") + + def test_property_decorator_subclass(self): + # see #1620 + sub = SubClass() + self.assertRaises(PropertyGet, getattr, sub, "spam") + self.assertRaises(PropertySet, setattr, sub, "spam", None) + self.assertRaises(PropertyDel, delattr, sub, "spam") + self.assertEqual(sub.__class__.spam.__doc__, "SubClass.getter") + + def test_property_decorator_doc(self): + base = PropertyDocBase() + sub = PropertyDocSub() + self.assertEqual(base.__class__.spam.__doc__, "spam spam spam") + self.assertEqual(sub.__class__.spam.__doc__, "spam spam spam") + +def test_main(): + run_unittest(PropertyTests) + +if __name__ == '__main__': + test_main() |