2007-03-01 02:16:43 -04:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
2007-10-08 18:26:58 -03:00
|
|
|
from collections import named_tuple
|
2007-03-01 02:16:43 -04:00
|
|
|
|
|
|
|
class TestNamedTuple(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_factory(self):
|
2007-10-08 18:26:58 -03:00
|
|
|
Point = named_tuple('Point', 'x y')
|
2007-03-01 02:16:43 -04:00
|
|
|
self.assertEqual(Point.__name__, 'Point')
|
|
|
|
self.assertEqual(Point.__doc__, 'Point(x, y)')
|
|
|
|
self.assertEqual(Point.__slots__, ())
|
|
|
|
self.assertEqual(Point.__module__, __name__)
|
|
|
|
self.assertEqual(Point.__getitem__, tuple.__getitem__)
|
2007-10-16 18:28:32 -03:00
|
|
|
|
|
|
|
self.assertRaises(ValueError, named_tuple, 'abc%', 'efg ghi') # type has non-alpha char
|
|
|
|
self.assertRaises(ValueError, named_tuple, 'class', 'efg ghi') # type has keyword
|
|
|
|
self.assertRaises(ValueError, named_tuple, '9abc', 'efg ghi') # type starts with digit
|
|
|
|
|
|
|
|
self.assertRaises(ValueError, named_tuple, 'abc', 'efg g%hi') # field with non-alpha char
|
|
|
|
self.assertRaises(ValueError, named_tuple, 'abc', 'abc class') # field has keyword
|
|
|
|
self.assertRaises(ValueError, named_tuple, 'abc', '8efg 9ghi') # field starts with digit
|
|
|
|
self.assertRaises(ValueError, named_tuple, 'abc', '__efg__ ghi') # field with double underscores
|
|
|
|
self.assertRaises(ValueError, named_tuple, 'abc', 'efg efg ghi') # duplicate field
|
|
|
|
|
2007-10-08 18:26:58 -03:00
|
|
|
named_tuple('Point0', 'x1 y2') # Verify that numbers are allowed in names
|
2007-03-01 02:16:43 -04:00
|
|
|
|
|
|
|
def test_instance(self):
|
2007-10-08 18:26:58 -03:00
|
|
|
Point = named_tuple('Point', 'x y')
|
2007-03-01 02:16:43 -04:00
|
|
|
p = Point(11, 22)
|
|
|
|
self.assertEqual(p, Point(x=11, y=22))
|
|
|
|
self.assertEqual(p, Point(11, y=22))
|
|
|
|
self.assertEqual(p, Point(y=22, x=11))
|
|
|
|
self.assertEqual(p, Point(*(11, 22)))
|
|
|
|
self.assertEqual(p, Point(**dict(x=11, y=22)))
|
|
|
|
self.assertRaises(TypeError, Point, 1) # too few args
|
|
|
|
self.assertRaises(TypeError, Point, 1, 2, 3) # too many args
|
|
|
|
self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals()) # wrong keyword argument
|
|
|
|
self.assertRaises(TypeError, eval, 'Point(x=1)', locals()) # missing keyword argument
|
|
|
|
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))
|
2007-09-16 21:55:00 -03:00
|
|
|
self.assertEqual(p.__fields__, ('x', 'y')) # test __fields__ attribute
|
|
|
|
self.assertEqual(p.__replace__('x', 1), (1, 22)) # test __replace__ method
|
2007-10-04 23:47:07 -03:00
|
|
|
self.assertEqual(p.__asdict__(), dict(x=11, y=22)) # test __dict__ method
|
2007-09-16 21:55:00 -03:00
|
|
|
|
|
|
|
# verify that field string can have commas
|
2007-10-08 18:26:58 -03:00
|
|
|
Point = named_tuple('Point', 'x, y')
|
2007-09-16 21:55:00 -03:00
|
|
|
p = Point(x=11, y=22)
|
|
|
|
self.assertEqual(repr(p), 'Point(x=11, y=22)')
|
2007-03-01 02:16:43 -04:00
|
|
|
|
2007-10-08 06:14:28 -03:00
|
|
|
# verify that fieldspec can be a non-string sequence
|
2007-10-08 18:26:58 -03:00
|
|
|
Point = named_tuple('Point', ('x', 'y'))
|
2007-10-08 06:14:28 -03:00
|
|
|
p = Point(x=11, y=22)
|
|
|
|
self.assertEqual(repr(p), 'Point(x=11, y=22)')
|
|
|
|
|
2007-03-01 02:16:43 -04:00
|
|
|
def test_tupleness(self):
|
2007-10-08 18:26:58 -03:00
|
|
|
Point = named_tuple('Point', 'x y')
|
2007-03-01 02:16:43 -04:00
|
|
|
p = Point(11, 22)
|
|
|
|
|
|
|
|
self.assert_(isinstance(p, tuple))
|
|
|
|
self.assertEqual(p, (11, 22)) # matches a real tuple
|
|
|
|
self.assertEqual(tuple(p), (11, 22)) # coercable to a real tuple
|
|
|
|
self.assertEqual(list(p), [11, 22]) # coercable to a list
|
|
|
|
self.assertEqual(max(p), 22) # iterable
|
|
|
|
self.assertEqual(max(*p), 22) # star-able
|
|
|
|
x, y = p
|
|
|
|
self.assertEqual(p, (x, y)) # unpacks like a tuple
|
|
|
|
self.assertEqual((p[0], p[1]), (11, 22)) # indexable like a tuple
|
|
|
|
self.assertRaises(IndexError, p.__getitem__, 3)
|
|
|
|
|
|
|
|
self.assertEqual(p.x, x)
|
|
|
|
self.assertEqual(p.y, y)
|
|
|
|
self.assertRaises(AttributeError, eval, 'p.z', locals())
|
|
|
|
|
2007-09-18 00:33:19 -03:00
|
|
|
def test_odd_sizes(self):
|
2007-10-08 18:26:58 -03:00
|
|
|
Zero = named_tuple('Zero', '')
|
2007-09-18 00:33:19 -03:00
|
|
|
self.assertEqual(Zero(), ())
|
2007-10-08 18:26:58 -03:00
|
|
|
Dot = named_tuple('Dot', 'd')
|
2007-09-18 00:33:19 -03:00
|
|
|
self.assertEqual(Dot(1), (1,))
|
|
|
|
|
2007-03-01 02:16:43 -04:00
|
|
|
def test_main(verbose=None):
|
2007-05-18 22:11:16 -03:00
|
|
|
import collections as CollectionsModule
|
2007-03-01 02:16:43 -04:00
|
|
|
test_classes = [TestNamedTuple]
|
|
|
|
test_support.run_unittest(*test_classes)
|
2007-05-18 22:11:16 -03:00
|
|
|
test_support.run_doctest(CollectionsModule, verbose)
|
2007-03-01 02:16:43 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main(verbose=True)
|