1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/macrotest.py Sun Jan 16 02:02:42 2005 +0000
1.3 @@ -0,0 +1,99 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"A test of macros."
1.7 +
1.8 +from libxml2dom.macrolib import *
1.9 +import libxml2
1.10 +import xml.dom
1.11 +
1.12 +class Container:
1.13 + pass
1.14 +
1.15 +doc = """<?xml version="1.0"?>
1.16 +<doc>
1.17 + <element attr="value">
1.18 + <subelement/>
1.19 + </element>
1.20 +</doc>
1.21 +"""
1.22 +
1.23 +def find_root(x2_d):
1.24 + x2_root = None
1.25 +
1.26 + # Property access should be transformed.
1.27 +
1.28 + for x2_n in x2_d.childNodes:
1.29 + if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
1.30 + x2_root = x2_n
1.31 + break
1.32 +
1.33 + return x2_root
1.34 +
1.35 +def test():
1.36 + global doc
1.37 +
1.38 + # Assignment should not be transformed.
1.39 +
1.40 + x2_d = parseString(doc)
1.41 + return process(x2_d)
1.42 +
1.43 +def test_file(filename):
1.44 +
1.45 + # Assignment should not be transformed.
1.46 +
1.47 + x2_d = parseFile(filename)
1.48 + return process(x2_d)
1.49 +
1.50 +def process(x2_d):
1.51 +
1.52 + # Not even within containers, and not special names alone.
1.53 +
1.54 + c = Container()
1.55 + c.x2_d = x2_d
1.56 +
1.57 + # Find the root element.
1.58 +
1.59 + x2_root = find_root(x2_d)
1.60 + c.x2_root = x2_root
1.61 +
1.62 + # Create new attributes.
1.63 + # Method access should be transformed.
1.64 +
1.65 + x2_root.setAttributeNS("ns", "xxx:yyy", "zzz")
1.66 + c.x2_root.setAttributeNS("ns", "XXX:YYY", "ZZZ")
1.67 +
1.68 + # Create new elements.
1.69 + # Method access should be transformed.
1.70 +
1.71 + x2_new = x2_d.createElementNS("ns2", "ppp:qqq")
1.72 + x2_root.appendChild(x2_new)
1.73 + x2_new2 = c.x2_d.createElementNS("ns2", "PPP:QQQ")
1.74 + c.x2_root.appendChild(x2_new2)
1.75 +
1.76 + # Create new elements using ownerDocument.
1.77 + # Chaining properties is not
1.78 +
1.79 + x2_new3 = x2_new.ownerDocument.createElement("fff")
1.80 + x2_new.appendChild(x2_new3)
1.81 + x2_new4 = x2_new2.ownerDocument.createElement("FFF")
1.82 + x2_new2.appendChild(x2_new4)
1.83 +
1.84 + return x2_d
1.85 +
1.86 +def test_import(x2_d):
1.87 +
1.88 + x2_root = find_root(x2_d)
1.89 +
1.90 + # Create a new document.
1.91 +
1.92 + x2_d2 = createDocument("nsD", "newdoc", None)
1.93 + x2_root2 = find_root(x2_d2)
1.94 +
1.95 + # Attempt to import nodes from the original document.
1.96 +
1.97 + x2_imported = x2_d2.importNode(x2_root, 1)
1.98 + x2_d2.replaceChild(x2_imported, x2_root2)
1.99 +
1.100 + return x2_d, x2_d2
1.101 +
1.102 +# vim: tabstop=4 expandtab shiftwidth=4