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