diff options
author | Raymond Hettinger <python@rcn.com> | 2009-05-27 01:53:46 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-05-27 01:53:46 (GMT) |
commit | 499b2ee8a8c2a498cb068f09f6be1bfb6da85225 (patch) | |
tree | 888bcf7499f22a3f644287af607cef4fb90a69d6 /Lib/test/test_collections.py | |
parent | 089ba7f69012a51c3c809a4d7bde62d67ebbb537 (diff) | |
download | cpython-499b2ee8a8c2a498cb068f09f6be1bfb6da85225.zip cpython-499b2ee8a8c2a498cb068f09f6be1bfb6da85225.tar.gz cpython-499b2ee8a8c2a498cb068f09f6be1bfb6da85225.tar.bz2 |
Stronger tests for namedtuple() to prevent future name conflict errors.
Diffstat (limited to 'Lib/test/test_collections.py')
-rw-r--r-- | Lib/test/test_collections.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index ad79ad5..8387564 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -8,6 +8,8 @@ from test import mapping_tests import pickle, copy from random import randrange, shuffle import operator +import keyword +import re from collections import Hashable, Iterable, Iterator from collections import Sized, Container, Callable from collections import Set, MutableSet @@ -182,6 +184,35 @@ class TestNamedTuple(unittest.TestCase): newt = t._replace(itemgetter=10, property=20, self=30, cls=40, tuple=50) self.assertEqual(newt, (10,20,30,40,50)) + # Broader test of all interesting names in a template + with support.captured_stdout() as template: + T = namedtuple('T', 'x', verbose=True) + words = set(re.findall('[A-Za-z]+', template.getvalue())) + words -= set(keyword.kwlist) + T = namedtuple('T', words) + # test __new__ + values = tuple(range(len(words))) + t = T(*values) + self.assertEqual(t, values) + t = T(**dict(zip(T._fields, values))) + self.assertEqual(t, values) + # test _make + t = T._make(values) + self.assertEqual(t, values) + # exercise __repr__ + repr(t) + # test _asdict + self.assertEqual(t._asdict(), dict(zip(T._fields, values))) + # test _replace + t = T._make(values) + newvalues = tuple(v*10 for v in values) + newt = t._replace(**dict(zip(T._fields, newvalues))) + self.assertEqual(newt, newvalues) + # test _fields + self.assertEqual(T._fields, tuple(words)) + # test __getnewargs__ + self.assertEqual(t.__getnewargs__(), values) + class ABCTestCase(unittest.TestCase): def validate_abstract_methods(self, abc, *names): |