|
5 | 5 | Revise if output destination changes (http://bugs.python.org/issue18318). |
6 | 6 | Make sure warnings module is left unaltered (http://bugs.python.org/issue18081). |
7 | 7 | ''' |
8 | | -from idlelib import run |
9 | | -from idlelib import pyshell as shell |
| 8 | +import sys |
10 | 9 | import unittest |
11 | | -from test.support import captured_stderr |
12 | 10 | import warnings |
| 11 | +from test.support import captured_stderr |
| 12 | +from types import ModuleType |
| 13 | + |
| 14 | +from idlelib import pyshell as shell |
| 15 | +from idlelib import run |
| 16 | + |
13 | 17 |
|
14 | 18 | # Try to capture default showwarning before Idle modules are imported. |
15 | 19 | showwarning = warnings.showwarning |
@@ -68,6 +72,47 @@ def test_shell_show(self): |
68 | 72 | 'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code') |
69 | 73 | self.assertEqual(shellmsg.splitlines(), f.getvalue().splitlines()) |
70 | 74 |
|
| 75 | +class TestDeprecatedHelp(unittest.TestCase): |
| 76 | + def setUp(self): |
| 77 | + self.module = ModuleType("testmodule") |
| 78 | + self.code = r""" |
| 79 | + from warnings import deprecated |
| 80 | +
|
| 81 | + @deprecated("Test") |
| 82 | + class A: |
| 83 | + \"\"\"This is class A's docstring.\"\"\" |
| 84 | + pass |
| 85 | + """ |
| 86 | + exec(self.code, self.module.__dict__) |
| 87 | + sys.modules["testmodule"] = self.module |
| 88 | + |
| 89 | + def tearDown(self): |
| 90 | + if "testmodule" in sys.modules: |
| 91 | + del sys.modules["testmodule"] |
| 92 | + |
| 93 | + def test_help_output(self): |
| 94 | + # Capture the help output |
| 95 | + import io |
| 96 | + from contextlib import redirect_stdout |
| 97 | + |
| 98 | + f = io.StringIO() |
| 99 | + with redirect_stdout(f): |
| 100 | + help(self.module) |
| 101 | + |
| 102 | + help_output = f.getvalue() |
| 103 | + |
| 104 | + self.assertIn("[DEPRECATED] Test", help_output) |
| 105 | + self.assertIn("This is class A's docstring", help_output) |
| 106 | + |
| 107 | + def test_deprecation_warning(self): |
| 108 | + # Verify the deprecation warning is raised when instantiating the class |
| 109 | + with warnings.catch_warnings(record=True) as w: |
| 110 | + warnings.simplefilter("always") |
| 111 | + self.module.A() |
| 112 | + |
| 113 | + self.assertEqual(len(w), 1) |
| 114 | + self.assertTrue(issubclass(w[0].category, DeprecationWarning)) |
| 115 | + self.assertEqual(str(w[0].message), "Test") |
71 | 116 |
|
72 | 117 | if __name__ == '__main__': |
73 | 118 | unittest.main(verbosity=2) |
0 commit comments