Skip to content

Commit

Permalink
Run tests on both DOMlite and etree trees. At the moment further tree…
Browse files Browse the repository at this point in the history
… imps have to be added by hand

--HG--
extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40400
  • Loading branch information
jgraham committed Jan 2, 2007
1 parent 9f9b8d0 commit 2566f09
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
15 changes: 12 additions & 3 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def parse():
except IndexError:
print "No filename provided. Use -h for help"
sys.exit(1)
if hasattr(opts, "treebuilder"):
if opts.treebuilder is not None:
try:
#This isn't a great way to do this
exec("import treebuilders.%s")%opts.treebuilder.split(".")[0]
Expand All @@ -42,7 +42,9 @@ def parse():
except:
treebuilder = treebuilders.DOMlite.TreeBuilder
else:
treebuilder = treebuilders.DOMlite.TreeBuilder
import treebuilders.DOMlite
treebuilder = treebuilders.DOMlite

p = parser.HTMLParser(tree=treebuilder)

if opts.profile:
Expand All @@ -62,12 +64,16 @@ def parse():
document = p.parse(f)
t1 = time.time()
print p.tree.testSerializer(document)
if opts.error:
print "\nParse errors:\n" + "\n".join(p.errors)
t2 = time.time()
print "\n\nRun took: %fs (plus %fs to print the output)"%(t1-t0, t2-t1)
else:
document = p.parse(f)
print document
print p.tree.testSerializer(document)
print "\nParse errors:\n" + "\n".join(p.errors)
if opts.error:
print "\nParse errors:\n" + "\n".join(p.errors)

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

parser.add_option("-e", "--error", action="store_true", default=False,
dest="error", help="Print a list of parse errors")

return parser

if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions src/treebuilders/DOMlite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import base
import _base

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

class TreeBuilder(base.TreeBuilder):
class TreeBuilder(_base.TreeBuilder):
documentClass = Document
doctypeClass = DocumentType
elementClass = Element
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/treebuilders/etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
except ImportError:
from elementtree import ElementTree

import base
import _base

class Element(object):
def __init__(self, name):
Expand Down Expand Up @@ -146,7 +146,7 @@ def serializeElement(element, indent=0):

return "\n".join(rv)

class TreeBuilder(base.TreeBuilder):
class TreeBuilder(_base.TreeBuilder):
documentClass = Document
doctypeClass = DocumentType
elementClass = Element
Expand All @@ -164,4 +164,4 @@ def reparentChildren(self, oldParent, newParent):
else:
newParent._element.text += oldParent._element.text
oldParent._element.text = ""
base.TreeBuilder.reparentChildren(self, oldParent, newParent)
_base.TreeBuilder.reparentChildren(self, oldParent, newParent)
43 changes: 23 additions & 20 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
sys.path.insert(0, os.path.abspath(os.path.join(os.pardir, "src")))

import parser
import treebuilders
#Run tests over all treebuilders
#XXX - it would be nice to automate finding all treebuilders or to allow running just one
from treebuilders import DOMlite, etree

treetypes = {"DOMlite":DOMlite.TreeBuilder,
"ElementTree":etree.TreeBuilder}

def parseTestcase(testString):
testString = testString.split("\n")
Expand Down Expand Up @@ -52,39 +57,40 @@ def convertTreeDump(treedump):
return "\n".join(rv)

class TestCase(unittest.TestCase):
def runParserTest(self, input, output, errors):
def runParserTest(self, input, output, errors, treeClass):
#XXX - move this out into the setup function