Issue #8816: Extra tests for some built-in functions. These tests are

ports of IronPython tests.  Thanks Gregory Nofi.
This commit is contained in:
Mark Dickinson 2010-05-25 19:01:08 +00:00
parent 172e06e019
commit 2066559913
3 changed files with 84 additions and 14 deletions

View File

@ -90,6 +90,16 @@ class BuiltinTest(unittest.TestCase):
self.assertEqual(abs(-1234L), 1234L) self.assertEqual(abs(-1234L), 1234L)
# str # str
self.assertRaises(TypeError, abs, 'a') self.assertRaises(TypeError, abs, 'a')
# bool
self.assertEqual(abs(True), 1)
self.assertEqual(abs(False), 0)
# other
self.assertRaises(TypeError, abs)
self.assertRaises(TypeError, abs, None)
class AbsClass(object):
def __abs__(self):
return -5
self.assertEqual(abs(AbsClass()), -5)
def test_all(self): def test_all(self):
self.assertEqual(all([2, 4, 6]), True) self.assertEqual(all([2, 4, 6]), True)
@ -152,19 +162,44 @@ class BuiltinTest(unittest.TestCase):
def test_callable(self): def test_callable(self):
self.assertTrue(callable(len)) self.assertTrue(callable(len))
self.assertFalse(callable("a"))
self.assertTrue(callable(callable))
self.assertTrue(callable(lambda x, y: x + y))
self.assertFalse(callable(__builtins__))
def f(): pass def f(): pass
self.assertTrue(callable(f)) self.assertTrue(callable(f))
class C:
class Classic:
def meth(self): pass def meth(self): pass
self.assertTrue(callable(C)) self.assertTrue(callable(Classic))
x = C() c = Classic()
self.assertTrue(callable(x.meth)) self.assertTrue(callable(c.meth))
self.assertTrue(not callable(x)) self.assertFalse(callable(c))
class D(C):
class NewStyle(object):
def meth(self): pass
self.assertTrue(callable(NewStyle))
n = NewStyle()
self.assertTrue(callable(n.meth))
self.assertFalse(callable(n))
# Classic and new-style classes evaluate __call__() differently
c.__call__ = None
self.assertTrue(callable(c))
del c.__call__
self.assertFalse(callable(c))
n.__call__ = None
self.assertFalse(callable(n))
del n.__call__
self.assertFalse(callable(n))
class N2(object):
def __call__(self): pass def __call__(self): pass
y = D() n2 = N2()
self.assertTrue(callable(y)) self.assertTrue(callable(n2))
y() class N3(N2): pass
n3 = N3()
self.assertTrue(callable(n3))
def test_chr(self): def test_chr(self):
self.assertEqual(chr(32), ' ') self.assertEqual(chr(32), ' ')
@ -729,6 +764,11 @@ class BuiltinTest(unittest.TestCase):
def __len__(self): def __len__(self):
raise ValueError raise ValueError
self.assertRaises(ValueError, len, BadSeq()) self.assertRaises(ValueError, len, BadSeq())
self.assertRaises(TypeError, len, 2)
class ClassicStyle: pass
class NewStyle(object): pass
self.assertRaises(AttributeError, len, ClassicStyle())
self.assertRaises(TypeError, len, NewStyle())
def test_map(self): def test_map(self):
self.assertEqual( self.assertEqual(
@ -1188,9 +1228,10 @@ class BuiltinTest(unittest.TestCase):
unlink(TESTFN) unlink(TESTFN)
def test_reduce(self): def test_reduce(self):
self.assertEqual(reduce(lambda x, y: x+y, ['a', 'b', 'c'], ''), 'abc') add = lambda x, y: x+y
self.assertEqual(reduce(add, ['a', 'b', 'c'], ''), 'abc')
self.assertEqual( self.assertEqual(
reduce(lambda x, y: x+y, [['a', 'c'], [], ['d', 'w']], []), reduce(add, [['a', 'c'], [], ['d', 'w']], []),
['a','c','d','w'] ['a','c','d','w']
) )
self.assertEqual(reduce(lambda x, y: x*y, range(2,8), 1), 5040) self.assertEqual(reduce(lambda x, y: x*y, range(2,8), 1), 5040)
@ -1198,15 +1239,23 @@ class BuiltinTest(unittest.TestCase):
reduce(lambda x, y: x*y, range(2,21), 1L), reduce(lambda x, y: x*y, range(2,21), 1L),
2432902008176640000L 2432902008176640000L
) )
self.assertEqual(reduce(lambda x, y: x+y, Squares(10)), 285) self.assertEqual(reduce(add, Squares(10)), 285)
self.assertEqual(reduce(lambda x, y: x+y, Squares(10), 0), 285) self.assertEqual(reduce(add, Squares(10), 0), 285)
self.assertEqual(reduce(lambda x, y: x+y, Squares(0), 0), 0) self.assertEqual(reduce(add, Squares(0), 0), 0)
self.assertRaises(TypeError, reduce) self.assertRaises(TypeError, reduce)
self.assertRaises(TypeError, reduce, 42)
self.assertRaises(TypeError, reduce, 42, 42) self.assertRaises(TypeError, reduce, 42, 42)
self.assertRaises(TypeError, reduce, 42, 42, 42) self.assertRaises(TypeError, reduce, 42, 42, 42)
self.assertRaises(TypeError, reduce, None, range(5))
self.assertRaises(TypeError, reduce, add, 42)
self.assertEqual(reduce(42, "1"), "1") # func is never called with one item self.assertEqual(reduce(42, "1"), "1") # func is never called with one item
self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item
self.assertRaises(TypeError, reduce, 42, (42, 42)) self.assertRaises(TypeError, reduce, 42, (42, 42))
self.assertRaises(TypeError, reduce, add, []) # arg 2 must not be empty sequence with no initial value
self.assertRaises(TypeError, reduce, add, "")
self.assertRaises(TypeError, reduce, add, ())
self.assertEqual(reduce(add, [], None), None)
self.assertEqual(reduce(add, [], 42), 42)
class BadSeq: class BadSeq:
def __getitem__(self, index): def __getitem__(self, index):
@ -1391,6 +1440,11 @@ class BuiltinTest(unittest.TestCase):
b = 2 b = 2
return vars() return vars()
class C_get_vars(object):
def getDict(self):
return {'a':2}
__dict__ = property(fget=getDict)
def test_vars(self): def test_vars(self):
self.assertEqual(set(vars()), set(dir())) self.assertEqual(set(vars()), set(dir()))
import sys import sys
@ -1399,6 +1453,7 @@ class BuiltinTest(unittest.TestCase):
self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2}) self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2})
self.assertRaises(TypeError, vars, 42, 42) self.assertRaises(TypeError, vars, 42, 42)
self.assertRaises(TypeError, vars, 42) self.assertRaises(TypeError, vars, 42)
self.assertEqual(vars(self.C_get_vars()), {'a':2})
def test_zip(self): def test_zip(self):
a = (1, 2, 3) a = (1, 2, 3)

