]> granicus.if.org Git - python/commitdiff
Merged revisions 81525 via svnmerge from
authorMark Dickinson <dickinsm@gmail.com>
Tue, 25 May 2010 19:06:24 +0000 (19:06 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Tue, 25 May 2010 19:06:24 +0000 (19:06 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r81525 | mark.dickinson | 2010-05-25 20:01:08 +0100 (Tue, 25 May 2010) | 3 lines

  Issue #8816: Extra tests for some built-in functions.  These tests are
  ports of IronPython tests.  Thanks Gregory Nofi.
........

Lib/test/test_builtin.py
Lib/test/test_enumerate.py

index 94c68787857bb1fd4028831dde89063328f6538f..3f412b785196349e6cc9ff12dac1fccd99c99666 100644 (file)
@@ -124,6 +124,16 @@ class BuiltinTest(unittest.TestCase):
         self.assertEqual(abs(-3.14), 3.14)
         # str
         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):
         self.assertEqual(all([2, 4, 6]), True)
@@ -600,6 +610,8 @@ class BuiltinTest(unittest.TestCase):
             def __len__(self):
                 return sys.maxsize + 1
         self.assertRaises(OverflowError, len, HugeLen())
+        class NoLenMethod(object): pass
+        self.assertRaises(TypeError, len, NoLenMethod())
 
     def test_map(self):
         self.assertEqual(
@@ -1187,6 +1199,11 @@ class BuiltinTest(unittest.TestCase):
         b = 2
         return vars()
 
+    class C_get_vars(object):
+        def getDict(self):
+            return {'a':2}
+        __dict__ = property(fget=getDict)
+
     def test_vars(self):
         self.assertEqual(set(vars()), set(dir()))
         import sys
@@ -1195,6 +1212,7 @@ class BuiltinTest(unittest.TestCase):
         self.assertEqual(self.get_vars_f2(), {'a': 1, 'b': 2})
         self.assertRaises(TypeError, vars, 42, 42)
         self.assertRaises(TypeError, vars, 42)
+        self.assertEqual(vars(self.C_get_vars()), {'a':2})
 
     def test_zip(self):
         a = (1, 2, 3)
index 315069c48e7bbb5f9b4fe41c0a4990fb5d204665..b8b971928b397d61745ee647246db0a5643c1954 100644 (file)
@@ -198,6 +198,18 @@ class TestReversed(unittest.TestCase):
                 self.fail("non-callable __reversed__ didn't raise!")
         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):