• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python -u
2import sys
3import libxml2
4try:
5    import StringIO
6    str_io = StringIO.StringIO
7except:
8    import io
9    str_io = io.StringIO
10
11def testSimpleBufferWrites():
12    f = str_io()
13    buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
14    buf.write(3, "foo")
15    buf.writeString("bar")
16    buf.close()
17
18    if f.getvalue() != "foobar":
19        print("Failed to save to StringIO")
20        sys.exit(1)
21
22def testSaveDocToBuffer():
23    """
24    Regression test for bug #154294.
25    """
26    input = '<foo>Hello</foo>'
27    expected = '''\
28<?xml version="1.0" encoding="UTF-8"?>
29<foo>Hello</foo>
30'''
31    f = str_io()
32    buf = libxml2.createOutputBuffer(f, 'UTF-8')
33    doc = libxml2.parseDoc(input)
34    doc.saveFileTo(buf, 'UTF-8')
35    doc.freeDoc()
36    if f.getvalue() != expected:
37        print('xmlDoc.saveFileTo() call failed.')
38        print('     got: %s' % repr(f.getvalue()))
39        print('expected: %s' % repr(expected))
40        sys.exit(1)
41
42def testSaveFormattedDocToBuffer():
43    input = '<outer><inner>Some text</inner><inner/></outer>'
44    # The formatted and non-formatted versions of the output.
45    expected = ('''\
46<?xml version="1.0" encoding="UTF-8"?>
47<outer><inner>Some text</inner><inner/></outer>
48''', '''\
49<?xml version="1.0" encoding="UTF-8"?>
50<outer>
51  <inner>Some text</inner>
52  <inner/>
53</outer>
54''')
55    doc = libxml2.parseDoc(input)
56    for i in (0, 1):
57        f = str_io()
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]))
64            sys.exit(1)
65    doc.freeDoc()
66
67def testSaveIntoOutputBuffer():
68    """
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.
71    """
72    input = '<foo>Hello</foo>'
73    expected = '''\
74<?xml version="1.0" encoding="UTF-8"?>
75<foo>Hello</foo>
76'''
77    f = str_io()
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))
85        sys.exit(1)
86    f = str_io()
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))
93        sys.exit(1)
94    doc.freeDoc()
95
96if __name__ == '__main__':
97    # Memory debug specific
98    libxml2.debugMemory(1)
99
100    testSimpleBufferWrites()
101    testSaveDocToBuffer()
102    testSaveFormattedDocToBuffer()
103    testSaveIntoOutputBuffer()
104
105    libxml2.cleanupParser()
106    if libxml2.debugMemory(1) == 0:
107        print("OK")
108    else:
109        print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
110        libxml2.dumpMemory()
111