outbuf.py 3.0 KB

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