1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#!/usr/local/bin/python
#
# File: testexceptions
# Copyright: (c) 2001 The Regents of the University of California
# Revision: @(#) $Revision: 4434 $
# Date: $Date: 2005-03-17 09:05:29 -0800 (Thu, 17 Mar 2005) $
# Description: Exercise exceptions
#
# Try to exercise everything possible in Python.
import ExceptionTest.Fib
import ExceptionTest.FibException
import ExceptionTest.NegativeValueException
import ExceptionTest.TooDeepException
import ExceptionTest.TooBigException
from Numeric import *
import sys
from synch.ResultType import *
from synch import RegOut
class TestCounter:
def __init__(self, numparts = -1):
self.partno = 0
self.tracker = RegOut.RegOut()
self.tracker.setExpectations(numparts)
def describeTest(self, description):
self.partno = self.partno + 1
self.tracker.startPart(self.partno)
self.tracker.writeComment(description)
def evalTest(self, result, expected):
if (result):
if (expected):
self.tracker.endPart(self.partno, PASS)
else:
self.tracker.endPart(self.partno, XPASS)
else:
if (expected):
self.tracker.endPart(self.partno, FAIL)
else:
self.tracker.endPart(self.partno, XFAIL)
def finish(self):
self.tracker.close()
if __name__ == '__main__':
counter = TestCounter(5)
counter.describeTest("fib = ExceptionTest.Fib.Fib()")
fib = ExceptionTest.Fib.Fib()
counter.evalTest(fib != None, 1)
counter.describeTest("fib.getFib(10, 25, 200, 0) -> no exception expected")
try:
fib.getFib(10, 25, 200, 0)
except:
counter.evalTest(0, 1)
else:
counter.evalTest(1, 1)
counter.describeTest(
"fib.getFib(-1, 10, 10, 0) -> NegativeValueException expected")
try:
fib.getFib(-1, 10, 10, 0)
except ExceptionTest.NegativeValueException.Exception:
(etype, eobj, etb) = sys.exc_info()
if (eobj.isType("ExceptionTest.NegativeValueException")):
counter.evalTest(1, 1)
else:
counter.evalTest(0, 1)
except:
print "Wrong exception thrown:", sys.exc_info()
counter.evalTest(0, 1)
else:
print "Unexpected result: No exception thrown\n"
counter.evalTest(0, 1)
counter.describeTest("fib.getFib(10, 1, 1000, 0) -> TooDeepException")
try:
fib.getFib(10, 1, 1000, 0)
except ExceptionTest.FibException.Exception:
(etype, eobj, etb) = sys.exc_info()
if (eobj.isType("ExceptionTest.TooDeepException")):
counter.evalTest(1, 1)
else:
counter.evalTest(0, 1)
except:
print "Wrong exception thrown:", sys.exc_info()
counter.evalTest(0, 1)
else:
print "Unexpected result: No exception thrown\n"
counter.evalTest(0, 1)
counter.describeTest("fib.getFib(10, 1000, 1, 0) -> TooBigException")
try:
fib.getFib(10, 1000, 1, 0)
except ExceptionTest.FibException.Exception:
(etype, eobj, etb) = sys.exc_info()
if (eobj.isType("ExceptionTest.TooBigException")):
counter.evalTest(1, 1)
else:
counter.evalTest(0, 1)
except:
print sys.exc_info()
counter.evalTest(0, 1)
else:
print "Unexpected result: No exception thrown\n"
counter.evalTest(0, 1)
counter.finish()
0
|