77except ImportError :
88import 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):
8388options = dict ([(str (k ),v )for k ,v in options .iteritems ()])
8489return 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+
87101result = serialize_html (input ,options )
88102if len (expected )== 1 :
89103assert 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):
114128self .throwsWithLatin1 ([["Doctype" ,u"potato" ,u"potato" ,u"\u0101 " ]])
115129
116130def 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
121134def 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
125138def testStartTagName (self ):
126139self .throwsWithLatin1 ([["StartTag" ,u"http://www.w3.org/1999/xhtml" ,u"\u0101 " , []]])
@@ -132,9 +145,9 @@ def testAttributeName(self):
132145self .throwsWithLatin1 ([["StartTag" ,u"http://www.w3.org/1999/xhtml" ,u"span" , [{"namespace" :None ,"name" :u"\u0101 " ,"value" :u"potato" }]]])
133146
134147def 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
139152def testEndTagName (self ):
140153self .throwsWithLatin1 ([["EndTag" ,u"http://www.w3.org/1999/xhtml" ,u"\u0101 " ]])
@@ -154,27 +167,27 @@ def testEntityReplacement(self):
154167doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>"""
155168tree = etree .fromstring (doc ,parser = self .parser ).getroottree ()
156169result = 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
159172def testEntityXML (self ):
160173doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>></html>"""
161174tree = etree .fromstring (doc ,parser = self .parser ).getroottree ()
162175result = 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
165178def testEntityNoResolve (self ):
166179doc = """<!DOCTYPE html SYSTEM "about:legacy-compat"><html>β</html>"""
167180tree = etree .fromstring (doc ,parser = self .parser ).getroottree ()
168181result = serializer .serialize (tree ,tree = "lxml" ,omit_optional_tags = False ,
169182resolve_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 ():
173186for filename in html5lib_test_files ('serializer' ,'*.test' ):
174- tests = json .load (file (filename ))
187+ tests = json .load (open (filename ))
175188test_name = os .path .basename (filename ).replace ('.test' ,'' )
176189for index ,test in enumerate (tests ['tests' ]):
177190xhtml = test .get ("xhtml" ,test ["expected" ])
178191if test_name == 'optionaltags' :
179192xhtml = None
180- yield make_test ,test ["input" ],test ["expected" ],xhtml ,test .get ("options" , {})
193+ yield runSerializerTest ,test ["input" ],test ["expected" ],xhtml ,test .get ("options" , {})