[Python-checkins] r55703 - python/branches/cpy_merge/Lib/test/test_string_io.py

alexandre.vassalotti python-checkins at python.org
Thu May 31 01:52:01 CEST 2007


Author: alexandre.vassalotti
Date: Thu May 31 01:52:00 2007
New Revision: 55703

Added:
   python/branches/cpy_merge/Lib/test/test_string_io.py
Log:
Add a temporary unit test for _string_io.

Added: python/branches/cpy_merge/Lib/test/test_string_io.py
==============================================================================
--- (empty file)
+++ python/branches/cpy_merge/Lib/test/test_string_io.py	Thu May 31 01:52:00 2007
@@ -0,0 +1,132 @@
+"""Temporary unit tests for StringIO"""
+
+# Based on test_StringIO.py
+
+import unittest
+import _string_io
+import types
+from test import test_support
+
+
+class TestGenericStringIO(unittest.TestCase):
+    # use a class variable MODULE to define which module is being tested
+
+    # Line of data to test as string
+    _line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'
+
+    # Constructor to use for the test data (._line is passed to this
+    # constructor)
+    constructor = unicode
+
+    def setUp(self):
+        self._line = self.constructor(self._line)
+        self._lines = self.constructor((self._line + '\n') * 5)
+        self._fp = self.MODULE.StringIO(self._lines)
+
+    def test_reads(self):
+        eq = self.assertEqual
+        self.assertRaises(TypeError, self._fp.seek)
+        eq(self._fp.read(10), self._line[:10])
+        eq(self._fp.readline(), self._line[10:] + '\n')
+        eq(len(self._fp.readlines(60)), 2)
+
+    def test_writes(self):
+        f = self.MODULE.StringIO()
+        self.assertRaises(TypeError, f.seek)
+        f.write(self._line[:6])
+        f.seek(3)
+        f.write(self._line[20:26])
+        f.write(self._line[52])
+        self.assertEqual(f.getvalue(), u'abcuvwxyz!')
+
+    def test_writelines(self):
+        f = self.MODULE.StringIO()
+        f.writelines(map(self.constructor, [self._line[0], self._line[1], self._line[2]]))
+        f.seek(0)
+        self.assertEqual(f.getvalue(), u'abc')
+
+    def test_writelines_error(self):
+        def errorGen():
+            yield u'a'
+            raise KeyboardInterrupt()
+        f = self.MODULE.StringIO()
+        self.assertRaises(KeyboardInterrupt, f.writelines, errorGen())
+
+    def test_truncate(self):
+        eq = self.assertEqual
+        f = self.MODULE.StringIO()
+        f.write(self._lines)
+        f.seek(10)
+        f.truncate()
+        eq(f.getvalue(), u'abcdefghij')
+        f.truncate(5)
+        eq(f.getvalue(), u'abcde')
+        f.write(u'xyz')
+        eq(f.getvalue(), u'abcdexyz')
+        self.assertRaises(IOError, f.truncate, -1)
+        f.close()
+        self.assertRaises(ValueError, f.write, u'frobnitz')
+
+    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(u"abc")
+        self.assertEqual(f.closed, False)
+        f.close()
+        self.assertEqual(f.closed, True)
+
+    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)
+
+    def test_iterator(self):
+        eq = self.assertEqual
+        unless = self.failUnless
+        eq(iter(self._fp), self._fp)
+        # Does this object support the iteration protocol?
+        unless(hasattr(self._fp, '__iter__'))
+        unless(hasattr(self._fp, '__next__'))
+        i = 0
+        for line in self._fp:
+            eq(line, self._line + '\n')
+            i += 1
+        eq(i, 5)
+        self._fp.close()
+        self.assertRaises(ValueError, next, self._fp)
+
+    def test_getvalue(self):
+        f = self.MODULE.StringIO()
+        f.close()
+        self.assertRaises(ValueError, f.getvalue)
+
+class TestStringIO(TestGenericStringIO):
+    MODULE = _string_io
+
+    def test_unicode(self):
+
+        if not test_support.have_unicode: return
+
+        # The StringIO module also supports concatenating Unicode
+        # snippets to larger Unicode strings. This is tested by this
+        # method.
+
+        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)
+
+
+def test_main():
+    test_support.run_unittest(TestStringIO)
+
+if __name__ == '__main__':
+    test_main()


More information about the Python-checkins mailing list