Package cssutils :: Package tests :: Module basetest |
[hide private]
[frames] | no frames]

# Source Code for Module cssutils.tests.basetest

      1  """
      2  base class for all tests
      3  """
      4  __author__ = '$LastChangedBy: cthedot $'
      5  __date__ = '$LastChangedDate: 2007-10-20 20:10:12 +0200 (Sa, 20 Okt 2007) $'
      6  __version__ = '$LastChangedRevision: 553 $'
      7
      8  import logging
      9  import unittest
     10  import sys
     11  import cssutils
     12
     13  cssutils.log.setloglevel(logging.FATAL)
     14
     15 -class BaseTestCase(unittest.TestCase):
     16
     17 -    def assertRaisesEx(self, exception, callable, *args, **kwargs):
     18          """
     19          from
     20          http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307970
     21          """
     22          if "exc_args" in kwargs:
     23              exc_args = kwargs["exc_args"]
     24              del kwargs["exc_args"]
     25          else:
     26              exc_args = None
     27          if "exc_pattern" in kwargs:
     28              exc_pattern = kwargs["exc_pattern"]
     29              del kwargs["exc_pattern"]
     30          else:
     31              exc_pattern = None
     32
     33          argv = [repr(a) for a in args]\
     34                 + ["%s=%r" % (k,v)  for k,v in kwargs.items()]
     35          callsig = "%s(%s)" % (callable.__name__, ", ".join(argv))
     36
     37          try:
     38              callable(*args, **kwargs)
     39          except exception, exc:
     40              if exc_args is not None:
     41                  self.failIf(exc.args != exc_args,
     42                              "%s raised %s with unexpected args: "\
     43                              "expected=%r, actual=%r"\
     44                              % (callsig, exc.__class__, exc_args, exc.args))
     45              if exc_pattern is not None:
     46                  self.failUnless(exc_pattern.search(str(exc)),
     47                                  "%s raised %s, but the exception "\
     48                                  "does not match '%s': %r"\
     49                                  % (callsig, exc.__class__, exc_pattern.pattern,
     50                                     str(exc)))
     51          except:
     52              exc_info = sys.exc_info()
     53              print exc_info
     54              self.fail("%s raised an unexpected exception type: "\
     55                        "expected=%s, actual=%s"\
     56                        % (callsig, exception, exc_info[0]))
     57          else:
     58              self.fail("%s did not raise %s" % (callsig, exception))
     59
     60 -    def assertRaisesMsg(self, excClass, msg, callableObj, *args, **kwargs):
     61          """
     62          Just like unittest.TestCase.assertRaises,
     63          but checks that the message is right too.
     64
     65          Usage::
     66
     67              self.assertRaisesMsg(
     68                  MyException, "Exception message",
     69                  my_function, (arg1, arg2)
     70                  )
     71
     72          from
     73          http://www.nedbatchelder.com/blog/200609.html#e20060905T064418
     74          """
     75          try:
     76              callableObj(*args, **kwargs)
     77          except excClass, exc:
     78              excMsg = str(exc)
     79              if not msg:
     80                  # No message provided: any message is fine.
     81                  return
     82              elif excMsg == msg:
     83                  # Message provided, and we got the right message: passes.
     84                  return
     85              else:
     86                  # Message provided, and it didn't match: fail!
     87                  raise self.failureException(
     88                  "Right exception, wrong message: got '%s' expected '%s'" %
     89                  (excMsg, msg)
     90                  )
     91          else:
     92              if hasattr(excClass, '__name__'):
     93                  excName = excClass.__name__
     94              else:
     95                  excName = str(excClass)
     96              raise self.failureException(
     97                  "Expected to raise %s, didn't get an exception at all" %
     98                  excName
     99                  )
    100
    101 -    def setUp(self):
    102          self.p = cssutils.CSSParser(raiseExceptions=True)
    103
    104 -    def do_equal_p(self, tests, att='cssText', debug=False, raising=True):
    105          """
    106          if raising self.p is used for parsing, else self.pf
    107          """
    108          p = cssutils.CSSParser(raiseExceptions=raising)
    109          # parses with self.p and checks att of result
    110          for test, expected in tests.items():
    111              if debug:
    112                  print '"%s"' % test
    113              s = p.parseString(test)
    114              if expected is None:
    115                  expected = test
    116              self.assertEqual(expected, s.__getattribute__(att))
    117
    118 -    def do_raise_p(self, tests, debug=False, raising=True):
    119          # parses with self.p and expects raise
    120          p = cssutils.CSSParser(raiseExceptions=raising)
    121          for test, expected in tests.items():
    122              if debug:
    123                  print '"%s"' % test
    124              self.assertRaises(expected, p.parseString, test)
    125
    126
    127 -    def do_equal_r(self, tests, att='cssText', debug=False):
    128          # sets attribute att of self.r and asserts Equal
    129          for test, expected in tests.items():
    130              if debug:
    131                  print '"%s"' % test
    132              self.r.__setattr__(att, test)
    133              if expected is None:
    134                  expected = test
    135              self.assertEqual(expected, self.r.__getattribute__(att))
    136
    137 -    def do_raise_r(self, tests, att='_setCssText', debug=False):
    138          # sets self.r and asserts raise
    139          for test, expected in tests.items():
    140              if debug:
    141                  print '"%s"' % test
    142              self.assertRaises(expected, self.r.__getattribute__(att), test)
    143