Skip to content

Commit 4499161

Browse files
gh-138669: Increase test coverage for difflib (GH-138670)
1 parent 4f3cab9 commit 4499161

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)
@@ -293,6 +303,15 @@ def test_close_matches_aligned(self):
293303
'+ kitten\n',
294304
'+ puppy\n'])
295305

306+
def test_one_insert(self):
307+
m = difflib.Differ().compare('b' * 2, 'a' + 'b' * 2)
308+
self.assertEqual(list(m), ['+ a', ' b', ' b'])
309+
310+
def test_one_delete(self):
311+
m = difflib.Differ().compare('a' + 'b' * 2, 'b' * 2)
312+
self.assertEqual(list(m), ['- a', ' b', ' b'])
313+
314+
296315
class TestOutputFormat(unittest.TestCase):
297316
def test_tab_delimiter(self):
298317
args = [['one'], ['two'], 'Original', 'Current',
@@ -601,6 +620,26 @@ def test_longest_match_with_popular_chars(self):
601620
self.assertFalse(self.longer_match_exists(a, b, match.size))
602621

603622

623+
class TestCloseMatches(unittest.TestCase):
624+
# Happy paths are tested in the doctests of `difflib.get_close_matches`.
625+
626+
def test_invalid_inputs(self):
627+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], n=0)
628+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], n=-1)
629+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], cutoff=1.1)
630+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], cutoff=-0.1)
631+
632+
633+
class TestRestore(unittest.TestCase):
634+
# Happy paths are tested in the doctests of `difflib.restore`.
635+
636+
def test_invalid_input(self):
637+
with self.assertRaises(ValueError):
638+
''.join(difflib.restore([], 0))
639+
with self.assertRaises(ValueError):
640+
''.join(difflib.restore([], 3))
641+
642+
604643
def setUpModule():
605644
difflib.HtmlDiff._default_prefix = 0
606645

0 commit comments

Comments
 (0)