1- import sys
1+ import os
22import unittest
33from support import simplejson , html5lib_test_files
44
1414#from html5lib.treewalkers._base import TreeWalker
1515#END RELEASE
1616
17- #Run the serialize error checks
18- checkSerializeErrors = False
19-
2017class JsonWalker (TreeWalker ):
2118 def __iter__ (self ):
2219 for token in self .tree :
@@ -39,47 +36,50 @@ def __iter__(self):
3936 raise ValueError ("Unknown token type: " + type )
4037
4138class TestCase (unittest .TestCase ):
42- def addTest (cls , name , expected , input , description , options ):
43- func = lambda self : self .mockTest (expected , input , options )
44- func .__doc__ = "\t " .join ([description , str (input ), str (options )])
39+ def addTest (cls , name , description , input , expected , xhtml , options ):
40+ func = lambda self : self .mockTest (input , options , expected , xhtml )
41+ func .__doc__ = "\t " .join ([name , description , str (input ), str (options )])
4542 setattr (cls , name , func )
4643 addTest = classmethod (addTest )
4744
48- def mockTest (self , expected , input , options ):
45+ def mockTest (self , input , options , expected , xhtml ):
4946 result = self .serialize_html (input , options )
5047 if len (expected ) == 1 :
5148 self .assertEquals (expected [0 ], result )
5249 elif result not in expected :
5350 self .fail ("Expected: %s, Received: %s" % (expected , result ))
5451
52+ if not xhtml : return
53+
54+ result = self .serialize_xhtml (input , options )
55+ if len (xhtml ) == 1 :
56+ self .assertEquals (xhtml [0 ], result )
57+ elif result not in xhtml :
58+ self .fail ("Expected: %s, Received: %s" % (xhtml , result ))
59+
5560 def serialize_html (self , input , options ):
5661 options = dict ([(str (k ),v ) for k ,v in options .iteritems ()])
5762 return u'' .join (serializer .HTMLSerializer (** options ).
5863 serialize (JsonWalker (input ),options .get ("encoding" ,None )))
5964
60- def test_serializer ():
61- for filename in html5lib_test_files ('serializer' , '*.test' ):
62- tests = simplejson .load (file (filename ))
63- for test in tests ['tests' ]:
64- yield test
65+ def serialize_xhtml (self , input , options ):
66+ options = dict ([(str (k ),v ) for k ,v in options .iteritems ()])
67+ return u'' .join (serializer .XHTMLSerializer (** options ).
68+ serialize (JsonWalker (input ),options .get ("encoding" ,None )))
6569
6670def buildTestSuite ():
67- tests = 0
68- for test in test_serializer ():
69- tests += 1
70- testName = 'test%d' % tests
71- TestCase .addTest (testName , test ["expected" ], test ["input" ], \
72- test ["description" ], test .get ("options" , {}))
71+ for filename in html5lib_test_files ('serializer' , '*.test' ):
72+ test_name = os .path .basename (filename ).replace ('.test' ,'' )
73+ tests = simplejson .load (file (filename ))
74+ for index , test in enumerate (tests ['tests' ]):
75+ xhtml = test .get ("xhtml" , test ["expected" ])
76+ if test_name == 'optionaltags' : xhtml = None
77+ TestCase .addTest ('test_%s_%d' % (test_name , index + 1 ),
78+ test ["description" ], test ["input" ], test ["expected" ], xhtml ,
79+ test .get ("options" , {}))
7380 return unittest .TestLoader ().loadTestsFromTestCase (TestCase )
7481
7582def main ():
76- # the following is temporary while the unit tests for parse errors are
77- # still in flux
78- if '-p' in sys .argv : # suppress check for serialize errors
79- sys .argv .remove ('-p' )
80- global checkSerializeErrors
81- checkSerializeErrors = False
82-
8383 buildTestSuite ()
8484 unittest .main ()
8585
0 commit comments