6 def testSimpleBufferWrites():
7 f
= StringIO
.StringIO()
8 buf
= libxml2
.createOutputBuffer(f
, "ISO-8859-1")
10 buf
.writeString("bar")
13 if f
.getvalue() != "foobar":
14 print "Failed to save to StringIO"
17 def testSaveDocToBuffer():
19 Regression test for bug #154294.
21 input = '<foo>Hello</foo>'
23 <?xml version="1.0" encoding="UTF-8"?>
26 f
= StringIO
.StringIO()
27 buf
= libxml2
.createOutputBuffer(f
, 'UTF-8')
28 doc
= libxml2
.parseDoc(input)
29 doc
.saveFileTo(buf
, 'UTF-8')
31 if f
.getvalue() != expected
:
32 print 'xmlDoc.saveFileTo() call failed.'
33 print ' got: %s' % repr(f
.getvalue())
34 print 'expected: %s' % repr(expected
)
37 def testSaveFormattedDocToBuffer():
38 input = '<outer><inner>Some text</inner><inner/></outer>'
39 # The formatted and non-formatted versions of the output.
41 <?xml version="1.0" encoding="UTF-8"?>
42 <outer><inner>Some text</inner><inner/></outer>
44 <?xml version="1.0" encoding="UTF-8"?>
46 <inner>Some text</inner>
50 doc
= libxml2
.parseDoc(input)
52 f
= StringIO
.StringIO()
53 buf
= libxml2
.createOutputBuffer(f
, 'UTF-8')
54 doc
.saveFormatFileTo(buf
, 'UTF-8', i
)
55 if f
.getvalue() != expected
[i
]:
56 print 'xmlDoc.saveFormatFileTo() call failed.'
57 print ' got: %s' % repr(f
.getvalue())
58 print 'expected: %s' % repr(expected
[i
])
62 def testSaveIntoOutputBuffer():
64 Similar to the previous two tests, except this time we invoke the save
65 methods on the output buffer object and pass in an XML node object.
67 input = '<foo>Hello</foo>'
69 <?xml version="1.0" encoding="UTF-8"?>
72 f
= StringIO
.StringIO()
73 doc
= libxml2
.parseDoc(input)
74 buf
= libxml2
.createOutputBuffer(f
, 'UTF-8')
75 buf
.saveFileTo(doc
, 'UTF-8')
76 if f
.getvalue() != expected
:
77 print 'outputBuffer.saveFileTo() call failed.'
78 print ' got: %s' % repr(f
.getvalue())
79 print 'expected: %s' % repr(expected
)
81 f
= StringIO
.StringIO()
82 buf
= libxml2
.createOutputBuffer(f
, 'UTF-8')
83 buf
.saveFormatFileTo(doc
, 'UTF-8', 1)
84 if f
.getvalue() != expected
:
85 print 'outputBuffer.saveFormatFileTo() call failed.'
86 print ' got: %s' % repr(f
.getvalue())
87 print 'expected: %s' % repr(expected
)
91 if __name__
== '__main__':
92 # Memory debug specific
93 libxml2
.debugMemory(1)
95 testSimpleBufferWrites()
97 testSaveFormattedDocToBuffer()
98 testSaveIntoOutputBuffer()
100 libxml2
.cleanupParser()
101 if libxml2
.debugMemory(1) == 0:
104 print "Memory leak %d bytes" % (libxml2
.debugMemory(1))