blob: ea237cd138a90babb004b9b9c60891745e01ebd3 [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()
17 f = module.StringIO()
Fred Drake95438332000-09-28 04:25:33 +000018 f.write(s)
19 f.seek(10)
20 f.truncate()
21 print `f.getvalue()`
Jim Fultond1229f52000-10-06 19:21:32 +000022 f.seek(0)
23 f.truncate(5)
24 print `f.getvalue()`
25 f.close()
26 try:
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."
Fred Drake95438332000-09-28 04:25:33 +000033
Martin v. Löwisb96e0e52000-09-19 16:35:39 +000034# Don't bother testing cStringIO without
35import StringIO, cStringIO
36do_test(StringIO)
37do_test(cStringIO)