2
2
import sys
3
3
import StringIO
4
4
import unittest
5
- from support import html5lib_test_files
5
+ from support import html5lib_test_files , TestData
6
6
7
7
from html5lib import html5parser , treebuilders
8
8
46
46
#Run the parse error checks
47
47
checkParseErrors = False # TODO
48
48
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 :])
71
57
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 )
94
68
95
69
import re
96
70
attrlist = re .compile (r"^(\s+)\w+=.*(\n\1\w+=.*)+" ,re .M )
@@ -108,11 +82,14 @@ def runParserTest(self, innerHTML, input, expected, errors, treeClass):
108
82
document = p .parseFragment (StringIO .StringIO (input ), innerHTML )
109
83
else :
110
84
document = p .parse (StringIO .StringIO (input ))
85
+
111
86
output = convertTreeDump (p .tree .testSerializer (document ))
112
87
output = attrlist .sub (sortattrs , output )
88
+
89
+ expected = convertExpected (expected )
113
90
expected = attrlist .sub (sortattrs , expected )
114
- errorMsg = "\n " .join (["\n \n Expected:" , expected ,
115
- "\n Recieved:" , output ])
91
+ errorMsg = "\n " .join (["\n \n Input:" , input , " \ n Expected:" , expected ,
92
+ "\n Recieved:" , output ])
116
93
self .assertEquals (expected , output , errorMsg )
117
94
errStr = ["Line: %i Col: %i %s" % (line , col , message ) for
118
95
((line ,col ), message ) in p .errors ]
@@ -130,21 +107,19 @@ def buildTestSuite():
130
107
for treeName , treeCls in treeTypes .iteritems ():
131
108
for filename in html5lib_test_files ('tree-construction' ):
132
109
testName = os .path .basename (filename ).replace (".dat" ,"" )
110
+ if testName == "tests5" : continue # TODO
133
111
134
- f = open (filename )
135
- tests = f . read (). split ( "#data \n " )
112
+ tests = TestData (filename , ( "data" , "errors" , "document-fragment" ,
113
+ "document" ) )
136
114
137
115
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 ):
145
119
return self .runParserTest (innerHTML , input , expected , errors , treeCls )
146
120
setattr (TestCase , "test_%s_%d_%s" % (testName ,index + 1 ,treeName ),
147
121
testFunc )
122
+
148
123
return unittest .TestLoader ().loadTestsFromTestCase (TestCase )
149
124
150
125
def main ():
0 commit comments