libxml2 2.9.1 clean sources
[tomato.git] / release / src / router / libxml2 / python / tests / xpathret.py
blob11c8b324e66f707f64e6bc2c6477fdc6e6823428
1 #!/usr/bin/python -u
2 import sys
3 import libxml2
5 #memory debug specific
6 libxml2.debugMemory(1)
9 # A document hosting the nodes returned from the extension function
11 mydoc = libxml2.newDoc("1.0")
13 def foo(ctx, str):
14 global mydoc
17 # test returning a node set works as expected
19 parent = mydoc.newDocNode(None, 'p', None)
20 mydoc.addChild(parent)
21 node = mydoc.newDocText(str)
22 parent.addChild(node)
23 return [parent]
25 doc = libxml2.parseFile("tst.xml")
26 ctxt = doc.xpathNewContext()
27 libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
28 res = ctxt.xpathEval("foo('hello')")
29 if type(res) != type([]):
30 print("Failed to return a nodeset")
31 sys.exit(1)
32 if len(res) != 1:
33 print("Unexpected nodeset size")
34 sys.exit(1)
35 node = res[0]
36 if node.name != 'p':
37 print("Unexpected nodeset element result")
38 sys.exit(1)
39 node = node.children
40 if node.type != 'text':
41 print("Unexpected nodeset element children type")
42 sys.exit(1)
43 if node.content != 'hello':
44 print("Unexpected nodeset element children content")
45 sys.exit(1)
47 doc.freeDoc()
48 mydoc.freeDoc()
49 ctxt.xpathFreeContext()
51 #memory debug specific
52 libxml2.cleanupParser()
53 if libxml2.debugMemory(1) == 0:
54 print("OK")
55 else:
56 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
57 libxml2.dumpMemory()