#!/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
