1 #!/usr/bin/env python3
2 #
3 # this tests the basic APIs of the XmlTextReader interface
4 #
5 import setup_test
6 import libxml2
7 import sys
8 try:
9 import StringIO
10 str_io = StringIO.StringIO
11 except:
12 import io
13 str_io = io.StringIO
14
15 # Memory debug specific
16 libxml2.debugMemory(1)
17
18 def tst_reader(s):
19 f = str_io(s)
20 input = libxml2.inputBuffer(f)
21 reader = input.newTextReader("tst")
22 res = ""
23 while reader.Read():
24 res=res + "%s (%s) [%s] %d\n" % (reader.NodeType(),reader.Name(),
25 reader.Value(), reader.IsEmptyElement())
26 if reader.NodeType() == 1: # Element
27 while reader.MoveToNextAttribute():
28 res = res + "-- %s (%s) [%s]\n" % (reader.NodeType(),
29 reader.Name(),reader.Value())
30 return res
31
32 expect="""1 (test) [None] 0
33 1 (b) [None] 1
34 1 (c) [None] 1
35 15 (test) [None] 0
36 """
37
38 res = tst_reader("""<test><b/><c/></test>""")
39
40 if res != expect:
41 print("Did not get the expected error message:")
42 print(res)
43 sys.exit(1)
44
45 # Memory debug specific
46 libxml2.cleanupParser()
47 if libxml2.debugMemory(1) == 0:
48 print("OK")
49 else:
50 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))