# HG changeset patch # User Paul Boddie # Date 1298829307 -3600 # Node ID 15a02d623ce7662269424ef6a6e94672642b2362 # Parent a9f6c8cd83408c2d2537c76174bfa3cc438172c8 Tidied up the tests, moving libxml2macro-related tests into their own directory. diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/begat.py --- a/tests/begat.py Sun Feb 27 18:32:58 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,83 +0,0 @@ -#!/usr/bin/env python - -""" -The controversial "begat" benchmark. This module must be compiled using -libxml2macro.py before use, and must then be invoked directly as a compiled -module - ie. as begat.pyc. -""" - -import libxml2macro as n_ - -def test_begat_libxml2macro(n_doc, full_xpath): - l = [] - - if full_xpath: - for n_node in n_doc.xpath("//v[contains(text(), 'begat')]"): - text = n_node.nodeValue - l.append(text) - else: - # NOTE: Code corresponding to this was suggested for cElementTree, but why not - # NOTE: take full advantage of XPath if you have most of the code written in C? - - for n_node in n_doc.xpath("//v"): - text = n_node.nodeValue - if text.find(u'begat') != -1: - l.append(text) - - return l - -def test_begat_libxml2dom(doc, full_xpath): - l = [] - - if full_xpath: - for node in doc.xpath("//v[contains(text(), 'begat')]"): - text = node.nodeValue - l.append(text) - else: - # NOTE: Code corresponding to this was suggested for cElementTree, but why not - # NOTE: take full advantage of XPath if you have most of the code written in C? - - for node in doc.xpath("//v"): - text = node.nodeValue - if text.find(u'begat') != -1: - l.append(text) - - return l - -if __name__ == "__main__": - import time, os - import sys - - ot_locations = [arg for arg in sys.argv if arg.endswith("ot.xml")] - full_xpath = "--full" in sys.argv - use_libxml2dom = "libxml2dom" in sys.argv - use_libxml2macro = "libxml2macro" in sys.argv - iterations = [int(arg.split("-")[0]) for arg in sys.argv if arg.endswith("-times")] - - if len(ot_locations) == 0: - print "Please specify the location of the ot.xml file." - sys.exit(1) - - if len(iterations) == 0: - iterations = 1 - else: - iterations = iterations[0] - - raw_input("Start your engines with ps -p %s -fv" % os.getpid()) - t = time.time() - - for i in range(0, iterations): - if use_libxml2macro: - n_doc = parseFile(ot_locations[0]) - l = test_begat_libxml2macro(n_doc, full_xpath) - else: # use_libxml2dom: - import libxml2dom - doc = libxml2dom.parse(ot_locations[0]) - l = test_begat_libxml2dom(doc, full_xpath) - - print "Time taken", time.time() - t - raw_input("Stop your engines!") - - print l - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/importnode.py --- a/tests/importnode.py Sun Feb 27 18:32:58 2011 +0100 +++ b/tests/importnode.py Sun Feb 27 18:55:07 2011 +0100 @@ -3,21 +3,19 @@ import libxml2dom import sys -d = libxml2dom.parse(sys.argv[1]) -root = d.xpath("*[1]")[0] +test = """ + + text + +""" + +d = libxml2dom.parseString(test) +root = d.documentElement d2 = libxml2dom.createDocument(None, "new", None) -root2 = d2.xpath("*[1]")[0] +root2 = d2.documentElement for i in range(0, 10): imported = d2.importNode(root, 1) root2.appendChild(imported) -libxml2dom.toStream(d2, sys.stdout) -#del root2 -_d2 = d2.as_native_node() -#del d2 -_d2.freeDoc() -#del root -_d = d.as_native_node() -#del d -_d.freeDoc() +d2.toStream(sys.stdout, prettyprint=1) # vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/libxml2macro/begat.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libxml2macro/begat.py Sun Feb 27 18:55:07 2011 +0100 @@ -0,0 +1,83 @@ +#!/usr/bin/env python + +""" +The controversial "begat" benchmark. This module must be compiled using +libxml2macro.py before use, and must then be invoked directly as a compiled +module - ie. as begat.pyc. +""" + +import libxml2macro as n_ + +def test_begat_libxml2macro(n_doc, full_xpath): + l = [] + + if full_xpath: + for n_node in n_doc.xpath("//v[contains(text(), 'begat')]"): + text = n_node.nodeValue + l.append(text) + else: + # NOTE: Code corresponding to this was suggested for cElementTree, but why not + # NOTE: take full advantage of XPath if you have most of the code written in C? + + for n_node in n_doc.xpath("//v"): + text = n_node.nodeValue + if text.find(u'begat') != -1: + l.append(text) + + return l + +def test_begat_libxml2dom(doc, full_xpath): + l = [] + + if full_xpath: + for node in doc.xpath("//v[contains(text(), 'begat')]"): + text = node.nodeValue + l.append(text) + else: + # NOTE: Code corresponding to this was suggested for cElementTree, but why not + # NOTE: take full advantage of XPath if you have most of the code written in C? + + for node in doc.xpath("//v"): + text = node.nodeValue + if text.find(u'begat') != -1: + l.append(text) + + return l + +if __name__ == "__main__": + import time, os + import sys + + ot_locations = [arg for arg in sys.argv if arg.endswith("ot.xml")] + full_xpath = "--full" in sys.argv + use_libxml2dom = "libxml2dom" in sys.argv + use_libxml2macro = "libxml2macro" in sys.argv + iterations = [int(arg.split("-")[0]) for arg in sys.argv if arg.endswith("-times")] + + if len(ot_locations) == 0: + print "Please specify the location of the ot.xml file." + sys.exit(1) + + if len(iterations) == 0: + iterations = 1 + else: + iterations = iterations[0] + + raw_input("Start your engines with ps -p %s -fv" % os.getpid()) + t = time.time() + + for i in range(0, iterations): + if use_libxml2macro: + n_doc = parseFile(ot_locations[0]) + l = test_begat_libxml2macro(n_doc, full_xpath) + else: # use_libxml2dom: + import libxml2dom + doc = libxml2dom.parse(ot_locations[0]) + l = test_begat_libxml2dom(doc, full_xpath) + + print "Time taken", time.time() - t + raw_input("Stop your engines!") + + print l + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/libxml2macro/macrotest.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libxml2macro/macrotest.py Sun Feb 27 18:55:07 2011 +0100 @@ -0,0 +1,116 @@ +#!/usr/bin/env python + +""" +A test of macros. This file must be compiled using libxml2macro.py. It may then +be imported normally in Python, but if run then the compiled module should be +invoked directly - ie. as macrotest.pyc. +""" + +import libxml2macro as x2_ +import xml.dom + +class Container: + pass + +doc = """ + + + + + +""" + +def find_root(x2_d): + x2_root = None + + # Property access should be transformed. + + for x2_n in x2_d.childNodes: + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE: + x2_root = x2_n + break + + return x2_root + +def test(): + global doc + + # Assignment should not be transformed. + + x2_d = parseString(doc) + return process(x2_d) + +def test_file(filename): + + # Assignment should not be transformed. + + x2_d = parseFile(filename) + return process(x2_d) + +def process(x2_d): + + # Not even within containers, and not special names alone. + + c = Container() + c.x2_d = x2_d + + # Find the root element. + + x2_root = find_root(x2_d) + c.x2_root = x2_root + + # Create new attributes. + # Method access should be transformed. + + x2_root.setAttributeNS("ns", "xxx:yyy", "zzz") + c.x2_root.setAttributeNS("ns", "XXX:YYY", "ZZZ") + + # Create new elements. + # Method access should be transformed. + + x2_new = x2_d.createElementNS("ns2", "ppp:qqq") + x2_root.appendChild(x2_new) + x2_new2 = c.x2_d.createElementNS("ns2", "PPP:QQQ") + c.x2_root.appendChild(x2_new2) + + # Create new elements using ownerDocument. + # Chaining properties is not + + x2_new3 = x2_new.ownerDocument.createElement("fff") + x2_new.appendChild(x2_new3) + x2_new4 = x2_new2.ownerDocument.createElement("FFF") + x2_new2.appendChild(x2_new4) + + return x2_d + +def test_import(x2_d): + + # Change the prefix. + + import libxml2macro as node_ + node_d = x2_d + + node_root = find_root(node_d) + + # Create a new document. + + node_d2 = createDocument("nsD", "newdoc", None) + node_root2 = find_root(node_d2) + + # Attempt to import nodes from the original document. + + node_imported = node_d2.importNode(node_root, 1) + node_d2.replaceChild(node_imported, node_root2) + + return node_d, node_d2 + +if __name__ == "__main__": + import sys + if len(sys.argv) > 1: + print "Running a simple test on", sys.argv[1] + test_file(sys.argv[1]) + else: + print "Running a simple test on some built-in string." + test() + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/libxml2macro/nodes.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libxml2macro/nodes.py Sun Feb 27 18:55:07 2011 +0100 @@ -0,0 +1,102 @@ +#!/usr/bin/env python + +"Test of elements and attribute interfaces." + +import libxml2macro as n_ + +try: + do_not_have_libxml2macro = n_ + import libxml2dom + use_libxml2dom = 1 +except NameError: + use_libxml2dom = 0 + +if __name__ == "__main__": + import sys + + s = "" + + if use_libxml2dom: + n_d = libxml2dom.parseString(s) + else: + n_d = parseString(s) + + n_e = n_d.xpath("*")[0] + assert n_e.parentNode == n_d + assert n_e.namespaceURI == "http://ddd" + assert n_e.nodeName == n_e.tagName == "ddd:doc" + assert n_e.localName == "doc" + print n_d.toString() + + n_e.setAttributeNS("http://xxx", "xxx:x", "y") + assert n_e.getAttributeNS("http://xxx", "x") == "y" + l = n_e.xpath("@*") + assert len(l) == 1 + n_a = l[0] + assert n_a.parentNode == n_e + assert n_a.namespaceURI == "http://xxx" + assert n_a.nodeName == "xxx:x" + assert n_a.localName == "x" + assert n_a.nodeValue == n_a.value == "y" + print n_d.toString() + + n_a2 = n_d.createAttributeNS("http://aaa", "aaa:a") + n_a2.nodeValue = "b" + assert n_a2.namespaceURI == "http://aaa" + assert n_a2.nodeName == "aaa:a" + assert n_a2.localName == "a" + assert n_a2.nodeValue == n_a2.value == "b" + print n_d.toString() + + n_e.setAttributeNodeNS(n_a2) + l2 = n_e.xpath("@*") + assert len(l2) == 2 + print n_d.toString() + + n_e.setAttributeNS("http://ccc", "ccc:c", "d") + assert n_e.getAttributeNS("http://ccc", "c") == "d" + l3 = n_e.xpath("@*") + assert len(l3) == 3 + n_e.setAttribute("p", "q") + assert n_e.getAttribute("p") == "q" + l4 = n_e.xpath("@*") + assert len(l4) == 4 + al = n_e.attributes + assert len(al.items()) == 4 + + if use_libxml2dom: + n_a3 = al.getNamedItemNS("http://ccc", "c") + assert n_a3.namespaceURI == "http://ccc" + assert n_a3.nodeName == "ccc:c" + assert n_a3.localName == "c" + assert n_a3.nodeValue == "d" + print n_d.toString() + + n_a4 = n_e.createAttribute("m") + n_a4.nodeValue = "n" + assert n_a4.namespaceURI == None + assert n_a4.nodeName == "m" + assert n_a4.localName == "m" + assert n_a4.nodeValue == n_a4.value == "n" + print n_d.toString() + + if use_libxml2dom: + n_a4_old = al.setNamedItem(n_a4) + assert n_a4_old == None + assert len(al.items()) == 5 + assert n_e.getAttribute("m") == "n" + al.removeNamedItem("m") + assert not n_e.hasAttribute("m") + assert len(al.items()) == 4 + + n_e.removeAttributeNS("http://ccc", "c") + assert not n_e.hasAttributeNS("http://ccc", "c") + l5 = n_e.xpath("@*") + assert len(l5) == 3 + n_e.removeAttribute("p") + assert not n_e.hasAttribute("p") + l6 = n_e.xpath("@*") + assert len(l6) == 2 + print n_d.toString() + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/libxml2macro/performance.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libxml2macro/performance.py Sun Feb 27 18:55:07 2011 +0100 @@ -0,0 +1,84 @@ +#!/usr/bin/env python + +""" +A performance test using libxml2dom.macrolib, libxml2dom and minidom. +This module must be compiled using libxml2macro.py and run only as a compiled +module - ie. as performance.pyc. +""" + +import xml.dom + +def find_root(d): + root = None + for n in d.childNodes: + if n.nodeType == xml.dom.Node.ELEMENT_NODE: + root = n + break + return root + +import libxml2macro as x2_ + +def find_root_libxml2macro(x2_d): + x2_root = None + for x2_n in x2_d.childNodes: + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE: + x2_root = x2_n + break + return x2_root + +def test_import_libxml2macro(x2_d): + x2_d2 = createDocument("nsD", "newdoc", None) + x2_imported = x2_d.importNode(find_root_libxml2macro(x2_d), 1) + x2_d2.replaceChild(x2_imported, find_root_libxml2macro(x2_d2)) + return x2_d, x2_d2 + +def test_import_minidom(d): + d2 = xml.dom.minidom.getDOMImplementation().createDocument("nsD", "newdoc", None) + imported = d2.importNode(find_root(d), 1) + d2.replaceChild(imported, find_root(d2)) + return d, d2 + +def test_import_libxml2dom(d): + d2 = libxml2dom.createDocument("nsD", "newdoc", None) + imported = d2.importNode(find_root(d), 1) + d2.replaceChild(imported, find_root(d2)) + return d, d2 + +if __name__ == "__main__": + import sys + import time + + if len(sys.argv) < 3: + print "Please specify a filename (of a fairly large XML document) and the testing mode." + print "There are quite a few large files in the libxml2 distribution." + print "For the testing mode, choose one of libxml2macro, minidom, libxml2dom." + sys.exit(1) + + if sys.argv[2] == "libxml2macro": + + x2_d = parseFile(sys.argv[1]) + + t = time.time() + x2_d1, x2_d2 = test_import_libxml2macro(x2_d) + toFile(x2_d2, "/tmp/xxx_libxml2macro.xml") + print "Time", time.time() - t, "seconds" + + elif sys.argv[2] == "minidom": + import xml.dom.minidom + d = xml.dom.minidom.parse(sys.argv[1]) + + t = time.time() + d1, d2 = test_import_minidom(d) + open("/tmp/xxx_minidom.xml", "wb").write(d2.toxml("utf-8")) + print "Time", time.time() - t, "seconds" + + elif sys.argv[2] == "libxml2dom": + import libxml2dom + d = libxml2dom.parse(sys.argv[1]) + + t = time.time() + d1, d2 = test_import_libxml2dom(d) + libxml2dom.toStream(d2, open("/tmp/xxx_libxml2dom.xml", "wb")) + print "Time", time.time() - t, "seconds" + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/macrotest.py --- a/tests/macrotest.py Sun Feb 27 18:32:58 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,116 +0,0 @@ -#!/usr/bin/env python - -""" -A test of macros. This file must be compiled using libxml2macro.py. It may then -be imported normally in Python, but if run then the compiled module should be -invoked directly - ie. as macrotest.pyc. -""" - -import libxml2macro as x2_ -import xml.dom - -class Container: - pass - -doc = """ - - - - - -""" - -def find_root(x2_d): - x2_root = None - - # Property access should be transformed. - - for x2_n in x2_d.childNodes: - if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE: - x2_root = x2_n - break - - return x2_root - -def test(): - global doc - - # Assignment should not be transformed. - - x2_d = parseString(doc) - return process(x2_d) - -def test_file(filename): - - # Assignment should not be transformed. - - x2_d = parseFile(filename) - return process(x2_d) - -def process(x2_d): - - # Not even within containers, and not special names alone. - - c = Container() - c.x2_d = x2_d - - # Find the root element. - - x2_root = find_root(x2_d) - c.x2_root = x2_root - - # Create new attributes. - # Method access should be transformed. - - x2_root.setAttributeNS("ns", "xxx:yyy", "zzz") - c.x2_root.setAttributeNS("ns", "XXX:YYY", "ZZZ") - - # Create new elements. - # Method access should be transformed. - - x2_new = x2_d.createElementNS("ns2", "ppp:qqq") - x2_root.appendChild(x2_new) - x2_new2 = c.x2_d.createElementNS("ns2", "PPP:QQQ") - c.x2_root.appendChild(x2_new2) - - # Create new elements using ownerDocument. - # Chaining properties is not - - x2_new3 = x2_new.ownerDocument.createElement("fff") - x2_new.appendChild(x2_new3) - x2_new4 = x2_new2.ownerDocument.createElement("FFF") - x2_new2.appendChild(x2_new4) - - return x2_d - -def test_import(x2_d): - - # Change the prefix. - - import libxml2macro as node_ - node_d = x2_d - - node_root = find_root(node_d) - - # Create a new document. - - node_d2 = createDocument("nsD", "newdoc", None) - node_root2 = find_root(node_d2) - - # Attempt to import nodes from the original document. - - node_imported = node_d2.importNode(node_root, 1) - node_d2.replaceChild(node_imported, node_root2) - - return node_d, node_d2 - -if __name__ == "__main__": - import sys - if len(sys.argv) > 1: - print "Running a simple test on", sys.argv[1] - test_file(sys.argv[1]) - else: - print "Running a simple test on some built-in string." - test() - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/namespaces2.py --- a/tests/namespaces2.py Sun Feb 27 18:32:58 2011 +0100 +++ b/tests/namespaces2.py Sun Feb 27 18:55:07 2011 +0100 @@ -2,6 +2,9 @@ import libxml2dom, xml.dom, xml.dom.minidom +print "Creating libxml2dom document..." +print + document = libxml2dom.createDocument(None, "doc", None) top = document.xpath("*")[0] elem1 = document.createElementNS("DAV:", "myns:href") @@ -10,6 +13,9 @@ document.replaceChild(elem1, top) print document.toString() +print "Creating minidom document..." +print + document2 = xml.dom.minidom.Document() elem1 = document2.createElementNS("DAV:", "myns:href") elem1.setAttributeNS(xml.dom.XMLNS_NAMESPACE, "xmlns:myns", "DAV:") diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/namespaces3.py --- a/tests/namespaces3.py Sun Feb 27 18:32:58 2011 +0100 +++ b/tests/namespaces3.py Sun Feb 27 18:55:07 2011 +0100 @@ -16,11 +16,17 @@ e3.setAttributeNS("DAV:", "fgh", "ijk") e3.setAttributeNS(None, "nop", "wxy") +print "Creating libxml2dom document..." +print + document = libxml2dom.createDocument(None, "doc", None) e = document.xpath("*")[0] test(document, e) print document.toString(prettyprint=1) +print "Creating minidom document..." +print + document = xml.dom.minidom.getDOMImplementation().createDocument(None, "doc", None) e = document.documentElement test(document, e) @@ -34,6 +40,10 @@ try: import pxdom + + print "Creating pxdom document..." + print + document = pxdom.getDOMImplementation("").createDocument(None, "doc", None) e = document.documentElement test(document, e) diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/nodes.py --- a/tests/nodes.py Sun Feb 27 18:32:58 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,102 +0,0 @@ -#!/usr/bin/env python - -"Test of elements and attribute interfaces." - -import libxml2macro as n_ - -try: - do_not_have_libxml2macro = n_ - import libxml2dom - use_libxml2dom = 1 -except NameError: - use_libxml2dom = 0 - -if __name__ == "__main__": - import sys - - s = "" - - if use_libxml2dom: - n_d = libxml2dom.parseString(s) - else: - n_d = parseString(s) - - n_e = n_d.xpath("*")[0] - assert n_e.parentNode == n_d - assert n_e.namespaceURI == "http://ddd" - assert n_e.nodeName == n_e.tagName == "ddd:doc" - assert n_e.localName == "doc" - print n_d.toString() - - n_e.setAttributeNS("http://xxx", "xxx:x", "y") - assert n_e.getAttributeNS("http://xxx", "x") == "y" - l = n_e.xpath("@*") - assert len(l) == 1 - n_a = l[0] - assert n_a.parentNode == n_e - assert n_a.namespaceURI == "http://xxx" - assert n_a.nodeName == "xxx:x" - assert n_a.localName == "x" - assert n_a.nodeValue == n_a.value == "y" - print n_d.toString() - - n_a2 = n_d.createAttributeNS("http://aaa", "aaa:a") - n_a2.nodeValue = "b" - assert n_a2.namespaceURI == "http://aaa" - assert n_a2.nodeName == "aaa:a" - assert n_a2.localName == "a" - assert n_a2.nodeValue == n_a2.value == "b" - print n_d.toString() - - n_e.setAttributeNodeNS(n_a2) - l2 = n_e.xpath("@*") - assert len(l2) == 2 - print n_d.toString() - - n_e.setAttributeNS("http://ccc", "ccc:c", "d") - assert n_e.getAttributeNS("http://ccc", "c") == "d" - l3 = n_e.xpath("@*") - assert len(l3) == 3 - n_e.setAttribute("p", "q") - assert n_e.getAttribute("p") == "q" - l4 = n_e.xpath("@*") - assert len(l4) == 4 - al = n_e.attributes - assert len(al.items()) == 4 - - if use_libxml2dom: - n_a3 = al.getNamedItemNS("http://ccc", "c") - assert n_a3.namespaceURI == "http://ccc" - assert n_a3.nodeName == "ccc:c" - assert n_a3.localName == "c" - assert n_a3.nodeValue == "d" - print n_d.toString() - - n_a4 = n_e.createAttribute("m") - n_a4.nodeValue = "n" - assert n_a4.namespaceURI == None - assert n_a4.nodeName == "m" - assert n_a4.localName == "m" - assert n_a4.nodeValue == n_a4.value == "n" - print n_d.toString() - - if use_libxml2dom: - n_a4_old = al.setNamedItem(n_a4) - assert n_a4_old == None - assert len(al.items()) == 5 - assert n_e.getAttribute("m") == "n" - al.removeNamedItem("m") - assert not n_e.hasAttribute("m") - assert len(al.items()) == 4 - - n_e.removeAttributeNS("http://ccc", "c") - assert not n_e.hasAttributeNS("http://ccc", "c") - l5 = n_e.xpath("@*") - assert len(l5) == 3 - n_e.removeAttribute("p") - assert not n_e.hasAttribute("p") - l6 = n_e.xpath("@*") - assert len(l6) == 2 - print n_d.toString() - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/performance.py --- a/tests/performance.py Sun Feb 27 18:32:58 2011 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,84 +0,0 @@ -#!/usr/bin/env python - -""" -A performance test using libxml2dom.macrolib, libxml2dom and minidom. -This module must be compiled using libxml2macro.py and run only as a compiled -module - ie. as performance.pyc. -""" - -import xml.dom - -def find_root(d): - root = None - for n in d.childNodes: - if n.nodeType == xml.dom.Node.ELEMENT_NODE: - root = n - break - return root - -import libxml2macro as x2_ - -def find_root_libxml2macro(x2_d): - x2_root = None - for x2_n in x2_d.childNodes: - if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE: - x2_root = x2_n - break - return x2_root - -def test_import_libxml2macro(x2_d): - x2_d2 = createDocument("nsD", "newdoc", None) - x2_imported = x2_d.importNode(find_root_libxml2macro(x2_d), 1) - x2_d2.replaceChild(x2_imported, find_root_libxml2macro(x2_d2)) - return x2_d, x2_d2 - -def test_import_minidom(d): - d2 = xml.dom.minidom.getDOMImplementation().createDocument("nsD", "newdoc", None) - imported = d2.importNode(find_root(d), 1) - d2.replaceChild(imported, find_root(d2)) - return d, d2 - -def test_import_libxml2dom(d): - d2 = libxml2dom.createDocument("nsD", "newdoc", None) - imported = d2.importNode(find_root(d), 1) - d2.replaceChild(imported, find_root(d2)) - return d, d2 - -if __name__ == "__main__": - import sys - import time - - if len(sys.argv) < 3: - print "Please specify a filename (of a fairly large XML document) and the testing mode." - print "There are quite a few large files in the libxml2 distribution." - print "For the testing mode, choose one of libxml2macro, minidom, libxml2dom." - sys.exit(1) - - if sys.argv[2] == "libxml2macro": - - x2_d = parseFile(sys.argv[1]) - - t = time.time() - x2_d1, x2_d2 = test_import_libxml2macro(x2_d) - toFile(x2_d2, "/tmp/xxx_libxml2macro.xml") - print "Time", time.time() - t, "seconds" - - elif sys.argv[2] == "minidom": - import xml.dom.minidom - d = xml.dom.minidom.parse(sys.argv[1]) - - t = time.time() - d1, d2 = test_import_minidom(d) - open("/tmp/xxx_minidom.xml", "wb").write(d2.toxml("utf-8")) - print "Time", time.time() - t, "seconds" - - elif sys.argv[2] == "libxml2dom": - import libxml2dom - d = libxml2dom.parse(sys.argv[1]) - - t = time.time() - d1, d2 = test_import_libxml2dom(d) - libxml2dom.toStream(d2, open("/tmp/xxx_libxml2dom.xml", "wb")) - print "Time", time.time() - t, "seconds" - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/prettyprint.py --- a/tests/prettyprint.py Sun Feb 27 18:32:58 2011 +0100 +++ b/tests/prettyprint.py Sun Feb 27 18:55:07 2011 +0100 @@ -1,10 +1,24 @@ #!/usr/bin/env python import libxml2dom -from xml.dom.ext import PrettyPrint import sys d = libxml2dom.parse(sys.argv[1]) -PrettyPrint(d) + +print "Prettyprint using libxml2dom..." +print + +d.toStream(sys.stdout, prettyprint=1) + +try: + from xml.dom.ext import PrettyPrint + + print "Prettyprint using xml.dom..." + print + + PrettyPrint(d) + +except ImportError: + print "xml.dom not tested." # vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/test_valid_relaxng.py --- a/tests/test_valid_relaxng.py Sun Feb 27 18:32:58 2011 +0100 +++ b/tests/test_valid_relaxng.py Sun Feb 27 18:55:07 2011 +0100 @@ -4,14 +4,18 @@ schema = libxml2dom.parse("tests/test_valid_relaxng.xml") d = libxml2dom.parse("tests/test_valid.xml") +print "Test of valid document..." print d.validate(schema) print d.validateDocument(schema) print d.getParameter("error-handler") +print schema = libxml2dom.parse("tests/test_invalid_relaxng.xml") d = libxml2dom.parse("tests/test_invalid.xml") +print "Test of invalid document..." print d.validate(schema) print d.validateDocument(schema) print d.getParameter("error-handler") +print # vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/test_valid_schema.py --- a/tests/test_valid_schema.py Sun Feb 27 18:32:58 2011 +0100 +++ b/tests/test_valid_schema.py Sun Feb 27 18:55:07 2011 +0100 @@ -4,14 +4,18 @@ schema = libxml2dom.parse("tests/test_valid_schema.xml") d = libxml2dom.parse("tests/test_valid.xml") +print "Test of valid document..." print d.validate(schema) print d.validateDocument(schema) print d.getParameter("error-handler") +print schema = libxml2dom.parse("tests/test_invalid_schema.xml") d = libxml2dom.parse("tests/test_invalid.xml") +print "Test of invalid document..." print d.validate(schema) print d.validateDocument(schema) print d.getParameter("error-handler") +print # vim: tabstop=4 expandtab shiftwidth=4 diff -r a9f6c8cd8340 -r 15a02d623ce7 tests/test_valid_schematron.py --- a/tests/test_valid_schematron.py Sun Feb 27 18:32:58 2011 +0100 +++ b/tests/test_valid_schematron.py Sun Feb 27 18:55:07 2011 +0100 @@ -4,14 +4,18 @@ schema = libxml2dom.parse("tests/test_valid_schematron.xml") d = libxml2dom.parse("tests/test_valid.xml") +print "Test of valid document..." print d.validate(schema) print d.validateDocument(schema) print d.getParameter("error-handler") +print schema = libxml2dom.parse("tests/test_invalid_schematron.xml") d = libxml2dom.parse("tests/test_invalid.xml") +print "Test of invalid document..." print d.validate(schema) print d.validateDocument(schema) print d.getParameter("error-handler") +print # vim: tabstop=4 expandtab shiftwidth=4