Annotation of embedaddon/libxml2/python/tests/outbuf.py, revision 1.1.1.2
1.1 misho 1: #!/usr/bin/python -u
2: import sys
3: import libxml2
1.1.1.2 ! misho 4: try:
! 5: import StringIO
! 6: str_io = StringIO.StringIO
! 7: except:
! 8: import io
! 9: str_io = io.StringIO
1.1 misho 10:
11: def testSimpleBufferWrites():
1.1.1.2 ! misho 12: f = str_io()
1.1 misho 13: buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
14: buf.write(3, "foo")
15: buf.writeString("bar")
16: buf.close()
1.1.1.2 ! misho 17:
1.1 misho 18: if f.getvalue() != "foobar":
1.1.1.2 ! misho 19: print("Failed to save to StringIO")
1.1 misho 20: sys.exit(1)
21:
22: def 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: '''
1.1.1.2 ! misho 31: f = str_io()
1.1 misho 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:
1.1.1.2 ! misho 37: print('xmlDoc.saveFileTo() call failed.')
! 38: print(' got: %s' % repr(f.getvalue()))
! 39: print('expected: %s' % repr(expected))
1.1 misho 40: sys.exit(1)
41:
42: def 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):
1.1.1.2 ! misho 57: f = str_io()
1.1 misho 58: buf = libxml2.createOutputBuffer(f, 'UTF-8')
59: doc.saveFormatFileTo(buf, 'UTF-8', i)
60: if f.getvalue() != expected[i]:
1.1.1.2 ! misho 61: print('xmlDoc.saveFormatFileTo() call failed.')
! 62: print(' got: %s' % repr(f.getvalue()))
! 63: print('expected: %s' % repr(expected[i]))
1.1 misho 64: sys.exit(1)
65: doc.freeDoc()
66:
67: def 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: '''
1.1.1.2 ! misho 77: f = str_io()
1.1 misho 78: doc = libxml2.parseDoc(input)
79: buf = libxml2.createOutputBuffer(f, 'UTF-8')
80: buf.saveFileTo(doc, 'UTF-8')
81: if f.getvalue() != expected:
1.1.1.2 ! misho 82: print('outputBuffer.saveFileTo() call failed.')
! 83: print(' got: %s' % repr(f.getvalue()))
! 84: print('expected: %s' % repr(expected))
1.1 misho 85: sys.exit(1)
1.1.1.2 ! misho 86: f = str_io()
1.1 misho 87: buf = libxml2.createOutputBuffer(f, 'UTF-8')
88: buf.saveFormatFileTo(doc, 'UTF-8', 1)
89: if f.getvalue() != expected:
1.1.1.2 ! misho 90: print('outputBuffer.saveFormatFileTo() call failed.')
! 91: print(' got: %s' % repr(f.getvalue()))
! 92: print('expected: %s' % repr(expected))
1.1 misho 93: sys.exit(1)
94: doc.freeDoc()
95:
96: if __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:
1.1.1.2 ! misho 107: print("OK")
1.1 misho 108: else:
1.1.1.2 ! misho 109: print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
1.1 misho 110: libxml2.dumpMemory()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>