PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 32ms app.codeStats 0ms

/Lib/test/test_opcodes.py

http://unladen-swallow.googlecode.com/
Python | 116 lines | 87 code | 27 blank | 2 comment | 4 complexity | ecb3bb7dc12a0a4920d41d5e0d35ae53 MD5 | raw file
  1# Python test set -- part 2, opcodes
  2
  3from test.test_support import run_unittest
  4import unittest
  5
  6class OpcodeTest(unittest.TestCase):
  7
  8    def test_try_inside_for_loop(self):
  9        n = 0
 10        for i in range(10):
 11            n = n+i
 12            try: 1/0
 13            except NameError: pass
 14            except ZeroDivisionError: pass
 15            except TypeError: pass
 16            try: pass
 17            except: pass
 18            try: pass
 19            finally: pass
 20            n = n+i
 21        if n != 90:
 22            self.fail('try inside for')
 23
 24    def test_raise_class_exceptions(self):
 25
 26        class AClass: pass
 27        class BClass(AClass): pass
 28        class CClass: pass
 29        class DClass(AClass):
 30            def __init__(self, ignore):
 31                pass
 32
 33        try: raise AClass()
 34        except: pass
 35
 36        try: raise AClass()
 37        except AClass: pass
 38
 39        try: raise BClass()
 40        except AClass: pass
 41
 42        try: raise BClass()
 43        except CClass: self.fail()
 44        except: pass
 45
 46        a = AClass()
 47        b = BClass()
 48
 49        try: raise AClass, b
 50        except BClass, v:
 51            self.assertEqual(v, b)
 52        else: self.fail("no exception")
 53
 54        try: raise b
 55        except AClass, v:
 56            self.assertEqual(v, b)
 57        else:
 58            self.fail("no exception")
 59
 60        # not enough arguments
 61        try:  raise BClass, a
 62        except TypeError: pass
 63        else: self.fail("no exception")
 64
 65        try:  raise DClass, a
 66        except DClass, v:
 67            self.assert_(isinstance(v, DClass))
 68        else:
 69            self.fail("no exception")
 70
 71    def test_compare_function_objects(self):
 72
 73        f = eval('lambda: None')
 74        g = eval('lambda: None')
 75        self.assertNotEquals(f, g)
 76
 77        f = eval('lambda a: a')
 78        g = eval('lambda a: a')
 79        self.assertNotEquals(f, g)
 80
 81        f = eval('lambda a=1: a')
 82        g = eval('lambda a=1: a')
 83        self.assertNotEquals(f, g)
 84
 85        f = eval('lambda: 0')
 86        g = eval('lambda: 1')
 87        self.assertNotEquals(f, g)
 88
 89        f = eval('lambda: None')
 90        g = eval('lambda a: None')
 91        self.assertNotEquals(f, g)
 92
 93        f = eval('lambda a: None')
 94        g = eval('lambda b: None')
 95        self.assertNotEquals(f, g)
 96
 97        f = eval('lambda a: None')
 98        g = eval('lambda a=None: None')
 99        self.assertNotEquals(f, g)
100
101        f = eval('lambda a=0: None')
102        g = eval('lambda a=1: None')
103        self.assertNotEquals(f, g)
104
105    def test_modulo_of_string_subclasses(self):
106        class MyString(str):
107            def __mod__(self, value):
108                return 42
109        self.assertEqual(MyString() % 3, 42)
110
111
112def test_main():
113    run_unittest(OpcodeTest)
114
115if __name__ == '__main__':
116    test_main()