MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_factory

Method test_factory

Lib/test/test_collections.py:320–347  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

318class TestNamedTuple(unittest.TestCase):
319
320 def test_factory(self):
321 Point = namedtuple('Point', 'x y')
322 self.assertEqual(Point.__name__, 'Point')
323 self.assertEqual(Point.__slots__, ())
324 self.assertEqual(Point.__module__, __name__)
325 self.assertEqual(Point.__getitem__, tuple.__getitem__)
326 self.assertEqual(Point._fields, ('x', 'y'))
327
328 self.assertRaises(ValueError, namedtuple, 'abc%', 'efg ghi') # type has non-alpha char
329 self.assertRaises(ValueError, namedtuple, 'class', 'efg ghi') # type has keyword
330 self.assertRaises(ValueError, namedtuple, '9abc', 'efg ghi') # type starts with digit
331
332 self.assertRaises(ValueError, namedtuple, 'abc', 'efg g%hi') # field with non-alpha char
333 self.assertRaises(ValueError, namedtuple, 'abc', 'abc class') # field has keyword
334 self.assertRaises(ValueError, namedtuple, 'abc', '8efg 9ghi') # field starts with digit
335 self.assertRaises(ValueError, namedtuple, 'abc', '_efg ghi') # field with leading underscore
336 self.assertRaises(ValueError, namedtuple, 'abc', 'efg efg ghi') # duplicate field
337
338 namedtuple('Point0', 'x1 y2') # Verify that numbers are allowed in names
339 namedtuple('_', 'a b c') # Test leading underscores in a typename
340
341 nt = namedtuple('nt', 'the quick brown fox') # check unicode input
342 self.assertNotIn("u'", repr(nt._fields))
343 nt = namedtuple('nt', ('the', 'quick')) # check unicode input
344 self.assertNotIn("u'", repr(nt._fields))
345
346 self.assertRaises(TypeError, Point._make, [11]) # catch too few args
347 self.assertRaises(TypeError, Point._make, [11, 22, 33]) # catch too many args
348
349 def test_defaults(self):
350 Point = namedtuple('Point', 'x y', defaults=(10, 20)) # 2 defaults

Callers

nothing calls this directly

Calls 5

namedtupleFunction · 0.90
reprFunction · 0.85
assertNotInMethod · 0.80
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected