1 | n/a | '''Test warnings replacement in pyshell.py and run.py. |
---|
2 | n/a | |
---|
3 | n/a | This file could be expanded to include traceback overrides |
---|
4 | n/a | (in same two modules). If so, change name. |
---|
5 | n/a | Revise if output destination changes (http://bugs.python.org/issue18318). |
---|
6 | n/a | Make sure warnings module is left unaltered (http://bugs.python.org/issue18081). |
---|
7 | n/a | ''' |
---|
8 | n/a | |
---|
9 | n/a | import unittest |
---|
10 | n/a | from test.support import captured_stderr |
---|
11 | n/a | |
---|
12 | n/a | import warnings |
---|
13 | n/a | # Try to capture default showwarning before Idle modules are imported. |
---|
14 | n/a | showwarning = warnings.showwarning |
---|
15 | n/a | # But if we run this file within idle, we are in the middle of the run.main loop |
---|
16 | n/a | # and default showwarnings has already been replaced. |
---|
17 | n/a | running_in_idle = 'idle' in showwarning.__name__ |
---|
18 | n/a | |
---|
19 | n/a | from idlelib import run |
---|
20 | n/a | from idlelib import pyshell as shell |
---|
21 | n/a | |
---|
22 | n/a | # The following was generated from pyshell.idle_formatwarning |
---|
23 | n/a | # and checked as matching expectation. |
---|
24 | n/a | idlemsg = ''' |
---|
25 | n/a | Warning (from warnings module): |
---|
26 | n/a | File "test_warning.py", line 99 |
---|
27 | n/a | Line of code |
---|
28 | n/a | UserWarning: Test |
---|
29 | n/a | ''' |
---|
30 | n/a | shellmsg = idlemsg + ">>> " |
---|
31 | n/a | |
---|
32 | n/a | class RunWarnTest(unittest.TestCase): |
---|
33 | n/a | |
---|
34 | n/a | @unittest.skipIf(running_in_idle, "Does not work when run within Idle.") |
---|
35 | n/a | def test_showwarnings(self): |
---|
36 | n/a | self.assertIs(warnings.showwarning, showwarning) |
---|
37 | n/a | run.capture_warnings(True) |
---|
38 | n/a | self.assertIs(warnings.showwarning, run.idle_showwarning_subproc) |
---|
39 | n/a | run.capture_warnings(False) |
---|
40 | n/a | self.assertIs(warnings.showwarning, showwarning) |
---|
41 | n/a | |
---|
42 | n/a | def test_run_show(self): |
---|
43 | n/a | with captured_stderr() as f: |
---|
44 | n/a | run.idle_showwarning_subproc( |
---|
45 | n/a | 'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code') |
---|
46 | n/a | # The following uses .splitlines to erase line-ending differences |
---|
47 | n/a | self.assertEqual(idlemsg.splitlines(), f.getvalue().splitlines()) |
---|
48 | n/a | |
---|
49 | n/a | class ShellWarnTest(unittest.TestCase): |
---|
50 | n/a | |
---|
51 | n/a | @unittest.skipIf(running_in_idle, "Does not work when run within Idle.") |
---|
52 | n/a | def test_showwarnings(self): |
---|
53 | n/a | self.assertIs(warnings.showwarning, showwarning) |
---|
54 | n/a | shell.capture_warnings(True) |
---|
55 | n/a | self.assertIs(warnings.showwarning, shell.idle_showwarning) |
---|
56 | n/a | shell.capture_warnings(False) |
---|
57 | n/a | self.assertIs(warnings.showwarning, showwarning) |
---|
58 | n/a | |
---|
59 | n/a | def test_idle_formatter(self): |
---|
60 | n/a | # Will fail if format changed without regenerating idlemsg |
---|
61 | n/a | s = shell.idle_formatwarning( |
---|
62 | n/a | 'Test', UserWarning, 'test_warning.py', 99, 'Line of code') |
---|
63 | n/a | self.assertEqual(idlemsg, s) |
---|
64 | n/a | |
---|
65 | n/a | def test_shell_show(self): |
---|
66 | n/a | with captured_stderr() as f: |
---|
67 | n/a | shell.idle_showwarning( |
---|
68 | n/a | 'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code') |
---|
69 | n/a | self.assertEqual(shellmsg.splitlines(), f.getvalue().splitlines()) |
---|
70 | n/a | |
---|
71 | n/a | |
---|
72 | n/a | if __name__ == '__main__': |
---|
73 | n/a | unittest.main(verbosity=2, exit=False) |
---|