libxml2 2.9.1 clean sources
[tomato.git] / release / src / router / libxml2 / python / tests / walker.py
blob47f0557c1cd15b0babd32b687548788ac7be6bc6
1 #!/usr/bin/python -u
3 # this tests the entities substitutions with the XmlTextReader interface
5 import sys
6 import libxml2
8 # Memory debug specific
9 libxml2.debugMemory(1)
11 result = ""
12 def processNode(reader):
13 global result
15 result = result + "%d %d %s %d\n" % (reader.Depth(), reader.NodeType(),
16 reader.Name(), reader.IsEmptyElement())
19 # Parse a document testing the readerForxxx API
21 docstr="""<foo>
22 <label>some text</label>
23 <item>100</item>
24 </foo>"""
25 expect="""0 1 foo 0
26 1 14 #text 0
27 1 1 label 0
28 2 3 #text 0
29 1 15 label 0
30 1 14 #text 0
31 1 1 item 0
32 2 3 #text 0
33 1 15 item 0
34 1 14 #text 0
35 0 15 foo 0
36 """
37 result = ""
39 doc = libxml2.parseDoc(docstr)
40 reader = doc.readerWalker();
41 ret = reader.Read()
42 while ret == 1:
43 processNode(reader)
44 ret = reader.Read()
46 if ret != 0:
47 print("Error parsing the document test1")
48 sys.exit(1)
50 if result != expect:
51 print("Unexpected result for test1")
52 print(result)
53 sys.exit(1)
55 doc.freeDoc()
58 # Reuse the reader for another document testing the ReaderNewWalker API
60 docstr="""<foo>
61 <label>some text</label>
62 <item>1000</item>
63 </foo>"""
64 expect="""0 1 foo 0
65 1 14 #text 0
66 1 1 label 0
67 2 3 #text 0
68 1 15 label 0
69 1 14 #text 0
70 1 1 item 0
71 2 3 #text 0
72 1 15 item 0
73 1 14 #text 0
74 0 15 foo 0
75 """
76 result = ""
78 doc = libxml2.parseDoc(docstr)
79 reader.NewWalker(doc)
81 ret = reader.Read()
82 while ret == 1:
83 processNode(reader)
84 ret = reader.Read()
86 if ret != 0:
87 print("Error parsing the document test2")
88 sys.exit(1)
90 if result != expect:
91 print("Unexpected result for test2")
92 print(result)
93 sys.exit(1)
95 doc.freeDoc()
98 # Reuse the reader for another document testing the ReaderNewxxx API
100 docstr="""<foo>
101 <label>some text</label>
102 <item>1000</item>
103 </foo>"""
104 expect="""0 1 foo 0
105 1 14 #text 0
106 1 1 label 0
107 2 3 #text 0
108 1 15 label 0
109 1 14 #text 0
110 1 1 item 0
111 2 3 #text 0
112 1 15 item 0
113 1 14 #text 0
114 0 15 foo 0
116 result = ""
118 reader.NewDoc(docstr, "test3", None, 0)
119 ret = reader.Read()
120 while ret == 1:
121 processNode(reader)
122 ret = reader.Read()
124 if ret != 0:
125 print("Error parsing the document test3")
126 sys.exit(1)
128 if result != expect:
129 print("Unexpected result for test3")
130 print(result)
131 sys.exit(1)
134 # cleanup
136 del reader
138 # Memory debug specific
139 libxml2.cleanupParser()
140 if libxml2.debugMemory(1) == 0:
141 print("OK")
142 else:
143 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
144 libxml2.dumpMemory()