/usr/share/pyshared/cssutils/tests/basetest.py is in python-cssutils 0.9.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | """Base class for all tests"""
import logging
import os
import sys
import StringIO
import unittest
import urllib2
from email import message_from_string, message_from_file
# add src to PYTHONPATH
sys.path.append(os.path.join(os.path.abspath('.'), '..'))
import cssutils
PY2x = sys.version_info < (3,0)
def msg3x(msg):
"""msg might contain unicode repr `u'...'` which in py3 is `u'...`
needed by tests using ``assertRaisesMsg``"""
if not PY2x and msg.find("u'"):
msg = msg.replace("u'", "'")
return msg
class BaseTestCase(unittest.TestCase):
def _tempSer(self):
"Replace default ser with temp ser."
self._ser = cssutils.ser
cssutils.ser = cssutils.serialize.CSSSerializer()
def _restoreSer(self):
"Restore the default ser."
cssutils.ser = self._ser
def setUp(self):
# a raising parser!!!
cssutils.log.raiseExceptions = True
cssutils.log.setLevel(logging.FATAL)
self.p = cssutils.CSSParser(raiseExceptions=True)
def tearDown(self):
if hasattr(self, '_ser'):
self._restoreSer()
def assertRaisesEx(self, exception, callable, *args, **kwargs):
"""
from
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307970
"""
if "exc_args" in kwargs:
exc_args = kwargs["exc_args"]
del kwargs["exc_args"]
else:
exc_args = None
if "exc_pattern" in kwargs:
exc_pattern = kwargs["exc_pattern"]
del kwargs["exc_pattern"]
else:
exc_pattern = None
argv = [repr(a) for a in args]\
+ ["%s=%r" % (k,v) for k,v in kwargs.items()]
callsig = "%s(%s)" % (callable.__name__, ", ".join(argv))
try:
callable(*args, **kwargs)
except exception, exc:
if exc_args is not None:
self.failIf(exc.args != exc_args,
"%s raised %s with unexpected args: "\
"expected=%r, actual=%r"\
% (callsig, exc.__class__, exc_args, exc.args))
if exc_pattern is not None:
self.failUnless(exc_pattern.search(str(exc)),
"%s raised %s, but the exception "\
"does not match '%s': %r"\
% (callsig, exc.__class__, exc_pattern.pattern,
str(exc)))
except:
exc_info = sys.exc_info()
print exc_info
self.fail("%s raised an unexpected exception type: "\
"expected=%s, actual=%s"\
% (callsig, exception, exc_info[0]))
else:
self.fail("%s did not raise %s" % (callsig, exception))
def assertRaisesMsg(self, excClass, msg, callableObj, *args, **kwargs):
"""
Just like unittest.TestCase.assertRaises,
but checks that the message is right too.
Usage::
self.assertRaisesMsg(
MyException, "Exception message",
my_function, (arg1, arg2)
)
from
http://www.nedbatchelder.com/blog/200609.html#e20060905T064418
"""
try:
callableObj(*args, **kwargs)
except excClass, exc:
excMsg = unicode(exc)
if not msg:
# No message provided: any message is fine.
return
elif excMsg == msg:
# Message provided, and we got the right message: passes.
return
else:
# Message provided, and it didn't match: fail!
raise self.failureException(
u"Right exception, wrong message: got '%s' instead of '%s'" %
(excMsg, msg))
else:
if hasattr(excClass, '__name__'):
excName = excClass.__name__
else:
excName = str(excClass)
raise self.failureException(
"Expected to raise %s, didn't get an exception at all" %
excName
)
def do_equal_p(self, tests, att='cssText', debug=False, raising=True):
"""
if raising self.p is used for parsing, else self.pf
"""
p = cssutils.CSSParser(raiseExceptions=raising)
# parses with self.p and checks att of result
for test, expected in tests.items():
if debug:
print '"%s"' % test
s = p.parseString(test)
if expected is None:
expected = test
self.assertEqual(expected, unicode(s.__getattribute__(att), 'utf-8'))
def do_raise_p(self, tests, debug=False, raising=True):
# parses with self.p and expects raise
p = cssutils.CSSParser(raiseExceptions=raising)
for test, expected in tests.items():
if debug:
print '"%s"' % test
self.assertRaises(expected, p.parseString, test)
def do_equal_r(self, tests, att='cssText', debug=False):
# sets attribute att of self.r and asserts Equal
for test, expected in tests.items():
if debug:
print '"%s"' % test
self.r.__setattr__(att, test)
if expected is None:
expected = test
self.assertEqual(expected, self.r.__getattribute__(att))
def do_raise_r(self, tests, att='_setCssText', debug=False):
# sets self.r and asserts raise
for test, expected in tests.items():
if debug:
print '"%s"' % test
self.assertRaises(expected, self.r.__getattribute__(att), test)
|