Assert that two multi-line strings are equal.
(self, first, second, msg=None)
| 1256 | self.fail(msg) |
| 1257 | |
| 1258 | def assertMultiLineEqual(self, first, second, msg=None): |
| 1259 | """Assert that two multi-line strings are equal.""" |
| 1260 | self.assertIsInstance(first, str, "First argument is not a string") |
| 1261 | self.assertIsInstance(second, str, "Second argument is not a string") |
| 1262 | |
| 1263 | if first != second: |
| 1264 | # Don't use difflib if the strings are too long |
| 1265 | if (len(first) > self._diffThreshold or |
| 1266 | len(second) > self._diffThreshold): |
| 1267 | self._baseAssertEqual(first, second, msg) |
| 1268 | |
| 1269 | # Append \n to both strings if either is missing the \n. |
| 1270 | # This allows the final ndiff to show the \n difference. The |
| 1271 | # exception here is if the string is empty, in which case no |
| 1272 | # \n should be added |
| 1273 | first_presplit = first |
| 1274 | second_presplit = second |
| 1275 | if first and second: |
| 1276 | if first[-1] != '\n' or second[-1] != '\n': |
| 1277 | first_presplit += '\n' |
| 1278 | second_presplit += '\n' |
| 1279 | elif second and second[-1] != '\n': |
| 1280 | second_presplit += '\n' |
| 1281 | elif first and first[-1] != '\n': |
| 1282 | first_presplit += '\n' |
| 1283 | |
| 1284 | firstlines = first_presplit.splitlines(keepends=True) |
| 1285 | secondlines = second_presplit.splitlines(keepends=True) |
| 1286 | |
| 1287 | # Generate the message and diff, then raise the exception |
| 1288 | standardMsg = '%s != %s' % _common_shorten_repr(first, second) |
| 1289 | diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines)) |
| 1290 | standardMsg = self._truncateMessage(standardMsg, diff) |
| 1291 | self.fail(self._formatMessage(msg, standardMsg)) |
| 1292 | |
| 1293 | def assertLess(self, a, b, msg=None): |
| 1294 | """Just like self.assertTrue(a < b), but with a nicer default message.""" |