summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_collections.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-03-24 03:33:30 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-03-24 03:33:30 (GMT)
commitf6d3e8eaef83c82416c2ca9d639ed59beba1f877 (patch)
tree18ad65b96e2635fe4ca08e61ce7c783777e3b9f3 /Lib/test/test_collections.py
parent5d43cff62373f8405765faeef48f0bd2ccc22d9b (diff)
downloadcpython-f6d3e8eaef83c82416c2ca9d639ed59beba1f877.zip
cpython-f6d3e8eaef83c82416c2ca9d639ed59beba1f877.tar.gz
cpython-f6d3e8eaef83c82416c2ca9d639ed59beba1f877.tar.bz2
Add tests for _source to importable and exec'able.
Move __name__ back out of the template; the responsibility for setting __name__ lies with the caller (which knows something about the new namespace), not with the class definition (which doesn't know about the namespace it is being built in).
Diffstat (limited to 'Lib/test/test_collections.py')
-rw-r--r--Lib/test/test_collections.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index cdc7db9..4ef27ce 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -1,6 +1,7 @@
"""Unit tests for collections.py."""
import unittest, doctest, operator
+from test.support import TESTFN, forget, unlink
import inspect
from test import support
from collections import namedtuple, Counter, OrderedDict, _count_elements
@@ -327,6 +328,39 @@ class TestNamedTuple(unittest.TestCase):
pass
self.assertEqual(repr(B(1)), 'B(x=1)')
+ def test_source(self):
+ # verify that _source can be run through exec()
+ tmp = namedtuple('Color', 'red green blue')
+ self.assertNotIn('Color', globals())
+ exec(tmp._source, globals())
+ self.assertIn('Color', globals())
+ c = Color(10, 20, 30)
+ self.assertEqual((c.red, c.green, c.blue), (10, 20, 30))
+ self.assertEqual(Color._fields, ('red', 'green', 'blue'))
+
+ def test_source_importable(self):
+ tmp = namedtuple('Color', 'hue sat val')
+
+ compiled = None
+ source = TESTFN + '.py'
+ with open(source, 'w') as f:
+ print(tmp._source, file=f)
+
+ if TESTFN in sys.modules:
+ del sys.modules[TESTFN]
+ try:
+ mod = __import__(TESTFN)
+ compiled = mod.__file__
+ Color = mod.Color
+ c = Color(10, 20, 30)
+ self.assertEqual((c.hue, c.sat, c.val), (10, 20, 30))
+ self.assertEqual(Color._fields, ('hue', 'sat', 'val'))
+ finally:
+ forget(TESTFN)
+ if compiled:
+ unlink(compiled)
+ unlink(source)
+
################################################################################
### Abstract Base Classes