mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Issue 21786: Clean-up test_pydoc taking taking advantage of diffing in unittest.
This commit is contained in:
parent
986efa074e
commit
bb91c1d019
1 changed files with 7 additions and 33 deletions
|
@ -2,7 +2,6 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import builtins
|
import builtins
|
||||||
import contextlib
|
import contextlib
|
||||||
import difflib
|
|
||||||
import inspect
|
import inspect
|
||||||
import pydoc
|
import pydoc
|
||||||
import keyword
|
import keyword
|
||||||
|
@ -356,15 +355,6 @@ def get_pydoc_text(module):
|
||||||
output = patt.sub('', output)
|
output = patt.sub('', output)
|
||||||
return output.strip(), loc
|
return output.strip(), loc
|
||||||
|
|
||||||
def print_diffs(text1, text2):
|
|
||||||
"Prints unified diffs for two texts"
|
|
||||||
# XXX now obsolete, use unittest built-in support
|
|
||||||
lines1 = text1.splitlines(keepends=True)
|
|
||||||
lines2 = text2.splitlines(keepends=True)
|
|
||||||
diffs = difflib.unified_diff(lines1, lines2, n=0, fromfile='expected',
|
|
||||||
tofile='got')
|
|
||||||
print('\n' + ''.join(diffs))
|
|
||||||
|
|
||||||
def get_html_title(text):
|
def get_html_title(text):
|
||||||
# Bit of hack, but good enough for test purposes
|
# Bit of hack, but good enough for test purposes
|
||||||
header, _, _ = text.partition("</head>")
|
header, _, _ = text.partition("</head>")
|
||||||
|
@ -414,9 +404,7 @@ class PydocDocTest(unittest.TestCase):
|
||||||
expected_html = expected_html_pattern % (
|
expected_html = expected_html_pattern % (
|
||||||
(mod_url, mod_file, doc_loc) +
|
(mod_url, mod_file, doc_loc) +
|
||||||
expected_html_data_docstrings)
|
expected_html_data_docstrings)
|
||||||
if result != expected_html:
|
self.assertEqual(result, expected_html)
|
||||||
print_diffs(expected_html, result)
|
|
||||||
self.fail("outputs are not equal, see diff above")
|
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
||||||
"Docstrings are omitted with -O2 and above")
|
"Docstrings are omitted with -O2 and above")
|
||||||
|
@ -429,9 +417,7 @@ class PydocDocTest(unittest.TestCase):
|
||||||
(doc_loc,) +
|
(doc_loc,) +
|
||||||
expected_text_data_docstrings +
|
expected_text_data_docstrings +
|
||||||
(inspect.getabsfile(pydoc_mod),))
|
(inspect.getabsfile(pydoc_mod),))
|
||||||
if result != expected_text:
|
self.assertEqual(expected_text, result)
|
||||||
print_diffs(expected_text, result)
|
|
||||||
self.fail("outputs are not equal, see diff above")
|
|
||||||
|
|
||||||
def test_text_enum_member_with_value_zero(self):
|
def test_text_enum_member_with_value_zero(self):
|
||||||
# Test issue #20654 to ensure enum member with value 0 can be
|
# Test issue #20654 to ensure enum member with value 0 can be
|
||||||
|
@ -887,9 +873,7 @@ class PydocWithMetaClasses(unittest.TestCase):
|
||||||
expected_text = expected_dynamicattribute_pattern % (
|
expected_text = expected_dynamicattribute_pattern % (
|
||||||
(__name__,) + expected_text_data_docstrings[:2])
|
(__name__,) + expected_text_data_docstrings[:2])
|
||||||
result = output.getvalue().strip()
|
result = output.getvalue().strip()
|
||||||
if result != expected_text:
|
self.assertEqual(expected_text, result)
|
||||||
print_diffs(expected_text, result)
|
|
||||||
self.fail("outputs are not equal, see diff above")
|
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
||||||
"Docstrings are omitted with -O2 and above")
|
"Docstrings are omitted with -O2 and above")
|
||||||
|
@ -910,9 +894,7 @@ class PydocWithMetaClasses(unittest.TestCase):
|
||||||
helper(Class)
|
helper(Class)
|
||||||
expected_text = expected_virtualattribute_pattern1 % __name__
|
expected_text = expected_virtualattribute_pattern1 % __name__
|
||||||
result = output.getvalue().strip()
|
result = output.getvalue().strip()
|
||||||
if result != expected_text:
|
self.assertEqual(expected_text, result)
|
||||||
print_diffs(expected_text, result)
|
|
||||||
self.fail("outputs are not equal, see diff above")
|
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
||||||
"Docstrings are omitted with -O2 and above")
|
"Docstrings are omitted with -O2 and above")
|
||||||
|
@ -952,19 +934,13 @@ class PydocWithMetaClasses(unittest.TestCase):
|
||||||
helper(Class1)
|
helper(Class1)
|
||||||
expected_text1 = expected_virtualattribute_pattern2 % __name__
|
expected_text1 = expected_virtualattribute_pattern2 % __name__
|
||||||
result1 = output.getvalue().strip()
|
result1 = output.getvalue().strip()
|
||||||
if result1 != expected_text1:
|
self.assertEqual(expected_text1, result1)
|
||||||
print_diffs(expected_text1, result1)
|
|
||||||
fail1 = True
|
|
||||||
output = StringIO()
|
output = StringIO()
|
||||||
helper = pydoc.Helper(output=output)
|
helper = pydoc.Helper(output=output)
|
||||||
helper(Class2)
|
helper(Class2)
|
||||||
expected_text2 = expected_virtualattribute_pattern3 % __name__
|
expected_text2 = expected_virtualattribute_pattern3 % __name__
|
||||||
result2 = output.getvalue().strip()
|
result2 = output.getvalue().strip()
|
||||||
if result2 != expected_text2:
|
self.assertEqual(expected_text2, result2)
|
||||||
print_diffs(expected_text2, result2)
|
|
||||||
fail2 = True
|
|
||||||
if fail1 or fail2:
|
|
||||||
self.fail("outputs are not equal, see diff above")
|
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize >= 2,
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
||||||
"Docstrings are omitted with -O2 and above")
|
"Docstrings are omitted with -O2 and above")
|
||||||
|
@ -981,9 +957,7 @@ class PydocWithMetaClasses(unittest.TestCase):
|
||||||
helper(C)
|
helper(C)
|
||||||
expected_text = expected_missingattribute_pattern % __name__
|
expected_text = expected_missingattribute_pattern % __name__
|
||||||
result = output.getvalue().strip()
|
result = output.getvalue().strip()
|
||||||
if result != expected_text:
|
self.assertEqual(expected_text, result)
|
||||||
print_diffs(expected_text, result)
|
|
||||||
self.fail("outputs are not equal, see diff above")
|
|
||||||
|
|
||||||
|
|
||||||
@reap_threads
|
@reap_threads
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue