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

Method test_instance

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

Source from the content-addressed store, hash-verified

469 self.assertEqual(NT.__module__, collections)
470
471 def test_instance(self):
472 Point = namedtuple('Point', 'x y')
473 p = Point(11, 22)
474 self.assertEqual(p, Point(x=11, y=22))
475 self.assertEqual(p, Point(11, y=22))
476 self.assertEqual(p, Point(y=22, x=11))
477 self.assertEqual(p, Point(*(11, 22)))
478 self.assertEqual(p, Point(**dict(x=11, y=22)))
479 self.assertRaises(TypeError, Point, 1) # too few args
480 self.assertRaises(TypeError, Point, 1, 2, 3) # too many args
481 with self.assertRaises(TypeError): # wrong keyword argument
482 Point(XXX=1, y=2)
483 with self.assertRaises(TypeError): # missing keyword argument
484 Point(x=1)
485 self.assertEqual(repr(p), 'Point(x=11, y=22)')
486 self.assertNotIn('__weakref__', dir(p))
487 self.assertEqual(p, Point._make([11, 22])) # test _make classmethod
488 self.assertEqual(p._fields, ('x', 'y')) # test _fields attribute
489 self.assertEqual(p._replace(x=1), (1, 22)) # test _replace method
490 self.assertEqual(p._asdict(), dict(x=11, y=22)) # test _asdict method
491
492 with self.assertRaises(TypeError):
493 p._replace(x=1, error=2)
494
495 # verify that field string can have commas
496 Point = namedtuple('Point', 'x, y')
497 p = Point(x=11, y=22)
498 self.assertEqual(repr(p), 'Point(x=11, y=22)')
499
500 # verify that fieldspec can be a non-string sequence
501 Point = namedtuple('Point', ('x', 'y'))
502 p = Point(x=11, y=22)
503 self.assertEqual(repr(p), 'Point(x=11, y=22)')
504
505 def test_tupleness(self):
506 Point = namedtuple('Point', 'x y')

Callers

nothing calls this directly

Calls 8

namedtupleFunction · 0.90
reprFunction · 0.85
dirFunction · 0.85
assertNotInMethod · 0.80
_makeMethod · 0.80
PointClass · 0.70
assertEqualMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected