readernext.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/python -u
  2. # -*- coding: ISO-8859-1 -*-
  3. #
  4. # this tests the next API of the XmlTextReader interface
  5. #
  6. import libxml2
  7. import StringIO
  8. import sys
  9. # Memory debug specific
  10. libxml2.debugMemory(1)
  11. f = StringIO.StringIO("""<a><b><c /></b><d>content of d</d></a>""")
  12. input = libxml2.inputBuffer(f)
  13. reader = input.newTextReader("test_next")
  14. ret = reader.Read()
  15. if ret != 1:
  16. print "test_next: Error reading to first element"
  17. sys.exit(1)
  18. if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
  19. reader.NodeType() != 1 or reader.HasAttributes() != 0:
  20. print "test_next: Error reading the first element"
  21. sys.exit(1)
  22. ret = reader.Read()
  23. if ret != 1:
  24. print "test_next: Error reading to second element"
  25. sys.exit(1)
  26. if reader.Name() != "b" or reader.IsEmptyElement() != 0 or \
  27. reader.NodeType() != 1 or reader.HasAttributes() != 0:
  28. print "test_next: Error reading the second element"
  29. sys.exit(1)
  30. ret = reader.Read()
  31. if ret != 1:
  32. print "test_next: Error reading to third element"
  33. sys.exit(1)
  34. if reader.Name() != "c" or reader.NodeType() != 1 or \
  35. reader.HasAttributes() != 0:
  36. print "test_next: Error reading the third element"
  37. sys.exit(1)
  38. ret = reader.Read()
  39. if ret != 1:
  40. print "test_next: Error reading to end of third element"
  41. sys.exit(1)
  42. if reader.Name() != "b" or reader.NodeType() != 15:
  43. print "test_next: Error reading to end of second element"
  44. sys.exit(1)
  45. ret = reader.Next()
  46. if ret != 1:
  47. print "test_next: Error moving to third element"
  48. sys.exit(1)
  49. if reader.Name() != "d" or reader.IsEmptyElement() != 0 or \
  50. reader.NodeType() != 1 or reader.HasAttributes() != 0:
  51. print "test_next: Error reading third element"
  52. sys.exit(1)
  53. ret = reader.Next()
  54. if ret != 1:
  55. print "test_next: Error reading to end of first element"
  56. sys.exit(1)
  57. if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
  58. reader.NodeType() != 15 or reader.HasAttributes() != 0:
  59. print "test_next: Error reading the end of first element"
  60. sys.exit(1)
  61. ret = reader.Read()
  62. if ret != 0:
  63. print "test_next: Error reading to end of document"
  64. sys.exit(1)
  65. #
  66. # cleanup for memory allocation counting
  67. #
  68. del f
  69. del input
  70. del reader
  71. # Memory debug specific
  72. libxml2.cleanupParser()
  73. if libxml2.debugMemory(1) == 0:
  74. print "OK"
  75. else:
  76. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  77. libxml2.dumpMemory()