test_StringIO.py 4.18 KB
Newer Older
1 2
# Tests StringIO and cStringIO

3 4 5 6
import unittest
import StringIO
import cStringIO
import types
7
from test import test_support
8 9 10 11 12


class TestGenericStringIO(unittest.TestCase):
    # use a class variable MODULE to define which module is being tested

13 14 15 16 17 18 19
    # Line of data to test as string
    _line = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'

    # Constructor to use for the test data (._line is passed to this
    # constructor)
    constructor = str

20
    def setUp(self):
21 22
        self._line = self.constructor(self._line)
        self._lines = self.constructor((self._line + '\n') * 5)
23 24 25 26
        self._fp = self.MODULE.StringIO(self._lines)

    def test_reads(self):
        eq = self.assertEqual
27
        self.assertRaises(TypeError, self._fp.seek)
28 29
        eq(self._fp.read(10), self._line[:10])
        eq(self._fp.readline(), self._line[10:] + '\n')
30 31 32 33
        eq(len(self._fp.readlines(60)), 2)

    def test_writes(self):
        f = self.MODULE.StringIO()
34
        self.assertRaises(TypeError, f.seek)
35
        f.write(self._line[:6])
36
        f.seek(3)
37 38
        f.write(self._line[20:26])
        f.write(self._line[52])
39 40 41 42
        self.assertEqual(f.getvalue(), 'abcuvwxyz!')

    def test_writelines(self):
        f = self.MODULE.StringIO()
43
        f.writelines([self._line[0], self._line[1], self._line[2]])
44 45 46
        f.seek(0)
        self.assertEqual(f.getvalue(), 'abc')

47 48 49 50 51 52 53
    def test_writelines_error(self):
        def errorGen():
            yield 'a'
            raise KeyboardInterrupt()
        f = self.MODULE.StringIO()
        self.assertRaises(KeyboardInterrupt, f.writelines, errorGen())

54 55 56 57 58 59 60 61 62
    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.truncate(5)
        eq(f.getvalue(), 'abcde')
63 64
        f.write('xyz')
        eq(f.getvalue(), 'abcdexyz')
65
        self.assertRaises(IOError, f.truncate, -1)
66 67 68
        f.close()
        self.assertRaises(ValueError, f.write, 'frobnitz')

69 70 71 72 73 74 75 76 77 78
    def test_closed_flag(self):
        f = self.MODULE.StringIO()
        self.assertEqual(f.closed, False)
        f.close()
        self.assertEqual(f.closed, True)
        f = self.MODULE.StringIO("abc")
        self.assertEqual(f.closed, False)
        f.close()
        self.assertEqual(f.closed, True)

79 80 81 82 83 84 85
    def test_isatty(self):
        f = self.MODULE.StringIO()
        self.assertRaises(TypeError, f.isatty, None)
        self.assertEqual(f.isatty(), False)
        f.close()
        self.assertRaises(ValueError, f.isatty)

86 87
    def test_iterator(self):
        eq = self.assertEqual
88
        unless = self.failUnless
89
        eq(iter(self._fp), self._fp)
90
        # Does this object support the iteration protocol?
91 92
        unless(hasattr(self._fp, '__iter__'))
        unless(hasattr(self._fp, 'next'))
93 94 95 96 97
        i = 0
        for line in self._fp:
            eq(line, self._line + '\n')
            i += 1
        eq(i, 5)
98 99
        self._fp.close()
        self.assertRaises(ValueError, self._fp.next)
100 101 102 103

class TestStringIO(TestGenericStringIO):
    MODULE = StringIO

104 105
    def test_unicode(self):

106 107
        if not test_support.have_unicode: return

108 109 110
        # 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.
Tim Peters's avatar
Tim Peters committed
111

112 113 114 115 116 117 118 119 120
        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)

121 122 123
class TestcStringIO(TestGenericStringIO):
    MODULE = cStringIO

124 125 126 127 128 129
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

130 131 132 133 134 135
class TestBufferStringIO(TestStringIO):
    constructor = buffer

class TestBuffercStringIO(TestcStringIO):
    constructor = buffer

136 137

def test_main():
138 139 140 141 142 143
    test_support.run_unittest(
        TestStringIO,
        TestcStringIO,
        TestBufferStringIO,
        TestBuffercStringIO
    )
144 145 146

if __name__ == '__main__':
    test_main()