diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2011-04-27 10:20:38 +0300 |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2011-04-27 10:20:38 +0300 |
commit | 08710e805dc32e9055672e0d544de4dfc698d96a (patch) | |
tree | c05ed41aee56aa53d6d1dbea615fa7a6dd6eb17d /Lib/unittest | |
parent | ce00de62f6fd214178030690079722ab2060f0a1 (diff) | |
parent | 5b512f008a3b53079f145a8eba7fe2cea8f80a06 (diff) | |
download | cpython-08710e805dc32e9055672e0d544de4dfc698d96a.tar.gz |
#11763: merge with 3.1.
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/case.py | 8 | ||||
-rw-r--r-- | Lib/unittest/test/test_case.py | 36 |
2 files changed, 44 insertions, 0 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 01c5a7bc2f..3f2f37d32e 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -263,6 +263,10 @@ class TestCase(object): maxDiff = 80*8 + # If a string is longer than _diffThreshold, use normal comparison instead + # of difflib. See #11763. + _diffThreshold = 2**16 + # Attribute used by TestSuite for classSetUp _classSetupFailed = False @@ -1048,6 +1052,10 @@ class TestCase(object): self.assertIsInstance(second, str, 'Second argument is not a string') if first != second: + # 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(True) secondlines = second.splitlines(True) if len(firstlines) == 1 and first.strip('\r\n') == first: diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index 1db433f32c..c74a539515 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -685,6 +685,42 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): else: self.fail('assertMultiLineEqual did not fail') + def testAssertEqual_diffThreshold(self): + # check threshold value + self.assertEqual(self._diffThreshold, 2**16) + # disable madDiff to get diff markers + self.maxDiff = None + + # set a lower threshold value and add a cleanup to restore it + old_threshold = self._diffThreshold + self._diffThreshold = 2**8 + self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold)) + + # under the threshold: diff marker (^) in error message + s = 'x' * (2**7) + with self.assertRaises(self.failureException) as cm: + self.assertEqual(s + 'a', s + 'b') + self.assertIn('^', str(cm.exception)) + self.assertEqual(s + 'a', s + 'a') + + # over the threshold: diff not used and marker (^) not in error message + s = 'x' * (2**9) + # if the path that uses difflib is taken, _truncateMessage will be + # called -- replace it with explodingTruncation to verify that this + # doesn't happen + def explodingTruncation(message, diff): + raise SystemError('this should not be raised') + old_truncate = self._truncateMessage + self._truncateMessage = explodingTruncation + self.addCleanup(lambda: setattr(self, '_truncateMessage', old_truncate)) + + s1, s2 = s + 'a', s + 'b' + with self.assertRaises(self.failureException) as cm: + self.assertEqual(s1, s2) + self.assertNotIn('^', str(cm.exception)) + self.assertEqual(str(cm.exception), '%r != %r' % (s1, s2)) + self.assertEqual(s + 'a', s + 'a') + def testAssertCountEqual(self): a = object() self.assertCountEqual([1, 2, 3], [3, 2, 1]) |