mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Merged revisions 81747 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r81747 | michael.foord | 2010-06-05 13:58:39 +0100 (Sat, 05 Jun 2010) | 1 line unittest.TestCase.assertDictEqual and assertMultilineEqual provide better default failure messages in the event of long diffs. ........
This commit is contained in:
parent
02ff2100d3
commit
cb11b251a0
2 changed files with 12 additions and 6 deletions
|
@ -811,10 +811,11 @@ class TestCase(object):
|
||||||
self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary')
|
self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary')
|
||||||
|
|
||||||
if d1 != d2:
|
if d1 != d2:
|
||||||
|
standardMsg = '%s != %s' % (safe_repr(d1, True), safe_repr(d2, True))
|
||||||
diff = ('\n' + '\n'.join(difflib.ndiff(
|
diff = ('\n' + '\n'.join(difflib.ndiff(
|
||||||
pprint.pformat(d1).splitlines(),
|
pprint.pformat(d1).splitlines(),
|
||||||
pprint.pformat(d2).splitlines())))
|
pprint.pformat(d2).splitlines())))
|
||||||
standardMsg = self._truncateMessage('', diff)
|
standardMsg = self._truncateMessage(standardMsg, diff)
|
||||||
self.fail(self._formatMessage(msg, standardMsg))
|
self.fail(self._formatMessage(msg, standardMsg))
|
||||||
|
|
||||||
def assertDictContainsSubset(self, expected, actual, msg=None):
|
def assertDictContainsSubset(self, expected, actual, msg=None):
|
||||||
|
@ -931,9 +932,10 @@ class TestCase(object):
|
||||||
'Second argument is not a string'))
|
'Second argument is not a string'))
|
||||||
|
|
||||||
if first != second:
|
if first != second:
|
||||||
|
standardMsg = '%s != %s' % (safe_repr(d1, True), safe_repr(d2, True))
|
||||||
diff = '\n' + ''.join(difflib.ndiff(first.splitlines(True),
|
diff = '\n' + ''.join(difflib.ndiff(first.splitlines(True),
|
||||||
second.splitlines(True)))
|
second.splitlines(True)))
|
||||||
standardMsg = self._truncateMessage('', diff)
|
standardMsg = self._truncateMessage(standardMsg, diff)
|
||||||
self.fail(self._formatMessage(msg, standardMsg))
|
self.fail(self._formatMessage(msg, standardMsg))
|
||||||
|
|
||||||
def assertLess(self, a, b, msg=None):
|
def assertLess(self, a, b, msg=None):
|
||||||
|
|
|
@ -2,12 +2,16 @@
|
||||||
|
|
||||||
__unittest = True
|
__unittest = True
|
||||||
|
|
||||||
|
_MAX_LENGTH = 80
|
||||||
def safe_repr(obj):
|
def safe_repr(obj, short=False):
|
||||||
try:
|
try:
|
||||||
return repr(obj)
|
result = repr(obj)
|
||||||
except Exception:
|
except Exception:
|
||||||
return object.__repr__(obj)
|
result = object.__repr__(obj)
|
||||||
|
if not short or len(result) < _MAX_LENGTH:
|
||||||
|
return result
|
||||||
|
return result[:_MAX_LENGTH] + ' [truncated]...'
|
||||||
|
|
||||||
|
|
||||||
def strclass(cls):
|
def strclass(cls):
|
||||||
return "%s.%s" % (cls.__module__, cls.__name__)
|
return "%s.%s" % (cls.__module__, cls.__name__)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue