MCPcopy Index your code
hub / github.com/python/cpython / test_methodcaller

Method test_methodcaller

Lib/test/test_operator.py:474–512  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

472 self.assertEqual(operator.itemgetter(0)(range(100, 200)), 100)
473
474 def test_methodcaller(self):
475 operator = self.module
476 self.assertRaises(TypeError, operator.methodcaller)
477 self.assertRaises(TypeError, operator.methodcaller, 12)
478 class A:
479 def foo(self, *args, **kwds):
480 return args[0] + args[1]
481 def bar(self, f=42):
482 return f
483 def baz(*args, **kwds):
484 return kwds['name'], kwds['self']
485 def return_arguments(self, *args, **kwds):
486 return args, kwds
487 a = A()
488 f = operator.methodcaller('foo')
489 self.assertRaises(IndexError, f, a)
490 f = operator.methodcaller('foo', 1, 2)
491 self.assertEqual(f(a), 3)
492 self.assertRaises(TypeError, f)
493 self.assertRaises(TypeError, f, a, 3)
494 self.assertRaises(TypeError, f, a, spam=3)
495 f = operator.methodcaller('bar')
496 self.assertEqual(f(a), 42)
497 self.assertRaises(TypeError, f, a, a)
498 f = operator.methodcaller('bar', f=5)
499 self.assertEqual(f(a), 5)
500 f = operator.methodcaller('baz', name='spam', self='eggs')
501 self.assertEqual(f(a), ('spam', 'eggs'))
502
503 many_positional_arguments = tuple(range(10))
504 many_kw_arguments = dict(zip('abcdefghij', range(10)))
505 f = operator.methodcaller('return_arguments', *many_positional_arguments)
506 self.assertEqual(f(a), (many_positional_arguments, {}))
507
508 f = operator.methodcaller('return_arguments', **many_kw_arguments)
509 self.assertEqual(f(a), ((), many_kw_arguments))
510
511 f = operator.methodcaller('return_arguments', *many_positional_arguments, **many_kw_arguments)
512 self.assertEqual(f(a), (many_positional_arguments, many_kw_arguments))
513
514 def test_inplace(self):
515 operator = self.module

Callers

nothing calls this directly

Calls 4

AClass · 0.70
fFunction · 0.70
assertRaisesMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected