Skip to content

Commit e196818

Browse files
committed
new test cases for help() about dunder methods
1 parent 35b4470 commit e196818

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

Lib/test/test_pydoc/test_pydoc.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,12 @@ class C(builtins.object)
229229
for s in expected_data_docstrings)
230230

231231
# output pattern for missing module
232-
missing_pattern = ('''\
232+
def missing_pattern(name, dunder=False):
233+
return ('''\
233234
No help entry found for %%r.
234-
Use help() to get the interactive help utility.
235+
%%sUse help() to get the interactive help utility.
235236
Use help(str) for help on the str class.
236-
Additional documentation is available online at https://docs.python.org/%s.%s/''' % sys.version_info[:2]).replace('\n', os.linesep)
237+
Additional documentation is available online at https://docs.python.org/%s.%s/''' % sys.version_info[:2]).replace('\n', os.linesep) % (name, "Use help('specialnames') for a list of special names for which help is available.\n" if dunder else "")
237238

238239
# output pattern for module with bad imports
239240
badimport_pattern = "problem in %s - ModuleNotFoundError: No module named %r"
@@ -498,7 +499,33 @@ class B:
498499
def test_not_here(self):
499500
missing_module = "test.i_am_not_here"
500501
result = str(run_pydoc_fail(missing_module), 'ascii')
501-
expected = missing_pattern % missing_module
502+
expected = missing_pattern(missing_module)
503+
self.assertEqual(expected, result,
504+
"documentation for missing module found")
505+
506+
def test_dunder_help(self):
507+
def get_main_output(topic):
508+
return run_pydoc(topic).split(b'Related help topics:')[0].strip()
509+
510+
# check that each dunder method maps to a help topic that includes its
511+
# name
512+
for name in pydoc.Helper.dunders:
513+
self.assertIn(name.encode('ascii'), get_main_output(name))
514+
515+
# check that right-hand and in-place versions match
516+
self.assertEqual(get_main_output('__add__'), get_main_output('__radd__'))
517+
self.assertEqual(get_main_output('__add__'), get_main_output('__iadd__'))
518+
519+
def test_dunder_main(self):
520+
self.assertEqual(run_pydoc('__main__').splitlines(False)[0],
521+
'"__main__" — Top-level code environment'.encode('utf-8'))
522+
self.assertEqual(run_pydoc('__name__').splitlines(False)[0],
523+
'''"__name__ == '__main__'"'''.encode('utf-8'))
524+
525+
def test_dunder_not_here(self):
526+
missing_module = "__dict__"
527+
result = str(run_pydoc_fail(missing_module), 'ascii')
528+
expected = missing_pattern(missing_module, dunder=True)
502529
self.assertEqual(expected, result,
503530
"documentation for missing module found")
504531

@@ -511,7 +538,7 @@ def test_not_ascii(self):
511538
def test_input_strip(self):
512539
missing_module = " test.i_am_not_here "
513540
result = str(run_pydoc_fail(missing_module), 'ascii')
514-
expected = missing_pattern % missing_module.strip()
541+
expected = missing_pattern(missing_module.strip())
515542
self.assertEqual(expected, result)
516543

517544
def test_stripid(self):
@@ -653,6 +680,8 @@ class ZeroDivisionError(ArithmeticError)
653680
# Testing that the subclasses section does not appear
654681
self.assertNotIn('Built-in subclasses', text)
655682

683+
684+
656685
def test_builtin_on_metaclasses(self):
657686
"""Tests help on metaclasses.
658687
@@ -665,7 +694,7 @@ def test_builtin_on_metaclasses(self):
665694
self.assertNotIn('Built-in subclasses', text)
666695

667696
def test_fail_help_cli(self):
668-
elines = (missing_pattern % 'abd').splitlines()
697+
elines = (missing_pattern("abd")).splitlines()
669698
with spawn_python("-c" "help()") as proc:
670699
out, _ = proc.communicate(b"abd")
671700
olines = out.decode().splitlines()[-10:-6]
@@ -676,7 +705,7 @@ def test_fail_help_output_redirect(self):
676705
with StringIO() as buf:
677706
helper = pydoc.Helper(output=buf)
678707
helper.help("abd")
679-
expected = missing_pattern % "abd"
708+
expected = missing_pattern("abd")
680709
self.assertEqual(expected, buf.getvalue().strip().replace('\n', os.linesep))
681710

682711
@unittest.skipIf(hasattr(sys, 'gettrace') and sys.gettrace(),
@@ -738,6 +767,8 @@ def run_pydoc_for_request(request, expected_text_part):
738767
run_pydoc_for_request('keywords', 'Here is a list of the Python keywords.')
739768
# test for "symbols"
740769
run_pydoc_for_request('symbols', 'Here is a list of the punctuation symbols')
770+
# test for "specialnames"
771+
run_pydoc_for_request('specialnames', 'Here is a list of special names')
741772
# test for "topics"
742773
run_pydoc_for_request('topics', 'Here is a list of available topics.')
743774
# test for "modules" skipped, see test_modules()

0 commit comments

Comments
 (0)