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