Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,33 @@ def __getitem__(self, other): return 5 # so that C is a sequence
self.assertEqual(operator.ixor (c, 5), "ixor")
self.assertEqual(operator.iconcat (c, c), "iadd")

def test_iconcat_without_getitem(self):
operator = self.module

msg = "'int' object can't be concatenated"
with self.assertRaisesRegex(TypeError, msg):
operator.iconcat(1, 0.5)

def test_index(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator.index is already tested in Lib/test/test_index.py.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess test_index only checks the C version of the module. This test covers the Python version with self.module used for C and Python version of the module.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Although it would be nice to extend this test. At least test it with int, float, Fraction, Decimal, None.

operator = self.module
class X:
def __index__(self):
return 1

self.assertEqual(operator.index(X()), 1)

def test_not_(self):
operator = self.module
class C:
def __bool__(self):
raise SyntaxError
self.assertRaises(TypeError, operator.not_)
self.assertRaises(SyntaxError, operator.not_, C())
self.assertFalse(operator.not_(5))
self.assertFalse(operator.not_([0]))
self.assertTrue(operator.not_(0))
self.assertTrue(operator.not_([]))

def test_length_hint(self):
operator = self.module
class X(object):
Expand All @@ -499,6 +526,13 @@ def __length_hint__(self):
with self.assertRaises(LookupError):
operator.length_hint(X(LookupError))

class Y: pass

msg = "'str' object cannot be interpreted as an integer"
with self.assertRaisesRegex(TypeError, msg):
operator.length_hint(X(2), "abc")
self.assertEqual(operator.length_hint(Y(), 10), 10)

def test_dunder_is_original(self):
operator = self.module

Expand Down