symbian-qemu-0.9.1-12/python-2.6.1/Lib/test/test_StringIO.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     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         self.assertRaises(IOError, f.truncate, -1)
       
    66         f.close()
       
    67         self.assertRaises(ValueError, f.write, 'frobnitz')
       
    68 
       
    69     def test_closed_flag(self):
       
    70         f = self.MODULE.StringIO()
       
    71         self.assertEqual(f.closed, False)
       
    72         f.close()
       
    73         self.assertEqual(f.closed, True)
       
    74         f = self.MODULE.StringIO("abc")
       
    75         self.assertEqual(f.closed, False)
       
    76         f.close()
       
    77         self.assertEqual(f.closed, True)
       
    78 
       
    79     def test_isatty(self):
       
    80         f = self.MODULE.StringIO()
       
    81         self.assertRaises(TypeError, f.isatty, None)
       
    82         self.assertEqual(f.isatty(), False)
       
    83         f.close()
       
    84         self.assertRaises(ValueError, f.isatty)
       
    85 
       
    86     def test_iterator(self):
       
    87         eq = self.assertEqual
       
    88         unless = self.failUnless
       
    89         eq(iter(self._fp), self._fp)
       
    90         # Does this object support the iteration protocol?
       
    91         unless(hasattr(self._fp, '__iter__'))
       
    92         unless(hasattr(self._fp, 'next'))
       
    93         i = 0
       
    94         for line in self._fp:
       
    95             eq(line, self._line + '\n')
       
    96             i += 1
       
    97         eq(i, 5)
       
    98         self._fp.close()
       
    99         self.assertRaises(ValueError, self._fp.next)
       
   100 
       
   101 class TestStringIO(TestGenericStringIO):
       
   102     MODULE = StringIO
       
   103 
       
   104     def test_unicode(self):
       
   105 
       
   106         if not test_support.have_unicode: return
       
   107 
       
   108         # The StringIO module also supports concatenating Unicode
       
   109         # snippets to larger Unicode strings. This is tested by this
       
   110         # method. Note that cStringIO does not support this extension.
       
   111 
       
   112         f = self.MODULE.StringIO()
       
   113         f.write(self._line[:6])
       
   114         f.seek(3)
       
   115         f.write(unicode(self._line[20:26]))
       
   116         f.write(unicode(self._line[52]))
       
   117         s = f.getvalue()
       
   118         self.assertEqual(s, unicode('abcuvwxyz!'))
       
   119         self.assertEqual(type(s), types.UnicodeType)
       
   120 
       
   121 class TestcStringIO(TestGenericStringIO):
       
   122     MODULE = cStringIO
       
   123 
       
   124 import sys
       
   125 if sys.platform.startswith('java'):
       
   126     # Jython doesn't have a buffer object, so we just do a useless
       
   127     # fake of the buffer tests.
       
   128     buffer = str
       
   129 
       
   130 class TestBufferStringIO(TestStringIO):
       
   131     constructor = buffer
       
   132 
       
   133 class TestBuffercStringIO(TestcStringIO):
       
   134     constructor = buffer
       
   135 
       
   136 
       
   137 def test_main():
       
   138     test_support.run_unittest(
       
   139         TestStringIO,
       
   140         TestcStringIO,
       
   141         TestBufferStringIO,
       
   142         TestBuffercStringIO
       
   143     )
       
   144 
       
   145 if __name__ == '__main__':
       
   146     test_main()