summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorMichael Blahay <mblahay@users.noreply.github.com>2023-05-04 22:37:17 (GMT)
committerGitHub <noreply@github.com>2023-05-04 22:37:17 (GMT)
commit46361bb84332800bc3632688e6ef3b4dd4a48723 (patch)
tree040035775dd304158eb242d04b38bd9324628afe /Lib/unittest
parent7d35c3121ade679dd6e8b4a0bac7b3702aee6921 (diff)
downloadcpython-46361bb84332800bc3632688e6ef3b4dd4a48723.zip
cpython-46361bb84332800bc3632688e6ef3b4dd4a48723.tar.gz
cpython-46361bb84332800bc3632688e6ef3b4dd4a48723.tar.bz2
gh-68968: Correcting message display issue with assertEqual (#103937)
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/case.py31
1 files changed, 23 insertions, 8 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 018f22e..001b640 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -1217,19 +1217,34 @@ class TestCase(object):
def assertMultiLineEqual(self, first, second, msg=None):
"""Assert that two multi-line strings are equal."""
- self.assertIsInstance(first, str, 'First argument is not a string')
- self.assertIsInstance(second, str, 'Second argument is not a string')
+ self.assertIsInstance(first, str, "First argument is not a string")
+ self.assertIsInstance(second, str, "Second argument is not a string")
if first != second:
- # don't use difflib if the strings are too long
+ # Don't use difflib if the strings are too long
if (len(first) > self._diffThreshold or
len(second) > self._diffThreshold):
self._baseAssertEqual(first, second, msg)
- firstlines = first.splitlines(keepends=True)
- secondlines = second.splitlines(keepends=True)
- if len(firstlines) == 1 and first.strip('\r\n') == first:
- firstlines = [first + '\n']
- secondlines = [second + '\n']
+
+ # Append \n to both strings if either is missing the \n.
+ # This allows the final ndiff to show the \n difference. The
+ # exception here is if the string is empty, in which case no
+ # \n should be added
+ first_presplit = first
+ second_presplit = second
+ if first and second:
+ if first[-1] != '\n' or second[-1] != '\n':
+ first_presplit += '\n'
+ second_presplit += '\n'
+ elif second and second[-1] != '\n':
+ second_presplit += '\n'
+ elif first and first[-1] != '\n':
+ first_presplit += '\n'
+
+ firstlines = first_presplit.splitlines(keepends=True)
+ secondlines = second_presplit.splitlines(keepends=True)
+
+ # Generate the message and diff, then raise the exception
standardMsg = '%s != %s' % _common_shorten_repr(first, second)
diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
standardMsg = self._truncateMessage(standardMsg, diff)