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