]> granicus.if.org Git - python/commitdiff
Converted test_StringIO.py to use unittest, so
authorBarry Warsaw <barry@python.org>
Sat, 22 Sep 2001 04:33:47 +0000 (04:33 +0000)
committerBarry Warsaw <barry@python.org>
Sat, 22 Sep 2001 04:33:47 +0000 (04:33 +0000)
Lib/test/output/test_StringIO is no longer necessary.

Also, added a test of the iterator protocol that's just been added to
StringIO's and cStringIO's.

Lib/test/output/test_StringIO [deleted file]
Lib/test/test_StringIO.py

diff --git a/Lib/test/output/test_StringIO b/Lib/test/output/test_StringIO
deleted file mode 100644 (file)
index 992d534..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-test_StringIO
-abcdefghij
-klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
-
-2
-'abcuvwxyz!'
-'abc'
-'abcdefghij'
-'abcde'
-Caught expected ValueError writing to closed StringIO:
-I/O operation on closed file
-abcdefghij
-klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
-
-2
-'abcuvwxyz!'
-'abc'
-'abcdefghij'
-'abcde'
-Caught expected ValueError writing to closed StringIO:
-I/O operation on closed file
index 8d3c85142aa396af01156491b0a5fc77ba9ce9e5..eddeb105be3df71b979ef205f51827d74f389f91 100644 (file)
@@ -1,43 +1,73 @@
 # Tests StringIO and cStringIO
 
-def do_test(module):
-    s = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+'\n')*5
-    f = module.StringIO(s)
-    print f.read(10)
-    print f.readline()
-    print len(f.readlines(60))
-
-    f = module.StringIO()
-    f.write('abcdef')
-    f.seek(3)
-    f.write('uvwxyz')
-    f.write('!')
-    print `f.getvalue()`
-    f.close()
-
-    f = module.StringIO()
-    f.writelines(["a", "b", "c"])
-    f.seek(0)
-    print `f.getvalue()`
-    f.close()
-
-    f = module.StringIO()
-    f.write(s)
-    f.seek(10)
-    f.truncate()
-    print `f.getvalue()`
-    f.seek(0)
-    f.truncate(5)
-    print `f.getvalue()`
-    f.close()
-    try:
-        f.write("frobnitz")
-    except ValueError, e:
-        print "Caught expected ValueError writing to closed StringIO:"
-        print e
-    else:
-        print "Failed to catch ValueError writing to closed StringIO."
-
-import StringIO, cStringIO
-do_test(StringIO)
-do_test(cStringIO)
+import unittest
+import StringIO
+import cStringIO
+import types
+import test_support
+
+
+class TestGenericStringIO(unittest.TestCase):
+    # use a class variable MODULE to define which module is being tested
+
+    def setUp(self):
+        self._line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+        self._lines = (self._line + '\n') * 5
+        self._fp = self.MODULE.StringIO(self._lines)
+
+    def test_reads(self):
+        eq = self.assertEqual
+        eq(self._fp.read(10), 'abcdefghij')
+        eq(self._fp.readline(), 'klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n')
+        eq(len(self._fp.readlines(60)), 2)
+
+    def test_writes(self):
+        f = self.MODULE.StringIO()
+        f.write('abcdef')
+        f.seek(3)
+        f.write('uvwxyz')
+        f.write('!')
+        self.assertEqual(f.getvalue(), 'abcuvwxyz!')
+
+    def test_writelines(self):
+        f = self.MODULE.StringIO()
+        f.writelines(['a', 'b', 'c'])
+        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
+        it = iter(self._fp)
+        self.failUnless(isinstance(it, types.FunctionIterType))
+        i = 0
+        for line in self._fp:
+            eq(line, self._line + '\n')
+            i += 1
+        eq(i, 5)
+
+class TestStringIO(TestGenericStringIO):
+    MODULE = StringIO
+
+class TestcStringIO(TestGenericStringIO):
+    MODULE = cStringIO
+
+
+def test_main():
+    test_support.run_unittest(TestStringIO)
+    test_support.run_unittest(TestcStringIO)
+
+if __name__ == '__main__':
+    test_main()