Annotation of embedaddon/libxml2/python/tests/reader.py, revision 1.1.1.2

1.1       misho       1: #!/usr/bin/python -u
                      2: # -*- coding: ISO-8859-1 -*-
                      3: #
                      4: # this tests the basic APIs of the XmlTextReader interface
                      5: #
                      6: import libxml2
                      7: import sys
1.1.1.2 ! misho       8: try:
        !             9:     import StringIO
        !            10:     str_io = StringIO.StringIO
        !            11: except:
        !            12:     import io
        !            13:     str_io = io.StringIO
1.1       misho      14: 
                     15: # Memory debug specific
                     16: libxml2.debugMemory(1)
                     17: 
1.1.1.2 ! misho      18: f = str_io("""<a><b b1="b1"/><c>content of c</c></a>""")
1.1       misho      19: input = libxml2.inputBuffer(f)
                     20: reader = input.newTextReader("test1")
                     21: ret = reader.Read()
                     22: if ret != 1:
1.1.1.2 ! misho      23:     print("test1: Error reading to first element")
1.1       misho      24:     sys.exit(1)
                     25: if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
                     26:    reader.NodeType() != 1 or reader.HasAttributes() != 0:
1.1.1.2 ! misho      27:     print("test1: Error reading the first element")
1.1       misho      28:     sys.exit(1)
                     29: ret = reader.Read()
                     30: if ret != 1:
1.1.1.2 ! misho      31:     print("test1: Error reading to second element")
1.1       misho      32:     sys.exit(1)
                     33: if reader.Name() != "b" or reader.IsEmptyElement() != 1 or \
                     34:    reader.NodeType() != 1 or reader.HasAttributes() != 1:
1.1.1.2 ! misho      35:     print("test1: Error reading the second element")
1.1       misho      36:     sys.exit(1)
                     37: ret = reader.Read()
                     38: if ret != 1:
1.1.1.2 ! misho      39:     print("test1: Error reading to third element")
1.1       misho      40:     sys.exit(1)
                     41: if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \
                     42:    reader.NodeType() != 1 or reader.HasAttributes() != 0:
1.1.1.2 ! misho      43:     print("test1: Error reading the third element")
1.1       misho      44:     sys.exit(1)
                     45: ret = reader.Read()
                     46: if ret != 1:
1.1.1.2 ! misho      47:     print("test1: Error reading to text node")
1.1       misho      48:     sys.exit(1)
                     49: if reader.Name() != "#text" or reader.IsEmptyElement() != 0 or \
                     50:    reader.NodeType() != 3 or reader.HasAttributes() != 0 or \
                     51:    reader.Value() != "content of c":
1.1.1.2 ! misho      52:     print("test1: Error reading the text node")
1.1       misho      53:     sys.exit(1)
                     54: ret = reader.Read()
                     55: if ret != 1:
1.1.1.2 ! misho      56:     print("test1: Error reading to end of third element")
1.1       misho      57:     sys.exit(1)
                     58: if reader.Name() != "c" or reader.IsEmptyElement() != 0 or \
                     59:    reader.NodeType() != 15 or reader.HasAttributes() != 0:
1.1.1.2 ! misho      60:     print("test1: Error reading the end of third element")
1.1       misho      61:     sys.exit(1)
                     62: ret = reader.Read()
                     63: if ret != 1:
1.1.1.2 ! misho      64:     print("test1: Error reading to end of first element")
1.1       misho      65:     sys.exit(1)
                     66: if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
                     67:    reader.NodeType() != 15 or reader.HasAttributes() != 0:
1.1.1.2 ! misho      68:     print("test1: Error reading the end of first element")
1.1       misho      69:     sys.exit(1)
                     70: ret = reader.Read()
                     71: if ret != 0:
1.1.1.2 ! misho      72:     print("test1: Error reading to end of document")
1.1       misho      73:     sys.exit(1)
                     74: 
                     75: #
                     76: # example from the XmlTextReader docs
                     77: #
1.1.1.2 ! misho      78: f = str_io("""<test xmlns:dt="urn:datatypes" dt:type="int"/>""")
1.1       misho      79: input = libxml2.inputBuffer(f)
                     80: reader = input.newTextReader("test2")
                     81: 
                     82: ret = reader.Read()
                     83: if ret != 1:
