xref: /aosp_15_r20/external/libxml2/python/tests/reader4.py (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1#!/usr/bin/env python3
2#
3# this tests the basic APIs of the XmlTextReader interface
4#
5import setup_test
6import libxml2
7import sys
8try:
9    import StringIO
10    str_io = StringIO.StringIO
11except:
12    import io
13    str_io = io.StringIO
14
15# Memory debug specific
16libxml2.debugMemory(1)
17
18def 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
32expect="""1 (test) [None] 0
331 (b) [None] 1
341 (c) [None] 1
3515 (test) [None] 0
36"""
37
38res = tst_reader("""<test><b/><c/></test>""")
39
40if res != expect:
41    print("Did not get the expected error message:")
42    print(res)
43    sys.exit(1)
44
45# Memory debug specific
46libxml2.cleanupParser()
47if libxml2.debugMemory(1) == 0:
48    print("OK")
49else:
50    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
51