77except ImportError :
88 import simplejson as json
99
10+ try :
11+ unittest .TestCase .assertEqual
12+ except AttributeError :
13+ unittest .TestCase .assertEqual = unittest .TestCase .assertEquals
14+
1015import html5lib
1116from html5lib import html5parser , serializer , constants
1217from html5lib .treewalkers ._base import TreeWalker
@@ -83,7 +88,16 @@ def serialize_xhtml(input, options):
8388 options = dict ([(str (k ),v ) for k ,v in options .iteritems ()])
8489 return serializer .XHTMLSerializer (** options ).render (JsonWalker (input ),options .get ("encoding" ,None ))
8590
86- def make_test (input , expected , xhtml , options ):
91+ def runSerializerTest (input , expected , xhtml , options ):
92+ encoding = options .get ("encoding" , None )
93+
94+ if encoding :
95+ encode = lambda x : x .encode (encoding )
96+ expected = map (encode , expected )
97+ if xhtml :
98+ xhtml = map (encode , xhtml )
99+
100+
87101 result = serialize_html (input , options )
88102 if len (expected ) == 1 :
89103 assert expected [0 ] == result , "Expected:\n %s\n Actual:\n %s\n Options\n xhtml:False\n %s" % (expected [0 ], result , str (options ))
@@ -114,13 +128,12 @@ def testDoctypeSystemId(self):
114128 self .throwsWithLatin1 ([["Doctype" , u"potato" , u"potato" , u"\u0101 " ]])
115129
116130 def testCdataCharacters (self ):
117- self .assertEquals ("<style>ā" , serialize_html ([["StartTag" , "http://www.w3.org/1999/xhtml" , "style" , {}],
118- ["Characters" , u"\u0101 " ]],
119- {"encoding" : "iso-8859-1" }))
131+ runSerializerTest ([["StartTag" , "http://www.w3.org/1999/xhtml" , "style" , {}], ["Characters" , u"\u0101 " ]],
132+ [u"<style>ā" ], None , {"encoding" : "iso-8859-1" })
120133
121134 def testCharacters (self ):
122- self . assertEquals ( "ā" , serialize_html ([["Characters" , u"\u0101 " ]],
123- {"encoding" : "iso-8859-1" }) )
135+ runSerializerTest ([["Characters" , u"\u0101 " ]],
136+ [ u"ā" ], None , {"encoding" : "iso-8859-1" })
124137
125138 def testStartTagName (self ):
126139 self .throwsWithLatin1 ([["StartTag" , u"http://www.w3.org/1999/xhtml" , u"\u0101 " , []]])
@@ -132,9 +145,9 @@ def testAttributeName(self):
132145 self .throwsWithLatin1 ([["StartTag" , u"http://www.w3.org/1999/xhtml" , u"span" , [{"namespace" : None , "name" : u"\u0101 " , "value" : u"potato" }]]])
133146
134147 def testAttributeValue (self ):
135- self . assertEquals ( "<span potato=ā>" , serialize_html ([["StartTag" , u"http://www.w3.org/1999/xhtml" , u"span" ,
136- [{"namespace" : None , "name" : u"potato" , "value" : u"\u0101 " }]]],
137- {"encoding" : "iso-8859-1" }) )
148+ runSerializerTest ([["StartTag" , u"http://www.w3.org/1999/xhtml" , u"span" ,
149+ [{"namespace" : None , "name" : u"potato" , "value" : u"\u0101 " }]]],
150+ [ u"<span potato=ā>" ], None , {"encoding" : "iso-8859-1" })
138151
139152 def testEndTagName (self ):
140153 self .throwsWithLatin1 ([["EndTag" , u"http://www.w3.org/1999/xhtml" , u"\u0101 " ]])
@@ -154,27 +167,27 @@ def testEntityReplacement(self):
154167 doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>"""
155168 tree = etree .fromstring (doc , parser = self .parser ).getroottree ()
156169 result = serializer .serialize (tree , tree = "lxml" , omit_optional_tags = False )
157- self .assertEquals (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>\u03B2 </html>""" , result )
170+ self .assertEqual (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>\u03B2 </html>""" , result )
158171
159172 def testEntityXML (self ):
160173 doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>></html>"""
161174 tree = etree .fromstring (doc , parser = self .parser ).getroottree ()
162175 result = serializer .serialize (tree , tree = "lxml" , omit_optional_tags = False )
163- self .assertEquals (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>></html>""" , result )
176+ self .assertEqual (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>></html>""" , result )
164177
165178 def testEntityNoResolve (self ):
166179 doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>"""
167180 tree = etree .fromstring (doc , parser = self .parser ).getroottree ()
168181 result = serializer .serialize (tree , tree = "lxml" , omit_optional_tags = False ,
169182 resolve_entities = False )
170- self .assertEquals (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>""" , result )
183+ self .assertEqual (u"""<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>""" , result )
171184
172185def test_serializer ():
173186 for filename in html5lib_test_files ('serializer' , '*.test' ):
174- tests = json .load (file (filename ))
187+ tests = json .load (open (filename ))
175188 test_name = os .path .basename (filename ).replace ('.test' ,'' )
176189 for index , test in enumerate (tests ['tests' ]):
177190 xhtml = test .get ("xhtml" , test ["expected" ])
178191 if test_name == 'optionaltags' :
179192 xhtml = None
180- yield make_test , test ["input" ], test ["expected" ], xhtml , test .get ("options" , {})
193+ yield runSerializerTest , test ["input" ], test ["expected" ], xhtml , test .get ("options" , {})
0 commit comments