View File

@ -204,6 +204,18 @@ class TestReversed(unittest.TestCase):
self.fail("non-callable __reversed__ didn't raise!") self.fail("non-callable __reversed__ didn't raise!")
self.assertEqual(rc, sys.getrefcount(r)) self.assertEqual(rc, sys.getrefcount(r))
def test_objmethods(self):
# Objects must have __len__() and __getitem__() implemented.
class NoLen(object):
def __getitem__(self): return 1
nl = NoLen()
self.assertRaises(TypeError, reversed, nl)
class NoGetItem(object):
def __len__(self): return 2
ngi = NoGetItem()
self.assertRaises(TypeError, reversed, ngi)
class EnumerateStartTestCase(EnumerateTestCase): class EnumerateStartTestCase(EnumerateTestCase):

View File

@ -320,6 +320,9 @@ class LongTest(unittest.TestCase):
self.assertEqual(long(3.5), 3L) self.assertEqual(long(3.5), 3L)
self.assertEqual(long(-3.5), -3L) self.assertEqual(long(-3.5), -3L)
self.assertEqual(long("-3"), -3L) self.assertEqual(long("-3"), -3L)
self.assertEqual(long("0b10", 2), 2L)
self.assertEqual(long("0o10", 8), 8L)
self.assertEqual(long("0x10", 16), 16L)
if test_support.have_unicode: if test_support.have_unicode:
self.assertEqual(long(unicode("-3")), -3L) self.assertEqual(long(unicode("-3")), -3L)
# Different base: # Different base: