1 """
2 base class for all tests
3 """
4 __author__ = '$LastChangedBy: cthedot $'
5 __date__ = '$LastChangedDate: 2007-11-01 22:39:10 +0100 (Do, 01 Nov 2007) $'
6 __version__ = '$LastChangedRevision: 619 $'
7
8 import logging
9 import unittest
10 import sys
11 import cssutils
12
13 cssutils.log.setloglevel(logging.FATAL)
14
16
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
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
81 return
82 elif excMsg == msg:
83
84 return
85 else:
86
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
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
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, unicode(s.__getattribute__(att), 'utf-8'))
117
118 - def do_raise_p(self, tests, debug=False, raising=True):
125
126
127 - def do_equal_r(self, tests, att='cssText', debug=False):
128
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
139 for test, expected in tests.items():
140 if debug:
141 print '"%s"' % test
142 self.assertRaises(expected, self.r.__getattribute__(att), test)
143