1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tests/libxml2macro/nodes.py Sun Feb 27 18:55:07 2011 +0100
1.3 @@ -0,0 +1,102 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"Test of elements and attribute interfaces."
1.7 +
1.8 +import libxml2macro as n_
1.9 +
1.10 +try:
1.11 + do_not_have_libxml2macro = n_
1.12 + import libxml2dom
1.13 + use_libxml2dom = 1
1.14 +except NameError:
1.15 + use_libxml2dom = 0
1.16 +
1.17 +if __name__ == "__main__":
1.18 + import sys
1.19 +
1.20 + s = "<ddd:doc xmlns:ddd='http://ddd'/>"
1.21 +
1.22 + if use_libxml2dom:
1.23 + n_d = libxml2dom.parseString(s)
1.24 + else:
1.25 + n_d = parseString(s)
1.26 +
1.27 + n_e = n_d.xpath("*")[0]
1.28 + assert n_e.parentNode == n_d
1.29 + assert n_e.namespaceURI == "http://ddd"
1.30 + assert n_e.nodeName == n_e.tagName == "ddd:doc"
1.31 + assert n_e.localName == "doc"
1.32 + print n_d.toString()
1.33 +
1.34 + n_e.setAttributeNS("http://xxx", "xxx:x", "y")
1.35 + assert n_e.getAttributeNS("http://xxx", "x") == "y"
1.36 + l = n_e.xpath("@*")
1.37 + assert len(l) == 1
1.38 + n_a = l[0]
1.39 + assert n_a.parentNode == n_e
1.40 + assert n_a.namespaceURI == "http://xxx"
1.41 + assert n_a.nodeName == "xxx:x"
1.42 + assert n_a.localName == "x"
1.43 + assert n_a.nodeValue == n_a.value == "y"
1.44 + print n_d.toString()
1.45 +
1.46 + n_a2 = n_d.createAttributeNS("http://aaa", "aaa:a")
1.47 + n_a2.nodeValue = "b"
1.48 + assert n_a2.namespaceURI == "http://aaa"
1.49 + assert n_a2.nodeName == "aaa:a"
1.50 + assert n_a2.localName == "a"
1.51 + assert n_a2.nodeValue == n_a2.value == "b"
1.52 + print n_d.toString()
1.53 +
1.54 + n_e.setAttributeNodeNS(n_a2)
1.55 + l2 = n_e.xpath("@*")
1.56 + assert len(l2) == 2
1.57 + print n_d.toString()
1.58 +
1.59 + n_e.setAttributeNS("http://ccc", "ccc:c", "d")
1.60 + assert n_e.getAttributeNS("http://ccc", "c") == "d"
1.61 + l3 = n_e.xpath("@*")
1.62 + assert len(l3) == 3
1.63 + n_e.setAttribute("p", "q")
1.64 + assert n_e.getAttribute("p") == "q"
1.65 + l4 = n_e.xpath("@*")
1.66 + assert len(l4) == 4
1.67 + al = n_e.attributes
1.68 + assert len(al.items()) == 4
1.69 +
1.70 + if use_libxml2dom:
1.71 + n_a3 = al.getNamedItemNS("http://ccc", "c")
1.72 + assert n_a3.namespaceURI == "http://ccc"
1.73 + assert n_a3.nodeName == "ccc:c"
1.74 + assert n_a3.localName == "c"
1.75 + assert n_a3.nodeValue == "d"
1.76 + print n_d.toString()
1.77 +
1.78 + n_a4 = n_e.createAttribute("m")
1.79 + n_a4.nodeValue = "n"
1.80 + assert n_a4.namespaceURI == None
1.81 + assert n_a4.nodeName == "m"
1.82 + assert n_a4.localName == "m"
1.83 + assert n_a4.nodeValue == n_a4.value == "n"
1.84 + print n_d.toString()
1.85 +
1.86 + if use_libxml2dom:
1.87 + n_a4_old = al.setNamedItem(n_a4)
1.88 + assert n_a4_old == None
1.89 + assert len(al.items()) == 5
1.90 + assert n_e.getAttribute("m") == "n"
1.91 + al.removeNamedItem("m")
1.92 + assert not n_e.hasAttribute("m")
1.93 + assert len(al.items()) == 4
1.94 +
1.95 + n_e.removeAttributeNS("http://ccc", "c")
1.96 + assert not n_e.hasAttributeNS("http://ccc", "c")
1.97 + l5 = n_e.xpath("@*")
1.98 + assert len(l5) == 3
1.99 + n_e.removeAttribute("p")
1.100 + assert not n_e.hasAttribute("p")
1.101 + l6 = n_e.xpath("@*")
1.102 + assert len(l6) == 2
1.103 + print n_d.toString()
1.104 +
1.105 +# vim: tabstop=4 expandtab shiftwidth=4