• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2import io
3from idlelib.PyShell import PseudoInputFile, PseudoOutputFile
4from test import test_support as support
5
6
7class Base(object):
8    def __str__(self):
9        return '%s:str' % type(self).__name__
10    def __unicode__(self):
11        return '%s:unicode' % type(self).__name__
12    def __len__(self):
13        return 3
14    def __iter__(self):
15        return iter('abc')
16    def __getitem__(self, *args):
17        return '%s:item' % type(self).__name__
18    def __getslice__(self, *args):
19        return '%s:slice' % type(self).__name__
20
21class S(Base, str):
22    pass
23
24class U(Base, unicode):
25    pass
26
27class BA(Base, bytearray):
28    pass
29
30class MockShell:
31    def __init__(self):
32        self.reset()
33
34    def write(self, *args):
35        self.written.append(args)
36
37    def readline(self):
38        return self.lines.pop()
39
40    def close(self):
41        pass
42
43    def reset(self):
44        self.written = []
45
46    def push(self, lines):
47        self.lines = list(lines)[::-1]
48
49
50class PseudeOutputFilesTest(unittest.TestCase):
51    def test_misc(self):
52        shell = MockShell()
53        f = PseudoOutputFile(shell, 'stdout', 'utf-8')
54        self.assertIsInstance(f, io.TextIOBase)
55        self.assertEqual(f.encoding, 'utf-8')
56        self.assertIsNone(f.errors)
57        self.assertIsNone(f.newlines)
58        self.assertEqual(f.name, '<stdout>')
59        self.assertFalse(f.closed)
60        self.assertTrue(f.isatty())
61        self.assertFalse(f.readable())
62        self.assertTrue(f.writable())
63        self.assertFalse(f.seekable())
64
65    def test_unsupported(self):
66        shell = MockShell()
67        f = PseudoOutputFile(shell, 'stdout', 'utf-8')
68        self.assertRaises(IOError, f.fileno)
69        self.assertRaises(IOError, f.tell)
70        self.assertRaises(IOError, f.seek, 0)
71        self.assertRaises(IOError, f.read, 0)
72        self.assertRaises(IOError, f.readline, 0)
73
74    def test_write(self):
75        shell = MockShell()
76        f = PseudoOutputFile(shell, 'stdout', 'utf-8')
77        f.write('test')
78        self.assertEqual(shell.written, [('test', 'stdout')])
79        shell.reset()
80        f.write('t\xe8st')
81        self.assertEqual(shell.written, [('t\xe8st', 'stdout')])
82        shell.reset()
83        f.write(u't\xe8st')
84        self.assertEqual(shell.written, [(u't\xe8st', 'stdout')])
85        shell.reset()
86
87        f.write(S('t\xe8st'))
88        self.assertEqual(shell.written, [('t\xe8st', 'stdout')])
89        self.assertEqual(type(shell.written[0][0]), str)
90        shell.reset()
91        f.write(BA('t\xe8st'))
92        self.assertEqual(shell.written, [('t\xe8st', 'stdout')])
93        self.assertEqual(type(shell.written[0][0]), str)
94        shell.reset()
95        f.write(U(u't\xe8st'))
96        self.assertEqual(shell.written, [(u't\xe8st', 'stdout')])
97        self.assertEqual(type(shell.written[0][0]), unicode)
98        shell.reset()
99
100        self.assertRaises(TypeError, f.write)
101        self.assertEqual(shell.written, [])
102        self.assertRaises(TypeError, f.write, 123)
103        self.assertEqual(shell.written, [])
104        self.assertRaises(TypeError, f.write, 'test', 'spam')
105        self.assertEqual(shell.written, [])
106
107    def test_writelines(self):
108        shell = MockShell()
109        f = PseudoOutputFile(shell, 'stdout', 'utf-8')
110        f.writelines([])
111        self.assertEqual(shell.written, [])
112        shell.reset()
113        f.writelines(['one\n', 'two'])
114        self.assertEqual(shell.written,
115                         [('one\n', 'stdout'), ('two', 'stdout')])
116        shell.reset()
117        f.writelines(['on\xe8\n', 'tw\xf2'])
118        self.assertEqual(shell.written,
119                         [('on\xe8\n', 'stdout'), ('tw\xf2', 'stdout')])
120        shell.reset()
121        f.writelines([u'on\xe8\n', u'tw\xf2'])
122        self.assertEqual(shell.written,
123                         [(u'on\xe8\n', 'stdout'), (u'tw\xf2', 'stdout')])
124        shell.reset()
125
126        f.writelines([S('t\xe8st')])
127        self.assertEqual(shell.written, [('t\xe8st', 'stdout')])
128        self.assertEqual(type(shell.written[0][0]), str)
129        shell.reset()
130        f.writelines([BA('t\xe8st')])
131        self.assertEqual(shell.written, [('t\xe8st', 'stdout')])
132        self.assertEqual(type(shell.written[0][0]), str)
133        shell.reset()
134        f.writelines([U(u't\xe8st')])
135        self.assertEqual(shell.written, [(u't\xe8st', 'stdout')])
136        self.assertEqual(type(shell.written[0][0]), unicode)
137        shell.reset()
138
139        self.assertRaises(TypeError, f.writelines)
140        self.assertEqual(shell.written, [])
141        self.assertRaises(TypeError, f.writelines, 123)
142        self.assertEqual(shell.written, [])
143        self.assertRaises(TypeError, f.writelines, [123])
144        self.assertEqual(shell.written, [])
145        self.assertRaises(TypeError, f.writelines, [], [])
146        self.assertEqual(shell.written, [])
147
148    def test_close(self):
149        shell = MockShell()
150        f = PseudoOutputFile(shell, 'stdout', 'utf-8')
151        self.assertFalse(f.closed)
152        f.write('test')
153        f.close()
154        self.assertTrue(f.closed)
155        self.assertRaises(ValueError, f.write, 'x')
156        self.assertEqual(shell.written, [('test', 'stdout')])
157        f.close()
158        self.assertRaises(TypeError, f.close, 1)
159
160
161class PseudeInputFilesTest(unittest.TestCase):
162    def test_misc(self):
163        shell = MockShell()
164        f = PseudoInputFile(shell, 'stdin', 'utf-8')
165        self.assertIsInstance(f, io.TextIOBase)
166        self.assertEqual(f.encoding, 'utf-8')
167        self.assertIsNone(f.errors)
168        self.assertIsNone(f.newlines)
169        self.assertEqual(f.name, '<stdin>')
170        self.assertFalse(f.closed)
171        self.assertTrue(f.isatty())
172        self.assertTrue(f.readable())
173        self.assertFalse(f.writable())
174        self.assertFalse(f.seekable())
175
176    def test_unsupported(self):
177        shell = MockShell()
178        f = PseudoInputFile(shell, 'stdin', 'utf-8')
179        self.assertRaises(IOError, f.fileno)
180        self.assertRaises(IOError, f.tell)
181        self.assertRaises(IOError, f.seek, 0)
182        self.assertRaises(IOError, f.write, 'x')
183        self.assertRaises(IOError, f.writelines, ['x'])
184
185    def test_read(self):
186        shell = MockShell()
187        f = PseudoInputFile(shell, 'stdin', 'utf-8')
188        shell.push(['one\n', 'two\n', ''])
189        self.assertEqual(f.read(), 'one\ntwo\n')
190        shell.push(['one\n', 'two\n', ''])
191        self.assertEqual(f.read(-1), 'one\ntwo\n')
192        shell.push(['one\n', 'two\n', ''])
193        self.assertEqual(f.read(None), 'one\ntwo\n')
194        shell.push(['one\n', 'two\n', 'three\n', ''])
195        self.assertEqual(f.read(2), 'on')
196        self.assertEqual(f.read(3), 'e\nt')
197        self.assertEqual(f.read(10), 'wo\nthree\n')
198
199        shell.push(['one\n', 'two\n'])
200        self.assertEqual(f.read(0), '')
201        self.assertRaises(TypeError, f.read, 1.5)
202        self.assertRaises(TypeError, f.read, '1')
203        self.assertRaises(TypeError, f.read, 1, 1)
204
205    def test_readline(self):
206        shell = MockShell()
207        f = PseudoInputFile(shell, 'stdin', 'utf-8')
208        shell.push(['one\n', 'two\n', 'three\n', 'four\n'])
209        self.assertEqual(f.readline(), 'one\n')
210        self.assertEqual(f.readline(-1), 'two\n')
211        self.assertEqual(f.readline(None), 'three\n')
212        shell.push(['one\ntwo\n'])
213        self.assertEqual(f.readline(), 'one\n')
214        self.assertEqual(f.readline(), 'two\n')
215        shell.push(['one', 'two', 'three'])
216        self.assertEqual(f.readline(), 'one')
217        self.assertEqual(f.readline(), 'two')
218        shell.push(['one\n', 'two\n', 'three\n'])
219        self.assertEqual(f.readline(2), 'on')
220        self.assertEqual(f.readline(1), 'e')
221        self.assertEqual(f.readline(1), '\n')
222        self.assertEqual(f.readline(10), 'two\n')
223
224        shell.push(['one\n', 'two\n'])
225        self.assertEqual(f.readline(0), '')
226        self.assertRaises(TypeError, f.readlines, 1.5)
227        self.assertRaises(TypeError, f.readlines, '1')
228        self.assertRaises(TypeError, f.readlines, 1, 1)
229
230    def test_readlines(self):
231        shell = MockShell()
232        f = PseudoInputFile(shell, 'stdin', 'utf-8')
233        shell.push(['one\n', 'two\n', ''])
234        self.assertEqual(f.readlines(), ['one\n', 'two\n'])
235        shell.push(['one\n', 'two\n', ''])
236        self.assertEqual(f.readlines(-1), ['one\n', 'two\n'])
237        shell.push(['one\n', 'two\n', ''])
238        self.assertEqual(f.readlines(None), ['one\n', 'two\n'])
239        shell.push(['one\n', 'two\n', ''])
240        self.assertEqual(f.readlines(0), ['one\n', 'two\n'])
241        shell.push(['one\n', 'two\n', ''])
242        self.assertEqual(f.readlines(3), ['one\n'])
243        shell.push(['one\n', 'two\n', ''])
244        self.assertEqual(f.readlines(4), ['one\n', 'two\n'])
245
246        shell.push(['one\n', 'two\n', ''])
247        self.assertRaises(TypeError, f.readlines, 1.5)
248        self.assertRaises(TypeError, f.readlines, '1')
249        self.assertRaises(TypeError, f.readlines, 1, 1)
250
251    def test_close(self):
252        shell = MockShell()
253        f = PseudoInputFile(shell, 'stdin', 'utf-8')
254        shell.push(['one\n', 'two\n', ''])
255        self.assertFalse(f.closed)
256        self.assertEqual(f.readline(), 'one\n')
257        f.close()
258        self.assertFalse(f.closed)
259        self.assertEqual(f.readline(), 'two\n')
260        self.assertRaises(TypeError, f.close, 1)
261
262
263def test_main():
264    support.run_unittest(PseudeOutputFilesTest, PseudeInputFilesTest)
265
266if __name__ == '__main__':
267    test_main()
268