libxml2dom

Annotated tests/libxml2macro/macrotest.py

354:15a02d623ce7
2011-02-27 Paul Boddie Tidied up the tests, moving libxml2macro-related tests into their own directory.
paulb@49 1
#!/usr/bin/env python
paulb@49 2
paulb@65 3
"""
paulb@65 4
A test of macros. This file must be compiled using libxml2macro.py. It may then
paulb@65 5
be imported normally in Python, but if run then the compiled module should be
paulb@65 6
invoked directly - ie. as macrotest.pyc.
paulb@65 7
"""
paulb@49 8
paulb@58 9
import libxml2macro as x2_
paulb@49 10
import xml.dom
paulb@49 11
paulb@49 12
class Container:
paulb@49 13
    pass
paulb@49 14
paulb@49 15
doc = """<?xml version="1.0"?>
paulb@49 16
<doc>
paulb@49 17
    <element attr="value">
paulb@49 18
        <subelement/>
paulb@49 19
    </element>
paulb@49 20
</doc>
paulb@49 21
"""
paulb@49 22
paulb@49 23
def find_root(x2_d):
paulb@49 24
    x2_root = None
paulb@49 25
paulb@49 26
    # Property access should be transformed.
paulb@49 27
paulb@49 28
    for x2_n in x2_d.childNodes:
paulb@49 29
        if x2_n.nodeType == xml.dom.Node.ELEMENT_NODE:
paulb@49 30
            x2_root = x2_n
paulb@49 31
            break
paulb@49 32
paulb@49 33
    return x2_root
paulb@49 34
paulb@49 35
def test():
paulb@49 36
    global doc
paulb@49 37
paulb@49 38
    # Assignment should not be transformed.
paulb@49 39
paulb@49 40
    x2_d = parseString(doc)
paulb@49 41
    return process(x2_d)
paulb@49 42
paulb@49 43
def test_file(filename):
paulb@49 44
paulb@49 45
    # Assignment should not be transformed.
paulb@49 46
paulb@49 47
    x2_d = parseFile(filename)
paulb@49 48
    return process(x2_d)
paulb@49 49
paulb@49 50
def process(x2_d):
paulb@49 51
paulb@49 52
    # Not even within containers, and not special names alone.
paulb@49 53
paulb@49 54
    c = Container()
paulb@49 55
    c.x2_d = x2_d
paulb@49 56
paulb@49 57
    # Find the root element.
paulb@49 58
paulb@49 59
    x2_root = find_root(x2_d)
paulb@49 60
    c.x2_root = x2_root
paulb@49 61
paulb@49 62
    # Create new attributes.
paulb@49 63
    # Method access should be transformed.
paulb@49 64
paulb@49 65
    x2_root.setAttributeNS("ns", "xxx:yyy", "zzz")
paulb@49 66
    c.x2_root.setAttributeNS("ns", "XXX:YYY", "ZZZ")
paulb@49 67
paulb@49 68
    # Create new elements.
paulb@49 69
    # Method access should be transformed.
paulb@49 70
paulb@49 71
    x2_new = x2_d.createElementNS("ns2", "ppp:qqq")
paulb@49 72
    x2_root.appendChild(x2_new)
paulb@49 73
    x2_new2 = c.x2_d.createElementNS("ns2", "PPP:QQQ")
paulb@49 74
    c.x2_root.appendChild(x2_new2)
paulb@49 75
paulb@49 76
    # Create new elements using ownerDocument.
paulb@49 77
    # Chaining properties is not
paulb@49 78
paulb@49 79
    x2_new3 = x2_new.ownerDocument.createElement("fff")
paulb@49 80
    x2_new.appendChild(x2_new3)
paulb@49 81
    x2_new4 = x2_new2.ownerDocument.createElement("FFF")
paulb@49 82
    x2_new2.appendChild(x2_new4)
paulb@49 83
paulb@49 84
    return x2_d
paulb@49 85
paulb@49 86
def test_import(x2_d):
paulb@49 87
paulb@58 88
    # Change the prefix.
paulb@58 89
paulb@58 90
    import libxml2macro as node_
paulb@58 91
    node_d = x2_d
paulb@58 92
paulb@58 93
    node_root = find_root(node_d)
paulb@49 94
paulb@49 95
    # Create a new document.
paulb@49 96
paulb@58 97
    node_d2 = createDocument("nsD", "newdoc", None)
paulb@58 98
    node_root2 = find_root(node_d2)
paulb@49 99
paulb@49 100
    # Attempt to import nodes from the original document.
paulb@49 101
paulb@58 102
    node_imported = node_d2.importNode(node_root, 1)
paulb@58 103
    node_d2.replaceChild(node_imported, node_root2)
paulb@49 104
paulb@58 105
    return node_d, node_d2
paulb@49 106
paulb@60 107
if __name__ == "__main__":
paulb@60 108
    import sys
paulb@60 109
    if len(sys.argv) > 1:
paulb@65 110
        print "Running a simple test on", sys.argv[1]
paulb@60 111
        test_file(sys.argv[1])
paulb@60 112
    else:
paulb@65 113
        print "Running a simple test on some built-in string."
paulb@60 114
        test()
paulb@60 115
paulb@49 116
# vim: tabstop=4 expandtab shiftwidth=4