outbuf.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/python -u
  2. import sys
  3. import libxml2
  4. import StringIO
  5. def testSimpleBufferWrites():
  6. f = StringIO.StringIO()
  7. buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
  8. buf.write(3, "foo")
  9. buf.writeString("bar")
  10. buf.close()
  11. if f.getvalue() != "foobar":
  12. print
  13. "Failed to save to StringIO"
  14. sys.exit(1)
  15. def testSaveDocToBuffer():
  16. """
  17. Regression test for bug #154294.
  18. """
  19. input = '<foo>Hello</foo>'
  20. expected = '''\
  21. <?xml version="1.0" encoding="UTF-8"?>
  22. <foo>Hello</foo>
  23. '''
  24. f = StringIO.StringIO()
  25. buf = libxml2.createOutputBuffer(f, 'UTF-8')
  26. doc = libxml2.parseDoc(input)
  27. doc.saveFileTo(buf, 'UTF-8')
  28. doc.freeDoc()
  29. def testSaveFormattedDocToBuffer():
  30. input = '<outer><inner>Some text</inner><inner/></outer>'
  31. # The formatted and non-formatted versions of the output.
  32. expected = ('''\
  33. <?xml version="1.0" encoding="UTF-8"?>
  34. <outer><inner>Some text</inner><inner/></outer>
  35. ''', '''\
  36. <?xml version="1.0" encoding="UTF-8"?>
  37. <outer>
  38. <inner>Some text</inner>
  39. <inner/>
  40. </outer>
  41. ''')
  42. doc = libxml2.parseDoc(input)
  43. for i in (0, 1):
  44. f = StringIO.StringIO()
  45. buf = libxml2.createOutputBuffer(f, 'UTF-8')
  46. doc.saveFormatFileTo(buf, 'UTF-8', i)
  47. if f.getvalue() != expected[i]:
  48. print
  49. 'xmlDoc.saveFormatFileTo() call failed.'
  50. print
  51. ' got: %s' % repr(f.getvalue())
  52. print
  53. 'expected: %s' % repr(expected[i])
  54. sys.exit(1)
  55. doc.freeDoc()
  56. def testSaveIntoOutputBuffer():
  57. """
  58. Similar to the previous two tests, except this time we invoke the save
  59. methods on the output buffer object and pass in an XML node object.
  60. """
  61. input = '<foo>Hello</foo>'
  62. expected = '''\
  63. <?xml version="1.0" encoding="UTF-8"?>
  64. <foo>Hello</foo>
  65. '''
  66. f = StringIO.StringIO()
  67. doc = libxml2.parseDoc(input)
  68. buf = libxml2.createOutputBuffer(f, 'UTF-8')
  69. buf.saveFileTo(doc, 'UTF-8')
  70. if f.getvalue() != expected:
  71. print
  72. 'outputBuffer.saveFileTo() call failed.'
  73. print
  74. ' got: %s' % repr(f.getvalue())
  75. print
  76. 'expected: %s' % repr(expected)
  77. sys.exit(1)
  78. f = StringIO.StringIO()
  79. buf = libxml2.createOutputBuffer(f, 'UTF-8')
  80. buf.saveFormatFileTo(doc, 'UTF-8', 1)
  81. if f.getvalue() != expected:
  82. print
  83. 'outputBuffer.saveFormatFileTo() call failed.'
  84. print
  85. ' got: %s' % repr(f.getvalue())
  86. print
  87. 'expected: %s' % repr(expected)
  88. sys.exit(1)
  89. doc.freeDoc()
  90. if __name__ == '__main__':
  91. # Memory debug specific
  92. libxml2.debugMemory(1)
  93. testSimpleBufferWrites()
  94. testSaveDocToBuffer()
  95. testSaveFormattedDocToBuffer()
  96. testSaveIntoOutputBuffer()
  97. libxml2.cleanupParser()
  98. if libxml2.debugMemory(1) == 0:
  99. print
  100. "OK"
  101. else:
  102. print
  103. "Memory leak %d bytes" % (libxml2.debugMemory(1))
  104. libxml2.dumpMemory()