1111
1212import parser
1313
14- def testParser (testString ):
14+ def parseTestcase (testString ):
1515 testString = testString .split ("\n " )
1616 try :
1717 assert testString [0 ] == "#data"
@@ -25,7 +25,7 @@ def testParser(testString):
2525 if line and line [0 ] != "#" :
2626 if currentList is output :
2727 assert line [0 ] == "|"
28- currentList .append (line [1 :])
28+ currentList .append (line [2 :])
2929 else :
3030 currentList .append (line )
3131 elif line == "#errors" :
@@ -34,6 +34,17 @@ def testParser(testString):
3434 currentList = output
3535 return "\n " .join (input ), "\n " .join (output ), errors
3636
37+ def convertTreeDump (treedump ):
38+ """convert the output of str(document) to the format used in the testcases"""
39+ treedump = treedump .split ("\n " )
40+ rv = []
41+ for line in treedump :
42+ if line .startswith ("#document" ):
43+ pass
44+ else :
45+ rv .append (line [3 :])
46+ return "\n " .join (rv )
47+
3748def test_parser ():
3849 for filename in glob .glob ('tree-construction/*.dat' ):
3950 f = file (filename )
@@ -45,36 +56,49 @@ def test_parser():
4556 #Strip out newlinw characters from the end of the string
4657 test .append (line [:- 1 ])
4758 else :
48- input , output , errors = testParser ("\n " .join (test ))
49- yield TestCase . runParserTest , input , output , errors
59+ input , output , errors = parseTestcase ("\n " .join (test ))
60+ yield runParserTest , input , output , errors
5061 test = []
5162 lastLine = line
5263
53- class TestCase (unittest .TestCase ):
54- def runParserTest (self , input , output , errors ):
55- #XXX - move this out into the setup function
56- #concatenate all consecutive character tokens into a single token
57- p = parser .HTMLParser ()
58- document = p .parse (StringIO .StringIO (input ))
59- try :
60- #Need a check on the number of parse errors here
61- print str (document )
62- self .assertTrue (output == str (document ))
63- except AssertionError :
64- raise
64+ def runParserTest (input , output , errors ):
65+ #XXX - move this out into the setup function
66+ #concatenate all consecutive character tokens into a single token
67+ p = parser .HTMLParser ()
68+ document = p .parse (StringIO .StringIO (input ))
69+ try :
70+ #Need a check on the number of parse errors here
71+ assert output == convertTreeDump (document .printTree ())
72+ except AssertionError :
73+ print "input"
74+ print input
75+ print "expected output"
76+ print output
77+ print "recieved"
78+ print convertTreeDump (document .printTree ())
79+ print ""
80+ raise
81+
6582def main ():
6683 failed = 0
6784 tests = 0
6885 for func , input , output , errors in test_parser ():
6986 tests += 1
7087 testName = 'test%d' % tests
71- def testFunc (self , method = func , input = input ,
72- output = output , errors = errors ):
73- method (self , input , output , errors )
74- testFunc .__doc__ = "\t " .join ([str (input )])
75- instanceMethod = new .instancemethod (testFunc , None , TestCase )
76- setattr (TestCase , testName , instanceMethod )
77- unittest .main ()
88+ try :
89+ runParserTest (input , output , errors )
90+ except AssertionError :
91+ failed += 1
92+
93+ print "Ran %i tests, failed %i" % (tests , failed )
94+
95+ # def testFunc(self, method=func, input=input,
96+ # output=output, errors=errors):
97+ # method(self, input, output, errors)
98+ # testFunc.__doc__ = "\t".join([str(input)])
99+ # instanceMethod = new
100+ # setattr(TestCase, testName, instanceMethod)
101+ # unittest.main()
78102
79103if __name__ == "__main__" :
80104 main ()
0 commit comments