File: basetest.py

package info (click to toggle)
python-css-parser 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,248 kB
  • sloc: python: 20,584; sh: 11; makefile: 7
file content (312 lines) | stat: -rw-r--r-- 10,973 bytes parent folder | download | duplicates (2)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
from __future__ import absolute_import, print_function, unicode_literals

import logging
import os
import re
import sys
import unittest
from contextlib import contextmanager
from io import StringIO

import css_parser

"""Base class for all tests"""


TEST_HOME = os.path.dirname(os.path.abspath(__file__))
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


def get_resource_filename(resource_name):
    """Get the resource filename.
    """
    return os.path.join(TEST_HOME, *resource_name.split('/'))


def get_sheet_filename(sheet_name):
    """Get the filename for the given sheet."""
    # Extract all sheets since they might use @import
    sheet_dir = get_resource_filename('sheets')
    return os.path.join(sheet_dir, sheet_name)


class BaseTestCase(unittest.TestCase):

    def _tempSer(self):
        "Replace default ser with temp ser."
        self._ser = css_parser.ser
        css_parser.ser = css_parser.serialize.CSSSerializer()

    def _restoreSer(self):
        "Restore the default ser."
        css_parser.ser = self._ser

    @staticmethod
    def _setHandler():
        "sets log's new handler and returns StringIO instance to getvalue"
        s = StringIO()
        h = logging.StreamHandler(s)
        h.setFormatter(logging.Formatter('%(levelname)s    %(message)s'))
        # remove if present already
        css_parser.log.removeHandler(h)
        css_parser.log.addHandler(h)
        return s

    @staticmethod
    def captureLog(log_level, callable, *args, **kwargs):
        """returns the output of an ad hoc created log
        (which doesn't affect the standard one).
        Example usage:
        warning = self.captureLog(logging.WARNING,
                                   css_parser.stylesheets.MediaQuery,
                                   'unknown-media')
        self.assertEqual(warning, 'WARNING [...]')
        """
        old_log = css_parser.log._log
        old_level = css_parser.log.getEffectiveLevel()
        css_parser.log.setLog(logging.getLogger('CSS_PARSER-IGNORE'))
        css_parser.log.setLevel(log_level)
        s = BaseTestCase._setHandler()
        callable(*args, **kwargs)
        result = s.getvalue()
        css_parser.log.setLog(old_log)
        css_parser.log.setLevel(old_level)
        return result

    def setUp(self):
        # a raising parser!!!
        css_parser.log.raiseExceptions = True
        css_parser.log.setLevel(logging.FATAL)
        self.p = css_parser.CSSParser(raiseExceptions=True)

    @contextmanager
    def patch_default_fetcher(self, return_value):
        import css_parser.util as cu
        orig = cu._defaultFetcher

        def defaultFetcher(*a):
            return return_value
        if callable(return_value):
            cu._defaultFetcher = return_value
        else:
            cu._defaultFetcher = defaultFetcher
        try:
            yield
        finally:
            cu._defaultFetcher = orig

    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 as exc:
            if exc_args is not None:
                self.assertEqual(
                    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.assertTrue(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 Exception:
            exc_info = sys.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 _assertRaisesMsgSubstring(self, excClass, msg, substring_match, callableObj, *args, **kwargs):
        try:
            callableObj(*args, **kwargs)
        except excClass as exc:
            excMsg = str(exc)
            if not msg:
                # No message provided: any message is fine.
                return
            elif (msg in excMsg if substring_match else msg == excMsg):
                # Message provided, and we got the right message: passes.
                return
            else:
                # Message provided, and it didn't match: fail!
                raise self.failureException(
                    "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 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,
                kwarg1=val, kwarg2=val)

        from
        http://www.nedbatchelder.com/blog/200609.html#e20060905T064418
        """
        return self._assertRaisesMsgSubstring(excClass, msg, False, callableObj, *args, **kwargs)

    def assertRaisesMsgSubstring(self, excClass, msg, callableObj, *args, **kwargs):
        """
        Just like assertRaisesMsg, but looks for substring in the message.
        """
        return self._assertRaisesMsgSubstring(excClass, msg, True, callableObj, *args, **kwargs)

    def do_equal_p(self, tests, att='cssText', debug=False, raising=True):
        """
        if raising self.p is used for parsing, else self.pf
        """
        p = css_parser.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
            ans = s.__getattribute__(att)
            if isinstance(ans, bytes):
                ans = ans.decode('utf-8')
            self.assertEqual(expected, ans)

    def do_raise_p(self, tests, debug=False, raising=True):
        # parses with self.p and expects raise
        p = css_parser.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)

    def do_raise_r_list(self, tests, err, att='_setCssText', debug=False):
        # sets self.r and asserts raise
        for test in tests:
            if debug:
                print(('"%s"' % test))
            self.assertRaises(err, self.r.__getattribute__(att), test)


class GenerateTests(type):
    """Metaclass to handle a parametrized test.

    This works by generating many test methods from a single method.

    To generate the methods, you need the base method with the prefix
    "gen_test_", which takes the parameters. Then you define the attribute
    "cases" on this method with a list of cases. Each case is a tuple, which is
    unpacked when the test is called.

    Example::

        def gen_test_length(self, string, expected):
            self.assertEqual(len(string), expected)
        gen_test_length.cases = [
            ("a", 1),
            ("aa", 2),
        ]
    """
    def __new__(cls, name, bases, attrs):
        new_attrs = {}
        for aname, aobj in attrs.items():
            if not aname.startswith("gen_test_"):
                new_attrs[aname] = aobj
                continue

            # Strip off the gen_
            test_name = aname[4:]
            cases = aobj.cases
            for case_num, case in enumerate(cases):
                stringed_case = cls.make_case_repr(case)
                case_name = "%s_%s_%s" % (test_name, case_num, stringed_case)
                # Force the closure binding

                def make_wrapper(case=case, aobj=aobj):
                    def wrapper(self):
                        aobj(self, *case)
                    return wrapper
                wrapper = make_wrapper()
                wrapper.__name__ = str(case_name)
                wrapper.__doc__ = "%s(%s)" % (test_name,
                                              ", ".join(map(repr, case)))
                if aobj.__doc__ is not None:
                    wrapper.__doc__ += "\n\n" + aobj.__doc__
                new_attrs[case_name] = wrapper
        return type(name, bases, new_attrs)

    @classmethod
    def make_case_repr(cls, case):
        if isinstance(case, type('')):
            value = case
        else:
            try:
                iter(case)
            except TypeError:
                value = repr(case)
            else:
                value = '_'.join(cls.make_case_repr(x) for x in case)
        value = re.sub('[^A-Za-z_]', '_', value)
        value = re.sub('_{2,}', '_', value)
        return value