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

Method test_attrgetter

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

Source from the content-addressed store, hash-verified

368 self.assertFalse(operator.is_not_none(c))
369
370 def test_attrgetter(self):
371 operator = self.module
372 class A:
373 pass
374 a = A()
375 a.name = 'arthur'
376 f = operator.attrgetter('name')
377 self.assertEqual(f(a), 'arthur')
378 self.assertRaises(TypeError, f)
379 self.assertRaises(TypeError, f, a, 'dent')
380 self.assertRaises(TypeError, f, a, surname='dent')
381 f = operator.attrgetter('rank')
382 self.assertRaises(AttributeError, f, a)
383 self.assertRaises(TypeError, operator.attrgetter, 2)
384 self.assertRaises(TypeError, operator.attrgetter)
385
386 # multiple gets
387 record = A()
388 record.x = 'X'
389 record.y = 'Y'
390 record.z = 'Z'
391 self.assertEqual(operator.attrgetter('x','z','y')(record), ('X', 'Z', 'Y'))
392 self.assertRaises(TypeError, operator.attrgetter, ('x', (), 'y'))
393
394 class C(object):
395 def __getattr__(self, name):
396 raise SyntaxError
397 self.assertRaises(SyntaxError, operator.attrgetter('foo'), C())
398
399 # recursive gets
400 a = A()
401 a.name = 'arthur'
402 a.child = A()
403 a.child.name = 'thomas'
404 f = operator.attrgetter('child.name')
405 self.assertEqual(f(a), 'thomas')
406 self.assertRaises(AttributeError, f, a.child)
407 f = operator.attrgetter('name', 'child.name')
408 self.assertEqual(f(a), ('arthur', 'thomas'))
409 f = operator.attrgetter('name', 'child.name', 'child.child.name')
410 self.assertRaises(AttributeError, f, a)
411 f = operator.attrgetter('child.')
412 self.assertRaises(AttributeError, f, a)
413 f = operator.attrgetter('.child')
414 self.assertRaises(AttributeError, f, a)
415
416 a.child.child = A()
417 a.child.child.name = 'johnson'
418 f = operator.attrgetter('child.child.name')
419 self.assertEqual(f(a), 'johnson')
420 f = operator.attrgetter('name', 'child.name', 'child.child.name')
421 self.assertEqual(f(a), ('arthur', 'thomas', 'johnson'))
422
423 def test_itemgetter(self):
424 operator = self.module

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected