Update version number and release date.
[python/dscho.git] / Lib / test / test_StringIO.py
blob74bdf599f830e6098d490dca3a211936667fbea0
1 # Tests StringIO and cStringIO
3 import unittest
4 import StringIO
5 import cStringIO
6 import types
7 from test 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 eq(iter(self._fp), self._fp)
62 # Does this object support the iteration protocol?
63 unless(hasattr(self._fp, '__iter__'))
64 unless(hasattr(self._fp, '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 def test_unicode(self):
76 if not test_support.have_unicode: return
78 # The StringIO module also supports concatenating Unicode
79 # snippets to larger Unicode strings. This is tested by this
80 # method. Note that cStringIO does not support this extension.
82 f = self.MODULE.StringIO()
83 f.write(self._line[:6])
84 f.seek(3)
85 f.write(unicode(self._line[20:26]))
86 f.write(unicode(self._line[52]))
87 s = f.getvalue()
88 self.assertEqual(s, unicode('abcuvwxyz!'))
89 self.assertEqual(type(s), types.UnicodeType)
91 class TestcStringIO(TestGenericStringIO):
92 MODULE = cStringIO
94 import sys
95 if sys.platform.startswith('java'):
96 # Jython doesn't have a buffer object, so we just do a useless
97 # fake of the buffer tests.
98 buffer = str
100 class TestBufferStringIO(TestStringIO):
101 constructor = buffer
103 class TestBuffercStringIO(TestcStringIO):
104 constructor = buffer
107 def test_main():
108 test_support.run_unittest(TestStringIO)
109 test_support.run_unittest(TestcStringIO)
110 test_support.run_unittest(TestBufferStringIO)
111 test_support.run_unittest(TestBuffercStringIO)
113 if __name__ == '__main__':
114 test_main()