Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / lib / python2.4 / test / test_StringIO.py
CommitLineData
86530b38
AT
1# Tests StringIO and cStringIO
2
3import unittest
4import StringIO
5import cStringIO
6import types
7from test import test_support
8
9
10class 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 f.close()
66 self.assertRaises(ValueError, f.write, 'frobnitz')
67
68 def test_closed_flag(self):
69 f = self.MODULE.StringIO()
70 self.assertEqual(f.closed, False)
71 f.close()
72 self.assertEqual(f.closed, True)
73 f = self.MODULE.StringIO("abc")
74 self.assertEqual(f.closed, False)
75 f.close()
76 self.assertEqual(f.closed, True)
77
78 def test_iterator(self):
79 eq = self.assertEqual
80 unless = self.failUnless
81 eq(iter(self._fp), self._fp)
82 # Does this object support the iteration protocol?
83 unless(hasattr(self._fp, '__iter__'))
84 unless(hasattr(self._fp, 'next'))
85 i = 0
86 for line in self._fp:
87 eq(line, self._line + '\n')
88 i += 1
89 eq(i, 5)
90
91class TestStringIO(TestGenericStringIO):
92 MODULE = StringIO
93
94 def test_unicode(self):
95
96 if not test_support.have_unicode: return
97
98 # The StringIO module also supports concatenating Unicode
99 # snippets to larger Unicode strings. This is tested by this
100 # method. Note that cStringIO does not support this extension.
101
102 f = self.MODULE.StringIO()
103 f.write(self._line[:6])
104 f.seek(3)
105 f.write(unicode(self._line[20:26]))
106 f.write(unicode(self._line[52]))
107 s = f.getvalue()
108 self.assertEqual(s, unicode('abcuvwxyz!'))
109 self.assertEqual(type(s), types.UnicodeType)
110
111class TestcStringIO(TestGenericStringIO):
112 MODULE = cStringIO
113
114import sys
115if sys.platform.startswith('java'):
116 # Jython doesn't have a buffer object, so we just do a useless
117 # fake of the buffer tests.
118 buffer = str
119
120class TestBufferStringIO(TestStringIO):
121 constructor = buffer
122
123class TestBuffercStringIO(TestcStringIO):
124 constructor = buffer
125
126
127def test_main():
128 test_support.run_unittest(
129 TestStringIO,
130 TestcStringIO,
131 TestBufferStringIO,
132 TestBuffercStringIO
133 )
134
135if __name__ == '__main__':
136 test_main()