Skip to content

Commit d3d1f46

Browse files
committed
Add a unit test to test formatting. Tests original whitespace (getHTML), AdvancedHTMLFormatter (via getFormattedHTML), and AdvancedHTMLMiniFormatter (via getMiniHTML)
1 parent 8e853a5 commit d3d1f46

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env GoodTests.py
2+
'''
3+
Test various formatting methods
4+
'''
5+
6+
import sys
7+
import subprocess
8+
import tempfile
9+
10+
from AdvancedHTMLParser.Parser import AdvancedHTMLParser
11+
12+
TEST_HTML = '''<html><head><title>Hello World</title></head>
13+
<body>
14+
<div>Hello world <span>And welcome to the show.</span>
15+
</div>
16+
</body></html>'''
17+
18+
class TestFormatting(object):
19+
20+
# TODO: Add a test which checks that we retain whitespace
21+
22+
def test_retainOriginalWhitespace(self):
23+
'''
24+
test_retainOriginalWhitespace - Test that we retain the original whitespacing
25+
'''
26+
parser = AdvancedHTMLParser()
27+
28+
parser.parseStr(TEST_HTML)
29+
30+
rawHtml = parser.getHTML()
31+
32+
# This will not equal the original HTML exactly because we fixup some tag issues, like ' >'
33+
assert rawHtml == '<html ><head ><title >Hello World</title></head>\n <body >\n <div >Hello world <span >And welcome to the show.</span>\n </div>\n </body></html>' , 'Did not retain original whitespace like expected'
34+
35+
36+
def test_getFormattedHTML(self):
37+
'''
38+
test_getFormattedHTML - Tests the getFormattedHTML call for pretty-printing HTML
39+
'''
40+
parser = AdvancedHTMLParser()
41+
42+
parser.parseStr(TEST_HTML)
43+
44+
formattedHTML = parser.getFormattedHTML()
45+
46+
assert formattedHTML == '\n<html >\n <head >\n <title >Hello World\n </title>\n </head> \n <body > \n <div >Hello world \n <span >And welcome to the show.\n </span> \n </div> \n </body>\n</html>' , 'Did not get expected formatting using default 4 spaces.'
47+
48+
formattedHTMLTabIndent = parser.getFormattedHTML('\t')
49+
50+
assert formattedHTMLTabIndent == '\n<html >\n\t<head >\n\t\t<title >Hello World\n\t\t</title>\n\t</head> \n\t<body > \n\t\t<div >Hello world \n\t\t\t<span >And welcome to the show.\n\t\t\t</span> \n\t\t</div> \n\t</body>\n</html>' , 'Did not get expected formatting using tabs.'
51+
52+
53+
def test_getMiniHTML(self):
54+
'''
55+
test_getMiniHTML - Gets a "mini" representation that only contains the functional whitespace characters in HTML repr
56+
'''
57+
parser = AdvancedHTMLParser()
58+
59+
parser.parseStr(TEST_HTML)
60+
61+
miniHTML = parser.getMiniHTML()
62+
63+
assert miniHTML == '<html ><head ><title >Hello World</title></head> <body > <div >Hello world <span >And welcome to the show.</span> </div> </body></html>'
64+
65+
66+
67+
if __name__ == '__main__':
68+
sys.exit(subprocess.Popen('GoodTests.py -n1 "%s" %s' %(sys.argv[0], ' '.join(['"%s"' %(arg.replace('"', '\\"'), ) for arg in sys.argv[1:]]) ), shell=True).wait())

0 commit comments

Comments
 (0)