1.1.1.2 ! misho      84:     print("Error reading test element")
1.1       misho      85:     sys.exit(1)
                     86: if reader.GetAttributeNo(0) != "urn:datatypes" or \
                     87:    reader.GetAttributeNo(1) != "int" or \
                     88:    reader.GetAttributeNs("type", "urn:datatypes") != "int" or \
                     89:    reader.GetAttribute("dt:type") != "int":
1.1.1.2 ! misho      90:     print("error reading test attributes")
1.1       misho      91:     sys.exit(1)
                     92: 
                     93: #
                     94: # example from the XmlTextReader docs
                     95: #
1.1.1.2 ! misho      96: f = str_io("""<root xmlns:a="urn:456">
1.1       misho      97: <item>
                     98: <ref href="a:b"/>
                     99: </item>
                    100: </root>""")
                    101: input = libxml2.inputBuffer(f)
                    102: reader = input.newTextReader("test3")
                    103: 
                    104: ret = reader.Read()
                    105: while ret == 1:
                    106:     if reader.Name() == "ref":
                    107:         if reader.LookupNamespace("a") != "urn:456":
1.1.1.2 ! misho     108:             print("error resolving namespace prefix")
1.1       misho     109:             sys.exit(1)
                    110:         break
                    111:     ret = reader.Read()
                    112: if ret != 1:
1.1.1.2 ! misho     113:     print("Error finding the ref element")
1.1       misho     114:     sys.exit(1)
                    115: 
                    116: #
                    117: # Home made example for the various attribute access functions
                    118: #
1.1.1.2 ! misho     119: f = str_io("""<testattr xmlns="urn:1" xmlns:a="urn:2" b="b" a:b="a:b"/>""")
1.1       misho     120: input = libxml2.inputBuffer(f)
                    121: reader = input.newTextReader("test4")
                    122: ret = reader.Read()
                    123: if ret != 1:
1.1.1.2 ! misho     124:     print("Error reading the testattr element")
1.1       misho     125:     sys.exit(1)
                    126: #
                    127: # Attribute exploration by index
                    128: #
                    129: if reader.MoveToAttributeNo(0) != 1:
1.1.1.2 ! misho     130:     print("Failed moveToAttribute(0)")
1.1       misho     131:     sys.exit(1)
                    132: if reader.Value() != "urn:1":
1.1.1.2 ! misho     133:     print("Failed to read attribute(0)")
1.1       misho     134:     sys.exit(1)
                    135: if reader.Name() != "xmlns":
1.1.1.2 ! misho     136:     print("Failed to read attribute(0) name")
1.1       misho     137:     sys.exit(1)
                    138: if reader.MoveToAttributeNo(1) != 1:
1.1.1.2 ! misho     139:     print("Failed moveToAttribute(1)")
1.1       misho     140:     sys.exit(1)
                    141: if reader.Value() != "urn:2":
1.1.1.2 ! misho     142:     print("Failed to read attribute(1)")
1.1       misho     143:     sys.exit(1)
                    144: if reader.Name() != "xmlns:a":
1.1.1.2 ! misho     145:     print("Failed to read attribute(1) name")
1.1       misho     146:     sys.exit(1)
                    147: if reader.MoveToAttributeNo(2) != 1:
1.1.1.2 ! misho     148:     print("Failed moveToAttribute(2)")
1.1       misho     149:     sys.exit(1)
                    150: if reader.Value() != "b":
1.1.1.2 ! misho     151:     print("Failed to read attribute(2)")
1.1       misho     152:     sys.exit(1)
                    153: if reader.Name() != "b":
1.1.1.2 ! misho     154:     print("Failed to read attribute(2) name")
1.1       misho     155:     sys.exit(1)
                    156: if reader.MoveToAttributeNo(3) != 1:
1.1.1.2 ! misho     157:     print("Failed moveToAttribute(3)")
1.1       misho     158:     sys.exit(1)
                    159: if reader.Value() != "a:b":
1.1.1.2 ! misho     160:     print("Failed to read attribute(3)")
1.1       misho     161:     sys.exit(1)
                    162: if reader.Name() != "a:b":
1.1.1.2 ! misho     163:     print("Failed to read attribute(3) name")
1.1       misho     164:     sys.exit(1)
                    165: #
                    166: # Attribute exploration by name
                    167: #
                    168: if reader.MoveToAttribute("xmlns") != 1:
1.1.1.2 ! misho     169:     print("Failed moveToAttribute('xmlns')")
1.1       misho     170:     sys.exit(1)
                    171: if reader.Value() != "urn:1":
1.1.1.2 ! misho     172:     print("Failed to read attribute('xmlns')")
1.1       misho     173:     sys.exit(1)
                    174: if reader.MoveToAttribute("xmlns:a") != 1:
1.1.1.2 ! misho     175:     print("Failed moveToAttribute('xmlns')")
1.1       misho     176:     sys.exit(1)
                    177: if reader.Value() != "urn:2":
1.1.1.2 ! misho     178:     print("Failed to read attribute('xmlns:a')")
1.1       misho     179:     sys.exit(1)
                    180: if reader.MoveToAttribute("b") != 1:
1.1.1.2 ! misho     181:     print("Failed moveToAttribute('b')")
1.1       misho     182:     sys.exit(1)
                    183: if reader.Value() != "b":
1.1.1.2 ! misho     184:     print("Failed to read attribute('b')")
1.1       misho     185:     sys.exit(1)
                    186: if reader.MoveToAttribute("a:b") != 1:
1.1.1.2 ! misho     187:     print("Failed moveToAttribute('a:b')")
1.1       misho     188:     sys.exit(1)
                    189: if reader.Value() != "a:b":
1.1.1.2 ! misho     190:     print("Failed to read attribute('a:b')")
1.1       misho     191:     sys.exit(1)
                    192: if reader.MoveToAttributeNs("b", "urn:2") != 1:
1.1.1.2 ! misho     193:     print("Failed moveToAttribute('b', 'urn:2')")
1.1       misho     194:     sys.exit(1)
                    195: if reader.Value() != "a:b":
1.1.1.2 ! misho     196:     print("Failed to read attribute('b', 'urn:2')")
1.1       misho     197:     sys.exit(1)
                    198: #
                    199: # Go back and read in sequence
                    200: #
                    201: if reader.MoveToElement() != 1:
1.1.1.2 ! misho     202:     print("Failed to move back to element")
1.1       misho     203:     sys.exit(1)
                    204: if reader.MoveToFirstAttribute() != 1:
1.1.1.2 ! misho     205:     print("Failed to move to first attribute")
1.1       misho     206:     sys.exit(1)
                    207: if reader.Value() != "urn:1":
1.1.1.2 ! misho     208:     print("Failed to read attribute(0)")
1.1       misho     209:     sys.exit(1)
                    210: if reader.Name() != "xmlns":
1.1.1.2 ! misho     211:     print("Failed to read attribute(0) name")
1.1       misho     212:     sys.exit(1)
                    213: if reader.MoveToNextAttribute() != 1:
1.1.1.2 ! misho     214:     print("Failed to move to next attribute")
1.1       misho     215:     sys.exit(1)
                    216: if reader.Value() != "urn:2":
1.1.1.2 ! misho     217:     print("Failed to read attribute(1)")
1.1       misho     218:     sys.exit(1)
                    219: if reader.Name() != "xmlns:a":
1.1.1.2 ! misho     220:     print("Failed to read attribute(1) name")
1.1       misho     221:     sys.exit(1)
                    222: if reader.MoveToNextAttribute() != 1:
1.1.1.2 ! misho     223:     print("Failed to move to next attribute")
1.1       misho     224:     sys.exit(1)
                    225: if reader.Value() != "b":
1.1.1.2 ! misho     226:     print("Failed to read attribute(2)")
1.1       misho     227:     sys.exit(1)
                    228: if reader.Name() != "b":
1.1.1.2 ! misho     229:     print("Failed to read attribute(2) name")
1.1       misho     230:     sys.exit(1)
                    231: if reader.MoveToNextAttribute() != 1:
1.1.1.2 ! misho     232:     print("Failed to move to next attribute")
1.1       misho     233:     sys.exit(1)
                    234: if reader.Value() != "a:b":
1.1.1.2 ! misho     235:     print("Failed to read attribute(3)")
1.1       misho     236:     sys.exit(1)
                    237: if reader.Name() != "a:b":
1.1.1.2 ! misho     238:     print("Failed to read attribute(3) name")
1.1       misho     239:     sys.exit(1)
                    240: if reader.MoveToNextAttribute() != 0:
1.1.1.2 ! misho     241:     print("Failed to detect last attribute")
1.1       misho     242:     sys.exit(1)
                    243: 
                    244:     
                    245: #
                    246: # a couple of tests for namespace nodes
                    247: #
