forked from disqus/toronado
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
225 lines (184 loc) · 5.95 KB
/
Copy pathtests.py
File metadata and controls
225 lines (184 loc) · 5.95 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
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
import pytest
import sys
import unittest
from exam import Exam, fixture
from lxml import etree, html
from lxml.cssselect import CSSSelector
from toronado import Rule, Properties, inline, from_string
try:
from lxml.html import soupparser
except ImportError:
soupparser = None
class TestCase(Exam, unittest.TestCase):
pass
class RuleTestCase(TestCase):
def test_compares_by_specificity(self):
self.assertGreater(Rule('#main'), Rule('div'))
self.assertEqual(Rule('div'), Rule('p'))
self.assertLess(Rule('div'), Rule('div.container'))
def test_combine_respects_specificity_rules(self):
properties = Rule.combine((
Rule('h1', {
'font-weight': 'bold',
'color': 'blue',
}),
Rule('h1#primary', {
'color': 'red',
}),
))
self.assertIsInstance(properties, Properties)
self.assertEqual(properties, {
'font-weight': 'bold',
'color': 'red',
})
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_from_string(self):
properties = Properties.from_string('color: red; font-weight: bold')
self.assertEqual(properties, {
'color': 'red',
'font-weight': 'bold',
})
def test_from_string_cleans_whitespace(self):
properties = Properties.from_string('color : red;\nfont-weight: bold ;')
self.assertEqual(properties, {
'color': 'red',
'font-weight': 'bold',
})
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')
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)