8000 Minor refactoring/cleanup · html5lib/html5lib-python@668cd3a · GitHub
[go: up one dir, main page]

Skip to content

Commit 668cd3a

Browse files
committed
Minor refactoring/cleanup
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40845
1 parent 9066d8a commit 668cd3a

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

tests/support.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __iter__(self):
8787
def isSectionHeading(self, line):
8888
"""If the current heading is a test section heading return the heading,
8989
otherwise return False"""
90-
line=line.strip()
90+
line = line.strip()
9191
if line.startswith("#") and line[1:] in self.sections:
9292
return line[1:]
9393
else:
@@ -98,7 +98,22 @@ def normaliseOutput(self, data):
9898
for key,value in data.iteritems():
9999
if value.endswith("\n"):
100100
data[key] = value[:-1]
101+
result = []
101102
for heading in self.sections:
102-
if heading not in data:
103-
data[heading] = None
104-
return data
103+
result.append(data.get(heading))
104+
return result
105+
106+
def convert(stripChars):
107+
def convertData(data):
108+
"""convert the output of str(document) to the format used in the testcases"""
109+
data = data.split("\n")
110+
rv = []
111+
for line in data:
112+
if line.startswith("|"):
113+
rv.append(line[stripChars:])
114+
else:
115+
rv.append(line)
116+
return "\n".join(rv)
117+
return convertData
118+
119+
convertExpected = convert(2)

tests/test_parser.py

Lines changed: 5 additions & 23 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, TestData
5+
from support import html5lib_test_files, TestData, convert, convertExpected
66

77
from html5lib import html5parser, treebuilders
88

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

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:])
57-
else:
58-
rv.append(line)
59-
return "\n".join(rv)
60-
return convertData
6149
#XXX - There should just be one function here but for some reason the testcase
6250
#format differs from the treedump format by a single space character
63-
6451
def convertTreeDump(data):
6552
return "\n".join(convert(3)(data).split("\n")[1:])
6653

67-
convertExpected = convert(2)
68-
6954
import re
7055
attrlist = re.compile(r"^(\s+)\w+=.*(\n\1\w+=.*)+",re.M)
7156
def sortattrs(x):
@@ -98,9 +83,6 @@ def runParserTest(self, innerHTML, input, expected, errors, treeClass):
9883
if checkParseErrors:
9984
self.assertEquals(len(p.errors), len(errors), errorMsg2)
10085

101-
def test_parser():
102-
yield innerHTML, input, expected, errors, treeName, treeCls
103-
10486
def buildTestSuite():
10587
sys.stdout.write('Testing tree builders '+ " ".join(treeTypes.keys()) + "\n")
10688

@@ -112,10 +94,10 @@ def buildTestSuite():
11294
tests = TestData(filename, ("data", "errors", "document-fragment",
11395
"document"))
11496

115-
for index, test in enumerate(tests):
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):
97+
for index, (input, errors, innerHTML, expected) in enumerate(tests):
98+
errors = errors.split("\n")
99+
def testFunc(self, innerHTML=innerHTML, input=input,
100+
expected=expected, errors=errors, treeCls=treeCls):
119101
return self.runParserTest(innerHTML, input, expected, errors, treeCls)
120102
setattr(TestCase, "test_%s_%d_%s" % (testName,index+1,treeName),
121103
testFunc)

tests/test_treewalkers.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import StringIO
44
import unittest
55

6-
from support import html5lib_test_files, TestData
6+
from support import html5lib_test_files, TestData, convertExpected
77

88
from html5lib import html5parser, treewalkers, treebuilders
99
from html5lib.filters.lint import Filter as LintFilter, LintError
1010

11-
from test_parser import convertExpected
12-
1311
def PullDOMAdapter(node):
1412
from xml.dom import Node
1513
from xml.dom.pulldom import START_ELEMENT, END_ELEMENT, COMMENT, CHARACTERS
@@ -231,11 +229,10 @@ def buildTestSuite():
231229
tests = TestData(filename, ("data", "errors", "document-fragment",
232230
"document"))
233231

234-
for index, test in enumerate(tests):
235-
errors = test['errors'].split("\n")
236-
def testFunc(self, innerHTML=test['document-fragment'],
237-
input=test['data'], expected=test['document'],
238-
errors=errors, treeCls=treeCls):
232+
for index, (input, errors, innerHTML, expected) in enumerate(tests):
233+
errors = errors.split("\n")
234+
def testFunc(self, innerHTML=innerHTML, input=input,
235+
expected=expected, errors=errors, treeCls=treeCls):
239236
self.runTest(innerHTML, input, expected, errors, treeCls)
240237
setattr(TestCase, "test_%s_%d_%s" % (testName,index+1,treeName),
241238
testFunc)

0 commit comments

Comments
 (0)
0