Skip to content

Commit 2566f09

Browse files
committed
Run tests on both DOMlite and etree trees. At the moment further tree imps have to be added by hand
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40400
1 parent 9f9b8d0 commit 2566f09

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

parse.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def parse():
3131
except IndexError:
3232
print "No filename provided. Use -h for help"
3333
sys.exit(1)
34-
if hasattr(opts, "treebuilder"):
34+
if opts.treebuilder is not None:
3535
try:
3636
#This isn't a great way to do this
3737
exec("import treebuilders.%s")%opts.treebuilder.split(".")[0]
@@ -42,7 +42,9 @@ def parse():
4242
except:
4343
treebuilder = treebuilders.DOMlite.TreeBuilder
4444
else:
45-
treebuilder = treebuilders.DOMlite.TreeBuilder
45+
import treebuilders.DOMlite
46+
treebuilder = treebuilders.DOMlite
47+
4648
p = parser.HTMLParser(tree=treebuilder)
4749

4850
if opts.profile:
@@ -62,12 +64,16 @@ def parse():
6264
document = p.parse(f)
6365
t1 = time.time()
6466
print p.tree.testSerializer(document)
67+
if opts.error:
68+
print "\nParse errors:\n" + "\n".join(p.errors)
6569
t2 = time.time()
6670
print "\n\nRun took: %fs (plus %fs to print the output)"%(t1-t0, t2-t1)
6771
else:
6872
document = p.parse(f)
73+
print document
6974
print p.tree.testSerializer(document)
70-
print "\nParse errors:\n" + "\n".join(p.errors)
75+
if opts.error:
76+
print "\nParse errors:\n" + "\n".join(p.errors)
7177

7278
def getOptParser():
7379
parser = OptionParser(usage=__doc__)
@@ -83,6 +89,9 @@ def getOptParser():
8389
parser.add_option("-b", "--treebuilder", action="store", type="string",
8490
dest="treebuilder")
8591

92+
parser.add_option("-e", "--error", action="store_true", default=False,
93+
dest="error", help="Print a list of parse errors")
94+
8695
return parser
8796

8897
if __name__ == "__main__":

src/treebuilders/DOMlite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import base
1+
import _base
22

33
# Really crappy basic implementation of a DOM-core like thing
44
class Node(object):
@@ -117,7 +117,7 @@ def __init__(self, data):
117117
def __str__(self):
118118
return "<!-- %s -->" % self.data
119119

120-
class TreeBuilder(base.TreeBuilder):
120+
class TreeBuilder(_base.TreeBuilder):
121121
documentClass = Document
122122
doctypeClass = DocumentType
123123
elementClass = Element
File renamed without changes.

src/treebuilders/etree.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
except ImportError:
44
from elementtree import ElementTree
55

6-
import base
6+
import _base
77

88
class Element(object):
99
def __init__(self, name):
@@ -146,7 +146,7 @@ def serializeElement(element, indent=0):
146146

147147
return "\n".join(rv)
148148

149-
class TreeBuilder(base.TreeBuilder):
149+
class TreeBuilder(_base.TreeBuilder):
150150
documentClass = Document
151151
doctypeClass = DocumentType
152152
elementClass = Element
@@ -164,4 +164,4 @@ def reparentChildren(self, oldParent, newParent):
164164
else:
165165
newParent._element.text += oldParent._element.text
166166
oldParent._element.text = ""
167-
base.TreeBuilder.reparentChildren(self, oldParent, newParent)
167+
_base.TreeBuilder.reparentChildren(self, oldParent, newParent)

tests/test_parser.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
sys.path.insert(0, os.path.abspath(os.path.join(os.pardir, "src")))
1111

1212
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}
1419

1520
def parseTestcase(testString):
1621
testString = testString.split("\n")
@@ -52,39 +57,40 @@ def convertTreeDump(treedump):
5257
return "\n".join(rv)
5358

5459
class TestCase(unittest.TestCase):
55-
def runParserTest(self, input, output, errors):
60+
def runParserTest(self, input, output, errors, treeClass):
5661
#XXX - move this out into the setup function
5762
#concatenate all consecutive character tokens into a single token
58-
p = parser.HTMLParser()
63+
p = parser.HTMLParser(tree = treeClass)
5964
document = p.parse(StringIO.StringIO(input))
6065
errorMsg = "\n".join(["\n\nExpected:", output, "\nRecieved:",
61-
convertTreeDump(p.tree.testSerializer(document))])
62-
self.assertEquals(output,
66+
convertTreeDump(p.tree.testSerializer(document))])
67+
self.assertEquals(output,
6368
convertTreeDump(p.tree.testSerializer(document)),
6469
errorMsg)
6570
# errorMsg2 = "\n".join(["\n\nInput errors:\n" + "\n".join(errors),
6671
# "Actual errors:\n" + "\n".join(p.errors)])
6772
# self.assertEquals(len(p.errors), len(errors), errorMsg2)
6873

6974
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
7985

8086
def buildTestSuite():
8187
tests = 0
82-
for func, input, output, errors in test_parser():
88+
for func, input, output, errors, treeName, treeCls in test_parser():
8389
tests += 1
8490
testName = 'test%d' % tests
8591
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)
8894
instanceMethod = new.instancemethod(testFunc, None, TestCase)
8995
setattr(TestCase, testName, instanceMethod)
9096
return unittest.TestLoader().loadTestsFromTestCase(TestCase)
@@ -94,7 +100,4 @@ def main():
94100
unittest.main()
95101

96102
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")))
100103
main()

0 commit comments

Comments
 (0)