10
10
sys .path .insert (0 , os .path .abspath (os .path .join (os .pardir , "src" )))
11
11
12
12
import parser
13
- import treebuilders
13
+ #Run tests over all treebuilders
14
+ #XXX - it would be nice to automate finding all treebuilders or to allow running just one
15
+ from treebuilders import DOMlite , etree
16
+
17
+ treetypes = {"DOMlite" :DOMlite .TreeBuilder ,
18
+ "ElementTree" :etree .TreeBuilder }
14
19
15
20
def parseTestcase (testString ):
16
21
testString = testString .split ("\n " )
@@ -52,39 +57,40 @@ def convertTreeDump(treedump):
52
57
return "\n " .join (rv )
53
58
54
59
class TestCase (unittest .TestCase ):
55
- def runParserTest (self , input , output , errors ):
60
+ def runParserTest (self , input , output , errors , treeClass ):
56
61
#XXX - move this out into the setup function
57
62
#concatenate all consecutive character tokens into a single token
58
- p = parser .HTMLParser ()
63
+ p = parser .HTMLParser (tree = treeClass )
59
64
document = p .parse (StringIO .StringIO (input ))
60
65
errorMsg = "\n " .join (["\n \n Expected:" , output , "\n Recieved:" ,
61
- convertTreeDump (p .tree .testSerializer (document ))])
62
- self .assertEquals (output ,
66
+ convertTreeDump (p .tree .testSerializer (document ))])
67
+ self .assertEquals (output ,
63
68
convertTreeDump (p .tree .testSerializer (document )),
64
69
errorMsg )
65
70
# errorMsg2 = "\n".join(["\n\nInput errors:\n" + "\n".join(errors),
66
71
# "Actual errors:\n" + "\n".join(p.errors)])
67
72
# self.assertEquals(len(p.errors), len(errors), errorMsg2)
68
73
69
74
def test_parser ():
70
- for filename in glob .glob ('tree-construction/*.dat' ):
71
- f = open (filename )
72
- tests = f .read ().split ("#data\n " )
73
- for test in tests :
74
- if test == "" :
75
- continue
76
- test = "#data\n " + test
77
- input , output , errors = parseTestcase (test )
78
- yield TestCase .runParserTest , input , output , errors
75
+ for name , cls in treetypes .iteritems ():
76
+ for filename in glob .glob ('tree-construction/*.dat' ):
77
+ f = open (filename )
78
+ tests = f .read ().split ("#data\n " )
79
+ for test in tests :
80
+ if test == "" :
81
+ continue
82
+ test = "#data\n " + test
83
+ input , output , errors = parseTestcase (test )
84
+ yield TestCase .runParserTest , input , output , errors , name , cls
79
85
80
86
def buildTestSuite ():
81
87
tests = 0
82
- for func , input , output , errors in test_parser ():
88
+ for func , input , output , errors , treeName , treeCls in test_parser ():
83
89
tests += 1
84
90
testName = 'test%d' % tests
85
91
testFunc = lambda self , method = func , input = input , output = output , \
86
- errors = errors : method (self , input , output , errors )
87
- testFunc .__doc__ = 'Parser %s: %s' % (testName , input )
92
+ errors = errors , treeCls = treeCls : method (self , input , output , errors , treeCls )
93
+ testFunc .__doc__ = 'Parser %s Tree %s Input : %s' % (testName , treeName , input )
88
94
instanceMethod = new .instancemethod (testFunc , None , TestCase )
89
95
setattr (TestCase , testName , instanceMethod )
90
96
return unittest .TestLoader ().loadTestsFromTestCase (TestCase )
@@ -94,7 +100,4 @@ def main():
94
100
unittest .main ()
95
101
96
102
if __name__ == "__main__" :
97
- # XXX Allow us to import the sibling module
98
- os .chdir (os .path .split (os .path .abspath (__file__ ))[0 ])
99
- sys .path .insert (0 , os .path .abspath (os .path .join (os .pardir , "src" )))
100
103
main ()
0 commit comments