Skip to content

Commit 54cf4bf

Browse files
Increase test coverage for difflib
1 parent 097c563 commit 54cf4bf

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lib/test/test_difflib.py

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

32+
33+
def test_opcode_caching(self):
34+
sm = difflib.SequenceMatcher(None, 'b' * 100, 'a' + 'b' * 100)
35+
self.assertEqual(list(sm.get_opcodes()),
36+
[ ('insert', 0, 0, 0, 1),
37+
('equal', 0, 100, 1, 101)])
38+
39+
sm.a = 'a' * 40 + 'c' + 'b' * 40
40+
sm.b = 'a' * 40 + 'b' * 40
41+
self.assertEqual(list(sm.get_opcodes()),
42+
[ ('insert', 0, 0, 0, 1),
43+
('equal', 0, 100, 1, 101)])
44+
45+
# To avoid caching in set_seqs.
46+
sm.set_seqs("".join(list(sm.a)), "".join(list(sm.b)))
47+
self.assertEqual(list(sm.get_opcodes()),
48+
[ ('equal', 0, 40, 0, 40),
49+
('delete', 40, 41, 40, 40),
50+
('equal', 41, 81, 40, 80)])
51+
52+
3253
def test_bjunk(self):
3354
sm = difflib.SequenceMatcher(isjunk=lambda x: x == ' ',
3455
a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40)
@@ -293,6 +314,15 @@ def test_close_matches_aligned(self):
293314
'+ kitten\n',
294315
'+ puppy\n'])
295316

317+
def test_one_insert(self):
318+
m = difflib.Differ().compare('b' * 2, 'a' + 'b' * 2)
319+
self.assertEqual(list(m), ['+ a', ' b', ' b'])
320+
321+
def test_one_delete(self):
322+
m = difflib.Differ().compare('a' + 'b' * 2, 'b' * 2)
323+
self.assertEqual(list(m), ['- a', ' b', ' b'])
324+
325+
296326
class TestOutputFormat(unittest.TestCase):
297327
def test_tab_delimiter(self):
298328
args = [['one'], ['two'], 'Original', 'Current',
@@ -601,6 +631,22 @@ def test_longest_match_with_popular_chars(self):
601631
self.assertFalse(self.longer_match_exists(a, b, match.size))
602632

603633

634+
class TestCloseMatches(unittest.TestCase):
635+
def test_invalid_inputs(self):
636+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], n=0)
637+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], n=-1)
638+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], cutoff=1.1)
639+
self.assertRaises(ValueError, difflib.get_close_matches, "spam", ['egg'], cutoff=-0.1)
640+
641+
642+
class TestRestore(unittest.TestCase):
643+
def test_invalid_input(self):
644+
with self.assertRaises(ValueError):
645+
''.join(difflib.restore([], 0))
646+
with self.assertRaises(ValueError):
647+
''.join(difflib.restore([], 3))
648+
649+
604650
def setUpModule():
605651
difflib.HtmlDiff._default_prefix = 0
606652

0 commit comments

Comments
 (0)