1.1.1.2 ! misho     248: f = str_io("""<a xmlns="http://example.com/foo"/>""")
1.1       misho     249: input = libxml2.inputBuffer(f)
                    250: reader = input.newTextReader("test6")
                    251: ret = reader.Read()
                    252: if ret != 1:
1.1.1.2 ! misho     253:     print("test6: failed to Read()")
1.1       misho     254:     sys.exit(1)
                    255: ret = reader.MoveToFirstAttribute()
                    256: if ret != 1:
1.1.1.2 ! misho     257:     print("test6: failed to MoveToFirstAttribute()")
1.1       misho     258:     sys.exit(1)
                    259: if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/" or \
                    260:    reader.LocalName() != "xmlns" or reader.Name() != "xmlns" or \
                    261:    reader.Value() != "http://example.com/foo" or reader.NodeType() != 2:
1.1.1.2 ! misho     262:     print("test6: failed to read the namespace node")
1.1       misho     263:     sys.exit(1)
                    264: 
1.1.1.2 ! misho     265: f = str_io("""<a xmlns:prefix="http://example.com/foo"/>""")
1.1       misho     266: input = libxml2.inputBuffer(f)
                    267: reader = input.newTextReader("test7")
                    268: ret = reader.Read()
                    269: if ret != 1:
1.1.1.2 ! misho     270:     print("test7: failed to Read()")
1.1       misho     271:     sys.exit(1)
                    272: ret = reader.MoveToFirstAttribute()
                    273: if ret != 1:
1.1.1.2 ! misho     274:     print("test7: failed to MoveToFirstAttribute()")
1.1       misho     275:     sys.exit(1)
                    276: if reader.NamespaceUri() != "http://www.w3.org/2000/xmlns/" or \
                    277:    reader.LocalName() != "prefix" or reader.Name() != "xmlns:prefix" or \
                    278:    reader.Value() != "http://example.com/foo" or reader.NodeType() != 2:
1.1.1.2 ! misho     279:     print("test7: failed to read the namespace node")
1.1       misho     280:     sys.exit(1)
                    281: 
                    282: #
                    283: # Test for a limit case:
                    284: #
1.1.1.2 ! misho     285: f = str_io("""<a/>""")
1.1       misho     286: input = libxml2.inputBuffer(f)
                    287: reader = input.newTextReader("test8")
                    288: ret = reader.Read()
                    289: if ret != 1:
1.1.1.2 ! misho     290:     print("test8: failed to read the node")
1.1       misho     291:     sys.exit(1)
                    292: if reader.Name() != "a" or reader.IsEmptyElement() != 1:
1.1.1.2 ! misho     293:     print("test8: failed to analyze the node")
1.1       misho     294:     sys.exit(1)
                    295: ret = reader.Read()
                    296: if ret != 0:
1.1.1.2 ! misho     297:     print("test8: failed to detect the EOF")
1.1       misho     298:     sys.exit(1)
                    299: 
                    300: #
                    301: # Another test provided by Stéphane Bidoul and checked with C#
                    302: #
                    303: def tst_reader(s):
1.1.1.2 ! misho     304:     f = str_io(s)
1.1       misho     305:     input = libxml2.inputBuffer(f)
                    306:     reader = input.newTextReader("tst")
                    307:     res = ""
                    308:     while reader.Read():
                    309:         res=res + "%s (%s) [%s] %d %d\n" % (reader.NodeType(),reader.Name(),
                    310:                                       reader.Value(), reader.IsEmptyElement(),
                    311:                                       reader.Depth())
                    312:         if reader.NodeType() == 1: # Element
                    313:             while reader.MoveToNextAttribute():
                    314:                 res = res + "-- %s (%s) [%s] %d %d\n" % (reader.NodeType(),
                    315:                                        reader.Name(),reader.Value(),
                    316:                                        reader.IsEmptyElement(), reader.Depth())
                    317:     return res
                    318:     
                    319: doc="""<a><b b1="b1"/><c>content of c</c></a>"""
                    320: expect="""1 (a) [None] 0 0
                    321: 1 (b) [None] 1 1
                    322: -- 2 (b1) [b1] 0 2
                    323: 1 (c) [None] 0 1
                    324: 3 (#text) [content of c] 0 2
                    325: 15 (c) [None] 0 1
                    326: 15 (a) [None] 0 0
                    327: """
                    328: res = tst_reader(doc)
                    329: if res != expect:
1.1.1.2 ! misho     330:     print("test5 failed")
        !           331:     print(res)
1.1       misho     332:     sys.exit(1)
                    333: 
                    334: doc="""<test><b/><c/></test>"""
                    335: expect="""1 (test) [None] 0 0
                    336: 1 (b) [None] 1 1
                    337: 1 (c) [None] 1 1
                    338: 15 (test) [None] 0 0
                    339: """
                    340: res = tst_reader(doc)
                    341: if res != expect:
1.1.1.2 ! misho     342:     print("test9 failed")
        !           343:     print(res)
1.1       misho     344:     sys.exit(1)
                    345: 
                    346: doc="""<a><b>bbb</b><c>ccc</c></a>"""
                    347: expect="""1 (a) [None] 0 0
                    348: 1 (b) [None] 0 1
                    349: 3 (#text) [bbb] 0 2
                    350: 15 (b) [None] 0 1
                    351: 1 (c) [None] 0 1
                    352: 3 (#text) [ccc] 0 2
                    353: 15 (c) [None] 0 1
                    354: 15 (a) [None] 0 0
                    355: """
                    356: res = tst_reader(doc)
                    357: if res != expect:
1.1.1.2 ! misho     358:     print("test10 failed")
        !           359:     print(res)
1.1       misho     360:     sys.exit(1)
                    361: 
                    362: doc="""<test a="a"/>"""
                    363: expect="""1 (test) [None] 1 0
                    364: -- 2 (a) [a] 0 1
                    365: """
                    366: res = tst_reader(doc)
                    367: if res != expect:
1.1.1.2 ! misho     368:     print("test11 failed")
        !           369:     print(res)
1.1       misho     370:     sys.exit(1)
                    371: 
                    372: doc="""<test><a>aaa</a><b/></test>"""
                    373: expect="""1 (test) [None] 0 0
                    374: 1 (a) [None] 0 1
                    375: 3 (#text) [aaa] 0 2
                    376: 15 (a) [None] 0 1
                    377: 1 (b) [None] 1 1
                    378: 15 (test) [None] 0 0
                    379: """
                    380: res = tst_reader(doc)
                    381: if res != expect:
1.1.1.2 ! misho     382:     print("test12 failed")
        !           383:     print(res)
1.1       misho     384:     sys.exit(1)
                    385: 
                    386: doc="""<test><p></p></test>"""
                    387: expect="""1 (test) [None] 0 0
                    388: 1 (p) [None] 0 1
                    389: 15 (p) [None] 0 1
                    390: 15 (test) [None] 0 0
                    391: """
                    392: res = tst_reader(doc)
                    393: if res != expect:
1.1.1.2 ! misho     394:     print("test13 failed")
        !           395:     print(res)
1.1       misho     396:     sys.exit(1)
                    397: 
                    398: doc="""<p></p>"""
                    399: expect="""1 (p) [None] 0 0
                    400: 15 (p) [None] 0 0
                    401: """
                    402: res = tst_reader(doc)
                    403: if res != expect:
1.1.1.2 ! misho     404:     print("test14 failed")
        !           405:     print(res)
1.1       misho     406:     sys.exit(1)
                    407: 
                    408: #
                    409: # test from bug #108801 
                    410: #
                    411: doc="""<?xml version="1.0" standalone="no"?>
                    412: <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
                    413:                   "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
                    414: ]>
                    415: 
                    416: <article>
                    417: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                    418: </article>
                    419: """
                    420: expect="""10 (article) [None] 0 0
                    421: 1 (article) [None] 0 0
                    422: 3 (#text) [
                    423: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
                    424: ] 0 1
                    425: 15 (article) [None] 0 0
                    426: """
                    427: res = tst_reader(doc)
                    428: if res != expect:
1.1.1.2 ! misho     429:     print("test15 failed")
        !           430:     print(res)
1.1       misho     431:     sys.exit(1)
                    432: 
                    433: #
                    434: # cleanup for memory allocation counting
                    435: #
                    436: del f
                    437: del input
                    438: del reader
                    439: 
                    440: # Memory debug specific
                    441: libxml2.cleanupParser()
                    442: if libxml2.debugMemory(1) == 0:
1.1.1.2 ! misho     443:     print("OK")
1.1       misho     444: else:
1.1.1.2 ! misho     445:     print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
1.1       misho     446:     libxml2.dumpMemory()

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>