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

Method test_length_hint

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

Source from the content-addressed store, hash-verified

583 self.assertTrue(operator.not_([]))
584
585 def test_length_hint(self):
586 operator = self.module
587 class X(object):
588 def __init__(self, value):
589 self.value = value
590
591 def __length_hint__(self):
592 if type(self.value) is type:
593 raise self.value
594 else:
595 return self.value
596
597 self.assertEqual(operator.length_hint([], 2), 0)
598 self.assertEqual(operator.length_hint(iter([1, 2, 3])), 3)
599
600 self.assertEqual(operator.length_hint(X(2)), 2)
601 self.assertEqual(operator.length_hint(X(NotImplemented), 4), 4)
602 self.assertEqual(operator.length_hint(X(TypeError), 12), 12)
603 with self.assertRaises(TypeError):
604 operator.length_hint(X("abc"))
605 with self.assertRaises(ValueError):
606 operator.length_hint(X(-2))
607 with self.assertRaises(LookupError):
608 operator.length_hint(X(LookupError))
609
610 class Y: pass
611
612 msg = "'str' object cannot be interpreted as an integer"
613 with self.assertRaisesRegex(TypeError, msg):
614 operator.length_hint(X(2), "abc")
615 self.assertEqual(operator.length_hint(Y(), 10), 10)
616
617 def test_call(self):
618 operator = self.module

Callers

nothing calls this directly

Calls 7

iterFunction · 0.85
assertRaisesRegexMethod · 0.80
XClass · 0.70
YClass · 0.70
assertEqualMethod · 0.45
length_hintMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected