8000 Move parser test to generic testcase parser · awesome-python/html5lib-python@400ad9d · GitHub
[go: up one dir, main page]

Skip to content

Commit 400ad9d

Browse files
committed
Move parser test to generic testcase parser
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40839
1 parent 7979535 commit 400ad9d

File tree

1 file changed

+31
-56
lines changed

1 file changed

+31
-56
lines changed

tests/test_parser.py

Lines changed: 31 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import StringIO
44
import unittest
5-
from support import html5lib_test_files
5+
from support import html5lib_test_files, TestData
66

77
from html5lib import html5parser, treebuilders
88

@@ -46,51 +46,25 @@
4646
#Run the parse error checks
4747
checkParseErrors = False # TODO
4848

49-
def parseTestcase(testString):
50-
testString = testString.split("\n")
51-
try:
52-
if testString[0] != "#data":
53-
sys.stderr.write(testString)
54-
assert testString[0] == "#data"
55-
except:
56-
raise
57-
innerHTML = False
58-
input = []
59-
expected = []
60-
errors = []
61-
currentList = input
62-
for line in testString:
63-
if line and not (line.startswith("#errors") or
64-
line.startswith("#document") or line.startswith("#data") or
65-
line.startswith("#document-fragment")):
66-
if currentList is expected:
67-
if line.startswith("|"):
68-
currentList.append(line[2:])
69-
else:
70-
currentList.append(line)
49+
def convert(stripChars):
50+
def convertData(data):
51+
"""convert the output of str(document) to the format used in the testcases"""
52+
data = data.split("\n")
53+
rv = []
54+
for line in data:
55+
if line.startswith("|"):
56+
rv.append(line[stripChars:])
7157
else:
72-
currentList.append(line)
73-
elif line == "#errors":
74-
currentList = errors
75-
elif line == "#document" or line.startswith("#document-fragment"):
76-
if line.startswith("#document-fragment"):
77-
innerHTML = line[19:]
78-
if not innerHTML:
79-
sys.stderr.write(testString)
80-
assert innerHTML
81-
currentList = expected
82-
return innerHTML, "\n".join(input), "\n".join(expected), errors
83-
84-
def convertTreeDump(treedump):
85-
"""convert the output of str(document) to the format used in the testcases"""
86-
treedump = treedump.split("\n")[1:]
87-
rv = []
88-
for line in treedump:
89-
if line.startswith("|"):
90-
rv.append(line[3:])
91-
else:
92-
rv.append(line)
93-
return "\n".join(rv)
58+
rv.append(line)
59+
return "\n".join(rv)
60+
return convertData
61+
#XXX - There should just be one function here but for some reason the testcase
62+
#format differs from the treedump format by a single space character
63+
64+
def convertTreeDump(data):
65+
return "\n".join(convert(3)(data).split("\n")[1:])
66+
67+
convertExpected = convert(2)
9468

9569
import re
9670
attrlist = re.compile(r"^(\s+)\w+=.*(\n\1\w+=.*)+",re.M)
@@ -108,11 +82,14 @@ def runParserTest(self, innerHTML, input, expected, errors, treeClass):
10882
document = p.parseFragment(StringIO.StringIO(input), innerHTML)
10983
else:
11084
document = p.parse(StringIO.StringIO(input))
85+
11186
output = convertTreeDump(p.tree.testSerializer(document))
11287
output = attrlist.sub(sortattrs, output)
88+
89+
expected = convertExpected(expected)
11390
expected = attrlist.sub(sortattrs, expected)
114-
errorMsg = "\n".join(["\n\nExpected:", expected,
115-
"\nRecieved:", output])
91+
errorMsg = "\n".join(["\n\nInput:", input, "\nExpected:", expected,
92+
"\nRecieved:", output])
11693
self.assertEquals(expected, output, errorMsg)
11794
errStr = ["Line: %i Col: %i %s"%(line, col, message) for
11895
((line,col), message) in p.errors]
@@ -130,21 +107,19 @@ def buildTestSuite():
130107
for treeName, treeCls in treeTypes.iteritems():
131108
for filename in html5lib_test_files('tree-construction'):
132109
testName = os.path.basename(filename).replace(".dat","")
110+
if testName == "tests5": continue # TODO
133111

134-
f = open(filename)
135-
tests = f.read().split("#data\n")
112+
tests = TestData(filename, ("data", "errors", "document-fragment",
113+
"document"))
136114

137115
for index, test in enumerate(tests):
138-
if test == "": continue
139-
140-
test = "#data\n" + test
141-
innerHTML, input, expected, errors = parseTestcase(test)
142-
143-
def testFunc(self, innerHTML=innerHTML, input=input,
144-
expected=expected, errors=errors, treeCls=treeCls):
116+
errors = test['errors'].split("\n")
117+
def testFunc(self, innerHTML=test['document-fragment'], input=test['data'],
118+
expected=test['document'], errors=errors, treeCls=treeCls):
145119
return self.runParserTest(innerHTML, input, expected, errors, treeCls)
146120
setattr(TestCase, "test_%s_%d_%s" % (testName,index+1,treeName),
147121
testFunc)
122+
148123
return unittest.TestLoader().loadTestsFromTestCase(TestCase)
149124

150125
def main():

0 commit comments

Comments
 (0)
0