0001 # Tests StringIO and cStringIO 0002 0003 import unittest 0004 import StringIO 0005 import cStringIO 0006 import types 0007 from test import test_support 0008 0009 0010 class TestGenericStringIO(unittest.TestCase): 0011 # use a class variable MODULE to define which module is being tested 0012 0013 # Line of data to test as string 0014 _line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!' 0015 0016 # Constructor to use for the test data (._line is passed to this 0017 # constructor) 0018 constructor = str 0019 0020 def setUp(self): 0021 self._line = self.constructor(self._line) 0022 self._lines = self.constructor((self._line + '\n') * 5) 0023 self._fp = self.MODULE.StringIO(self._lines) 0024 0025 def test_reads(self): 0026 eq = self.assertEqual 0027 self.assertRaises(TypeError, self._fp.seek) 0028 eq(self._fp.read(10), self._line[:10]) 0029 eq(self._fp.readline(), self._line[10:] + '\n') 0030 eq(len(self._fp.readlines(60)), 2) 0031 0032 def test_writes(self): 0033 f = self.MODULE.StringIO() 0034 self.assertRaises(TypeError, f.seek) 0035 f.write(self._line[:6]) 0036 f.seek(3) 0037 f.write(self._line[20:26]) 0038 f.write(self._line[52]) 0039 self.assertEqual(f.getvalue(), 'abcuvwxyz!') 0040 0041 def test_writelines(self): 0042 f = self.MODULE.StringIO() 0043 f.writelines([self._line[0], self._line[1], self._line[2]]) 0044 f.seek(0) 0045 self.assertEqual(f.getvalue(), 'abc') 0046 0047 def test_truncate(self): 0048 eq = self.assertEqual 0049 f = self.MODULE.StringIO() 0050 f.write(self._lines) 0051 f.seek(10) 0052 f.truncate() 0053 eq(f.getvalue(), 'abcdefghij') 0054 f.truncate(5) 0055 eq(f.getvalue(), 'abcde') 0056 f.write('xyz') 0057 eq(f.getvalue(), 'abcdexyz') 0058 f.close() 0059 self.assertRaises(ValueError, f.write, 'frobnitz') 0060 0061 def test_closed_flag(self): 0062 f = self.MODULE.StringIO() 0063 self.assertEqual(f.closed, False) 0064 f.close() 0065 self.assertEqual(f.closed, True) 0066 f = self.MODULE.StringIO("abc") 0067 self.assertEqual(f.closed, False) 0068 f.close() 0069 self.assertEqual(f.closed, True) 0070 0071 def test_iterator(self): 0072 eq = self.assertEqual 0073 unless = self.failUnless 0074 eq(iter(self._fp), self._fp) 0075 # Does this object support the iteration protocol? 0076 unless(hasattr(self._fp, '__iter__')) 0077 unless(hasattr(self._fp, 'next')) 0078 i = 0 0079 for line in self._fp: 0080 eq(line, self._line + '\n') 0081 i += 1 0082 eq(i, 5) 0083 0084 class TestStringIO(TestGenericStringIO): 0085 MODULE = StringIO 0086 0087 def test_unicode(self): 0088 0089 if not test_support.have_unicode: return 0090 0091 # The StringIO module also supports concatenating Unicode 0092 # snippets to larger Unicode strings. This is tested by this 0093 # method. Note that cStringIO does not support this extension. 0094 0095 f = self.MODULE.StringIO() 0096 f.write(self._line[:6]) 0097 f.seek(3) 0098 f.write(unicode(self._line[20:26])) 0099 f.write(unicode(self._line[52])) 0100 s = f.getvalue() 0101 self.assertEqual(s, unicode('abcuvwxyz!')) 0102 self.assertEqual(type(s), types.UnicodeType) 0103 0104 class TestcStringIO(TestGenericStringIO): 0105 MODULE = cStringIO 0106 0107 import sys 0108 if sys.platform.startswith('java'): 0109 # Jython doesn't have a buffer object, so we just do a useless 0110 # fake of the buffer tests. 0111 buffer = str 0112 0113 class TestBufferStringIO(TestStringIO): 0114 constructor = buffer 0115 0116 class TestBuffercStringIO(TestcStringIO): 0117 constructor = buffer 0118 0119 0120 def test_main(): 0121 test_support.run_unittest( 0122 TestStringIO, 0123 TestcStringIO, 0124 TestBufferStringIO, 0125 TestBuffercStringIO 0126 ) 0127 0128 if __name__ == '__main__': 0129 test_main() 0130
Generated by PyXR 0.9.4