mirror of
https://github.com/python/cpython.git
synced 2025-11-26 13:22:51 +00:00
Issue #18996: TestCase.assertEqual() now more cleverly shorten differing
strings in error report.
This commit is contained in:
parent
463bd4b5c6
commit
77622f55c2
4 changed files with 81 additions and 16 deletions
|
|
@ -1,10 +1,47 @@
|
|||
"""Various utility functions."""
|
||||
|
||||
from collections import namedtuple, OrderedDict
|
||||
from os.path import commonprefix
|
||||
|
||||
__unittest = True
|
||||
|
||||
_MAX_LENGTH = 80
|
||||
_PLACEHOLDER_LEN = 12
|
||||
_MIN_BEGIN_LEN = 5
|
||||
_MIN_END_LEN = 5
|
||||
_MIN_COMMON_LEN = 5
|
||||
_MIN_DIFF_LEN = _MAX_LENGTH - \
|
||||
(_MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN +
|
||||
_PLACEHOLDER_LEN + _MIN_END_LEN)
|
||||
assert _MIN_DIFF_LEN >= 0
|
||||
|
||||
def _shorten(s, prefixlen, suffixlen):
|
||||
skip = len(s) - prefixlen - suffixlen
|
||||
if skip > _PLACEHOLDER_LEN:
|
||||
s = '%s[%d chars]%s' % (s[:prefixlen], skip, s[len(s) - suffixlen:])
|
||||
return s
|
||||
|
||||
def _common_shorten_repr(*args):
|
||||
args = tuple(map(safe_repr, args))
|
||||
maxlen = max(map(len, args))
|
||||
if maxlen <= _MAX_LENGTH:
|
||||
return args
|
||||
|
||||
prefix = commonprefix(args)
|
||||
prefixlen = len(prefix)
|
||||
|
||||
common_len = _MAX_LENGTH - \
|
||||
(maxlen - prefixlen + _MIN_BEGIN_LEN + _PLACEHOLDER_LEN)
|
||||
if common_len > _MIN_COMMON_LEN:
|
||||
assert _MIN_BEGIN_LEN + _PLACEHOLDER_LEN + _MIN_COMMON_LEN + \
|
||||
(maxlen - prefixlen) < _MAX_LENGTH
|
||||
prefix = _shorten(prefix, _MIN_BEGIN_LEN, common_len)
|
||||
return tuple(prefix + s[prefixlen:] for s in args)
|
||||
|
||||
prefix = _shorten(prefix, _MIN_BEGIN_LEN, _MIN_COMMON_LEN)
|
||||
return tuple(prefix + _shorten(s[prefixlen:], _MIN_DIFF_LEN, _MIN_END_LEN)
|
||||
for s in args)
|
||||
|
||||
def safe_repr(obj, short=False):
|
||||
try:
|
||||
result = repr(obj)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue