Martin v. Löwis | b96e0e5 | 2000-09-19 16:35:39 +0000 | [diff] [blame] | 1 | # Tests StringIO and cStringIO |
| 2 | |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | import StringIO |
| 5 | import cStringIO |
| 6 | import types |
| 7 | import test_support |
Martin v. Löwis | b96e0e5 | 2000-09-19 16:35:39 +0000 | [diff] [blame] | 8 | |
Jeremy Hylton | cafd495 | 2001-02-09 23:44:22 +0000 | [diff] [blame] | 9 | |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 10 | class TestGenericStringIO(unittest.TestCase): |
| 11 | # use a class variable MODULE to define which module is being tested |
Jeremy Hylton | cafd495 | 2001-02-09 23:44:22 +0000 | [diff] [blame] | 12 | |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 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 | |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 20 | def setUp(self): |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 21 | self._line = self.constructor(self._line) |
| 22 | self._lines = self.constructor((self._line + '\n') * 5) |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 23 | self._fp = self.MODULE.StringIO(self._lines) |
Fred Drake | 9543833 | 2000-09-28 04:25:33 +0000 | [diff] [blame] | 24 | |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 25 | def test_reads(self): |
| 26 | eq = self.assertEqual |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 27 | eq(self._fp.read(10), self._line[:10]) |
| 28 | eq(self._fp.readline(), self._line[10:] + '\n') |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 29 | eq(len(self._fp.readlines(60)), 2) |
| 30 | |
| 31 | def test_writes(self): |
| 32 | f = self.MODULE.StringIO() |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 33 | f.write(self._line[:6]) |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 34 | f.seek(3) |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 35 | f.write(self._line[20:26]) |
| 36 | f.write(self._line[52]) |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 37 | self.assertEqual(f.getvalue(), 'abcuvwxyz!') |
| 38 | |
| 39 | def test_writelines(self): |
| 40 | f = self.MODULE.StringIO() |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 41 | f.writelines([self._line[0], self._line[1], self._line[2]]) |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 42 | f.seek(0) |
| 43 | self.assertEqual(f.getvalue(), 'abc') |
| 44 | |
| 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') |
| 57 | |
| 58 | def test_iterator(self): |
| 59 | eq = self.assertEqual |
Barry Warsaw | 4565350 | 2001-09-25 21:40:04 +0000 | [diff] [blame] | 60 | unless = self.failUnless |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 61 | it = iter(self._fp) |
Barry Warsaw | 4565350 | 2001-09-25 21:40:04 +0000 | [diff] [blame] | 62 | # Does this object support the iteration protocol? |
| 63 | unless(hasattr(it, '__iter__')) |
| 64 | unless(hasattr(it, 'next')) |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 65 | i = 0 |
| 66 | for line in self._fp: |
| 67 | eq(line, self._line + '\n') |
| 68 | i += 1 |
| 69 | eq(i, 5) |
| 70 | |
| 71 | class TestStringIO(TestGenericStringIO): |
| 72 | MODULE = StringIO |
| 73 | |
Marc-André Lemburg | f853be9 | 2002-01-06 17:15:05 +0000 | [diff] [blame] | 74 | def test_unicode(self): |
| 75 | |
Michael W. Hudson | e1c67d1 | 2002-05-13 09:42:16 +0000 | [diff] [blame^] | 76 | if not test_support.have_unicode: return |
| 77 | |
Marc-André Lemburg | f853be9 | 2002-01-06 17:15:05 +0000 | [diff] [blame] | 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. |
Tim Peters | e441860 | 2002-02-16 07:34:19 +0000 | [diff] [blame] | 81 | |
Marc-André Lemburg | f853be9 | 2002-01-06 17:15:05 +0000 | [diff] [blame] | 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) |
| 90 | |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 91 | class TestcStringIO(TestGenericStringIO): |
| 92 | MODULE = cStringIO |
| 93 | |
Finn Bock | 793ead5 | 2001-12-09 20:06:32 +0000 | [diff] [blame] | 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 |
| 99 | |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 100 | class TestBufferStringIO(TestStringIO): |
| 101 | constructor = buffer |
| 102 | |
| 103 | class TestBuffercStringIO(TestcStringIO): |
| 104 | constructor = buffer |
| 105 | |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 106 | |
| 107 | def test_main(): |
| 108 | test_support.run_unittest(TestStringIO) |
| 109 | test_support.run_unittest(TestcStringIO) |
Marc-André Lemburg | e47df7a | 2001-09-24 17:34:52 +0000 | [diff] [blame] | 110 | test_support.run_unittest(TestBufferStringIO) |
| 111 | test_support.run_unittest(TestBuffercStringIO) |
Barry Warsaw | 7f8ff47 | 2001-09-22 04:33:47 +0000 | [diff] [blame] | 112 | |
| 113 | if __name__ == '__main__': |
| 114 | test_main() |