Jeremy Hylton | a8268e9 | 2000-10-16 17:33:50 +0000 | [diff] [blame] | 1 | # Tests StringIO and cStringIO |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +0000 | [diff] [blame] | 2 | |
Jeremy Hylton | a8268e9 | 2000-10-16 17:33:50 +0000 | [diff] [blame] | 3 | def do_test(module): |
| 4 | s = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+'\n')*5 |
| 5 | f = module.StringIO(s) |
| 6 | print f.read(10) |
| 7 | print f.readline() |
| 8 | print len(f.readlines(60)) |
Guido van Rossum | 8d691c8 | 2000-09-01 19:25:51 +0000 | [diff] [blame] | 9 | |
Jeremy Hylton | a8268e9 | 2000-10-16 17:33:50 +0000 | [diff] [blame] | 10 | f = module.StringIO() |
| 11 | f.write('abcdef') |
| 12 | f.seek(3) |
| 13 | f.write('uvwxyz') |
| 14 | f.write('!') |
| 15 | print `f.getvalue()` |
| 16 | f.close() |
| 17 | f = module.StringIO() |
| 18 | f.write(s) |
| 19 | f.seek(10) |
| 20 | f.truncate() |
| 21 | print `f.getvalue()` |
| 22 | f.seek(0) |
| 23 | f.truncate(5) |
| 24 | print `f.getvalue()` |
| 25 | f.close() |
Guido van Rossum | e8d2f55 | 2000-10-09 22:14:43 +0000 | [diff] [blame] | 26 | try: |
Jeremy Hylton | a8268e9 | 2000-10-16 17:33:50 +0000 | [diff] [blame] | 27 | f.write("frobnitz") |
| 28 | except ValueError, e: |
| 29 | print "Caught expected ValueError writing to closed StringIO:" |
| 30 | print e |
| 31 | else: |
| 32 | print "Failed to catch ValueError writing to closed StringIO." |
Guido van Rossum | e8d2f55 | 2000-10-09 22:14:43 +0000 | [diff] [blame] | 33 | |
Jeremy Hylton | a8268e9 | 2000-10-16 17:33:50 +0000 | [diff] [blame] | 34 | # Don't bother testing cStringIO without |
| 35 | import StringIO, cStringIO |
| 36 | do_test(StringIO) |
| 37 | do_test(cStringIO) |