1.1 --- a/tests/begat.py Sun Feb 27 18:32:58 2011 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,83 +0,0 @@
1.4 -#!/usr/bin/env python
1.5 -
1.6 -"""
1.7 -The controversial "begat" benchmark. This module must be compiled using
1.8 -libxml2macro.py before use, and must then be invoked directly as a compiled
1.9 -module - ie. as begat.pyc.
1.10 -"""
1.11 -
1.12 -import libxml2macro as n_
1.13 -
1.14 -def test_begat_libxml2macro(n_doc, full_xpath):
1.15 - l = []
1.16 -
1.17 - if full_xpath:
1.18 - for n_node in n_doc.xpath("//v[contains(text(), 'begat')]"):
1.19 - text = n_node.nodeValue
1.20 - l.append(text)
1.21 - else:
1.22 - # NOTE: Code corresponding to this was suggested for cElementTree, but why not
1.23 - # NOTE: take full advantage of XPath if you have most of the code written in C?
1.24 -
1.25 - for n_node in n_doc.xpath("//v"):
1.26 - text = n_node.nodeValue
1.27 - if text.find(u'begat') != -1:
1.28 - l.append(text)
1.29 -
1.30 - return l
1.31 -
1.32 -def test_begat_libxml2dom(doc, full_xpath):
1.33 - l = []
1.34 -
1.35 - if full_xpath:
1.36 - for node in doc.xpath("//v[contains(text(), 'begat')]"):
1.37 - text = node.nodeValue
1.38 - l.append(text)
1.39 - else:
1.40 - # NOTE: Code corresponding to this was suggested for cElementTree, but why not
1.41 - # NOTE: take full advantage of XPath if you have most of the code written in C?
1.42 -
1.43 - for node in doc.xpath("//v"):
1.44 - text = node.nodeValue
1.45 - if text.find(u'begat') != -1:
1.46 - l.append(text)
1.47 -
1.48 - return l
1.49 -
1.50 -if __name__ == "__main__":
1.51 - import time, os
1.52 - import sys
1.53 -
1.54 - ot_locations = [arg for arg in sys.argv if arg.endswith("ot.xml")]
1.55 - full_xpath = "--full" in sys.argv
1.56 - use_libxml2dom = "libxml2dom" in sys.argv
1.57 - use_libxml2macro = "libxml2macro" in sys.argv
1.58 - iterations = [int(arg.split("-")[0]) for arg in sys.argv if arg.endswith("-times")]
1.59 -
1.60 - if len(ot_locations) == 0:
1.61 - print "Please specify the location of the ot.xml file."
1.62 - sys.exit(1)
1.63 -
1.64 - if len(iterations) == 0:
1.65 - iterations = 1
1.66 - else:
1.67 - iterations = iterations[0]
1.68 -
1.69 - raw_input("Start your engines with ps -p %s -fv" % os.getpid())
1.70 - t = time.time()
1.71 -
1.72 - for i in range(0, iterations):
1.73 - if use_libxml2macro:
1.74 - n_doc = parseFile(ot_locations[0])
1.75 - l = test_begat_libxml2macro(n_doc, full_xpath)
1.76 - else: # use_libxml2dom:
1.77 - import libxml2dom
1.78 - doc = libxml2dom.parse(ot_locations[0])
1.79 - l = test_begat_libxml2dom(doc, full_xpath)
1.80 -
1.81 - print "Time taken", time.time() - t
1.82 - raw_input("Stop your engines!")
1.83 -
1.84 - print l
1.85 -
1.86 -# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/tests/importnode.py Sun Feb 27 18:32:58 2011 +0100
2.2 +++ b/tests/importnode.py Sun Feb 27 18:55:07 2011 +0100
2.3 @@ -3,21 +3,19 @@
2.4 import libxml2dom
2.5 import sys
2.6
2.7 -d = libxml2dom.parse(sys.argv[1])
2.8 -root = d.xpath("*[1]")[0]
2.9 +test = """<?xml version="1.0"?>
2.10 +<test>
2.11 + <element attr="value">text</element>
2.12 +</test>
2.13 +"""
2.14 +
2.15 +d = libxml2dom.parseString(test)
2.16 +root = d.documentElement
2.17 d2 = libxml2dom.createDocument(None, "new", None)
2.18 -root2 = d2.xpath("*[1]")[0]
2.19 +root2 = d2.documentElement
2.20 for i in range(0, 10):
2.21 imported = d2.importNode(root, 1)
2.22 root2.appendChild(imported)
2.23 -libxml2dom.toStream(d2, sys.stdout)
2.24 -#del root2
2.25 -_d2 = d2.as_native_node()
2.26 -#del d2
2.27 -_d2.freeDoc()
2.28 -#del root
2.29 -_d = d.as_native_node()
2.30 -#del d
2.31 -_d.freeDoc()
2.32 +d2.toStream(sys.stdout, prettyprint=1)
2.33
2.34 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/tests/libxml2macro/begat.py Sun Feb 27 18:55:07 2011 +0100
3.3 @@ -0,0 +1,83 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"""
3.7 +The controversial "begat" benchmark. This module must be compiled using
3.8 +libxml2macro.py before use, and must then be invoked directly as a compiled
3.9 +module - ie. as begat.pyc.
3.10 +"""
3.11 +
3.12 +import libxml2macro as n_
3.13 +
3.14 +def test_begat_libxml2macro(n_doc, full_xpath):
3.15 + l = []
3.16 +
3.17 + if full_xpath:
3.18 + for n_node in n_doc.xpath("//v[contains(text(), 'begat')]"):
3.19 + text = n_node.nodeValue
3.20 + l.append(text)
3.21 + else:
3.22 + # NOTE: Code corresponding to this was suggested for cElementTree, but why not
3.23 + # NOTE: take full advantage of XPath if you have most of the code written in C?
3.24 +
3.25 + for n_node in n_doc.xpath("//v"):
3.26 + text = n_node.nodeValue
3.27 + if text.find(u'begat') != -1:
3.28 + l.append(text)
3.29 +
3.30 + return l
3.31 +
3.32 +def test_begat_libxml2dom(doc, full_xpath):
3.33 + l = []
3.34 +
3.35 + if full_xpath:
3.36 + for node in doc.xpath("//v[contains(text(), 'begat')]"):
3.37 + text = node.nodeValue
3.38 + l.append(text)
3.39 + else:
3.40 + # NOTE: Code corresponding to this was suggested for cElementTree, but why not
3.41 + # NOTE: take full advantage of XPath if you have most of the code written in C?
3.42 +
3.43 + for node in doc.xpath("//v"):
3.44 + text = node.nodeValue
3.45 + if text.find(u'begat') != -1:
3.46 + l.append(text)
3.47 +
3.48 + return l
3.49 +
3.50 +if __name__ == "__main__":
3.51 + import time, os
3.52 + import sys
3.53 +
3.54 + ot_locations = [arg for arg in sys.argv if arg.endswith("ot.xml")]
3.55 + full_xpath = "--full" in sys.argv
3.56 + use_libxml2dom = "libxml2dom" in sys.argv
3.57 + use_libxml2macro = "libxml2macro" in sys.argv
3.58 + iterations = [int(arg.split("-")[0]) for arg in sys.argv if arg.endswith("-times")]
3.59 +
3.60 + if len(ot_locations) == 0:
3.61 + print "Please specify the location of the ot.xml file."
3.62 + sys.exit(1)
3.63 +
3.64 + if len(iterations) == 0:
3.65 + iterations = 1
3.66 + else:
3.67 + iterations = iterations[0]
3.68 +
3.69 + raw_input("Start your engines with ps -p %s -fv" % os.getpid())
3.70 + t = time.time()
3.71 +
3.72 + for i in range(0, iterations):
3.73 + if use_libxml2macro:
3.74 + n_doc = parseFile(ot_locations[0])
3.75 + l = test_begat_libxml2macro(n_doc, full_xpath)
3.76 + else: # use_libxml2dom:
3.77 + import libxml2dom
3.78 + doc = libxml2dom.parse(ot_locations[0])
3.79 + l = test_begat_libxml2dom(doc, full_xpath)
3.80 +
3.81 + print "Time taken", time.time() - t
3.82 + raw_input("Stop your engines!")
3.83 +
3.84 + print l
3.85 +
3.86 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/libxml2macro/macrotest.py Sun Feb 27 18:55:07 2011 +0100
4.3 @@ -0,0 +1,116 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +"""
4.7 +A test of macros. This file must be compiled using libxml2macro.py. It may then
4.8 +be imported normally in Python, but if run then the compiled module should be
4.9 +invoked directly - ie. as macrotest.pyc.
4.10 +"""
4.11 +
4.12 +import libxml2macro as x2_
4.13 +import xml.dom
4.14 +
4.15 +class Container:
4.16 + pass
4.17 +
4.18 +doc = """<?xml version="1.0"?>
4.19 +<doc>
4.20 + <element attr="value">
4.21 + <subelement/>
4.22 + </element>
4.23 +</doc>
4.24 +"""
4.25 +
4.26 +def find_root(x2_d):
4.27 + x2_root = None
4.28 +
4.29 + # Property access should be transformed.
4.30 +
4.31 + for x2_n in x2_d.childNodes:
4.32 + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
4.33 + x2_root = x2_n
4.34 + break
4.35 +
4.36 + return x2_root
4.37 +
4.38 +def test():
4.39 + global doc
4.40 +
4.41 + # Assignment should not be transformed.
4.42 +
4.43 + x2_d = parseString(doc)
4.44 + return process(x2_d)
4.45 +
4.46 +def test_file(filename):
4.47 +
4.48 + # Assignment should not be transformed.
4.49 +
4.50 + x2_d = parseFile(filename)
4.51 + return process(x2_d)
4.52 +
4.53 +def process(x2_d):
4.54 +
4.55 + # Not even within containers, and not special names alone.
4.56 +
4.57 + c = Container()
4.58 + c.x2_d = x2_d
4.59 +
4.60 + # Find the root element.
4.61 +
4.62 + x2_root = find_root(x2_d)
4.63 + c.x2_root = x2_root
4.64 +
4.65 + # Create new attributes.
4.66 + # Method access should be transformed.
4.67 +
4.68 + x2_root.setAttributeNS("ns", "xxx:yyy", "zzz")
4.69 + c.x2_root.setAttributeNS("ns", "XXX:YYY", "ZZZ")
4.70 +
4.71 + # Create new elements.
4.72 + # Method access should be transformed.
4.73 +
4.74 + x2_new = x2_d.createElementNS("ns2", "ppp:qqq")
4.75 + x2_root.appendChild(x2_new)
4.76 + x2_new2 = c.x2_d.createElementNS("ns2", "PPP:QQQ")
4.77 + c.x2_root.appendChild(x2_new2)
4.78 +
4.79 + # Create new elements using ownerDocument.
4.80 + # Chaining properties is not
4.81 +
4.82 + x2_new3 = x2_new.ownerDocument.createElement("fff")
4.83 + x2_new.appendChild(x2_new3)
4.84 + x2_new4 = x2_new2.ownerDocument.createElement("FFF")
4.85 + x2_new2.appendChild(x2_new4)
4.86 +
4.87 + return x2_d
4.88 +
4.89 +def test_import(x2_d):
4.90 +
4.91 + # Change the prefix.
4.92 +
4.93 + import libxml2macro as node_
4.94 + node_d = x2_d
4.95 +
4.96 + node_root = find_root(node_d)
4.97 +
4.98 + # Create a new document.
4.99 +
4.100 + node_d2 = createDocument("nsD", "newdoc", None)
4.101 + node_root2 = find_root(node_d2)
4.102 +
4.103 + # Attempt to import nodes from the original document.
4.104 +
4.105 + node_imported = node_d2.importNode(node_root, 1)
4.106 + node_d2.replaceChild(node_imported, node_root2)
4.107 +
4.108 + return node_d, node_d2
4.109 +
4.110 +if __name__ == "__main__":
4.111 + import sys
4.112 + if len(sys.argv) > 1:
4.113 + print "Running a simple test on", sys.argv[1]
4.114 + test_file(sys.argv[1])
4.115 + else:
4.116 + print "Running a simple test on some built-in string."
4.117 + test()
4.118 +
4.119 +# vim: tabstop=4 expandtab shiftwidth=4
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/tests/libxml2macro/nodes.py Sun Feb 27 18:55:07 2011 +0100
5.3 @@ -0,0 +1,102 @@
5.4 +#!/usr/bin/env python
5.5 +
5.6 +"Test of elements and attribute interfaces."
5.7 +
5.8 +import libxml2macro as n_
5.9 +
5.10 +try:
5.11 + do_not_have_libxml2macro = n_
5.12 + import libxml2dom
5.13 + use_libxml2dom = 1
5.14 +except NameError:
5.15 + use_libxml2dom = 0
5.16 +
5.17 +if __name__ == "__main__":
5.18 + import sys
5.19 +
5.20 + s = "<ddd:doc xmlns:ddd='http://ddd'/>"
5.21 +
5.22 + if use_libxml2dom:
5.23 + n_d = libxml2dom.parseString(s)
5.24 + else:
5.25 + n_d = parseString(s)
5.26 +
5.27 + n_e = n_d.xpath("*")[0]
5.28 + assert n_e.parentNode == n_d
5.29 + assert n_e.namespaceURI == "http://ddd"
5.30 + assert n_e.nodeName == n_e.tagName == "ddd:doc"
5.31 + assert n_e.localName == "doc"
5.32 + print n_d.toString()
5.33 +
5.34 + n_e.setAttributeNS("http://xxx", "xxx:x", "y")
5.35 + assert n_e.getAttributeNS("http://xxx", "x") == "y"
5.36 + l = n_e.xpath("@*")
5.37 + assert len(l) == 1
5.38 + n_a = l[0]
5.39 + assert n_a.parentNode == n_e
5.40 + assert n_a.namespaceURI == "http://xxx"
5.41 + assert n_a.nodeName == "xxx:x"
5.42 + assert n_a.localName == "x"
5.43 + assert n_a.nodeValue == n_a.value == "y"
5.44 + print n_d.toString()
5.45 +
5.46 + n_a2 = n_d.createAttributeNS("http://aaa", "aaa:a")
5.47 + n_a2.nodeValue = "b"
5.48 + assert n_a2.namespaceURI == "http://aaa"
5.49 + assert n_a2.nodeName == "aaa:a"
5.50 + assert n_a2.localName == "a"
5.51 + assert n_a2.nodeValue == n_a2.value == "b"
5.52 + print n_d.toString()
5.53 +
5.54 + n_e.setAttributeNodeNS(n_a2)
5.55 + l2 = n_e.xpath("@*")
5.56 + assert len(l2) == 2
5.57 + print n_d.toString()
5.58 +
5.59 + n_e.setAttributeNS("http://ccc", "ccc:c", "d")
5.60 + assert n_e.getAttributeNS("http://ccc", "c") == "d"
5.61 + l3 = n_e.xpath("@*")
5.62 + assert len(l3) == 3
5.63 + n_e.setAttribute("p", "q")
5.64 + assert n_e.getAttribute("p") == "q"
5.65 + l4 = n_e.xpath("@*")
5.66 + assert len(l4) == 4
5.67 + al = n_e.attributes
5.68 + assert len(al.items()) == 4
5.69 +
5.70 + if use_libxml2dom:
5.71 + n_a3 = al.getNamedItemNS("http://ccc", "c")
5.72 + assert n_a3.namespaceURI == "http://ccc"
5.73 + assert n_a3.nodeName == "ccc:c"
5.74 + assert n_a3.localName == "c"
5.75 + assert n_a3.nodeValue == "d"
5.76 + print n_d.toString()
5.77 +
5.78 + n_a4 = n_e.createAttribute("m")
5.79 + n_a4.nodeValue = "n"
5.80 + assert n_a4.namespaceURI == None
5.81 + assert n_a4.nodeName == "m"
5.82 + assert n_a4.localName == "m"
5.83 + assert n_a4.nodeValue == n_a4.value == "n"
5.84 + print n_d.toString()
5.85 +
5.86 + if use_libxml2dom:
5.87 + n_a4_old = al.setNamedItem(n_a4)
5.88 + assert n_a4_old == None
5.89 + assert len(al.items()) == 5
5.90 + assert n_e.getAttribute("m") == "n"
5.91 + al.removeNamedItem("m")
5.92 + assert not n_e.hasAttribute("m")
5.93 + assert len(al.items()) == 4
5.94 +
5.95 + n_e.removeAttributeNS("http://ccc", "c")
5.96 + assert not n_e.hasAttributeNS("http://ccc", "c")
5.97 + l5 = n_e.xpath("@*")
5.98 + assert len(l5) == 3
5.99 + n_e.removeAttribute("p")
5.100 + assert not n_e.hasAttribute("p")
5.101 + l6 = n_e.xpath("@*")
5.102 + assert len(l6) == 2
5.103 + print n_d.toString()
5.104 +
5.105 +# vim: tabstop=4 expandtab shiftwidth=4
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/tests/libxml2macro/performance.py Sun Feb 27 18:55:07 2011 +0100
6.3 @@ -0,0 +1,84 @@
6.4 +#!/usr/bin/env python
6.5 +
6.6 +"""
6.7 +A performance test using libxml2dom.macrolib, libxml2dom and minidom.
6.8 +This module must be compiled using libxml2macro.py and run only as a compiled
6.9 +module - ie. as performance.pyc.
6.10 +"""
6.11 +
6.12 +import xml.dom
6.13 +
6.14 +def find_root(d):
6.15 + root = None
6.16 + for n in d.childNodes:
6.17 + if n.nodeType == xml.dom.Node.ELEMENT_NODE:
6.18 + root = n
6.19 + break
6.20 + return root
6.21 +
6.22 +import libxml2macro as x2_
6.23 +
6.24 +def find_root_libxml2macro(x2_d):
6.25 + x2_root = None
6.26 + for x2_n in x2_d.childNodes:
6.27 + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
6.28 + x2_root = x2_n
6.29 + break
6.30 + return x2_root
6.31 +
6.32 +def test_import_libxml2macro(x2_d):
6.33 + x2_d2 = createDocument("nsD", "newdoc", None)
6.34 + x2_imported = x2_d.importNode(find_root_libxml2macro(x2_d), 1)
6.35 + x2_d2.replaceChild(x2_imported, find_root_libxml2macro(x2_d2))
6.36 + return x2_d, x2_d2
6.37 +
6.38 +def test_import_minidom(d):
6.39 + d2 = xml.dom.minidom.getDOMImplementation().createDocument("nsD", "newdoc", None)
6.40 + imported = d2.importNode(find_root(d), 1)
6.41 + d2.replaceChild(imported, find_root(d2))
6.42 + return d, d2
6.43 +
6.44 +def test_import_libxml2dom(d):
6.45 + d2 = libxml2dom.createDocument("nsD", "newdoc", None)
6.46 + imported = d2.importNode(find_root(d), 1)
6.47 + d2.replaceChild(imported, find_root(d2))
6.48 + return d, d2
6.49 +
6.50 +if __name__ == "__main__":
6.51 + import sys
6.52 + import time
6.53 +
6.54 + if len(sys.argv) < 3:
6.55 + print "Please specify a filename (of a fairly large XML document) and the testing mode."
6.56 + print "There are quite a few large files in the libxml2 distribution."
6.57 + print "For the testing mode, choose one of libxml2macro, minidom, libxml2dom."
6.58 + sys.exit(1)
6.59 +
6.60 + if sys.argv[2] == "libxml2macro":
6.61 +
6.62 + x2_d = parseFile(sys.argv[1])
6.63 +
6.64 + t = time.time()
6.65 + x2_d1, x2_d2 = test_import_libxml2macro(x2_d)
6.66 + toFile(x2_d2, "/tmp/xxx_libxml2macro.xml")
6.67 + print "Time", time.time() - t, "seconds"
6.68 +
6.69 + elif sys.argv[2] == "minidom":
6.70 + import xml.dom.minidom
6.71 + d = xml.dom.minidom.parse(sys.argv[1])
6.72 +
6.73 + t = time.time()
6.74 + d1, d2 = test_import_minidom(d)
6.75 + open("/tmp/xxx_minidom.xml", "wb").write(d2.toxml("utf-8"))
6.76 + print "Time", time.time() - t, "seconds"
6.77 +
6.78 + elif sys.argv[2] == "libxml2dom":
6.79 + import libxml2dom
6.80 + d = libxml2dom.parse(sys.argv[1])
6.81 +
6.82 + t = time.time()
6.83 + d1, d2 = test_import_libxml2dom(d)
6.84 + libxml2dom.toStream(d2, open("/tmp/xxx_libxml2dom.xml", "wb"))
6.85 + print "Time", time.time() - t, "seconds"
6.86 +
6.87 +# vim: tabstop=4 expandtab shiftwidth=4
7.1 --- a/tests/macrotest.py Sun Feb 27 18:32:58 2011 +0100
7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
7.3 @@ -1,116 +0,0 @@
7.4 -#!/usr/bin/env python
7.5 -
7.6 -"""
7.7 -A test of macros. This file must be compiled using libxml2macro.py. It may then
7.8 -be imported normally in Python, but if run then the compiled module should be
7.9 -invoked directly - ie. as macrotest.pyc.
7.10 -"""
7.11 -
7.12 -import libxml2macro as x2_
7.13 -import xml.dom
7.14 -
7.15 -class Container:
7.16 - pass
7.17 -
7.18 -doc = """<?xml version="1.0"?>
7.19 -<doc>
7.20 - <element attr="value">
7.21 - <subelement/>
7.22 - </element>
7.23 -</doc>
7.24 -"""
7.25 -
7.26 -def find_root(x2_d):
7.27 - x2_root = None
7.28 -
7.29 - # Property access should be transformed.
7.30 -
7.31 - for x2_n in x2_d.childNodes:
7.32 - if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
7.33 - x2_root = x2_n
7.34 - break
7.35 -
7.36 - return x2_root
7.37 -
7.38 -def test():
7.39 - global doc
7.40 -
7.41 - # Assignment should not be transformed.
7.42 -
7.43 - x2_d = parseString(doc)
7.44 - return process(x2_d)
7.45 -
7.46 -def test_file(filename):
7.47 -
7.48 - # Assignment should not be transformed.
7.49 -
7.50 - x2_d = parseFile(filename)
7.51 - return process(x2_d)
7.52 -
7.53 -def process(x2_d):
7.54 -
7.55 - # Not even within containers, and not special names alone.
7.56 -
7.57 - c = Container()
7.58 - c.x2_d = x2_d
7.59 -
7.60 - # Find the root element.
7.61 -
7.62 - x2_root = find_root(x2_d)
7.63 - c.x2_root = x2_root
7.64 -
7.65 - # Create new attributes.
7.66 - # Method access should be transformed.
7.67 -
7.68 - x2_root.setAttributeNS("ns", "xxx:yyy", "zzz")
7.69 - c.x2_root.setAttributeNS("ns", "XXX:YYY", "ZZZ")
7.70 -
7.71 - # Create new elements.
7.72 - # Method access should be transformed.
7.73 -
7.74 - x2_new = x2_d.createElementNS("ns2", "ppp:qqq")
7.75 - x2_root.appendChild(x2_new)
7.76 - x2_new2 = c.x2_d.createElementNS("ns2", "PPP:QQQ")
7.77 - c.x2_root.appendChild(x2_new2)
7.78 -
7.79 - # Create new elements using ownerDocument.
7.80 - # Chaining properties is not
7.81 -
7.82 - x2_new3 = x2_new.ownerDocument.createElement("fff")
7.83 - x2_new.appendChild(x2_new3)
7.84 - x2_new4 = x2_new2.ownerDocument.createElement("FFF")
7.85 - x2_new2.appendChild(x2_new4)
7.86 -
7.87 - return x2_d
7.88 -
7.89 -def test_import(x2_d):
7.90 -
7.91 - # Change the prefix.
7.92 -
7.93 - import libxml2macro as node_
7.94 - node_d = x2_d
7.95 -
7.96 - node_root = find_root(node_d)
7.97 -
7.98 - # Create a new document.
7.99 -
7.100 - node_d2 = createDocument("nsD", "newdoc", None)
7.101 - node_root2 = find_root(node_d2)
7.102 -
7.103 - # Attempt to import nodes from the original document.
7.104 -
7.105 - node_imported = node_d2.importNode(node_root, 1)
7.106 - node_d2.replaceChild(node_imported, node_root2)
7.107 -
7.108 - return node_d, node_d2
7.109 -
7.110 -if __name__ == "__main__":
7.111 - import sys
7.112 - if len(sys.argv) > 1:
7.113 - print "Running a simple test on", sys.argv[1]
7.114 - test_file(sys.argv[1])
7.115 - else:
7.116 - print "Running a simple test on some built-in string."
7.117 - test()
7.118 -
7.119 -# vim: tabstop=4 expandtab shiftwidth=4
8.1 --- a/tests/namespaces2.py Sun Feb 27 18:32:58 2011 +0100
8.2 +++ b/tests/namespaces2.py Sun Feb 27 18:55:07 2011 +0100
8.3 @@ -2,6 +2,9 @@
8.4
8.5 import libxml2dom, xml.dom, xml.dom.minidom
8.6
8.7 +print "Creating libxml2dom document..."
8.8 +print
8.9 +
8.10 document = libxml2dom.createDocument(None, "doc", None)
8.11 top = document.xpath("*")[0]
8.12 elem1 = document.createElementNS("DAV:", "myns:href")
8.13 @@ -10,6 +13,9 @@
8.14 document.replaceChild(elem1, top)
8.15 print document.toString()
8.16
8.17 +print "Creating minidom document..."
8.18 +print
8.19 +
8.20 document2 = xml.dom.minidom.Document()
8.21 elem1 = document2.createElementNS("DAV:", "myns:href")
8.22 elem1.setAttributeNS(xml.dom.XMLNS_NAMESPACE, "xmlns:myns", "DAV:")
9.1 --- a/tests/namespaces3.py Sun Feb 27 18:32:58 2011 +0100
9.2 +++ b/tests/namespaces3.py Sun Feb 27 18:55:07 2011 +0100
9.3 @@ -16,11 +16,17 @@
9.4 e3.setAttributeNS("DAV:", "fgh", "ijk")
9.5 e3.setAttributeNS(None, "nop", "wxy")
9.6
9.7 +print "Creating libxml2dom document..."
9.8 +print
9.9 +
9.10 document = libxml2dom.createDocument(None, "doc", None)
9.11 e = document.xpath("*")[0]
9.12 test(document, e)
9.13 print document.toString(prettyprint=1)
9.14
9.15 +print "Creating minidom document..."
9.16 +print
9.17 +
9.18 document = xml.dom.minidom.getDOMImplementation().createDocument(None, "doc", None)
9.19 e = document.documentElement
9.20 test(document, e)
9.21 @@ -34,6 +40,10 @@
9.22
9.23 try:
9.24 import pxdom
9.25 +
9.26 + print "Creating pxdom document..."
9.27 + print
9.28 +
9.29 document = pxdom.getDOMImplementation("").createDocument(None, "doc", None)
9.30 e = document.documentElement
9.31 test(document, e)
10.1 --- a/tests/nodes.py Sun Feb 27 18:32:58 2011 +0100
10.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
10.3 @@ -1,102 +0,0 @@
10.4 -#!/usr/bin/env python
10.5 -
10.6 -"Test of elements and attribute interfaces."
10.7 -
10.8 -import libxml2macro as n_
10.9 -
10.10 -try:
10.11 - do_not_have_libxml2macro = n_
10.12 - import libxml2dom
10.13 - use_libxml2dom = 1
10.14 -except NameError:
10.15 - use_libxml2dom = 0
10.16 -
10.17 -if __name__ == "__main__":
10.18 - import sys
10.19 -
10.20 - s = "<ddd:doc xmlns:ddd='http://ddd'/>"
10.21 -
10.22 - if use_libxml2dom:
10.23 - n_d = libxml2dom.parseString(s)
10.24 - else:
10.25 - n_d = parseString(s)
10.26 -
10.27 - n_e = n_d.xpath("*")[0]
10.28 - assert n_e.parentNode == n_d
10.29 - assert n_e.namespaceURI == "http://ddd"
10.30 - assert n_e.nodeName == n_e.tagName == "ddd:doc"
10.31 - assert n_e.localName == "doc"
10.32 - print n_d.toString()
10.33 -
10.34 - n_e.setAttributeNS("http://xxx", "xxx:x", "y")
10.35 - assert n_e.getAttributeNS("http://xxx", "x") == "y"
10.36 - l = n_e.xpath("@*")
10.37 - assert len(l) == 1
10.38 - n_a = l[0]
10.39 - assert n_a.parentNode == n_e
10.40 - assert n_a.namespaceURI == "http://xxx"
10.41 - assert n_a.nodeName == "xxx:x"
10.42 - assert n_a.localName == "x"
10.43 - assert n_a.nodeValue == n_a.value == "y"
10.44 - print n_d.toString()
10.45 -
10.46 - n_a2 = n_d.createAttributeNS("http://aaa", "aaa:a")
10.47 - n_a2.nodeValue = "b"
10.48 - assert n_a2.namespaceURI == "http://aaa"
10.49 - assert n_a2.nodeName == "aaa:a"
10.50 - assert n_a2.localName == "a"
10.51 - assert n_a2.nodeValue == n_a2.value == "b"
10.52 - print n_d.toString()
10.53 -
10.54 - n_e.setAttributeNodeNS(n_a2)
10.55 - l2 = n_e.xpath("@*")
10.56 - assert len(l2) == 2
10.57 - print n_d.toString()
10.58 -
10.59 - n_e.setAttributeNS("http://ccc", "ccc:c", "d")
10.60 - assert n_e.getAttributeNS("http://ccc", "c") == "d"
10.61 - l3 = n_e.xpath("@*")
10.62 - assert len(l3) == 3
10.63 - n_e.setAttribute("p", "q")
10.64 - assert n_e.getAttribute("p") == "q"
10.65 - l4 = n_e.xpath("@*")
10.66 - assert len(l4) == 4
10.67 - al = n_e.attributes
10.68 - assert len(al.items()) == 4
10.69 -
10.70 - if use_libxml2dom:
10.71 - n_a3 = al.getNamedItemNS("http://ccc", "c")
10.72 - assert n_a3.namespaceURI == "http://ccc"
10.73 - assert n_a3.nodeName == "ccc:c"
10.74 - assert n_a3.localName == "c"
10.75 - assert n_a3.nodeValue == "d"
10.76 - print n_d.toString()
10.77 -
10.78 - n_a4 = n_e.createAttribute("m")
10.79 - n_a4.nodeValue = "n"
10.80 - assert n_a4.namespaceURI == None
10.81 - assert n_a4.nodeName == "m"
10.82 - assert n_a4.localName == "m"
10.83 - assert n_a4.nodeValue == n_a4.value == "n"
10.84 - print n_d.toString()
10.85 -
10.86 - if use_libxml2dom:
10.87 - n_a4_old = al.setNamedItem(n_a4)
10.88 - assert n_a4_old == None
10.89 - assert len(al.items()) == 5
10.90 - assert n_e.getAttribute("m") == "n"
10.91 - al.removeNamedItem("m")
10.92 - assert not n_e.hasAttribute("m")
10.93 - assert len(al.items()) == 4
10.94 -
10.95 - n_e.removeAttributeNS("http://ccc", "c")
10.96 - assert not n_e.hasAttributeNS("http://ccc", "c")
10.97 - l5 = n_e.xpath("@*")
10.98 - assert len(l5) == 3
10.99 - n_e.removeAttribute("p")
10.100 - assert not n_e.hasAttribute("p")
10.101 - l6 = n_e.xpath("@*")
10.102 - assert len(l6) == 2
10.103 - print n_d.toString()
10.104 -
10.105 -# vim: tabstop=4 expandtab shiftwidth=4
11.1 --- a/tests/performance.py Sun Feb 27 18:32:58 2011 +0100
11.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
11.3 @@ -1,84 +0,0 @@
11.4 -#!/usr/bin/env python
11.5 -
11.6 -"""
11.7 -A performance test using libxml2dom.macrolib, libxml2dom and minidom.
11.8 -This module must be compiled using libxml2macro.py and run only as a compiled
11.9 -module - ie. as performance.pyc.
11.10 -"""
11.11 -
11.12 -import xml.dom
11.13 -
11.14 -def find_root(d):
11.15 - root = None
11.16 - for n in d.childNodes:
11.17 - if n.nodeType == xml.dom.Node.ELEMENT_NODE:
11.18 - root = n
11.19 - break
11.20 - return root
11.21 -
11.22 -import libxml2macro as x2_
11.23 -
11.24 -def find_root_libxml2macro(x2_d):
11.25 - x2_root = None
11.26 - for x2_n in x2_d.childNodes:
11.27 - if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
11.28 - x2_root = x2_n
11.29 - break
11.30 - return x2_root
11.31 -
11.32 -def test_import_libxml2macro(x2_d):
11.33 - x2_d2 = createDocument("nsD", "newdoc", None)
11.34 - x2_imported = x2_d.importNode(find_root_libxml2macro(x2_d), 1)
11.35 - x2_d2.replaceChild(x2_imported, find_root_libxml2macro(x2_d2))
11.36 - return x2_d, x2_d2
11.37 -
11.38 -def test_import_minidom(d):
11.39 - d2 = xml.dom.minidom.getDOMImplementation().createDocument("nsD", "newdoc", None)
11.40 - imported = d2.importNode(find_root(d), 1)
11.41 - d2.replaceChild(imported, find_root(d2))
11.42 - return d, d2
11.43 -
11.44 -def test_import_libxml2dom(d):
11.45 - d2 = libxml2dom.createDocument("nsD", "newdoc", None)
11.46 - imported = d2.importNode(find_root(d), 1)
11.47 - d2.replaceChild(imported, find_root(d2))
11.48 - return d, d2
11.49 -
11.50 -if __name__ == "__main__":
11.51 - import sys
11.52 - import time
11.53 -
11.54 - if len(sys.argv) < 3:
11.55 - print "Please specify a filename (of a fairly large XML document) and the testing mode."
11.56 - print "There are quite a few large files in the libxml2 distribution."
11.57 - print "For the testing mode, choose one of libxml2macro, minidom, libxml2dom."
11.58 - sys.exit(1)
11.59 -
11.60 - if sys.argv[2] == "libxml2macro":
11.61 -
11.62 - x2_d = parseFile(sys.argv[1])
11.63 -
11.64 - t = time.time()
11.65 - x2_d1, x2_d2 = test_import_libxml2macro(x2_d)
11.66 - toFile(x2_d2, "/tmp/xxx_libxml2macro.xml")
11.67 - print "Time", time.time() - t, "seconds"
11.68 -
11.69 - elif sys.argv[2] == "minidom":
11.70 - import xml.dom.minidom
11.71 - d = xml.dom.minidom.parse(sys.argv[1])
11.72 -
11.73 - t = time.time()
11.74 - d1, d2 = test_import_minidom(d)
11.75 - open("/tmp/xxx_minidom.xml", "wb").write(d2.toxml("utf-8"))
11.76 - print "Time", time.time() - t, "seconds"
11.77 -
11.78 - elif sys.argv[2] == "libxml2dom":
11.79 - import libxml2dom
11.80 - d = libxml2dom.parse(sys.argv[1])
11.81 -
11.82 - t = time.time()
11.83 - d1, d2 = test_import_libxml2dom(d)
11.84 - libxml2dom.toStream(d2, open("/tmp/xxx_libxml2dom.xml", "wb"))
11.85 - print "Time", time.time() - t, "seconds"
11.86 -
11.87 -# vim: tabstop=4 expandtab shiftwidth=4
12.1 --- a/tests/prettyprint.py Sun Feb 27 18:32:58 2011 +0100
12.2 +++ b/tests/prettyprint.py Sun Feb 27 18:55:07 2011 +0100
12.3 @@ -1,10 +1,24 @@
12.4 #!/usr/bin/env python
12.5
12.6 import libxml2dom
12.7 -from xml.dom.ext import PrettyPrint
12.8 import sys
12.9
12.10 d = libxml2dom.parse(sys.argv[1])
12.11 -PrettyPrint(d)
12.12 +
12.13 +print "Prettyprint using libxml2dom..."
12.14 +print
12.15 +
12.16 +d.toStream(sys.stdout, prettyprint=1)
12.17 +
12.18 +try:
12.19 + from xml.dom.ext import PrettyPrint
12.20 +
12.21 + print "Prettyprint using xml.dom..."
12.22 + print
12.23 +
12.24 + PrettyPrint(d)
12.25 +
12.26 +except ImportError:
12.27 + print "xml.dom not tested."
12.28
12.29 # vim: tabstop=4 expandtab shiftwidth=4
13.1 --- a/tests/test_valid_relaxng.py Sun Feb 27 18:32:58 2011 +0100
13.2 +++ b/tests/test_valid_relaxng.py Sun Feb 27 18:55:07 2011 +0100
13.3 @@ -4,14 +4,18 @@
13.4
13.5 schema = libxml2dom.parse("tests/test_valid_relaxng.xml")
13.6 d = libxml2dom.parse("tests/test_valid.xml")
13.7 +print "Test of valid document..."
13.8 print d.validate(schema)
13.9 print d.validateDocument(schema)
13.10 print d.getParameter("error-handler")
13.11 +print
13.12
13.13 schema = libxml2dom.parse("tests/test_invalid_relaxng.xml")
13.14 d = libxml2dom.parse("tests/test_invalid.xml")
13.15 +print "Test of invalid document..."
13.16 print d.validate(schema)
13.17 print d.validateDocument(schema)
13.18 print d.getParameter("error-handler")
13.19 +print
13.20
13.21 # vim: tabstop=4 expandtab shiftwidth=4
14.1 --- a/tests/test_valid_schema.py Sun Feb 27 18:32:58 2011 +0100
14.2 +++ b/tests/test_valid_schema.py Sun Feb 27 18:55:07 2011 +0100
14.3 @@ -4,14 +4,18 @@
14.4
14.5 schema = libxml2dom.parse("tests/test_valid_schema.xml")
14.6 d = libxml2dom.parse("tests/test_valid.xml")
14.7 +print "Test of valid document..."
14.8 print d.validate(schema)
14.9 print d.validateDocument(schema)
14.10 print d.getParameter("error-handler")
14.11 +print
14.12
14.13 schema = libxml2dom.parse("tests/test_invalid_schema.xml")
14.14 d = libxml2dom.parse("tests/test_invalid.xml")
14.15 +print "Test of invalid document..."
14.16 print d.validate(schema)
14.17 print d.validateDocument(schema)
14.18 print d.getParameter("error-handler")
14.19 +print
14.20
14.21 # vim: tabstop=4 expandtab shiftwidth=4
15.1 --- a/tests/test_valid_schematron.py Sun Feb 27 18:32:58 2011 +0100
15.2 +++ b/tests/test_valid_schematron.py Sun Feb 27 18:55:07 2011 +0100
15.3 @@ -4,14 +4,18 @@
15.4
15.5 schema = libxml2dom.parse("tests/test_valid_schematron.xml")
15.6 d = libxml2dom.parse("tests/test_valid.xml")
15.7 +print "Test of valid document..."
15.8 print d.validate(schema)
15.9 print d.validateDocument(schema)
15.10 print d.getParameter("error-handler")
15.11 +print
15.12
15.13 schema = libxml2dom.parse("tests/test_invalid_schematron.xml")
15.14 d = libxml2dom.parse("tests/test_invalid.xml")
15.15 +print "Test of invalid document..."
15.16 print d.validate(schema)
15.17 print d.validateDocument(schema)
15.18 print d.getParameter("error-handler")
15.19 +print
15.20
15.21 # vim: tabstop=4 expandtab shiftwidth=4