| 
									
										
										
										
											2000-09-19 16:35:39 +00:00
										 |  |  | # Tests StringIO and cStringIO | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  | import unittest | 
					
						
							|  |  |  | import StringIO | 
					
						
							|  |  |  | import cStringIO | 
					
						
							|  |  |  | import types | 
					
						
							| 
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 |  |  | from test import test_support | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestGenericStringIO(unittest.TestCase): | 
					
						
							|  |  |  |     # use a class variable MODULE to define which module is being tested | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-24 17:34:52 +00: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 04:33:47 +00:00
										 |  |  |     def setUp(self): | 
					
						
							| 
									
										
										
										
											2001-09-24 17:34:52 +00:00
										 |  |  |         self._line = self.constructor(self._line) | 
					
						
							|  |  |  |         self._lines = self.constructor((self._line + '\n') * 5) | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  |         self._fp = self.MODULE.StringIO(self._lines) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_reads(self): | 
					
						
							|  |  |  |         eq = self.assertEqual | 
					
						
							| 
									
										
										
										
											2001-09-24 17:34:52 +00:00
										 |  |  |         eq(self._fp.read(10), self._line[:10]) | 
					
						
							|  |  |  |         eq(self._fp.readline(), self._line[10:] + '\n') | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  |         eq(len(self._fp.readlines(60)), 2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_writes(self): | 
					
						
							|  |  |  |         f = self.MODULE.StringIO() | 
					
						
							| 
									
										
										
										
											2001-09-24 17:34:52 +00:00
										 |  |  |         f.write(self._line[:6]) | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  |         f.seek(3) | 
					
						
							| 
									
										
										
										
											2001-09-24 17:34:52 +00:00
										 |  |  |         f.write(self._line[20:26]) | 
					
						
							|  |  |  |         f.write(self._line[52]) | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  |         self.assertEqual(f.getvalue(), 'abcuvwxyz!') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_writelines(self): | 
					
						
							|  |  |  |         f = self.MODULE.StringIO() | 
					
						
							| 
									
										
										
										
											2001-09-24 17:34:52 +00:00
										 |  |  |         f.writelines([self._line[0], self._line[1], self._line[2]]) | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  |         f.seek(0) | 
					
						
							|  |  |  |         self.assertEqual(f.getvalue(), 'abc') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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.seek(0) | 
					
						
							|  |  |  |         f.truncate(5) | 
					
						
							|  |  |  |         eq(f.getvalue(), 'abcde') | 
					
						
							|  |  |  |         f.close() | 
					
						
							|  |  |  |         self.assertRaises(ValueError, f.write, 'frobnitz') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_iterator(self): | 
					
						
							|  |  |  |         eq = self.assertEqual | 
					
						
							| 
									
										
										
										
											2001-09-25 21:40:04 +00:00
										 |  |  |         unless = self.failUnless | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  |         it = iter(self._fp) | 
					
						
							| 
									
										
										
										
											2001-09-25 21:40:04 +00:00
										 |  |  |         # Does this object support the iteration protocol? | 
					
						
							|  |  |  |         unless(hasattr(it, '__iter__')) | 
					
						
							|  |  |  |         unless(hasattr(it, 'next')) | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  |         i = 0 | 
					
						
							|  |  |  |         for line in self._fp: | 
					
						
							|  |  |  |             eq(line, self._line + '\n') | 
					
						
							|  |  |  |             i += 1 | 
					
						
							|  |  |  |         eq(i, 5) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestStringIO(TestGenericStringIO): | 
					
						
							|  |  |  |     MODULE = StringIO | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-06 17:15:05 +00:00
										 |  |  |     def test_unicode(self): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-05-13 09:42:16 +00:00
										 |  |  |         if not test_support.have_unicode: return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-06 17:15:05 +00: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 07:34:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-06 17:15:05 +00: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 04:33:47 +00:00
										 |  |  | class TestcStringIO(TestGenericStringIO): | 
					
						
							|  |  |  |     MODULE = cStringIO | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-12-09 20:06:32 +00: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 17:34:52 +00:00
										 |  |  | class TestBufferStringIO(TestStringIO): | 
					
						
							|  |  |  |     constructor = buffer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestBuffercStringIO(TestcStringIO): | 
					
						
							|  |  |  |     constructor = buffer | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_main(): | 
					
						
							|  |  |  |     test_support.run_unittest(TestStringIO) | 
					
						
							|  |  |  |     test_support.run_unittest(TestcStringIO) | 
					
						
							| 
									
										
										
										
											2001-09-24 17:34:52 +00:00
										 |  |  |     test_support.run_unittest(TestBufferStringIO) | 
					
						
							|  |  |  |     test_support.run_unittest(TestBuffercStringIO) | 
					
						
							| 
									
										
										
										
											2001-09-22 04:33:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test_main() |