1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/libxml2macro/macrotest.py Sun Feb 27 18:55:07 2011 +0100
1.3 @@ -0,0 +1,116 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A test of macros. This file must be compiled using libxml2macro.py. It may then
1.8 +be imported normally in Python, but if run then the compiled module should be
1.9 +invoked directly - ie. as macrotest.pyc.
1.10 +"""
1.11 +
1.12 +import libxml2macro as x2_
1.13 +import xml.dom
1.14 +
1.15 +class Container:
1.16 + pass
1.17 +
1.18 +doc = """<?xml version="1.0"?>
1.19 +<doc>
1.20 + <element attr="value">
1.21 + <subelement/>
1.22 + </element>
1.23 +</doc>
1.24 +"""
1.25 +
1.26 +def find_root(x2_d):
1.27 + x2_root = None
1.28 +
1.29 + # Property access should be transformed.
1.30 +
1.31 + for x2_n in x2_d.childNodes:
1.32 + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
1.33 + x2_root = x2_n
1.34 + break
1.35 +
1.36 + return x2_root
1.37 +
1.38 +def test():
1.39 + global doc
1.40 +
1.41 + # Assignment should not be transformed.
1.42 +
1.43 + x2_d = parseString(doc)
1.44 + return process(x2_d)
1.45 +
1.46 +def test_file(filename):
1.47 +
1.48 + # Assignment should not be transformed.
1.49 +
1.50 + x2_d = parseFile(filename)
1.51 + return process(x2_d)
1.52 +
1.53 +def process(x2_d):
1.54 +
1.55 + # Not even within containers, and not special names alone.
1.56 +
1.57 + c = Container()
1.58 + c.x2_d = x2_d
1.59 +
1.60 + # Find the root element.
1.61 +
1.62 + x2_root = find_root(x2_d)
1.63 + c.x2_root = x2_root
1.64 +
1.65 + # Create new attributes.
1.66 + # Method access should be transformed.
1.67 +
1.68 + x2_root.setAttributeNS("ns", "xxx:yyy", "zzz")
1.69 + c.x2_root.setAttributeNS("ns", "XXX:YYY", "ZZZ")
1.70 +
1.71 + # Create new elements.
1.72 + # Method access should be transformed.
1.73 +
1.74 + x2_new = x2_d.createElementNS("ns2", "ppp:qqq")
1.75 + x2_root.appendChild(x2_new)
1.76 + x2_new2 = c.x2_d.createElementNS("ns2", "PPP:QQQ")
1.77 + c.x2_root.appendChild(x2_new2)
1.78 +
1.79 + # Create new elements using ownerDocument.
1.80 + # Chaining properties is not
1.81 +
1.82 + x2_new3 = x2_new.ownerDocument.createElement("fff")
1.83 + x2_new.appendChild(x2_new3)
1.84 + x2_new4 = x2_new2.ownerDocument.createElement("FFF")
1.85 + x2_new2.appendChild(x2_new4)
1.86 +
1.87 + return x2_d
1.88 +
1.89 +def test_import(x2_d):
1.90 +
1.91 + # Change the prefix.
1.92 +
1.93 + import libxml2macro as node_
1.94 + node_d = x2_d
1.95 +
1.96 + node_root = find_root(node_d)
1.97 +
1.98 + # Create a new document.
1.99 +
1.100 + node_d2 = createDocument("nsD", "newdoc", None)
1.101 + node_root2 = find_root(node_d2)
1.102 +
1.103 + # Attempt to import nodes from the original document.
1.104 +
1.105 + node_imported = node_d2.importNode(node_root, 1)
1.106 + node_d2.replaceChild(node_imported, node_root2)
1.107 +
1.108 + return node_d, node_d2
1.109 +
1.110 +if __name__ == "__main__":
1.111 + import sys
1.112 + if len(sys.argv) > 1:
1.113 + print "Running a simple test on", sys.argv[1]
1.114 + test_file(sys.argv[1])
1.115 + else:
1.116 + print "Running a simple test on some built-in string."
1.117 + test()
1.118 +
1.119 +# vim: tabstop=4 expandtab shiftwidth=4