blob: ea237cd138a90babb004b9b9c60891745e01ebd3 [file] [log] [blame]
Jeremy Hyltona8268e92000-10-16 17:33:50 +00001# Tests StringIO and cStringIO
Guido van Rossum8d691c82000-09-01 19:25:51 +00002
Jeremy Hyltona8268e92000-10-16 17:33:50 +00003def 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 Rossum8d691c82000-09-01 19:25:51 +00009
Jeremy Hyltona8268e92000-10-16 17:33:50 +000010 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 Rossume8d2f552000-10-09 22:14:43 +000026 try:
Jeremy Hyltona8268e92000-10-16 17:33:50 +000027 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 Rossume8d2f552000-10-09 22:14:43 +000033
Jeremy Hyltona8268e92000-10-16 17:33:50 +000034# Don't bother testing cStringIO without
35import StringIO, cStringIO
36do_test(StringIO)
37do_test(cStringIO)