1#!/usr/bin/env python3 2import sys 3import setup_test 4import libxml2 5try: 6 import StringIO 7 str_io = StringIO.StringIO 8except: 9 import io 10 str_io = io.StringIO 11 12def testSimpleBufferWrites(): 13 f = str_io() 14 buf = libxml2.createOutputBuffer(f, "ISO-8859-1") 15 buf.write(3, "foo") 16 buf.writeString("bar") 17 buf.close() 18 19 if f.getvalue() != "foobar": 20 print("Failed to save to StringIO") 21 sys.exit(1) 22 23def testSaveDocToBuffer(): 24 """ 25 Regression test for bug #154294. 26 """ 27 input = '<foo>Hello</foo>' 28 expected = '''\ 29<?xml version="1.0" encoding="UTF-8"?> 30<foo>Hello</foo> 31''' 32 f = str_io() 33 buf = libxml2.createOutputBuffer(f, 'UTF-8') 34 doc = libxml2.parseDoc(input) 35 doc.saveFileTo(buf, 'UTF-8') 36 doc.freeDoc() 37 if f.getvalue() != expected: 38 print('xmlDoc.saveFileTo() call failed.') 39 print(' got: %s' % repr(f.getvalue())) 40 print('expected: %s' % repr(expected)) 41 sys.exit(1) 42 43def testSaveFormattedDocToBuffer(): 44 input = '<outer><inner>Some text</inner><inner/></outer>' 45 # The formatted and non-formatted versions of the output. 46 expected = ('''\ 47<?xml version="1.0" encoding="UTF-8"?> 48<outer><inner>Some text</inner><inner/></outer> 49''', '''\ 50<?xml version="1.0" encoding="UTF-8"?> 51<outer> 52 <inner>Some text</inner> 53 <inner/> 54</outer> 55''') 56 doc = libxml2.parseDoc(input) 57 for i in (0, 1): 58 f = str_io() 59 buf = libxml2.createOutputBuffer(f, 'UTF-8') 60 doc.saveFormatFileTo(buf, 'UTF-8', i) 61 if f.getvalue() != expected[i]: 62 print('xmlDoc.saveFormatFileTo() call failed.') 63 print(' got: %s' % repr(f.getvalue())) 64 print('expected: %s' % repr(expected[i])) 65 sys.exit(1) 66 doc.freeDoc() 67 68def testSaveIntoOutputBuffer(): 69 """ 70 Similar to the previous two tests, except this time we invoke the save 71 methods on the output buffer object and pass in an XML node object. 72 """ 73 input = '<foo>Hello</foo>' 74 expected = '''\ 75<?xml version="1.0" encoding="UTF-8"?> 76<foo>Hello</foo> 77''' 78 f = str_io() 79 doc = libxml2.parseDoc(input) 80 buf = libxml2.createOutputBuffer(f, 'UTF-8') 81 buf.saveFileTo(doc, 'UTF-8') 82 if f.getvalue() != expected: 83 print('outputBuffer.saveFileTo() call failed.') 84 print(' got: %s' % repr(f.getvalue())) 85 print('expected: %s' % repr(expected)) 86 sys.exit(1) 87 f = str_io() 88 buf = libxml2.createOutputBuffer(f, 'UTF-8') 89 buf.saveFormatFileTo(doc, 'UTF-8', 1) 90 if f.getvalue() != expected: 91 print('outputBuffer.saveFormatFileTo() call failed.') 92 print(' got: %s' % repr(f.getvalue())) 93 print('expected: %s' % repr(expected)) 94 sys.exit(1) 95 doc.freeDoc() 96 97if __name__ == '__main__': 98 # Memory debug specific 99 libxml2.debugMemory(1) 100 101 testSimpleBufferWrites() 102 testSaveDocToBuffer() 103 testSaveFormattedDocToBuffer() 104 testSaveIntoOutputBuffer() 105 106 libxml2.cleanupParser() 107 if libxml2.debugMemory(1) == 0: 108 print("OK") 109 else: 110 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 111