blob: 09cd9b5e57bd3e8832ba78e159ef5ef5572ec493 [file] [log] [blame]
Daniel Veillardc6d4a932002-09-12 15:00:57 +00001#!/usr/bin/python -u
2import sys
3import libxml2
4import StringIO
5
Daniel Veillard263ec862004-10-04 10:26:54 +00006def testSimpleBufferWrites():
7 f = StringIO.StringIO()
8 buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
9 buf.write(3, "foo")
10 buf.writeString("bar")
11 buf.close()
12
13 if f.getvalue() != "foobar":
14 print "Failed to save to StringIO"
15 sys.exit(1)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000016
Daniel Veillard263ec862004-10-04 10:26:54 +000017def testSaveDocToBuffer():
18 """
19 Regression test for bug #154294.
20 """
21 input = '<foo>Hello</foo>'
22 expected = '''\
23<?xml version="1.0" encoding="UTF-8"?>
24<foo>Hello</foo>
25'''
26 f = StringIO.StringIO()
27 buf = libxml2.createOutputBuffer(f, 'UTF-8')
28 doc = libxml2.parseDoc(input)
29 doc.saveFileTo(buf, 'UTF-8')
30 doc.freeDoc()
31 if f.getvalue() != expected:
32 print 'xmlDoc.saveFileTo() call failed.'
33 print ' got: %s' % repr(f.getvalue())
34 print 'expected: %s' % repr(expected)
35 sys.exit(1)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000036
Daniel Veillard263ec862004-10-04 10:26:54 +000037def testSaveFormattedDocToBuffer():
38 input = '<outer><inner>Some text</inner><inner/></outer>'
39 # The formatted and non-formatted versions of the output.
40 expected = ('''\
41<?xml version="1.0" encoding="UTF-8"?>
42<outer><inner>Some text</inner><inner/></outer>
43''', '''\
44<?xml version="1.0" encoding="UTF-8"?>
45<outer>
46 <inner>Some text</inner>
47 <inner/>
48</outer>
49''')
50 doc = libxml2.parseDoc(input)
51 for i in (0, 1):
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])
59 sys.exit(1)
60 doc.freeDoc()
Daniel Veillardc6d4a932002-09-12 15:00:57 +000061
Daniel Veillard263ec862004-10-04 10:26:54 +000062def testSaveIntoOutputBuffer():
63 """
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.
66 """
67 input = '<foo>Hello</foo>'
68 expected = '''\
69<?xml version="1.0" encoding="UTF-8"?>
70<foo>Hello</foo>
71'''
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)
80 sys.exit(1)
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)
88 sys.exit(1)
89 doc.freeDoc()
Daniel Veillardc6d4a932002-09-12 15:00:57 +000090
Daniel Veillard263ec862004-10-04 10:26:54 +000091if __name__ == '__main__':
92 # Memory debug specific
93 libxml2.debugMemory(1)
Daniel Veillardc6d4a932002-09-12 15:00:57 +000094
Daniel Veillard263ec862004-10-04 10:26:54 +000095 testSimpleBufferWrites()
96 testSaveDocToBuffer()
97 testSaveFormattedDocToBuffer()
98 testSaveIntoOutputBuffer()
Daniel Veillardc6d4a932002-09-12 15:00:57 +000099
Daniel Veillard263ec862004-10-04 10:26:54 +0000100 libxml2.cleanupParser()
101 if libxml2.debugMemory(1) == 0:
102 print "OK"
103 else:
104 print "Memory leak %d bytes" % (libxml2.debugMemory(1))
105 libxml2.dumpMemory()