|
10 | 10 | class TestGenericStringIO(unittest.TestCase): |
11 | 11 | # use a class variable MODULE to define which module is being tested |
12 | 12 |
|
| 13 | + # Line of data to test as string |
| 14 | + _line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!' |
| 15 | + |
| 16 | + # Constructor to use for the test data (._line is passed to this |
| 17 | + # constructor) |
| 18 | + constructor = str |
| 19 | + |
13 | 20 | def setUp(self): |
14 | | - self._line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
15 | | - self._lines = (self._line + '\n') * 5 |
| 21 | + self._line = self.constructor(self._line) |
| 22 | + self._lines = self.constructor((self._line + '\n') * 5) |
16 | 23 | self._fp = self.MODULE.StringIO(self._lines) |
17 | 24 |
|
18 | 25 | def test_reads(self): |
19 | 26 | eq = self.assertEqual |
20 | | - eq(self._fp.read(10), 'abcdefghij') |
21 | | - eq(self._fp.readline(), 'klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n') |
| 27 | + eq(self._fp.read(10), self._line[:10]) |
| 28 | + eq(self._fp.readline(), self._line[10:] + '\n') |
22 | 29 | eq(len(self._fp.readlines(60)), 2) |
23 | 30 |
|
24 | 31 | def test_writes(self): |
25 | 32 | f = self.MODULE.StringIO() |
26 | | - f.write('abcdef') |
| 33 | + f.write(self._line[:6]) |
27 | 34 | f.seek(3) |
28 | | - f.write('uvwxyz') |
29 | | - f.write('!') |
| 35 | + f.write(self._line[20:26]) |
| 36 | + f.write(self._line[52]) |
30 | 37 | self.assertEqual(f.getvalue(), 'abcuvwxyz!') |
31 | 38 |
|
32 | 39 | def test_writelines(self): |
33 | 40 | f = self.MODULE.StringIO() |
34 | | - f.writelines(['a', 'b', 'c']) |
| 41 | + f.writelines([self._line[0], self._line[1], self._line[2]]) |
35 | 42 | f.seek(0) |
36 | 43 | self.assertEqual(f.getvalue(), 'abc') |
37 | 44 |
|
@@ -64,10 +71,18 @@ class TestStringIO(TestGenericStringIO): |
64 | 71 | class TestcStringIO(TestGenericStringIO): |
65 | 72 | MODULE = cStringIO |
66 | 73 |
|
| 74 | +class TestBufferStringIO(TestStringIO): |
| 75 | + constructor = buffer |
| 76 | + |
| 77 | +class TestBuffercStringIO(TestcStringIO): |
| 78 | + constructor = buffer |
| 79 | + |
67 | 80 |
|
68 | 81 | def test_main(): |
69 | 82 | test_support.run_unittest(TestStringIO) |
70 | 83 | test_support.run_unittest(TestcStringIO) |
| 84 | + test_support.run_unittest(TestBufferStringIO) |
| 85 | + test_support.run_unittest(TestBuffercStringIO) |
71 | 86 |
|
72 | 87 | if __name__ == '__main__': |
73 | 88 | test_main() |
0 commit comments