2000-09-19 13:35:39 -03:00
|
|
|
# Tests StringIO and cStringIO
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
import unittest
|
|
|
|
import StringIO
|
|
|
|
import cStringIO
|
|
|
|
import types
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2001-09-22 01:33:47 -03:00
|
|
|
|
|
|
|
|
|
|
|
class TestGenericStringIO(unittest.TestCase):
|
|
|
|
# use a class variable MODULE to define which module is being tested
|
|
|
|
|
2001-09-24 14:34:52 -03:00
|
|
|
# Line of data to test as string
|
|
|
|
_line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'
|
|
|
|
|
|
|
|
# Constructor to use for the test data (._line is passed to this
|
|
|
|
# constructor)
|
|
|
|
constructor = str
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
def setUp(self):
|
2001-09-24 14:34:52 -03:00
|
|
|
self._line = self.constructor(self._line)
|
|
|
|
self._lines = self.constructor((self._line + '\n') * 5)
|
2001-09-22 01:33:47 -03:00
|
|
|
self._fp = self.MODULE.StringIO(self._lines)
|
|
|
|
|
|
|
|
def test_reads(self):
|
|
|
|
eq = self.assertEqual
|
2004-11-01 13:10:19 -04:00
|
|
|
self.assertRaises(TypeError, self._fp.seek)
|
2001-09-24 14:34:52 -03:00
|
|
|
eq(self._fp.read(10), self._line[:10])
|
|
|
|
eq(self._fp.readline(), self._line[10:] + '\n')
|
2001-09-22 01:33:47 -03:00
|
|
|
eq(len(self._fp.readlines(60)), 2)
|
|
|
|
|
|
|
|
def test_writes(self):
|
|
|
|
f = self.MODULE.StringIO()
|
2004-11-01 13:10:19 -04:00
|
|
|
self.assertRaises(TypeError, f.seek)
|
2001-09-24 14:34:52 -03:00
|
|
|
f.write(self._line[:6])
|
2001-09-22 01:33:47 -03:00
|
|
|
f.seek(3)
|
2001-09-24 14:34:52 -03:00
|
|
|
f.write(self._line[20:26])
|
|
|
|
f.write(self._line[52])
|
2001-09-22 01:33:47 -03:00
|
|
|
self.assertEqual(f.getvalue(), 'abcuvwxyz!')
|
|
|
|
|
|
|
|
def test_writelines(self):
|
|
|
|
f = self.MODULE.StringIO()
|
2001-09-24 14:34:52 -03:00
|
|
|
f.writelines([self._line[0], self._line[1], self._line[2]])
|
2001-09-22 01:33:47 -03:00
|
|
|
f.seek(0)
|
|
|
|
self.assertEqual(f.getvalue(), 'abc')
|
|
|
|
|
2005-09-22 06:19:01 -03:00
|
|
|
def test_writelines_error(self):
|
|
|
|
def errorGen():
|
|
|
|
yield 'a'
|
|
|
|
raise KeyboardInterrupt()
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
self.assertRaises(KeyboardInterrupt, f.writelines, errorGen())
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
def test_truncate(self):
|
|
|
|
eq = self.assertEqual
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
f.write(self._lines)
|
|
|
|
f.seek(10)
|
|
|
|
f.truncate()
|
|
|
|
eq(f.getvalue(), 'abcdefghij')
|
|
|
|
f.truncate(5)
|
|
|
|
eq(f.getvalue(), 'abcde')
|
2004-08-21 03:55:43 -03:00
|
|
|
f.write('xyz')
|
|
|
|
eq(f.getvalue(), 'abcdexyz')
|
2001-09-22 01:33:47 -03:00
|
|
|
f.close()
|
|
|
|
self.assertRaises(ValueError, f.write, 'frobnitz')
|
|
|
|
|
2003-08-08 09:20:03 -03:00
|
|
|
def test_closed_flag(self):
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
self.assertEqual(f.closed, False)
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(f.closed, True)
|
|
|
|
f = self.MODULE.StringIO("abc")
|
|
|
|
self.assertEqual(f.closed, False)
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(f.closed, True)
|
|
|
|
|
2006-03-15 18:13:13 -04:00
|
|
|
def test_isatty(self):
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
self.assertRaises(TypeError, f.isatty, None)
|
|
|
|
self.assertEqual(f.isatty(), False)
|
|
|
|
f.close()
|
|
|
|
self.assertRaises(ValueError, f.isatty)
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
def test_iterator(self):
|
|
|
|
eq = self.assertEqual
|
2001-09-25 18:40:04 -03:00
|
|
|
unless = self.failUnless
|
2003-04-24 12:50:11 -03:00
|
|
|
eq(iter(self._fp), self._fp)
|
2001-09-25 18:40:04 -03:00
|
|
|
# Does this object support the iteration protocol?
|
2003-04-24 12:50:11 -03:00
|
|
|
unless(hasattr(self._fp, '__iter__'))
|
|
|
|
unless(hasattr(self._fp, 'next'))
|
2001-09-22 01:33:47 -03:00
|
|
|
i = 0
|
|
|
|
for line in self._fp:
|
|
|
|
eq(line, self._line + '\n')
|
|
|
|
i += 1
|
|
|
|
eq(i, 5)
|
2006-03-15 04:23:53 -04:00
|
|
|
self._fp.close()
|
|
|
|
self.assertRaises(ValueError, self._fp.next)
|
2001-09-22 01:33:47 -03:00
|
|
|
|
|
|
|
class TestStringIO(TestGenericStringIO):
|
|
|
|
MODULE = StringIO
|
|
|
|
|
2002-01-06 13:15:05 -04:00
|
|
|
def test_unicode(self):
|
|
|
|
|
2002-05-13 06:42:16 -03:00
|
|
|
if not test_support.have_unicode: return
|
|
|
|
|
2002-01-06 13:15:05 -04:00
|
|
|
# The StringIO module also supports concatenating Unicode
|
|
|
|
# snippets to larger Unicode strings. This is tested by this
|
|
|
|
# method. Note that cStringIO does not support this extension.
|
2002-02-16 03:34:19 -04:00
|
|
|
|
2002-01-06 13:15:05 -04:00
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
f.write(self._line[:6])
|
|
|
|
f.seek(3)
|
|
|
|
f.write(unicode(self._line[20:26]))
|
|
|
|
f.write(unicode(self._line[52]))
|
|
|
|
s = f.getvalue()
|
|
|
|
self.assertEqual(s, unicode('abcuvwxyz!'))
|
|
|
|
self.assertEqual(type(s), types.UnicodeType)
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
class TestcStringIO(TestGenericStringIO):
|
|
|
|
MODULE = cStringIO
|
|
|
|
|
2006-10-12 06:47:17 -03:00
|
|
|
def test_unicode(self):
|
|
|
|
|
|
|
|
if not test_support.have_unicode: return
|
|
|
|
|
|
|
|
# The cStringIO module converts Unicode strings to character
|
|
|
|
# strings when writing them to cStringIO objects.
|
|
|
|
# Check that this works.
|
|
|
|
|
|
|
|
f = self.MODULE.StringIO()
|
|
|
|
f.write(unicode(self._line[:5]))
|
|
|
|
s = f.getvalue()
|
|
|
|
self.assertEqual(s, 'abcde')
|
|
|
|
self.assertEqual(type(s), types.StringType)
|
|
|
|
|
|
|
|
f = self.MODULE.StringIO(unicode(self._line[:5]))
|
|
|
|
s = f.getvalue()
|
|
|
|
self.assertEqual(s, 'abcde')
|
|
|
|
self.assertEqual(type(s), types.StringType)
|
|
|
|
|
|
|
|
self.assertRaises(UnicodeEncodeError, self.MODULE.StringIO,
|
|
|
|
unicode('\xf4', 'latin-1'))
|
|
|
|
|
2001-12-09 16:06:32 -04:00
|
|
|
import sys
|
|
|
|
if sys.platform.startswith('java'):
|
|
|
|
# Jython doesn't have a buffer object, so we just do a useless
|
|
|
|
# fake of the buffer tests.
|
|
|
|
buffer = str
|
|
|
|
|
2001-09-24 14:34:52 -03:00
|
|
|
class TestBufferStringIO(TestStringIO):
|
|
|
|
constructor = buffer
|
|
|
|
|
|
|
|
class TestBuffercStringIO(TestcStringIO):
|
|
|
|
constructor = buffer
|
|
|
|
|
2001-09-22 01:33:47 -03:00
|
|
|
|
|
|
|
def test_main():
|
2003-05-01 14:45:56 -03:00
|
|
|
test_support.run_unittest(
|
|
|
|
TestStringIO,
|
|
|
|
TestcStringIO,
|
|
|
|
TestBufferStringIO,
|
|
|
|
TestBuffercStringIO
|
|
|
|
)
|
2001-09-22 01:33:47 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|