diff options
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_descr.py | 72 | ||||
-rw-r--r-- | Lib/test/test_operator.py | 40 |
2 files changed, 112 insertions, 0 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index f594ca8..2ea8186 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -3990,6 +3990,77 @@ def methodwrapper(): verify(l.__add__.__objclass__ is list) vereq(l.__add__.__doc__, list.__add__.__doc__) +def notimplemented(): + # all binary methods should be able to return a NotImplemented + if verbose: + print "Testing NotImplemented..." + + import sys + import types + import operator + + def specialmethod(self, other): + return NotImplemented + + def check(expr, x, y): + try: + exec expr in {'x': x, 'y': y, 'operator': operator} + except TypeError: + pass + else: + raise TestFailed("no TypeError from %r" % (expr,)) + + N1 = sys.maxint + 1L # might trigger OverflowErrors instead of TypeErrors + N2 = sys.maxint # if sizeof(int) < sizeof(long), might trigger + # ValueErrors instead of TypeErrors + for metaclass in [type, types.ClassType]: + for name, expr, iexpr in [ + ('__add__', 'x + y', 'x += y'), + ('__sub__', 'x - y', 'x -= y'), + ('__mul__', 'x * y', 'x *= y'), + ('__truediv__', 'operator.truediv(x, y)', None), + ('__floordiv__', 'operator.floordiv(x, y)', None), + ('__div__', 'x / y', 'x /= y'), + ('__mod__', 'x % y', 'x %= y'), + ('__divmod__', 'divmod(x, y)', None), + ('__pow__', 'x ** y', 'x **= y'), + ('__lshift__', 'x << y', 'x <<= y'), + ('__rshift__', 'x >> y', 'x >>= y'), + ('__and__', 'x & y', 'x &= y'), + ('__or__', 'x | y', 'x |= y'), + ('__xor__', 'x ^ y', 'x ^= y'), + ('__coerce__', 'coerce(x, y)', None)]: + if name == '__coerce__': + rname = name + else: + rname = '__r' + name[2:] + A = metaclass('A', (), {name: specialmethod}) + B = metaclass('B', (), {rname: specialmethod}) + a = A() + b = B() + check(expr, a, a) + check(expr, a, b) + check(expr, b, a) + check(expr, b, b) + check(expr, a, N1) + check(expr, a, N2) + check(expr, N1, b) + check(expr, N2, b) + if iexpr: + check(iexpr, a, a) + check(iexpr, a, b) + check(iexpr, b, a) + check(iexpr, b, b) + check(iexpr, a, N1) + check(iexpr, a, N2) + iname = '__i' + name[2:] + C = metaclass('C', (), {iname: specialmethod}) + c = C() + check(iexpr, c, a) + check(iexpr, c, b) + check(iexpr, c, N1) + check(iexpr, c, N2) + def test_main(): weakref_segfault() # Must be first, somehow do_this_first() @@ -4084,6 +4155,7 @@ def test_main(): vicious_descriptor_nonsense() test_init() methodwrapper() + notimplemented() if verbose: print "All OK" diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py index 725b2d9..6cc7945 100644 --- a/Lib/test/test_operator.py +++ b/Lib/test/test_operator.py @@ -3,6 +3,34 @@ import unittest from test import test_support +class Seq1: + def __init__(self, lst): + self.lst = lst + def __len__(self): + return len(self.lst) + def __getitem__(self, i): + return self.lst[i] + def __add__(self, other): + return self.lst + other.lst + def __mul__(self, other): + return self.lst * other + def __rmul__(self, other): + return other * self.lst + +class Seq2(object): + def __init__(self, lst): + self.lst = lst + def __len__(self): + return len(self.lst) + def __getitem__(self, i): + return self.lst[i] + def __add__(self, other): + return self.lst + other.lst + def __mul__(self, other): + return self.lst * other + def __rmul__(self, other): + return other * self.lst + class OperatorTestCase(unittest.TestCase): def test_lt(self): @@ -92,6 +120,9 @@ class OperatorTestCase(unittest.TestCase): self.failUnlessRaises(TypeError, operator.concat, None, None) self.failUnless(operator.concat('py', 'thon') == 'python') self.failUnless(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4]) + self.failUnless(operator.concat(Seq1([5, 6]), Seq1([7])) == [5, 6, 7]) + self.failUnless(operator.concat(Seq2([5, 6]), Seq2([7])) == [5, 6, 7]) + self.failUnlessRaises(TypeError, operator.concat, 13, 29) def test_countOf(self): self.failUnlessRaises(TypeError, operator.countOf) @@ -246,6 +277,15 @@ class OperatorTestCase(unittest.TestCase): self.failUnless(operator.repeat(a, 2) == a+a) self.failUnless(operator.repeat(a, 1) == a) self.failUnless(operator.repeat(a, 0) == '') + a = Seq1([4, 5, 6]) + self.failUnless(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6]) + self.failUnless(operator.repeat(a, 1) == [4, 5, 6]) + self.failUnless(operator.repeat(a, 0) == []) + a = Seq2([4, 5, 6]) + self.failUnless(operator.repeat(a, 2) == [4, 5, 6, 4, 5, 6]) + self.failUnless(operator.repeat(a, 1) == [4, 5, 6]) + self.failUnless(operator.repeat(a, 0) == []) + self.failUnlessRaises(TypeError, operator.repeat, 6, 7) def test_rshift(self): self.failUnlessRaises(TypeError, operator.rshift) |