8000 Slight change in testcase parsing per discussions with hsivonen; all … · awesome-python/html5lib-python@7476b19 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7476b19

Browse files
committed
Slight change in testcase parsing per discussions with hsivonen; all lines starting # are now considered new sest sections
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40885
1 parent b08102b commit 7476b19

File tree

5 files changed

+38
-29
lines changed

5 files changed

+38
-29
lines changed

tests/support.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,30 @@ def load(f):
6161
def html5lib_test_files(subdirectory, files='*.dat'):
6262
return glob.glob(os.path.join(os.path.pardir,os.path.pardir,'testdata',subdirectory,files))
6363

64+
class DefaultDict(dict):
65+
def __init__(self, default, *args, **kwargs):
66+
self.default = default
67+
dict.__init__(self, *args, **kwargs)
68+
69+
def __getitem__(self, key):
70+
return dict.get(self, key, self.default)
71+
6472
class TestData(object):
65-
def __init__(self, filename, sections):
73+
def __init__(self, filename, newTestHeading="data"):
6674
self.f = open(filename)
67-
self.sections = sections
75+
self.newTestHeading = newTestHeading
6876

6977
def __iter__(self):
70-
data = {}
78+
data = DefaultDict(None)
7179
key=None
7280
for line in self.f:
7381
heading = self.isSectionHeading(line)
7482
if heading:
75-
if data and heading == self.sections[0]:
83+
if data and heading == self.newTestHeading:
7684
#Remove trailing newline
7785
data[key] = data[key][:-1]
7886
yield self.normaliseOutput(data)
79-
data = {}
87+
data = DefaultDict(None)
8088
key = heading
8189
data[key]=""
8290
elif key is not None:
@@ -87,9 +95,8 @@ def __iter__(self):
8795
def isSectionHeading(self, line):
8896
"""If the current heading is a test section heading return the heading,
8997
otherwise return False"""
90-
line = line.strip()
91-
if line.startswith("#") and line[1:] in self.sections:
92-
return line[1:]
98+
if line.startswith("#"):
99+
return line[1:].strip()
93100
else:
94101
return False
95102

@@ -98,10 +105,7 @@ def normaliseOutput(self, data):
98105
for key,value in data.iteritems():
99106
if value.endswith("\n"):
100107
data[key] = value[:-1]
101-
result = []
102-
for heading in self.sections:
103-
result.append(data.get(heading))
104-
return result
108+
return data
105109

106110
def convert(stripChars):
107111
def convertData(data):

tests/test_encoding.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ def buildTestSuite():
1212
for filename in html5lib_test_files("encoding"):
1313
test_name = os.path.basename(filename).replace('.dat',''). \
1414
replace('-','')
15-
tests = TestData(filename, ("data", "encoding"))
16-
for idx, (data, encoding) in enumerate(tests):
17-
def encodingTest(self, data=data, encoding=encoding):
15+
tests = TestData(filename, "data")
16+
for idx, test in enumerate(tests):
17+
def encodingTest(self, data=test['data'], encoding=test['encoding']):
1818
stream = inputstream.HTMLInputStream(data,chardet=False)
1919
self.assertEquals(encoding.lower(), stream.charEncoding)
2020
setattr(Html5EncodingTestCase, 'test_%s_%d' % (test_name, idx+1),

tests/test_parser.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ def buildTestSuite():
8989
for filename in html5lib_test_files('tree-construction'):
9090
testName = os.path.basename(filename).replace(".dat","")
9191

92-
tests = TestData(filename, ("data", "errors", "document-fragment",
93-
"document"))
92+
tests = TestData(filename, "data")
9493

95-
for index, (input, errors, innerHTML, expected) in enumerate(tests):
94+
for index, test in enumerate(tests):
95+
input, errors, innerHTML, expected = [test[key] for key in
96+
'data', 'errors',
97+
'document-fragment',
98+
'document']
9699
if errors:
97100
errors = errors.split("\n")
98101
def testFunc(self, innerHTML=innerHTML, input=input,

tests/test_tokenizer.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ def processEmptyTag(self, token):
3737
self.outputTokens.append([u"StartTag", token["name"], token["data"]])
3838

3939
def processEndTag(self, token):
40-
if token["data"]:
41-
self.processParseError(None)
4240
self.outputTokens.append([u"EndTag", token["name"]])
4341

4442
def processComment(self, token):
@@ -55,7 +53,7 @@ def processEOF(self, token):
5553
pass
5654

5755
def processParseError(self, token):
58-
self.outputTokens.append(u"ParseError")
56+
self.outputTokens.append([u"ParseError", token["data"]])
5957

6058
def concatenateCharacterTokens(tokens):
6159
outputTokens = []
@@ -73,9 +71,10 @@ def concatenateCharacterTokens(tokens):
7371
def normalizeTokens(tokens):
7472
""" convert array of attributes to a dictionary """
7573
# TODO: convert tests to reflect arrays
76-
for token in tokens:
77-
if token[0] == 'StartTag':
78-
token[2] = dict(token[2][::-1])
74+
for i, token in enumerate(tokens):
75+
if token[0] == u'ParseError':
76+
tokens[i] = token[0]
77+
#token[2] = dict(token[2][::-1])
7978
return tokens
8079

8180
def tokensMatch(expectedTokens, recievedTokens):
@@ -102,14 +101,14 @@ def runTokenizerTest(self, test):
102101
test['lastStartTag'] = None
103102
parser = TokenizerTestParser(test['contentModelFlag'],
104103
test['lastStartTag'])
105-
106-
tokens = normalizeTokens(parser.parse(test['input']))
104+
tokens = parser.parse(test['input'])
107105
tokens = concatenateCharacterTokens(tokens)
108106
errorMsg = "\n".join(["\n\nContent Model Flag:",
109107
test['contentModelFlag'] ,
110108
"\nInput:", str(test['input']),
111109
"\nExpected:", str(output),
112110
"\nRecieved:", str(tokens)])
111+
tokens = normalizeTokens(tokens)
113112
self.assertEquals(tokensMatch(tokens, output), True, errorMsg)
114113

115114
def buildTestSuite():

tests/test_treewalkers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,13 @@ def buildTestSuite():
229229
testName = os.path.basename(filename).replace(".dat","")
230230
if testName == "tests5": continue # TODO
231231

232-
tests = TestData(filename, ("data", "errors", "document-fragment",
233-
"document"))
232+
tests = TestData(filename, "data")
234233

235-
for index, (input, errors, innerHTML, expected) in enumerate(tests):
234+
for index, test in enumerate(tests):
235+
(input, errors,
236+
innerHTML, expected) = [test[key] for key in ("data", "errors",
237+
"document-fragment",
238+
"document")]
236239
errors = errors.split("\n")
237240
def testFunc(self, innerHTML=innerHTML, input=input,
238241
expected=expected, errors=errors, treeCls=treeCls):

0 commit comments

Comments
 (0)
0