blob: f2d6dbf7be493499dcdb4d91c6957c289f3cc03b [file] [log] [blame]
Martin v. Löwisb96e0e52000-09-19 16:35:39 +00001# Tests StringIO and cStringIO
2
3import string
4
5def do_test(module):
6 s = (string.letters+'\n')*5
7 f = module.StringIO(s)
8 print f.read(10)
9 print f.readline()
10 print len(f.readlines(60))
11
Fred Drake95438332000-09-28 04:25:33 +000012 f = module.StringIO()
13 f.write(s)
14 f.seek(10)
15 f.truncate()
16 print `f.getvalue()`
Jim Fultond1229f52000-10-06 19:21:32 +000017 f.seek(0)
18 f.truncate(5)
19 print `f.getvalue()`
20 f.close()
21 try:
22 f.write("frobnitz")
23 except ValueError, e:
24 print "Caught expected ValueError writing to closed StringIO:"
25 print e
26 else:
27 print "Failed to catch ValueError writing to closed StringIO."
Fred Drake95438332000-09-28 04:25:33 +000028
Martin v. Löwisb96e0e52000-09-19 16:35:39 +000029# Don't bother testing cStringIO without
30import StringIO, cStringIO
31do_test(StringIO)
32do_test(cStringIO)