blob: 8d3c85142aa396af01156491b0a5fc77ba9ce9e5 [file] [log] [blame]
Martin v. Löwisb96e0e52000-09-19 16:35:39 +00001# Tests StringIO and cStringIO
2
Martin v. Löwisb96e0e52000-09-19 16:35:39 +00003def do_test(module):
Guido van Rossum4bbea052000-10-11 21:34:53 +00004 s = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+'\n')*5
Martin v. Löwisb96e0e52000-09-19 16:35:39 +00005 f = module.StringIO(s)
6 print f.read(10)
7 print f.readline()
8 print len(f.readlines(60))
9
Fred Drake95438332000-09-28 04:25:33 +000010 f = module.StringIO()
Guido van Rossum22d58952000-10-12 16:46:28 +000011 f.write('abcdef')
12 f.seek(3)
13 f.write('uvwxyz')
14 f.write('!')
15 print `f.getvalue()`
16 f.close()
Jeremy Hyltoncafd4952001-02-09 23:44:22 +000017
18 f = module.StringIO()
19 f.writelines(["a", "b", "c"])
20 f.seek(0)
21 print `f.getvalue()`
22 f.close()
23
Guido van Rossum22d58952000-10-12 16:46:28 +000024 f = module.StringIO()
Fred Drake95438332000-09-28 04:25:33 +000025 f.write(s)
26 f.seek(10)
27 f.truncate()
28 print `f.getvalue()`
Jim Fultond1229f52000-10-06 19:21:32 +000029 f.seek(0)
30 f.truncate(5)
31 print `f.getvalue()`
32 f.close()
33 try:
34 f.write("frobnitz")
35 except ValueError, e:
36 print "Caught expected ValueError writing to closed StringIO:"
37 print e
38 else:
39 print "Failed to catch ValueError writing to closed StringIO."
Fred Drake95438332000-09-28 04:25:33 +000040
Martin v. Löwisb96e0e52000-09-19 16:35:39 +000041import StringIO, cStringIO
42do_test(StringIO)
43do_test(cStringIO)