1 """Base class for all tests"""
2 __version__ = '$Id: basetest.py 1126 2008-03-09 20:21:59Z cthedot $'
3
4 import logging
5 import unittest
6 import sys
7 import StringIO
8 import urllib2
9 from email import message_from_string, message_from_file
10
11 import cssutils
12 from minimock import mock, restore
13
14 cssutils.log.setloglevel(logging.FATAL)
15
17
20
22 """
23 from
24 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307970
25 """
26 if "exc_args" in kwargs:
27 exc_args = kwargs["exc_args"]
28 del kwargs["exc_args"]
29 else:
30 exc_args = None
31 if "exc_pattern" in kwargs:
32 exc_pattern = kwargs["exc_pattern"]
33 del kwargs["exc_pattern"]
34 else:
35 exc_pattern = None
36
37 argv = [repr(a) for a in args]\
38 + ["%s=%r" % (k,v) for k,v in kwargs.items()]
39 callsig = "%s(%s)" % (callable.__name__, ", ".join(argv))
40
41 try:
42 callable(*args, **kwargs)
43 except exception, exc:
44 if exc_args is not None:
45 self.failIf(exc.args != exc_args,
46 "%s raised %s with unexpected args: "\
47 "expected=%r, actual=%r"\
48 % (callsig, exc.__class__, exc_args, exc.args))
49 if exc_pattern is not None:
50 self.failUnless(exc_pattern.search(str(exc)),
51 "%s raised %s, but the exception "\
52 "does not match '%s': %r"\
53 % (callsig, exc.__class__, exc_pattern.pattern,
54 str(exc)))
55 except:
56 exc_info = sys.exc_info()
57 print exc_info
58 self.fail("%s raised an unexpected exception type: "\
59 "expected=%s, actual=%s"\
60 % (callsig, exception, exc_info[0]))
61 else:
62 self.fail("%s did not raise %s" % (callsig, exception))
63
65 """
66 Just like unittest.TestCase.assertRaises,
67 but checks that the message is right too.
68
69 Usage::
70
71 self.assertRaisesMsg(
72 MyException, "Exception message",
73 my_function, (arg1, arg2)
74 )
75
76 from
77 http://www.nedbatchelder.com/blog/200609.html#e20060905T064418
78 """
79 try:
80 callableObj(*args, **kwargs)
81 except excClass, exc:
82 excMsg = str(exc)
83 if not msg:
84
85 return
86 elif excMsg == msg:
87
88 return
89 else:
90
91 raise self.failureException(
92 "Right exception, wrong message: got '%s' expected '%s'" %
93 (excMsg, msg)
94 )
95 else:
96 if hasattr(excClass, '__name__'):
97 excName = excClass.__name__
98 else:
99 excName = str(excClass)
100 raise self.failureException(
101 "Expected to raise %s, didn't get an exception at all" %
102 excName
103 )
104
105 - def do_equal_p(self, tests, att='cssText', debug=False, raising=True):
106 """
107 if raising self.p is used for parsing, else self.pf
108 """
109 p = cssutils.CSSParser(raiseExceptions=raising)
110
111 for test, expected in tests.items():
112 if debug:
113 print '"%s"' % test
114 s = p.parseString(test)
115 if expected is None:
116 expected = test
117 self.assertEqual(expected, unicode(s.__getattribute__(att), 'utf-8'))
118
119 - def do_raise_p(self, tests, debug=False, raising=True):
126
127
128 - def do_equal_r(self, tests, att='cssText', debug=False):
129
130 for test, expected in tests.items():
131 if debug:
132 print '"%s"' % test
133 self.r.__setattr__(att, test)
134 if expected is None:
135 expected = test
136 self.assertEqual(expected, self.r.__getattribute__(att))
137
138 - def do_raise_r(self, tests, att='_setCssText', debug=False):
139
140 for test, expected in tests.items():
141 if debug:
142 print '"%s"' % test
143 self.assertRaises(expected, self.r.__getattribute__(att), test)
144
145
146
147 - def _urlopen(self, url, text=None, error=None):
148
149 def x(*ignored):
150 return _Response(url, text=text, error=error)
151 return x
152
156
158 """urllib2.Reponse mock"""
159 - def __init__(self, url, text=u'', error=None):
160 self.url = url
161 self.text = text
162 self.error = error
163
166
168 class Info(object):
169 def gettype(self):
170 return 'text/css'
171 def getparam(self, name):
172 return 'UTF-8'
173
174 return Info()
175
177 if self.error:
178 raise Exception(self.error)
179 else:
180 return self.text
181