Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f8ff47

Browse files
committed
Converted test_StringIO.py to use unittest, so
Lib/test/output/test_StringIO is no longer necessary. Also, added a test of the iterator protocol that's just been added to StringIO's and cStringIO's.
1 parent 3208d4b commit 7f8ff47

2 files changed

Lines changed: 71 additions & 62 deletions

File tree

Lib/test/output/test_StringIO

Lines changed: 0 additions & 21 deletions
This file was deleted.

Lib/test/test_StringIO.py

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,73 @@
11
# Tests StringIO and cStringIO
22

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))
9-
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-
18-
f = module.StringIO()
19-
f.writelines(["a", "b", "c"])
20-
f.seek(0)
21-
print `f.getvalue()`
22-
f.close()
23-
24-
f = module.StringIO()
25-
f.write(s)
26-
f.seek(10)
27-
f.truncate()
28-
print `f.getvalue()`
29-
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."
40-
41-
import StringIO, cStringIO
42-
do_test(StringIO)
43-
do_test(cStringIO)
3+
import unittest
4+
import StringIO
5+
import cStringIO
6+
import types
7+
import test_support
8+
9+
10+
class TestGenericStringIO(unittest.TestCase):
11+
# use a class variable MODULE to define which module is being tested
12+
13+
def setUp(self):
14+
self._line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
15+
self._lines = (self._line + '\n') * 5
16+
self._fp = self.MODULE.StringIO(self._lines)
17+
18+
def test_reads(self):
19+
eq = self.assertEqual
20+
eq(self._fp.read(10), 'abcdefghij')
21+
eq(self._fp.readline(), 'klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n')
22+
eq(len(self._fp.readlines(60)), 2)
23+
24+
def test_writes(self):
25+
f = self.MODULE.StringIO()
26+
f.write('abcdef')
27+
f.seek(3)
28+
f.write('uvwxyz')
29+
f.write('!')
30+
self.assertEqual(f.getvalue(), 'abcuvwxyz!')
31+
32+
def test_writelines(self):
33+
f = self.MODULE.StringIO()
34+
f.writelines(['a', 'b', 'c'])
35+
f.seek(0)
36+
self.assertEqual(f.getvalue(), 'abc')
37+
38+
def test_truncate(self):
39+
eq = self.assertEqual
40+
f = self.MODULE.StringIO()
41+
f.write(self._lines)
42+
f.seek(10)
43+
f.truncate()
44+
eq(f.getvalue(), 'abcdefghij')
45+
f.seek(0)
46+
f.truncate(5)
47+
eq(f.getvalue(), 'abcde')
48+
f.close()
49+
self.assertRaises(ValueError, f.write, 'frobnitz')
50+
51+
def test_iterator(self):
52+
eq = self.assertEqual
53+
it = iter(self._fp)
54+
self.failUnless(isinstance(it, types.FunctionIterType))
55+
i = 0
56+
for line in self._fp:
57+
eq(line, self._line + '\n')
58+
i += 1
59+
eq(i, 5)
60+
61+
class TestStringIO(TestGenericStringIO):
62+
MODULE = StringIO
63+
64+
class TestcStringIO(TestGenericStringIO):
65+
MODULE = cStringIO
66+
67+
68+
def test_main():
69+
test_support.run_unittest(TestStringIO)
70+
test_support.run_unittest(TestcStringIO)
71+
72+
if __name__ == '__main__':
73+
test_main()

0 commit comments

Comments
 (0)