Skip to content

Commit d8d11d5

Browse files
[3.13] pythongh-138669: Increase test coverage for difflib (pythonGH-138670) (python#138818)
pythongh-138669: Increase test coverage for difflib (pythonGH-138670) (cherry picked from commit 4499161) Co-authored-by: Jan-Eric Nitschke <[email protected]>
1 parent 0b30228 commit d8d11d5

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Lib/test/test_difflib.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def test_one_delete(self):
2929
('delete', 40, 41, 40, 40),
3030
('equal', 41, 81, 40, 80)])
3131

32+
def test_opcode_caching(self):
33+
sm = difflib.SequenceMatcher(None, 'b' * 100, 'a' + 'b' * 100)
34+
opcode = sm.get_opcodes()
35+
self.assertEqual(opcode,
36+
[ ('insert', 0, 0, 0, 1),
37+
('equal', 0, 100, 1, 101)])
38+
# Implementation detail: opcodes are cached;
39+
# `get_opcodes()` returns the same object
40+
self.assertIs(opcode, sm.get_opcodes())
41+
3242
def test_bjunk(self):
3343
sm = difflib.SequenceMatcher(isjunk=lambda x: x == ' ',
3444
a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40)
@@ -273,6 +283,15 @@ def test_make_file_usascii_charset_with_nonascii_input(self):
273283
self.assertIn('&#305;mpl&#305;c&#305;t', output)
274284

275285

286+
def test_one_insert(self):
287+
m = difflib.Differ().compare('b' * 2, 'a' + 'b' * 2)
288+
self.assertEqual(list(m), ['+ a', ' b', ' b'])
289+
290+
def test_one_delete(self):
291+
m = difflib.Differ().compare('a' + 'b' * 2, 'b' * 2)
292+
self.assertEqual(list(m), ['- a', ' b', ' b'])
293+
294+
276295
class TestOutputFormat(unittest.TestCase):
277296
def test_tab_delimiter(self):
278297
args = ['one', 'two', 'Original', 'Current',
@@ -547,6 +566,26 @@ def test_longest_match_with_popular_chars(self):
547566
self.assertFalse(self.longer_match_exists(a, b, match.size))
548567

549568

569+
class TestCloseMatches(unittest.TestCase):
570+
# Happy paths are tested in the doctests of `difflib.get_close_matches`.
571+
572+
def test_invalid_inputs(self):
573+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], n=0)
574+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], n=-1)
575+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], cutoff=1.1)
576+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], cutoff=-0.1)
577+
578+
579+
class TestRestore(unittest.TestCase):
580+
# Happy paths are tested in the doctests of `difflib.restore`.
581+
582+
def test_invalid_input(self):
583+
with self.assertRaises(ValueError):
584+
''.join(difflib.restore([], 0))
585+
with self.assertRaises(ValueError):
586+
''.join(difflib.restore([], 3))
587+
588+
550589
def setUpModule():
551590
difflib.HtmlDiff._default_prefix = 0
552591

0 commit comments

Comments
 (0)