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