@@ -83,28 +83,21 @@ def serialize_xhtml(input, options):
83
83
options = dict ([(str (k ),v )for k ,v in options .iteritems ()])
84
84
return serializer .XHTMLSerializer (** options ).render (JsonWalker (input ),options .get ("encoding" ,None ))
85
85
86
+ def make_test (input ,expected ,xhtml ,options ):
87
+ result = serialize_html (input ,options )
88
+ if len (expected )== 1 :
89
+ assert expected [0 ]== result ,"Expected:\n %s\n Actual:\n %s\n Options\n xhtml:False\n %s" % (expected [0 ],result ,str (options ))
90
+ elif result not in expected :
91
+ assert False ,"Expected: %s, Received: %s" % (expected ,result )
86
92
87
- class TestCase (unittest .TestCase ):
88
- def addTest (cls ,name ,description ,input ,expected ,xhtml ,options ):
89
- func = lambda self :self .mockTest (input ,options ,expected ,xhtml )
90
- func .__doc__ = "\t " .join ([name ,description ,str (input ),str (options )])
91
- setattr (cls ,name ,func )
92
- addTest = classmethod (addTest )
93
+ if not xhtml :
94
+ return
93
95
94
- def mockTest (self ,input ,options ,expected ,xhtml ):
95
- result = serialize_html (input ,options )
96
- if len (expected )== 1 :
97
- self .assertEquals (expected [0 ],result ,"Expected:\n %s\n Actual:\n %s\n Options\n xhtml:False\n %s" % (expected [0 ],result ,str (options )))
98
- elif result not in expected :
99
- self .fail ("Expected: %s, Received: %s" % (expected ,result ))
100
-
101
- if not xhtml :return
102
-
103
- result = serialize_xhtml (input ,options )
104
- if len (xhtml )== 1 :
105
- self .assertEquals (xhtml [0 ],result ,"Expected:\n %s\n Actual:\n %s\n Options\n xhtml:True\n %s" % (xhtml [0 ],result ,str (options )))
106
- elif result not in xhtml :
107
- self .fail ("Expected: %s, Received: %s" % (xhtml ,result ))
96
+ result = serialize_xhtml (input ,options )
97
+ if len (xhtml )== 1 :
98
+ assert xhtml [0 ]== result ,"Expected:\n %s\n Actual:\n %s\n Options\n xhtml:True\n %s" % (xhtml [0 ],result ,str (options ))
99
+ elif result not in xhtml :
100
+ assert False ,"Expected: %s, Received: %s" % (xhtml ,result )
108
101
109
102
110
103
class EncodingTestCase (unittest .TestCase ):
@@ -150,55 +143,38 @@ def testComment(self):
150
143
self .throwsWithLatin1 ([["Comment" ,u"\u0101 " ]])
151
144
152
145
153
- class LxmlTestCase (unittest .TestCase ):
154
- def setUp (self ):
155
- self .parser = etree .XMLParser (resolve_entities = False )
156
- self .treewalker = html5lib .getTreeWalker ("lxml" )
157
- self .serializer = serializer .HTMLSerializer ()
158
-
159
- def testEntityReplacement (self ):
160
- doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>"""
161
- tree = etree .fromstring (doc ,parser = self .parser ).getroottree ()
162
- result = serializer .serialize (tree ,tree = "lxml" ,omit_optional_tags = False )
163
- self .assertEquals (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>\u03B2 </html>""" ,result )
164
-
165
- def testEntityXML (self ):
166
- doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>></html>"""
167
- tree = etree .fromstring (doc ,parser = self .parser ).getroottree ()
168
- result = serializer .serialize (tree ,tree = "lxml" ,omit_optional_tags = False )
169
- self .assertEquals (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>></html>""" ,result )
170
-
171
- def testEntityNoResolve (self ):
172
- doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>"""
173
- tree = etree .fromstring (doc ,parser = self .parser ).getroottree ()
174
- result = serializer .serialize (tree ,tree = "lxml" ,omit_optional_tags = False ,
175
- resolve_entities = False )
176
- self .assertEquals (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>""" ,result )
177
-
178
- def buildBasicTestSuite ():
146
+ if "lxml" in optionals_loaded :
147
+ class LxmlTestCase (unittest .TestCase ):
148
+ def setUp (self ):
149
+ self .parser = etree .XMLParser (resolve_entities = False )
150
+ self .treewalker = html5lib .getTreeWalker ("lxml" )
151
+ self .serializer = serializer .HTMLSerializer ()
152
+
153
+ def testEntityReplacement (self ):
154
+ doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>"""
155
+ tree = etree .fromstring (doc ,parser = self .parser ).getroottree ()
156
+ result = serializer .serialize (tree ,tree = "lxml" ,omit_optional_tags = False )
157
+ self .assertEquals (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>\u03B2 </html>""" ,result )
158
+
159
+ def testEntityXML (self ):
160
+ doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>></html>"""
161
+ tree = etree .fromstring (doc ,parser = self .parser ).getroottree ()
162
+ result = serializer .serialize (tree ,tree = "lxml" ,omit_optional_tags = False )
163
+ self .assertEquals (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>></html>""" ,result )
164
+
165
+ def testEntityNoResolve (self ):
166
+ doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>"""
167
+ tree = etree .fromstring (doc ,parser = self .parser ).getroottree ()
168
+ result = serializer .serialize (tree ,tree = "lxml" ,omit_optional_tags = False ,
169
+ resolve_entities = False )
170
+ self .assertEquals (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>""" ,result )
171
+
172
+ def test_serializer ():
179
173
for filename in html5lib_test_files ('serializer' ,'*.test' ):
180
- test_name = os .path .basename (filename ).replace ('.test' ,'' )
181
174
tests = json .load (file (filename ))
175
+ test_name = os .path .basename (filename ).replace ('.test' ,'' )
182
176
for index ,test in enumerate (tests ['tests' ]):
183
177
xhtml = test .get ("xhtml" ,test ["expected" ])
184
- if test_name == 'optionaltags' :xhtml = None
185
- TestCase .addTest ('test_%s_%d' % (test_name ,index + 1 ),
186
- test ["description" ],test ["input" ],test ["expected" ],xhtml ,
187
- test .get ("options" , {}))
188
- return unittest .TestLoader ().loadTestsFromTestCase (TestCase )
189
-
190
- def buildTestSuite ():
191
- allTests = [buildBasicTestSuite ()]
192
- allTests .append (unittest .TestLoader ().loadTestsFromTestCase (EncodingTestCase ))
193
- if "lxml" in optionals_loaded :
194
- allTests .append (unittest .TestLoader ().loadTestsFromTestCase (LxmlTestCase ))
195
-
196
- return unittest .TestSuite (allTests )
197
-
198
-
199
- def main ():
200
- buildTestSuite ()
201
- unittest .main ()
202
-
203
- if __name__ == "__main__" :
204
- main ()
178
+ if test_name == 'optionaltags' :
179
+ xhtml = None
180
+ yield make_test ,test ["input" ],test ["expected" ],xhtml ,test .get ("options" , {})