-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtests.py
369 lines (305 loc) · 9.29 KB
/
tests.py
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import pytest
import unittest
from exam import Exam
from lxml import etree, html
from lxml.cssselect import CSSSelector
from toronado import (
Properties,
Rule,
compress_box_property,
expand_shorthand_box_property,
from_string,
inline,
warn_unsupported_shorthand_property,
)
try:
from lxml.html import soupparser
except ImportError:
soupparser = None
class TestCase(Exam, unittest.TestCase):
pass
def test_compress_box_property():
compress = compress_box_property('margin', 'margin-{}')
assert compress({
'margin-top': '1px',
'margin-right': '1px',
'margin-bottom': '1px',
'margin-left': '1px',
}) == {
'margin': '1px',
}
assert compress({
'margin-top': '1px',
'margin-right': '2px',
'margin-bottom': '1px',
'margin-left': '2px',
}) == {
'margin': '1px 2px',
}
assert compress({
'margin-top': '1px',
'margin-right': '2px',
'margin-bottom': '3px',
'margin-left': '2px',
}) == {
'margin': '1px 2px 3px',
}
assert compress({
'margin-top': '1px',
'margin-right': '2px',
'margin-bottom': '3px',
'margin-left': '4px',
}) == {
'margin': '1px 2px 3px 4px',
}
assert compress({
'margin-top': '1px',
'margin-bottom': '1px',
}) == {
'margin-top': '1px',
'margin-bottom': '1px',
}
assert compress({
'margin-top': '1px',
'margin-right': '1px',
'margin-bottom': '1px',
'margin-left': '1px',
'other-property': 'foo',
}) == {
'margin': '1px',
'other-property': 'foo',
}
properties = {}
assert compress(properties) is properties
def test_expand_shorthand_box_property():
expand = expand_shorthand_box_property('margin-{}')
assert expand('1px') == {
'margin-top': '1px',
'margin-right': '1px',
'margin-bottom': '1px',
'margin-left': '1px',
}
assert expand('1px 2px') == {
'margin-top': '1px',
'margin-right': '2px',
'margin-bottom': '1px',
'margin-left': '2px',
}
assert expand('1px 2px 3px') == {
'margin-top': '1px',
'margin-right': '2px',
'margin-bottom': '3px',
'margin-left': '2px',
}
assert expand('1px 2px 3px 4px') == {
'margin-top': '1px',
'margin-right': '2px',
'margin-bottom': '3px',
'margin-left': '4px',
}
def test_warn_unsupported_shorthand_property():
assert warn_unsupported_shorthand_property('font')('10px sans-serif') == {
'font': '10px sans-serif',
}
class RuleTestCase(TestCase):
def test_compares_by_specificity(self):
self.assertGreater(Rule(0, '#main'), Rule(0, 'div'))
self.assertEqual(Rule(0, 'div'), Rule(0, 'p'))
self.assertLess(Rule(0, 'div'), Rule(0, 'div.container'))
def test_combine_respects_specificity_rules(self):
properties = Rule.combine((
Rule(0, 'h1', {
'font-weight': 'bold',
'color': 'blue',
}),
Rule(0, 'h1#primary', {
'color': 'red',
}),
))
self.assertIsInstance(properties, Properties)
self.assertEqual(properties, {
'font-weight': 'bold',
'color': 'red',
})
def tests_combine_respects_ordering(self):
properties = Rule.combine((
Rule(1, 'h1', {'font-size': '10px', 'font-weight': 'bold'}),
Rule(2, 'h1', {'font-size': '20px'})
))
self.assertIsInstance(properties, Properties)
self.assertEqual(properties, {
'font-weight': 'bold',
'font-size': '20px',
})
class PropertiesTestCase(TestCase):
def test_serializes_to_attribute_string(self):
properties = Properties({
'font-weight': 'bold',
'color': 'red',
})
# XXX: Ordering is non-deterministic, so we have to check both variations.
expected = set((
'font-weight: bold; color: red',
'color: red; font-weight: bold',
))
self.assertIn('%s' % (properties,), expected)
def test_compresses_shorthand_properties(self):
properties = Properties({
'margin-top': '10px',
'margin-right': '10px',
'margin-bottom': '10px',
'margin-left': '10px',
})
assert '%s' % (properties,) == 'margin: 10px'
def test_from_string(self):
properties = Properties.from_string('color: red; font-weight: bold')
self.assertEqual(properties, {
'color': 'red',
'font-weight': 'bold',
})
properties = Properties.from_string('padding: 0 10px')
self.assertEqual(properties, {
'padding-top': '0',
'padding-right': '10px',
'padding-bottom': '0',
'padding-left': '10px',
})
class InlineTestCase(TestCase):
def test_inlines_styles(self):
tree = html.document_fromstring("""
<html>
<head>
<style type="text/css">
h1 { color: red; }
</style>
</head>
<body>
<h1>Hello, world.</h1>
</body>
</html>
""")
inline(tree)
heading, = tree.cssselect('h1')
self.assertEqual(heading.attrib['style'], 'color: red')
def test_does_not_override_inlined_styles(self):
tree = html.document_fromstring("""
<html>
<head>
<style type="text/css">
h1 {
color: red;
display: block;
}
</style>
</head>
<body>
<h1 style="color: blue; font-weight: bold">Hello, world.</h1>
</body>
</html>
""")
inline(tree)
heading, = tree.cssselect('h1')
properties = Properties.from_string(heading.attrib['style'])
self.assertEqual(properties, {
'color': 'blue',
'display': 'block',
'font-weight': 'bold',
})
def test_removes_compiled_styles(self):
tree = html.document_fromstring("""
<html>
<head>
<style type="text/css">
h1 { font-weight: bold; }
</style>
</head>
<body>
<h1>Hello, world.</h1>
</body>
</html>
""")
inline(tree)
heading, = tree.cssselect('h1')
self.assertEqual(heading.attrib['style'], 'font-weight: bold')
self.assertEqual(len(tree.cssselect('style')), 0)
def test_skips_inline_false(self):
tree = html.document_fromstring("""
<html>
<head>
<style type="text/css">
h1 { font-weight: bold; }
</style>
<style type="text/css" inline="false">
h1 { color: red; }
</style>
</head>
<body>
<h1>Hello, world.</h1>
</body>
</html>
""")
inline(tree)
heading, = tree.cssselect('h1')
self.assertEqual(heading.attrib['style'], 'font-weight: bold')
stylesheet, = tree.cssselect('style')
self.assertNotIn('inline', stylesheet.attrib)
def test_important_styles(self):
tree = html.document_fromstring("""
<html>
<head>
<style type="text/css">
h1 { color: red !important; }
</style>
</head>
<body>
<h1>Hello, world.</h1>
</body>
</html>
""")
inline(tree)
heading = tree.cssselect('h1')[0]
self.assertEqual(heading.attrib['style'], 'color: red ! important')
def test_empty_styles(self):
tree = html.document_fromstring("""
<html>
<head>
<style type="text/css"></style>
</head>
<body>
<h1>Hello, world.</h1>
</body>
</html>
""")
inline(tree)
class ParserTestCase(TestCase):
document = """
<html>
<head>
<style type="text/css">
h1 { color: red; }
</style>
</head>
<body>
<h1>Hello, world.</h1>
</body>
</html>
"""
def assertInlines(self, tree):
inline(tree)
heading, = CSSSelector('h1')(tree)
self.assertEqual(heading.attrib['style'], 'color: red')
def test_etree(self):
tree = etree.fromstring(self.document)
self.assertInlines(tree)
def test_html(self):
tree = html.document_fromstring(self.document)
self.assertInlines(tree)
@pytest.mark.skipif(soupparser is None,
reason='BeautifulSoup is not installed')
def test_beautifulsoup(self):
tree = soupparser.fromstring(self.document)
self.assertInlines(tree)
def test_from_string(self):
result = from_string(self.document)
tree = etree.fromstring(result)
self.assertInlines(tree)