Return to outbuf.py CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / libxml2 / python / tests |
1.1 ! misho 1: #!/usr/bin/python -u ! 2: import sys ! 3: import libxml2 ! 4: import StringIO ! 5: ! 6: def 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) ! 16: ! 17: def 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) ! 36: ! 37: def 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() ! 61: ! 62: def 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() ! 90: ! 91: if __name__ == '__main__': ! 92: # Memory debug specific ! 93: libxml2.debugMemory(1) ! 94: ! 95: testSimpleBufferWrites() ! 96: testSaveDocToBuffer() ! 97: testSaveFormattedDocToBuffer() ! 98: testSaveIntoOutputBuffer() ! 99: ! 100: 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()