test_namedtuple.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import unittest, keyword
  2. from collections import namedtuple
  3. TestNT = namedtuple('TestNT', 'x y z') # type used for pickle tests
  4. class TestNamedTuple(unittest.TestCase):
  5. def test_factory(self):
  6. Point = namedtuple('Point', 'x y')
  7. # self.assertEqual(Point.__name__, 'Point')
  8. # self.assertEqual(Point.__slots__, ())
  9. # self.assertEqual(Point.__module__, __name__)
  10. # self.assertEqual(Point.__getitem__, tuple.__getitem__)
  11. # self.assertEqual(Point._fields, ('x', 'y'))
  12. self.assertRaises(ValueError, namedtuple, 'abc%', 'efg ghi') # type has non-alpha char
  13. self.assertRaises(ValueError, namedtuple, 'def', 'efg ghi') # type has keyword
  14. self.assertRaises(ValueError, namedtuple, '9abc', 'efg ghi') # type starts with digit
  15. self.assertRaises(ValueError, namedtuple, 'abc', 'efg g%hi') # field with non-alpha char
  16. self.assertRaises(ValueError, namedtuple, 'abc', 'abc class') # field has keyword
  17. self.assertRaises(ValueError, namedtuple, 'abc', '8efg 9ghi') # field starts with digit
  18. self.assertRaises(ValueError, namedtuple, 'abc', '_efg ghi') # field with leading underscore
  19. self.assertRaises(ValueError, namedtuple, 'abc', 'efg efg ghi') # duplicate field
  20. namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names
  21. namedtuple('_', 'a b c') # Test leading underscores in a typename
  22. nt = namedtuple('nt', u'the quick brown fox') # check unicode input
  23. #self.assertNotIn("u'", repr(nt._fields))
  24. nt = namedtuple('nt', (u'the', u'quick')) # check unicode input
  25. #self.assertNotIn("u'", repr(nt._fields))
  26. self.assertRaises(TypeError, Point, [11]) # catch too few args
  27. self.assertRaises(TypeError, Point, [11, 22, 33]) # catch too many args
  28. # def test_name_fixer(self):
  29. # for spec, renamed in [
  30. # [('efg', 'g%hi'), ('efg', '_1')], # field with non-alpha char
  31. # [('abc', 'class'), ('abc', '_1')], # field has keyword
  32. # [('8efg', '9ghi'), ('_0', '_1')], # field starts with digit
  33. # [('abc', '_efg'), ('abc', '_1')], # field with leading underscore
  34. # [('abc', 'efg', 'efg', 'ghi'), ('abc', 'efg', '_2', 'ghi')], # duplicate field
  35. # [('abc', '', 'x'), ('abc', '_1', 'x')], # fieldname is a space
  36. # ]:
  37. # self.assertEqual(namedtuple('NT', spec, rename=True)._fields, renamed)
  38. def test_instance(self):
  39. Point = namedtuple('Point', 'x y')
  40. p = Point(11, 22)
  41. #self.assertEqual(p, Point(x=11, y=22))
  42. self.assertEqual(p, Point(11, 22))
  43. #self.assertEqual(p, Point(y=22, x=11))
  44. self.assertEqual(p, Point(*(11, 22)))
  45. #self.assertEqual(p, Point(**dict(x=11, y=22)))
  46. self.assertRaises(TypeError, Point, 1) # too few args
  47. self.assertRaises(TypeError, Point, 1, 2, 3) # too many args
  48. # self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals()) # wrong keyword argument
  49. # self.assertRaises(TypeError, eval, 'Point(x=1)', locals()) # missing keyword argument
  50. self.assertEqual(repr(p), 'Point(x=11, y=22)')
  51. #self.assertNotIn('__weakref__', dir(p))
  52. #self.assertEqual(p, Point._make([11, 22])) # test _make classmethod
  53. #self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute
  54. #self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method
  55. #self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
  56. #self.assertEqual(vars(p), p._asdict()) # verify that vars() works
  57. # try:
  58. # p._replace(x=1, error=2)
  59. # except ValueError:
  60. # pass
  61. # else:
  62. # self._fail('Did not detect an incorrect fieldname')
  63. # p = Point(x=11, y=22)
  64. # self.assertEqual(repr(p), 'Point(x=11, y=22)')
  65. # verify that fieldspec can be a non-string sequence
  66. Point = namedtuple('Point', ('x', 'y'))
  67. p = Point(11, 22)
  68. self.assertEqual(repr(p), 'Point(x=11, y=22)')
  69. def test_tupleness(self):
  70. Point = namedtuple('Point', 'x y')
  71. p = Point(11, 22)
  72. #self.assertIsInstance(p, tuple)
  73. self.assertEqual(p, (11, 22)) # matches a real tuple
  74. self.assertEqual(tuple(p), (11, 22)) # coercable to a real tuple
  75. self.assertEqual(list(p), [11, 22]) # coercable to a list
  76. self.assertEqual(max(p), 22) # iterable
  77. self.assertEqual(max(*p), 22) # star-able
  78. x, y = p
  79. self.assertEqual(p, (x, y)) # unpacks like a tuple
  80. self.assertEqual((p[0], p[1]), (11, 22)) # indexable like a tuple
  81. #self.assertRaises(IndexError, p.__getitem__, 3)
  82. self.assertEqual(p.x, x)
  83. self.assertEqual(p.y, y)
  84. # self.assertRaises(AttributeError, eval, 'p.z', locals())
  85. def test_odd_sizes(self):
  86. Zero = namedtuple('Zero', '')
  87. #self.assertEqual(Zero(), ())
  88. #self.assertEqual(Zero._make([]), ())
  89. #self.assertEqual(repr(Zero()), 'Zero()')
  90. #self.assertEqual(Zero()._asdict(), {})
  91. #self.assertEqual(Zero()._fields, ())
  92. Dot = namedtuple('Dot', 'd')
  93. self.assertEqual(Dot(1), (1,))
  94. #self.assertEqual(Dot._make([1]), (1,))
  95. self.assertEqual(Dot(1).d, 1)
  96. #self.assertEqual(repr(Dot(1)), 'Dot(d=1)')
  97. #self.assertEqual(Dot(1)._asdict(), {'d':1})
  98. #self.assertEqual(Dot(1)._replace(d=999), (999,))
  99. #self.assertEqual(Dot(1)._fields, ('d',))
  100. n = 5000
  101. import string, random
  102. names = list(set(''.join([random.choice(string.ascii_letters)
  103. for j in range(10)]) for i in range(n)))
  104. n = len(names)
  105. Big = namedtuple('Big', names)
  106. b = Big(*range(n))
  107. self.assertEqual(b, tuple(range(n)))
  108. #self.assertEqual(Big._make(range(n)), tuple(range(n)))
  109. for pos, name in enumerate(names):
  110. self.assertEqual(getattr(b, name), pos)
  111. repr(b) # make sure repr() doesn't blow-up
  112. #d = b._asdict()
  113. #d_expected = dict(zip(names, range(n)))
  114. #self.assertEqual(d, d_expected)
  115. #b2 = b._replace(**dict([(names[1], 999),(names[-5], 42)]))
  116. #b2_expected = range(n)
  117. #b2_expected[1] = 999
  118. #b2_expected[-5] = 42
  119. #self.assertEqual(b2, tuple(b2_expected))
  120. #self.assertEqual(b._fields, tuple(names))
  121. def test_name_conflicts(self):
  122. # Some names like "self", "cls", "tuple", "itemgetter", and "property"
  123. # failed when used as field names. Test to make sure these now work.
  124. T = namedtuple('T', 'itemgetter property self cls tuple')
  125. t = T(1, 2, 3, 4, 5)
  126. self.assertEqual(t, (1,2,3,4,5))
  127. # newt = t._replace(itemgetter=10, property=20, self=30, cls=40, tuple=50)
  128. # self.assertEqual(newt, (10,20,30,40,50))
  129. # Broader test of all interesting names in a template
  130. # with test_support.captured_stdout() as template:
  131. # T = namedtuple('T', 'x', verbose=True)
  132. # words = set(re.findall('[A-Za-z]+', template.getvalue()))
  133. # words -= set(keyword.kwlist)
  134. # T = namedtuple('T', words)
  135. # # test __new__
  136. # values = tuple(range(len(words)))
  137. # t = T(*values)
  138. # self.assertEqual(t, values)
  139. # t = T(**dict(zip(T._fields, values)))
  140. # self.assertEqual(t, values)
  141. # # test _make
  142. # t = T._make(values)
  143. # self.assertEqual(t, values)
  144. # # exercise __repr__
  145. # repr(t)
  146. # # test _asdict
  147. # self.assertEqual(t._asdict(), dict(zip(T._fields, values)))
  148. # # test _replace
  149. # t = T._make(values)
  150. # newvalues = tuple(v*10 for v in values)
  151. # newt = t._replace(**dict(zip(T._fields, newvalues)))
  152. # self.assertEqual(newt, newvalues)
  153. # # test _fields
  154. # self.assertEqual(T._fields, tuple(words))
  155. # # test __getnewargs__
  156. # self.assertEqual(t.__getnewargs__(), values)
  157. if __name__ == "__main__":
  158. unittest.main(verbosity=2)