1#!/usr/bin/python -u
2#
3# this tests the entities substitutions with the XmlTextReader interface
4#
5import sys
6import libxml2
7
8# Memory debug specific
9libxml2.debugMemory(1)
10
11result = ""
12def processNode(reader):
13    global result
14
15    result = result + "%d %d %s %d\n" % (reader.Depth(), reader.NodeType(),
16			   reader.Name(), reader.IsEmptyElement())
17
18#
19# Parse a document testing the readerForxxx API
20#
21docstr="""<foo>
22<label>some text</label>
23<item>100</item>
24</foo>"""
25expect="""0 1 foo 0
261 14 #text 0
271 1 label 0
282 3 #text 0
291 15 label 0
301 14 #text 0
311 1 item 0
322 3 #text 0
331 15 item 0
341 14 #text 0
350 15 foo 0
36"""
37result = ""
38
39reader = libxml2.readerForDoc(docstr, "test1", None, 0)
40ret = reader.Read()
41while ret == 1:
42    processNode(reader)
43    ret = reader.Read()
44
45if ret != 0:
46    print("Error parsing the document test1")
47    sys.exit(1)
48
49if result != expect:
50    print("Unexpected result for test1")
51    print(result)
52    sys.exit(1)
53
54#
55# Reuse the reader for another document testing the ReaderNewxxx API
56#
57docstr="""<foo>
58<label>some text</label>
59<item>1000</item>
60</foo>"""
61expect="""0 1 foo 0
621 14 #text 0
631 1 label 0
642 3 #text 0
651 15 label 0
661 14 #text 0
671 1 item 0
682 3 #text 0
691 15 item 0
701 14 #text 0
710 15 foo 0
72"""
73result = ""
74
75reader.NewDoc(docstr, "test2", None, 0)
76ret = reader.Read()
77while ret == 1:
78    processNode(reader)
79    ret = reader.Read()
80
81if ret != 0:
82    print("Error parsing the document test2")
83    sys.exit(1)
84
85if result != expect:
86    print("Unexpected result for test2")
87    print(result)
88    sys.exit(1)
89
90#
91# cleanup
92#
93del reader
94
95# Memory debug specific
96libxml2.cleanupParser()
97if libxml2.debugMemory(1) == 0:
98    print("OK")
99else:
100    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
101    libxml2.dumpMemory()
102