ยปCore Development>Code coverage>Lib/idlelib/idle_test/test_warning.py

Python code coverage for Lib/idlelib/idle_test/test_warning.py

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