Skip to content

Commit 80e59a8

Browse files
[3.14] pythongh-138669: Increase test coverage for difflib (pythonGH-138670) (python#138817)
Co-authored-by: Jan-Eric Nitschke <[email protected]>
1 parent 18dfc1d commit 80e59a8

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',
@@ -585,6 +604,26 @@ def test_longest_match_with_popular_chars(self):
585604
self.assertFalse(self.longer_match_exists(a, b, match.size))
586605

587606

607+
class TestCloseMatches(unittest.TestCase):
608+
# Happy paths are tested in the doctests of `difflib.get_close_matches`.
609+
610+
def test_invalid_inputs(self):
611+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], n=0)
612+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], n=-1)
613+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], cutoff=1.1)
614+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], cutoff=-0.1)
615+
616+
617+
class TestRestore(unittest.TestCase):
618+
# Happy paths are tested in the doctests of `difflib.restore`.
619+
620+
def test_invalid_input(self):
621+
with self.assertRaises(ValueError):
622+
''.join(difflib.restore([], 0))
623+
with self.assertRaises(ValueError):
624+
''.join(difflib.restore([], 3))
625+
626+
588627
def setUpModule():
589628
difflib.HtmlDiff._default_prefix = 0
590629

0 commit comments

Comments
 (0)