Skip to content

Commit 382299e

Browse files
committed
Add test case for help(....) function
1 parent aab2f5b commit 382299e

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

Lib/idlelib/idle_test/test_warning.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
Revise if output destination changes (http://bugs.python.org/issue18318).
66
Make sure warnings module is left unaltered (http://bugs.python.org/issue18081).
77
'''
8-
from idlelib import run
9-
from idlelib import pyshell as shell
8+
import sys
109
import unittest
11-
from test.support import captured_stderr
1210
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+
1317

1418
# Try to capture default showwarning before Idle modules are imported.
1519
showwarning = warnings.showwarning
@@ -68,6 +72,47 @@ def test_shell_show(self):
6872
'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code')
6973
self.assertEqual(shellmsg.splitlines(), f.getvalue().splitlines())
7074

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")
71116

72117
if __name__ == '__main__':
73118
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)