Fix the availability statement for the spawn*() functions to reflect the
[python/dscho.git] / Lib / test / test_StringIO.py
blob8b934fffcdfe1151ca4972ea7f328be247b27728
1 # Tests StringIO and cStringIO
3 import unittest
4 import StringIO
5 import cStringIO
6 import types
7 import test_support
10 class TestGenericStringIO(unittest.TestCase):
11 # use a class variable MODULE to define which module is being tested
13 # Line of data to test as string
14 _line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'
16 # Constructor to use for the test data (._line is passed to this
17 # constructor)
18 constructor = str
20 def setUp(self):
21 self._line = self.constructor(self._line)
22 self._lines = self.constructor((self._line + '\n') * 5)
23 self._fp = self.MODULE.StringIO(self._lines)
25 def test_reads(self):
26 eq = self.assertEqual
27 eq(self._fp.read(10), self._line[:10])
28 eq(self._fp.readline(), self._line[10:] + '\n')
29 eq(len(self._fp.readlines(60)), 2)
31 def test_writes(self):
32 f = self.MODULE.StringIO()
33 f.write(self._line[:6])
34 f.seek(3)
35 f.write(self._line[20:26])
36 f.write(self._line[52])
37 self.assertEqual(f.getvalue(), 'abcuvwxyz!')
39 def test_writelines(self):
40 f = self.MODULE.StringIO()
41 f.writelines([self._line[0], self._line[1], self._line[2]])
42 f.seek(0)
43 self.assertEqual(f.getvalue(), 'abc')
45 def test_truncate(self):
46 eq = self.assertEqual
47 f = self.MODULE.StringIO()
48 f.write(self._lines)
49 f.seek(10)
50 f.truncate()
51 eq(f.getvalue(), 'abcdefghij')
52 f.seek(0)
53 f.truncate(5)
54 eq(f.getvalue(), 'abcde')
55 f.close()
56 self.assertRaises(ValueError, f.write, 'frobnitz')
58 def test_iterator(self):
59 eq = self.assertEqual
60 unless = self.failUnless
61 it = iter(self._fp)
62 # Does this object support the iteration protocol?
63 unless(hasattr(it, '__iter__'))
64 unless(hasattr(it, 'next'))
65 i = 0
66 for line in self._fp:
67 eq(line, self._line + '\n')
68 i += 1
69 eq(i, 5)
71 class TestStringIO(TestGenericStringIO):
72 MODULE = StringIO
74 class TestcStringIO(TestGenericStringIO):
75 MODULE = cStringIO
77 import sys
78 if sys.platform.startswith('java'):
79 # Jython doesn't have a buffer object, so we just do a useless
80 # fake of the buffer tests.
81 buffer = str
83 class TestBufferStringIO(TestStringIO):
84 constructor = buffer
86 class TestBuffercStringIO(TestcStringIO):
87 constructor = buffer
90 def test_main():
91 test_support.run_unittest(TestStringIO)
92 test_support.run_unittest(TestcStringIO)
93 test_support.run_unittest(TestBufferStringIO)
94 test_support.run_unittest(TestBuffercStringIO)
96 if __name__ == '__main__':
97 test_main()