|
1 | 1 | "Test run, coverage 49%." |
2 | 2 |
|
3 | 3 | from idlelib import run |
| 4 | +import io |
| 5 | +import sys |
| 6 | +from test.support import captured_output, captured_stderr |
4 | 7 | import unittest |
5 | 8 | from unittest import mock |
| 9 | +import idlelib |
6 | 10 | from idlelib.idle_test.mock_idle import Func |
7 | | -from test.support import captured_output, captured_stderr |
8 | 11 |
|
9 | | -import io |
10 | | -import sys |
| 12 | +idlelib.testing = True # Use {} for executing test user code. |
11 | 13 |
|
12 | 14 |
|
13 | | -class RunTest(unittest.TestCase): |
| 15 | +class PrintExceptionTest(unittest.TestCase): |
14 | 16 |
|
15 | 17 | def test_print_exception_unhashable(self): |
16 | 18 | class UnhashableException(Exception): |
@@ -351,5 +353,38 @@ def test_fatal_error(self): |
351 | 353 | self.assertIn('IndexError', msg) |
352 | 354 | eq(func.called, 2) |
353 | 355 |
|
| 356 | + |
| 357 | +class ExecRuncodeTest(unittest.TestCase): |
| 358 | + |
| 359 | + @classmethod |
| 360 | + def setUpClass(cls): |
| 361 | + cls.addClassCleanup(setattr,run,'print_exception',run.print_exception) |
| 362 | + cls.prt = Func() # Need reference. |
| 363 | + run.print_exception = cls.prt |
| 364 | + mockrpc = mock.Mock() |
| 365 | + mockrpc.console.getvar = Func(result=False) |
| 366 | + cls.ex = run.Executive(mockrpc) |
| 367 | + |
| 368 | + @classmethod |
| 369 | + def tearDownClass(cls): |
| 370 | + assert sys.excepthook == sys.__excepthook__ |
| 371 | + |
| 372 | + def test_exceptions(self): |
| 373 | + ex = self.ex |
| 374 | + ex.runcode('1/0') |
| 375 | + self.assertIs(ex.user_exc_info[0], ZeroDivisionError) |
| 376 | + |
| 377 | + self.addCleanup(setattr, sys, 'excepthook', sys.__excepthook__) |
| 378 | + sys.excepthook = lambda t, e, tb: run.print_exception(t) |
| 379 | + ex.runcode('1/0') |
| 380 | + self.assertIs(self.prt.args[0], ZeroDivisionError) |
| 381 | + |
| 382 | + sys.excepthook = lambda: None |
| 383 | + ex.runcode('1/0') |
| 384 | + t, e, tb = ex.user_exc_info |
| 385 | + self.assertIs(t, TypeError) |
| 386 | + self.assertTrue(isinstance(e.__context__, ZeroDivisionError)) |
| 387 | + |
| 388 | + |
354 | 389 | if __name__ == '__main__': |
355 | 390 | unittest.main(verbosity=2) |
0 commit comments