python-2.5.2/win32/Lib/test/test_StringIO.py
changeset 0 ae805ac0140d
equal deleted inserted replaced
-1:000000000000 0:ae805ac0140d
       
     1 # Tests StringIO and cStringIO
       
     2 
       
     3 import unittest
       
     4 import StringIO
       
     5 import cStringIO
       
     6 import types
       
     7 from test import test_support
       
     8 
       
     9 
       
    10 class TestGenericStringIO(unittest.TestCase):
       
    11     # use a class variable MODULE to define which module is being tested
       
    12 
       
    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 
       
    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)
       
    24 
       
    25     def test_reads(self):
       
    26         eq = self.assertEqual
       
    27         self.assertRaises(TypeError, self._fp.seek)
       
    28         eq(self._fp.read(10), self._line[:10])
       
    29         eq(self._fp.readline(), self._line[10:] + '\n')
       
    30         eq(len(self._fp.readlines(60)), 2)
       
    31 
       
    32     def test_writes(self):
       
    33         f = self.MODULE.StringIO()
       
    34         self.assertRaises(TypeError, f.seek)
       
    35         f.write(self._line[:6])
       
    36         f.seek(3)
       
    37         f.write(self._line[20:26])
       
    38         f.write(self._line[52])
       
    39         self.assertEqual(f.getvalue(), 'abcuvwxyz!')
       
    40 
       
    41     def test_writelines(self):
       
    42         f = self.MODULE.StringIO()
       
    43         f.writelines([self._line[0], self._line[1], self._line[2]])
       
    44         f.seek(0)
       
    45         self.assertEqual(f.getvalue(), 'abc')
       
    46 
       
    47     def test_writelines_error(self):
       
    48         def errorGen():
       
    49             yield 'a'
       
    50             raise KeyboardInterrupt()
       
    51         f = self.MODULE.StringIO()
       
    52         self.assertRaises(KeyboardInterrupt, f.writelines, errorGen())
       
    53 
       
    54     def test_truncate(self):
       
    55         eq = self.assertEqual
       
    56         f = self.MODULE.StringIO()
       
    57         f.write(self._lines)
       
    58         f.seek(10)
       
    59         f.truncate()
       
    60         eq(f.getvalue(), 'abcdefghij')
       
    61         f.truncate(5)
       
    62         eq(f.getvalue(), 'abcde')
       
    63         f.write('xyz')
       
    64         eq(f.getvalue(), 'abcdexyz')
       
    65         f.close()
       
    66         self.assertRaises(ValueError, f.write, 'frobnitz')
       
    67 
       
    68     def test_closed_flag(self):
       
    69         f = self.MODULE.StringIO()
       
    70         self.assertEqual(f.closed, False)
       
    71         f.close()
       
    72         self.assertEqual(f.closed, True)
       
    73         f = self.MODULE.StringIO("abc")
       
    74         self.assertEqual(f.closed, False)
       
    75         f.close()
       
    76         self.assertEqual(f.closed, True)
       
    77 
       
    78     def test_isatty(self):
       
    79         f = self.MODULE.StringIO()
       
    80         self.assertRaises(TypeError, f.isatty, None)
       
    81         self.assertEqual(f.isatty(), False)
       
    82         f.close()
       
    83         self.assertRaises(ValueError, f.isatty)
       
    84 
       
    85     def test_iterator(self):
       
    86         eq = self.assertEqual
       
    87         unless = self.failUnless
       
    88         eq(iter(self._fp), self._fp)
       
    89         # Does this object support the iteration protocol?
       
    90         unless(hasattr(self._fp, '__iter__'))
       
    91         unless(hasattr(self._fp, 'next'))
       
    92         i = 0
       
    93         for line in self._fp:
       
    94             eq(line, self._line + '\n')
       
    95             i += 1
       
    96         eq(i, 5)
       
    97         self._fp.close()
       
    98         self.assertRaises(ValueError, self._fp.next)
       
    99 
       
   100 class TestStringIO(TestGenericStringIO):
       
   101     MODULE = StringIO
       
   102 
       
   103     def test_unicode(self):
       
   104 
       
   105         if not test_support.have_unicode: return
       
   106 
       
   107         # The StringIO module also supports concatenating Unicode
       
   108         # snippets to larger Unicode strings. This is tested by this
       
   109         # method. Note that cStringIO does not support this extension.
       
   110 
       
   111         f = self.MODULE.StringIO()
       
   112         f.write(self._line[:6])
       
   113         f.seek(3)
       
   114         f.write(unicode(self._line[20:26]))
       
   115         f.write(unicode(self._line[52]))
       
   116         s = f.getvalue()
       
   117         self.assertEqual(s, unicode('abcuvwxyz!'))
       
   118         self.assertEqual(type(s), types.UnicodeType)
       
   119 
       
   120 class TestcStringIO(TestGenericStringIO):
       
   121     MODULE = cStringIO
       
   122 
       
   123 import sys
       
   124 if sys.platform.startswith('java'):
       
   125     # Jython doesn't have a buffer object, so we just do a useless
       
   126     # fake of the buffer tests.
       
   127     buffer = str
       
   128 
       
   129 class TestBufferStringIO(TestStringIO):
       
   130     constructor = buffer
       
   131 
       
   132 class TestBuffercStringIO(TestcStringIO):
       
   133     constructor = buffer
       
   134 
       
   135 
       
   136 def test_main():
       
   137     test_support.run_unittest(
       
   138         TestStringIO,
       
   139         TestcStringIO,
       
   140         TestBufferStringIO,
       
   141         TestBuffercStringIO
       
   142     )
       
   143 
       
   144 if __name__ == '__main__':
       
   145     test_main()