Convert test_opcodes to unittest.

This commit is contained in:
Georg Brandl 2006-10-28 13:56:58 +00:00
parent 2f03760a98
commit 0b679629c6
2 changed files with 105 additions and 102 deletions

View File

@ -1,6 +0,0 @@
test_opcodes
2. Opcodes
XXX Not yet fully implemented
2.1 try inside for loop
2.2 raise class exceptions
2.3 comparing function objects

View File

@ -1,12 +1,11 @@
# Python test set -- part 2, opcodes
from test.test_support import TestFailed
from test.test_support import run_unittest
import unittest
class OpcodeTest(unittest.TestCase):
print '2. Opcodes'
print 'XXX Not yet fully implemented'
print '2.1 try inside for loop'
def test_try_inside_for_loop(self):
n = 0
for i in range(10):
n = n+i
@ -20,10 +19,9 @@ for i in range(10):
finally: pass
n = n+i
if n != 90:
raise TestFailed, 'try inside for'
self.fail('try inside for')
print '2.2 raise class exceptions'
def test_raise_class_exceptions(self):
class AClass: pass
class BClass(AClass): pass
@ -42,7 +40,7 @@ try: raise BClass()
except AClass: pass
try: raise BClass()
except CClass: raise TestFailed
except CClass: self.fail()
except: pass
a = AClass()
@ -50,52 +48,63 @@ b = BClass()
try: raise AClass, b
except BClass, v:
if v != b: raise TestFailed, "v!=b"
else: raise TestFailed, "no exception"
if v != b: self.fail("v!=b")
else: self.fail("no exception")
try: raise b
except AClass, v:
if v != b: raise TestFailed, "v!=b AClass"
if v != b: self.fail("v!=b AClass")
else:
self.fail("no exception")
# not enough arguments
try: raise BClass, a
except TypeError: pass
else: self.fail("no exception")
try: raise DClass, a
except DClass, v:
if not isinstance(v, DClass):
raise TestFailed, "v not DClass"
self.assert_(isinstance(v, DClass))
else:
self.fail("no exception")
print '2.3 comparing function objects'
def test_compare_function_objects(self):
f = eval('lambda: None')
g = eval('lambda: None')
if f == g: raise TestFailed, "functions should not be same"
self.failIf(f == g)
f = eval('lambda a: a')
g = eval('lambda a: a')
if f == g: raise TestFailed, "functions should not be same"
self.failIf(f == g)
f = eval('lambda a=1: a')
g = eval('lambda a=1: a')
if f == g: raise TestFailed, "functions should not be same"
self.failIf(f == g)
f = eval('lambda: 0')
g = eval('lambda: 1')
if f == g: raise TestFailed
self.failIf(f == g)
f = eval('lambda: None')
g = eval('lambda a: None')
if f == g: raise TestFailed
self.failIf(f == g)
f = eval('lambda a: None')
g = eval('lambda b: None')
if f == g: raise TestFailed
self.failIf(f == g)
f = eval('lambda a: None')
g = eval('lambda a=None: None')
if f == g: raise TestFailed
self.failIf(f == g)
f = eval('lambda a=0: None')
g = eval('lambda a=1: None')
if f == g: raise TestFailed
self.failIf(f == g)
def test_main():
run_unittest(OpcodeTest)
if __name__ == '__main__':
test_main()