6 str_io = StringIO.StringIO
11 def testSimpleBufferWrites():
13 buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
15 buf.writeString("bar")
18 if f.getvalue() != "foobar":
19 print("Failed to save to StringIO")
22 def testSaveDocToBuffer():
24 Regression test for bug #154294.
26 input = '<foo>Hello</foo>'
28 <?xml version="1.0" encoding="UTF-8"?>
32 buf = libxml2.createOutputBuffer(f, 'UTF-8')
33 doc = libxml2.parseDoc(input)
34 doc.saveFileTo(buf, 'UTF-8')
36 if f.getvalue() != expected:
37 print('xmlDoc.saveFileTo() call failed.')
38 print(' got: %s' % repr(f.getvalue()))
39 print('expected: %s' % repr(expected))
42 def testSaveFormattedDocToBuffer():
43 input = '<outer><inner>Some text</inner><inner/></outer>'
44 # The formatted and non-formatted versions of the output.
46 <?xml version="1.0" encoding="UTF-8"?>
47 <outer><inner>Some text</inner><inner/></outer>
49 <?xml version="1.0" encoding="UTF-8"?>
51 <inner>Some text</inner>
55 doc = libxml2.parseDoc(input)
58 buf = libxml2.createOutputBuffer(f, 'UTF-8')
59 doc.saveFormatFileTo(buf, 'UTF-8', i)
60 if f.getvalue() != expected[i]:
61 print('xmlDoc.saveFormatFileTo() call failed.')
62 print(' got: %s' % repr(f.getvalue()))
63 print('expected: %s' % repr(expected[i]))
67 def testSaveIntoOutputBuffer():
69 Similar to the previous two tests, except this time we invoke the save
70 methods on the output buffer object and pass in an XML node object.
72 input = '<foo>Hello</foo>'
74 <?xml version="1.0" encoding="UTF-8"?>
78 doc = libxml2.parseDoc(input)
79 buf = libxml2.createOutputBuffer(f, 'UTF-8')
80 buf.saveFileTo(doc, 'UTF-8')
81 if f.getvalue() != expected:
82 print('outputBuffer.saveFileTo() call failed.')
83 print(' got: %s' % repr(f.getvalue()))
84 print('expected: %s' % repr(expected))
87 buf = libxml2.createOutputBuffer(f, 'UTF-8')
88 buf.saveFormatFileTo(doc, 'UTF-8', 1)
89 if f.getvalue() != expected:
90 print('outputBuffer.saveFormatFileTo() call failed.')
91 print(' got: %s' % repr(f.getvalue()))
92 print('expected: %s' % repr(expected))
96 if __name__ == '__main__':
97 # Memory debug specific
98 libxml2.debugMemory(1)
100 testSimpleBufferWrites()
101 testSaveDocToBuffer()
102 testSaveFormattedDocToBuffer()
103 testSaveIntoOutputBuffer()
105 libxml2.cleanupParser()
106 if libxml2.debugMemory(1) == 0:
109 print("Memory leak %d bytes" % (libxml2.debugMemory(1)))