/
tests.py
438 lines (392 loc) · 15.6 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
from __future__ import unicode_literals
import unittest
import htmlmin
from htmlmin.decorator import htmlmin as htmlmindecorator
from htmlmin.middleware import HTMLMinMiddleware
MINIFY_FUNCTION_TEXTS = {
'simple_text': (
' a b',
' a b'
),
'long_text': (
'''When doing test-driven development, or
running automated builds that need testing before they are deployed
\t\t for downloading or use, it's often useful to be able to run a project's
unit tests without actually deploying the project anywhere.\r\n\r\n\n\r\rThe
test command runs project's unit tests without actually deploying it, by
temporarily putting the project's source on sys.path, after first running
build_ext -i to ensure that any C extensions are built.
''',
("When doing test-driven development, or running automated "
"builds that need testing before they are deployed for "
"downloading or use, it's often useful to be able to run a "
"project's unit tests without actually deploying the project "
"anywhere. The test command runs project's unit tests without "
"actually deploying it, by temporarily putting the project's "
"source on sys.path, after first running build_ext -i to "
"ensure that any C extensions are built. ")
),
'simple_html': (
(' <body> <b> a <i pre>b </i>' # <b> is not closed
'<pre> x </pre> <textarea> Y </textarea></body> '),
(' <body> <b> a <i>b </i>'
'<pre> x </pre> <textarea> Y </textarea></body> ')
),
'with_doctype': (
'\n\n<!DOCTYPE html>\n\n<body> X Y </body>',
'<!DOCTYPE html>\n<body> X Y </body>'
),
}
FEATURES_TEXTS = {
'remove_tag_name_whitespace': (
'<body > <br /> <textarea > </ textarea ></ body> ',
'<body> <br> <textarea> </textarea></body> '
),
'remove_comments': (
'<body> this text should <!-- X --> have comments removed</body>',
'<body> this text should have comments removed</body>',
),
'keep_comments': (
'<body> this text should <!--! not --> have comments removed</body>',
'<body> this text should <!-- not --> have comments removed</body>',
),
'keep_pre_attribute': (
'<body>the <strong pre style="">pre</strong> should stay </body>',
'<body>the <strong pre style="">pre</strong> should stay </body>',
),
'custom_pre_attribute': (
'<body>the <strong pre > X </strong><span custom> Y </span></body>',
'<body>the <strong pre> X </strong><span> Y </span></body>',
),
'keep_empty': (
'<body> <div id="x" > A </div> <div id=" y "> B </div> </body>',
'<body> <div id="x"> A </div> <div id=" y "> B </div> </body>',
),
'remove_empty': (
('<body> \n <div id="x" > A </div>\r'
'<div id=" y "> B </div>\r\n <div> C </div> <div>D</div> </body>'),
('<body><div id="x"> A </div>'
'<div id=" y "> B </div><div> C </div> <div>D</div> </body>'),
),
'remove_all_empty': (
('<body> \n <div id="x" > A </div>\r'
'<div id=" y "> B </div>\r\n <div> C </div> <div>D</div> </body>'),
('<body><div id="x"> A </div>'
'<div id=" y "> B </div><div> C </div><div>D</div></body>'),
),
'dont_minify_div': (
'<body> <div> X </div> </body>',
'<body> <div> X </div> </body>',
),
'minify_pre': (
'<body> <pre> X </pre> </body>',
'<body> <pre> X </pre> </body>',
),
'remove_head_spaces': (
'<head> <title> X Y </title> </head>',
'<head><title>X Y</title></head>',
),
'dont_minify_scripts_or_styles': (
'<body> <script> X </script> <style> X</style> </body>',
'<body> <script> X </script> <style> X</style> </body>',
),
'remove_close_from_tags': (
('<body> <area/> <base/> <br /> <hr/> <img /> <input /> <keygen/> '
'<meta /><param/><source/><track /><wbr /> </body>'),
('<body> <area> <base> <br> <hr> <img> <input> <keygen> '
'<meta><param><source><track><wbr> </body>'),
),
'remove_space_from_self_closed_tags': (
'<body> <y /> <x /></body>',
'<body> <y/> <x/></body>',
),
}
SELF_CLOSE_TEXTS = {
'p_self_close': (
'<body> <p pre > X <p> Y ',
'<body> <p> X <p> Y ',
),
'li_self_close': (
'<body> <ul> <li pre > X <li> Y <li pre> Z</ul> Q',
'<body> <ul> <li> X <li> Y <li> Z</ul> Q',
),
'dt_self_close': (
'<body> <dl> <dt pre > X <dt> Y <dt pre> Z</dt></dl> Q',
'<body> <dl> <dt> X <dt> Y <dt> Z</dt></dl> Q',
),
'dd_self_close': (
'<body> <dl> <dd pre > X <dd> Y <dd pre> Z</dl> Q',
'<body> <dl> <dd> X <dd> Y <dd> Z</dl> Q',
),
'optgroup_self_close': (
('<body> <select a > <optgroup pre> <option> X</option> '
'<optgroup> <option> Y</option> </optgroup> </select> </body>'),
('<body> <select a> <optgroup> <option> X</option> '
'<optgroup> <option> Y</option> </optgroup> </select> </body>'),
),
'option_self_close': (
('<body> <select a > <option pre > X '
'<option> Y </option></select> </body>'),
('<body> <select a> <option> X '
'<option> Y </option></select> </body>'),
),
'colgroup_self_close': (
'<body> <table> <colgroup pre> </table></body>',
'<body> <table> <colgroup> </table></body>',
),
'tbody_self_close': (
('<body> <table> <tbody pre> <tr> <td> X </td></tr> \n'
'\n <tbody> <tr> <td>Y </td></tr>\n\n\n </body>'),
('<body> <table> <tbody> <tr> <td> X </td></tr> \n'
'\n <tbody> <tr> <td>Y </td></tr> </body>'),
),
'thead_self_close': (
('<body> <table> <thead pre> <tr> <td> X </td></tr> '
' <tbody> <tr> <td>Y </td></tr> </body>'),
('<body> <table> <thead> <tr> <td> X </td></tr> '
' <tbody> <tr> <td>Y </td></tr> </body>'),
),
'tfoot_self_close': (
('<body> <table> <tfoot pre> <tr> <td> X </td></tr> '
' <tbody> <tr> <td>Y </td></tr> </body>'),
('<body> <table> <tfoot> <tr> <td> X </td></tr> '
' <tbody> <tr> <td>Y </td></tr> </body>'),
),
'tr_self_close': (
('<body> <table> <thead> <tr pre > <td> X </td> '
' <tr> <td>Y </td> </body>'),
('<body> <table> <thead> <tr> <td> X </td> '
' <tr> <td>Y </td> </body>'),
),
'td_self_close': (
'<body> <table> <thead> <tr> <td pre> X <td>Y </body>',
'<body> <table> <thead> <tr> <td> X <td>Y </body>',
),
'th_self_close': (
'<body> <table> <thead> <tr> <th pre> X <th>Y </body>',
'<body> <table> <thead> <tr> <th> X <th>Y </body>',
),
'a_p_interaction': ( # the 'pre' functionality continues after the </a>
'<body><a> <p pre> X </a> <p> Y</body>',
'<body><a> <p> X </a> <p> Y</body>',
),
}
SELF_OPENING_TEXTS = {
'html_closed_no_open': (
'<head></head><body> X </body></html>',
'<head></head><body> X </body></html>'
),
'head_closed_no_open': (
' </head><body> X </body>',
' </head><body> X </body>' # TODO: we could theoretically kill that leading
# space. See HTMLMinParse.handle_endtag
),
'body_closed_no_open': (
' X </body>',
' X </body>'
),
'colgroup_self_open': (
'<body> <table> </colgroup> </table></body>',
'<body> <table> </colgroup> </table></body>',
),
'tbody_self_open': (
'<body> <table> </tbody> </table></body>',
'<body> <table> </tbody> </table></body>',
),
'p_closed_no_open': ( # this isn't valid html, but its worth accounting for
'<body><div pre> X </p> </div><div> Y </p> </div></body>',
'<body><div> X </p> </div><div> Y </p> </div></body>',
),
}
class HTMLMinTestMeta(type):
def __new__(cls, name, bases, dct):
def make_test(text):
def inner_test(self):
self.assertEqual(self.minify(text[0]), text[1])
return inner_test
for k, v in dct.get('__reference_texts__',{}).iteritems():
if 'test_'+k not in dct:
dct['test_'+k] = make_test(v)
return type.__new__(cls, name, bases, dct)
class HTMLMinTestCase(unittest.TestCase):
__metaclass__ = HTMLMinTestMeta
def setUp(self):
self.minify = htmlmin.minify
class TestMinifyFunction(HTMLMinTestCase):
__reference_texts__ = MINIFY_FUNCTION_TEXTS
def test_basic_minification_quality(self):
import codecs
with codecs.open('htmlmin/tests/large_test.html', encoding='utf-8') as inpf:
inp = inpf.read()
out = self.minify(inp)
self.assertEqual(len(inp) - len(out), 776)
def test_high_minification_quality(self):
import codecs
with codecs.open('htmlmin/tests/large_test.html', encoding='utf-8') as inpf:
inp = inpf.read()
out = self.minify(inp, remove_all_empty_space=True, remove_comments=True)
self.assertEqual(len(inp) - len(out), 4013)
class TestMinifierObject(HTMLMinTestCase):
__reference_texts__ = MINIFY_FUNCTION_TEXTS
def setUp(self):
HTMLMinTestCase.setUp(self)
self.minifier = htmlmin.Minifier()
self.minify = self.minifier.minify
def test_reuse(self):
text = self.__reference_texts__['simple_text']
self.assertEqual(self.minify(text[0]), text[1])
self.assertEqual(self.minify(text[0]), text[1])
def test_buffered_input(self):
text = self.__reference_texts__['long_text']
self.minifier.input(text[0][:len(text[0]) // 2])
self.minifier.input(text[0][len(text[0]) // 2:])
self.assertEqual(self.minifier.finalize(), text[1])
class TestMinifyFeatures(HTMLMinTestCase):
__reference_texts__ = FEATURES_TEXTS
def test_remove_comments(self):
text = self.__reference_texts__['remove_comments']
self.assertEqual(htmlmin.minify(text[0], remove_comments=True), text[1])
def test_keep_comments(self):
text = self.__reference_texts__['keep_comments']
self.assertEqual(htmlmin.minify(text[0], remove_comments=True), text[1])
def test_keep_pre_attribute(self):
text = self.__reference_texts__['keep_pre_attribute']
self.assertEqual(htmlmin.minify(text[0], keep_pre=True), text[1])
def test_custom_pre_attribute(self):
text = self.__reference_texts__['custom_pre_attribute']
self.assertEqual(htmlmin.minify(text[0], pre_attr='custom'), text[1])
def test_keep_empty(self):
text = self.__reference_texts__['keep_empty']
self.assertEqual(htmlmin.minify(text[0]), text[1])
def test_remove_empty(self):
text = self.__reference_texts__['remove_empty']
self.assertEqual(htmlmin.minify(text[0], remove_empty_space=True), text[1])
def test_remove_all_empty(self):
text = self.__reference_texts__['remove_all_empty']
self.assertEqual(htmlmin.minify(text[0], remove_all_empty_space=True),
text[1])
def test_dont_minify_div(self):
text = self.__reference_texts__['dont_minify_div']
self.assertEqual(htmlmin.minify(text[0], pre_tags=('div',)), text[1])
def test_minify_pre(self):
text = self.__reference_texts__['minify_pre']
self.assertEqual(htmlmin.minify(text[0], pre_tags=('div',)), text[1])
def test_remove_head_spaces(self):
text = self.__reference_texts__['remove_head_spaces']
self.assertEqual(htmlmin.minify(text[0]), text[1])
def test_dont_minify_scripts_or_styles(self):
text = self.__reference_texts__['dont_minify_scripts_or_styles']
self.assertEqual(htmlmin.minify(text[0], pre_tags=[]), text[1])
class TestSelfClosingTags(HTMLMinTestCase):
__reference_texts__ = SELF_CLOSE_TEXTS
class TestSelfOpeningTags(HTMLMinTestCase):
__reference_texts__ = SELF_OPENING_TEXTS
class TestDecorator(HTMLMinTestCase):
def test_direct_decorator(self):
@htmlmindecorator
def directly_decorated():
return ' X Y '
self.assertEqual(' X Y ', directly_decorated())
def test_options_decorator(self):
@htmlmindecorator(remove_comments=True)
def directly_decorated():
return ' X <!-- Removed --> Y '
self.assertEqual(' X Y ', directly_decorated())
class TestMiddleware(HTMLMinTestCase):
def setUp(self):
HTMLMinTestCase.setUp(self)
def wsgi_app(environ, start_response):
start_response(environ['status'], environ['headers'])
yield environ['content']
self.wsgi_app = wsgi_app
def call_app(self, app, status, headers, content):
response_status = [] # these need to be mutable so that they can be changed
response_headers = [] # within our inner function.
def start_response(status, headers, exc_info=None):
response_status.append(status)
response_headers.append(headers)
response_body = ''.join(app({'status': status,
'content': content,
'headers': headers},
start_response))
return response_status[0], response_headers[0], response_body
def test_middlware(self):
app = HTMLMinMiddleware(self.wsgi_app)
status, headers, body = self.call_app(
app, '200 OK', (('Content-Type', 'text/html'),),
' X Y ')
self.assertEqual(body, ' X Y ')
def test_middlware_minifier_options(self):
app = HTMLMinMiddleware(self.wsgi_app, remove_comments=True)
status, headers, body = self.call_app(
app, '200 OK', (('Content-Type', 'text/html'),),
' X Y <!-- Z -->')
self.assertEqual(body, ' X Y ')
def test_middlware_off_by_default(self):
app = HTMLMinMiddleware(self.wsgi_app, by_default=False)
status, headers, body = self.call_app(
app, '200 OK', (('Content-Type', 'text/html'),),
' X Y ')
self.assertEqual(body, ' X Y ')
def test_middlware_on_by_header(self):
app = HTMLMinMiddleware(self.wsgi_app, by_default=False)
status, headers, body = self.call_app(
app, '200 OK', (
('Content-Type', 'text/html'),
('X-HTML-Min-Enable', 'True'),
),
' X Y ')
self.assertEqual(body, ' X Y ')
def test_middlware_off_by_header(self):
app = HTMLMinMiddleware(self.wsgi_app)
status, headers, body = self.call_app(
app, '200 OK', (
('Content-Type', 'text/html'),
('X-HTML-Min-Enable', 'False'),
),
' X Y ')
self.assertEqual(body, ' X Y ')
def test_middlware_remove_header(self):
app = HTMLMinMiddleware(self.wsgi_app)
status, headers, body = self.call_app(
app, '200 OK', (
('Content-Type', 'text/html'),
('X-HTML-Min-Enable', 'False'),
),
' X Y ')
self.assertFalse(any((h == 'X-HTML-Min-Enable' for h, v in headers)))
def test_middlware_keep_header(self):
app = HTMLMinMiddleware(self.wsgi_app, keep_header=True)
status, headers, body = self.call_app(
app, '200 OK', [
('Content-Type', 'text/html'),
('X-HTML-Min-Enable', 'False'),
],
' X Y ')
self.assertTrue(any((h == 'X-HTML-Min-Enable' for h, v in headers)))
def suite():
minify_function_suite = unittest.TestLoader().\
loadTestsFromTestCase(TestMinifyFunction)
minifier_object_suite = unittest.TestLoader().\
loadTestsFromTestCase(TestMinifierObject)
minify_features_suite = unittest.TestLoader().\
loadTestsFromTestCase(TestMinifyFeatures)
self_closing_tags_suite = unittest.TestLoader().\
loadTestsFromTestCase(TestSelfClosingTags)
self_opening_tags_suite = unittest.TestLoader().\
loadTestsFromTestCase(TestSelfOpeningTags)
decorator_suite = unittest.TestLoader().\
loadTestsFromTestCase(TestDecorator)
middleware_suite = unittest.TestLoader().\
loadTestsFromTestCase(TestMiddleware)
return unittest.TestSuite([
minify_function_suite,
minifier_object_suite,
minify_features_suite,
self_closing_tags_suite,
self_opening_tags_suite,
decorator_suite,
middleware_suite,
])
if __name__ == '__main__':
unittest.main()