From 8717c9b9c80db4989d3eebc440b314e19fe2f8b3 Mon Sep 17 00:00:00 2001 From: Karthikeyan Singaravelan Date: Mon, 13 Jan 2020 23:56:53 +0530 Subject: [PATCH 1/2] Add tests for operator module. --- Lib/test/test_operator.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py index f46d94a226717a..3c860342c0c387 100644 --- a/Lib/test/test_operator.py +++ b/Lib/test/test_operator.py @@ -474,6 +474,30 @@ 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 + class X: pass + + msg = "'X' object can't be concatenated" + with self.assertRaisesRegex(TypeError, msg): + operator.iconcat(X(), X()) + + def test_index(self): + operator = self.module + class X: + def __index__(self): + return 1 + + self.assertEqual(operator.index(X()), 1) + + def test_not_(self): + operator = self.module + class X: + def __bool__(self): + return False + + self.assertEqual(operator.not_(X()), True) + def test_length_hint(self): operator = self.module class X(object): @@ -499,6 +523,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 From 4155be94c3bf0937190251e5910e9d4f7afc58cc Mon Sep 17 00:00:00 2001 From: Karthikeyan Singaravelan Date: Sat, 14 Mar 2020 17:58:35 +0530 Subject: [PATCH 2/2] Refactor not_ tests like truth test. Check iconcat with objects supporting addition. --- Lib/test/test_operator.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py index 3c860342c0c387..c8ef60387af2fb 100644 --- a/Lib/test/test_operator.py +++ b/Lib/test/test_operator.py @@ -476,11 +476,10 @@ def __getitem__(self, other): return 5 # so that C is a sequence def test_iconcat_without_getitem(self): operator = self.module - class X: pass - msg = "'X' object can't be concatenated" + msg = "'int' object can't be concatenated" with self.assertRaisesRegex(TypeError, msg): - operator.iconcat(X(), X()) + operator.iconcat(1, 0.5) def test_index(self): operator = self.module @@ -492,11 +491,15 @@ def __index__(self): def test_not_(self): operator = self.module - class X: + class C: def __bool__(self): - return False - - self.assertEqual(operator.not_(X()), True) + 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