#!/usr/local/bin/python
#
# File:        testexceptions
# Copyright:   (c) 2001 The Regents of the University of California
# Release:     $Name: release-0-8-8 $
# Revision:    @(#) $Revision: 1.7 $
# Date:        $Date: 2002/09/30 23:01:00 $
# 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

class TestCounter:
  partno = 0
  numpass = 0
  numfail = 0
  numxfail = 0
  numxpass = 0

  def __init__(self, numparts = -1):
    self.numparts = numparts
    print "NPARTS", numparts

  def describeTest(self, description):
    self.partno = self.partno + 1
    print "PART", self.partno
    print "COMMENT:", description

  def evalTest(self, result, expected):
    if (result):
      if (expected):
        self.numpass = self.numpass + 1
        print "RESULT", self.partno, "PASS"
      else:
        self.numxpass = self.numxpass + 1
        print "RESULT", self.partno, "XPASS"
    else:
      if (expected):
        self.numfail = self.numfail + 1
        print "RESULT", self.partno, "FAIL"
      else:
        self.numxfail = self.numxfail + 1
        print "RESULT", self.partno, "XFAIL"
        
  def finish(self):
    if (self.numparts < 0):
      self.numparts = self.partno
    if (self.numpass == self.numparts):
      print "TEST_RESULT PASS"
    else:
      if (self.numpass + self.numxfail == self.numparts):
        print "TEST_RESULT XFAIL"
      else:
        print "TEST_RESULT FAIL"
      

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
