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)
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(
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
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)
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):