forked from jaraco/cssutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_errorhandler.py
More file actions
146 lines (116 loc) · 4.38 KB
/
test_errorhandler.py
File metadata and controls
146 lines (116 loc) · 4.38 KB
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
"""Tests for parsing which does not raise Exceptions normally"""
import io
import logging
import sys
import xml.dom
import pytest
import cssutils
@pytest.fixture(autouse=True)
def save_log(monkeypatch):
"""
Replace default log and ignore its output.
"""
monkeypatch.setattr(cssutils.log, 'raiseExceptions', False)
monkeypatch.setattr(
cssutils.log, '_log', logging.getLogger('IGNORED-CSSUTILS-TEST')
)
class TestErrorHandler:
def _setHandler(self):
"sets new handler and returns StringIO instance to getvalue"
s = io.StringIO()
h = logging.StreamHandler(s)
h.setFormatter(logging.Formatter('%(levelname)s %(message)s'))
# remove if present already
cssutils.log.removeHandler(h)
cssutils.log.addHandler(h)
return s
def test_calls(self):
"cssutils.log.*"
s = self._setHandler()
cssutils.log.setLevel(logging.DEBUG)
cssutils.log.debug('msg', neverraise=True)
assert s.getvalue() == 'DEBUG msg\n'
s = self._setHandler()
cssutils.log.setLevel(logging.INFO)
cssutils.log.info('msg', neverraise=True)
assert s.getvalue() == 'INFO msg\n'
s = self._setHandler()
cssutils.log.setLevel(logging.WARNING)
cssutils.log.warn('msg', neverraise=True)
assert s.getvalue() == 'WARNING msg\n'
s = self._setHandler()
cssutils.log.setLevel(logging.ERROR)
cssutils.log.error('msg', neverraise=True)
assert s.getvalue() == 'ERROR msg\n'
s = self._setHandler()
cssutils.log.setLevel(logging.FATAL)
cssutils.log.fatal('msg', neverraise=True)
assert s.getvalue() == 'CRITICAL msg\n'
s = self._setHandler()
cssutils.log.setLevel(logging.CRITICAL)
cssutils.log.critical('msg', neverraise=True)
assert s.getvalue() == 'CRITICAL msg\n'
s = self._setHandler()
cssutils.log.setLevel(logging.CRITICAL)
cssutils.log.error('msg', neverraise=True)
assert s.getvalue() == ''
def test_linecol(self):
"cssutils.log line col"
o = cssutils.log.raiseExceptions
cssutils.log.raiseExceptions = True
s = cssutils.css.CSSStyleSheet()
try:
s.cssText = '@import x;'
except xml.dom.DOMException as e:
assert str(e) == 'CSSImportRule: Unexpected ident. [1:9: x]'
assert e.line == 1
assert e.col == 9
if sys.platform.startswith('java'):
assert e.msg == 'CSSImportRule: Unexpected ident. [1:9: x]'
else:
assert e.args == ('CSSImportRule: Unexpected ident. [1:9: x]',)
cssutils.log.raiseExceptions = o
@pytest.mark.network
def test_handlers(self):
"cssutils.log"
s = self._setHandler()
cssutils.log.setLevel(logging.FATAL)
assert cssutils.log.getEffectiveLevel() == logging.FATAL
cssutils.parseString('a { color: 1 }')
assert s.getvalue() == ''
cssutils.log.setLevel(logging.DEBUG)
cssutils.parseString('a { color: 1 }')
# TODO: Fix?
# self.assertEqual(
# s.getvalue(),
# u'ERROR Property: Invalid value for "CSS Color Module '
# 'Level 3/CSS Level 2.1" property: 1 [1:5: color]\n')
assert (
s.getvalue() == 'ERROR Property: Invalid value for "CSS Level 2.1" '
'property: 1 [1:5: color]\n'
)
s = self._setHandler()
cssutils.log.setLevel(logging.ERROR)
cssutils.parseUrl('http://example.com')
assert s.getvalue()[:38] == 'ERROR Expected "text/css" mime type'
def test_parsevalidation(self):
style = 'color: 1'
t = 'a { %s }' % style
cssutils.log.setLevel(logging.DEBUG)
# sheet
s = self._setHandler()
cssutils.parseString(t)
assert len(s.getvalue()) != 0
s = self._setHandler()
cssutils.parseString(t, validate=False)
assert s.getvalue() == ''
# style
s = self._setHandler()
cssutils.parseStyle(style)
assert len(s.getvalue()) != 0
s = self._setHandler()
cssutils.parseStyle(style, validate=True)
assert len(s.getvalue()) != 0
s = self._setHandler()
cssutils.parseStyle(style, validate=False)
assert s.getvalue() == ''