forked from rennat/pynliner
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tests.py
301 lines (237 loc) · 12.5 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
#!/usr/bin/env python
import unittest
import pynliner
import StringIO
import logging
import cssutils
from pynliner import Pynliner
class Basic(unittest.TestCase):
def setUp(self):
self.html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>"
self.p = Pynliner().from_string(self.html)
def test_01_fromString(self):
"""Test 'fromString' constructor"""
self.assertEqual(self.p.source_string, self.html)
def test_02_get_soup(self):
"""Test '_get_soup' method"""
self.p._get_soup()
self.assertEqual(unicode(self.p.soup), self.html)
def test_03_get_styles(self):
"""Test '_get_styles' method"""
self.p._get_soup()
self.p._get_styles()
self.assertEqual(self.p.style_string, u'h1 { color:#ffcc00; }\n')
self.assertEqual(unicode(self.p.soup), u'<h1>Hello World!</h1>')
def test_04_apply_styles(self):
"""Test '_apply_styles' method"""
self.p._get_soup()
self.p._get_styles()
self.p._apply_styles()
self.assertEqual(unicode(self.p.soup), u'<h1 style="color: #fc0">Hello World!</h1>')
def test_05_run(self):
"""Test 'run' method"""
output = self.p.run()
self.assertEqual(output, u'<h1 style="color: #fc0">Hello World!</h1>')
def test_06_with_cssString(self):
"""Test 'with_cssString' method"""
cssString = 'h1 {font-size: 2em;}'
self.p = Pynliner().from_string(self.html).with_cssString(cssString)
self.assertEqual(self.p.style_string, cssString + '\n')
output = self.p.run()
self.assertEqual(output, u'<h1 style="font-size: 2em; color: #fc0">Hello World!</h1>')
def test_07_fromString(self):
"""Test 'fromString' complete"""
output = pynliner.fromString(self.html)
desired = u'<h1 style="color: #fc0">Hello World!</h1>'
self.assertEqual(output, desired)
def test_08_fromURL(self):
"""Test 'fromURL' constructor"""
url = 'http://media.tannern.com/pynliner/test.html'
p = Pynliner()
p.from_url(url)
self.assertEqual(p.root_url, 'http://media.tannern.com')
self.assertEqual(p.relative_url, 'http://media.tannern.com/pynliner/')
p._get_soup()
p._get_external_styles()
self.assertEqual(p.style_string, "p {color: #999}")
p._get_internal_styles()
self.assertEqual(p.style_string, "p {color: #999}\nh1 {color: #ffcc00;}\n")
p._get_styles()
output = p.run()
desired = u"""<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
</head>
<body>
<h1 style="color: #fc0">Hello World!</h1>
<p style="color: #999">Possim tincidunt putamus iriure eu nulla. Facer qui volutpat ut aliquam sequitur. Mutationem legere feugiat autem clari notare. Nulla typi augue suscipit lectores in.</p>
<p style="color: #999">Facilisis claritatem eum decima dignissim legentis. Nulla per legentis odio molestie quarta. Et velit typi claritas ipsum ullamcorper.</p>
</body>
</html>"""
self.assertEqual(output, desired)
def test_09_overloadedStyles(self):
html = '<style>h1 { color: red; } #test { color: blue; }</style><h1 id="test">Hello world!</h1>'
expected = '<h1 id="test" style="color: blue">Hello world!</h1>'
output = Pynliner().from_string(html).run()
self.assertEqual(expected, output)
class CommaSelector(unittest.TestCase):
def setUp(self):
self.html = """<style>.b1,.b2 { font-weight:bold; } .c {color: red}</style><span class="b1">Bold</span><span class="b2 c">Bold Red</span>"""
self.p = Pynliner().from_string(self.html)
def test_01_fromString(self):
"""Test 'fromString' constructor"""
self.assertEqual(self.p.source_string, self.html)
def test_02_get_soup(self):
"""Test '_get_soup' method"""
self.p._get_soup()
self.assertEqual(unicode(self.p.soup), self.html)
def test_03_get_styles(self):
"""Test '_get_styles' method"""
self.p._get_soup()
self.p._get_styles()
self.assertEqual(self.p.style_string, u'.b1,.b2 { font-weight:bold; } .c {color: red}\n')
self.assertEqual(unicode(self.p.soup), u'<span class="b1">Bold</span><span class="b2 c">Bold Red</span>')
def test_04_apply_styles(self):
"""Test '_apply_styles' method"""
self.p._get_soup()
self.p._get_styles()
self.p._apply_styles()
self.assertEqual(unicode(self.p.soup), u'<span class="b1" style="font-weight: bold">Bold</span><span class="b2 c" style="color: red; font-weight: bold">Bold Red</span>')
def test_05_run(self):
"""Test 'run' method"""
output = self.p.run()
self.assertEqual(output, u'<span class="b1" style="font-weight: bold">Bold</span><span class="b2 c" style="color: red; font-weight: bold">Bold Red</span>')
def test_06_with_cssString(self):
"""Test 'with_cssString' method"""
cssString = '.b1,.b2 {font-size: 2em;}'
self.p = Pynliner().from_string(self.html).with_cssString(cssString)
self.assertEqual(self.p.style_string, cssString + '\n')
output = self.p.run()
self.assertEqual(output, u'<span class="b1" style="font-size: 2em; font-weight: bold">Bold</span><span class="b2 c" style="color: red; font-size: 2em; font-weight: bold">Bold Red</span>')
def test_07_fromString(self):
"""Test 'fromString' complete"""
output = pynliner.fromString(self.html)
desired = u'<span class="b1" style="font-weight: bold">Bold</span><span class="b2 c" style="color: red; font-weight: bold">Bold Red</span>'
self.assertEqual(output, desired)
def test_08_comma_whitespace(self):
"""Test excess whitespace in CSS"""
html = '<style>h1, h2 ,h3,\nh4{ color: #000} </style><h1>1</h1><h2>2</h2><h3>3</h3><h4>4</h4>'
desired_output = '<h1 style="color: #000">1</h1><h2 style="color: #000">2</h2><h3 style="color: #000">3</h3><h4 style="color: #000">4</h4>'
output = Pynliner().from_string(html).run()
self.assertEqual(output, desired_output)
class Extended(unittest.TestCase):
def test_overwrite(self):
"""Test overwrite inline styles"""
html = '<style>h1 {color: #000;}</style><h1 style="color: #fff">Foo</h1>'
desired_output = '<h1 style="color: #000; color: #fff">Foo</h1>'
output = Pynliner().from_string(html).run()
self.assertEqual(output, desired_output)
def test_overwrite_comma(self):
"""Test overwrite inline styles"""
html = '<style>h1,h2,h3 {color: #000;}</style><h1 style="color: #fff">Foo</h1><h3 style="color: #fff">Foo</h3>'
desired_output = '<h1 style="color: #000; color: #fff">Foo</h1><h3 style="color: #000; color: #fff">Foo</h3>'
output = Pynliner().from_string(html).run()
self.assertEqual(output, desired_output)
class LogOptions(unittest.TestCase):
def setUp(self):
self.html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>"
def test_no_log(self):
self.p = Pynliner()
self.assertEqual(self.p.log, None)
self.assertEqual(cssutils.log.enabled, False)
def test_custom_log(self):
self.log = logging.getLogger('testlog')
self.log.setLevel(logging.DEBUG)
self.logstream = StringIO.StringIO()
handler = logging.StreamHandler(self.logstream)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.p = Pynliner(self.log).from_string(self.html)
self.p.run()
log_contents = self.logstream.getvalue()
self.assertTrue("DEBUG" in log_contents)
class BeautifulSoupBugs(unittest.TestCase):
def test_double_doctype(self):
self.html = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">"""
output = pynliner.fromString(self.html)
self.assertTrue("<!<!" not in output)
def test_double_comment(self):
self.html = """<!-- comment -->"""
output = pynliner.fromString(self.html)
self.assertTrue("<!--<!--" not in output)
class ComplexSelectors(unittest.TestCase):
def test_multiple_class_selector(self):
html = """<h1 class="a b">Hello World!</h1>"""
css = """h1.a.b { color: red; }"""
expected = u"""<h1 class="a b" style="color: red">Hello World!</h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_combination_selector(self):
html = """<h1 id="a" class="b">Hello World!</h1>"""
css = """h1#a.b { color: red; }"""
expected = u"""<h1 id="a" class="b" style="color: red">Hello World!</h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_descendant_selector(self):
html = """<h1><span>Hello World!</span></h1>"""
css = """h1 span { color: red; }"""
expected = u"""<h1><span style="color: red">Hello World!</span></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_selector(self):
html = """<h1><span>Hello World!</span></h1>"""
css = """h1 > span { color: red; }"""
expected = u"""<h1><span style="color: red">Hello World!</span></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_adjacent_selector(self):
html = """<h1>Hello World!</h1><h2>How are you?</h2>"""
css = """h1 + h2 { color: red; }"""
expected = u"""<h1>Hello World!</h1><h2 style="color: red">How are you?</h2>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_attribute_selector(self):
html = """<h1 title="foo">Hello World!</h1>"""
css = """h1[title="foo"] { color: red; }"""
expected = u"""<h1 title="foo" style="color: red">Hello World!</h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
class CaseSensitive(unittest.TestCase):
def setUp(self):
self.pyn = Pynliner(case_sensitive=False)
def test_case_sensitive_tag(self):
# Test upper/lowercase tag names in style sheets
html = '<style>H1 {color: #000;}</style><H1 style="color: #fff">Foo</H1><h1>Bar</h1>'
desired_output = '<h1 style="color: #000; color: #fff">Foo</h1><h1 style="color: #000">Bar</h1>'
output = self.pyn.from_string(html).run()
self.assertEqual(output, desired_output)
def test_case_sensitive_tag_class(self):
# Test upper/lowercase tag names with class names
html = '<style>h1.b1 { font-weight:bold; } H1.c {color: red}</style><h1 class="b1">Bold</h1><H1 class="c">Bold Red</h1>'
desired_output = '<h1 class="b1" style="font-weight: bold">Bold</h1><h1 class="c" style="color: red">Bold Red</h1>'
output = self.pyn.from_string(html).run()
self.assertEqual(output, desired_output)
def test_case_sensitive_tag_id(self):
# Test case sensitivity of tags with class names
html = '<style>h1#tst { font-weight:bold; } H1#c {color: red}</style><h1 id="tst">Bold</h1><H1 id="c">Bold Red</h1>'
desired_output = '<h1 id="tst" style="font-weight: bold">Bold</h1><h1 id="c" style="color: red">Bold Red</h1>'
output = self.pyn.from_string(html).run()
self.assertEqual(output, desired_output)
def test_case_sensitive_class(self):
# Test case insensitivity of class names
html = '<style>h1.BOLD { font-weight:bold; }</style><h1 class="bold">Bold</h1><h1 class="BOLD">Bold</h1>'
desired_output = '<h1 class="bold" style="font-weight: bold">Bold</h1><h1 class="BOLD" style="font-weight: bold">Bold</h1>'
output = self.pyn.from_string(html).run()
self.assertEqual(output, desired_output)
class NewlineSeparator(unittest.TestCase):
def test_newline_multiple_styles(self):
"""Test that multiple CSS styles get separated with spaces instead of newlines"""
html = '<style>h1 { font-weight:bold; color: red}</style><h1>Bold Red</h1>'
desired_output = '<h1 style="font-weight: bold; color: red">Bold Red</h1>'
output = Pynliner().from_string(html).run()
self.assertEqual(output, desired_output)
if __name__ == '__main__':
unittest.main()