https://github.com/python/cpython/commit/d8d11d50bf54df3005c4833282a1988fe9b4a0e5
commit: d8d11d50bf54df3005c4833282a1988fe9b4a0e5
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2025-09-14T19:42:05Z
summary:

[3.13] gh-138669: Increase test coverage for difflib (GH-138670) (#138818)

gh-138669: Increase test coverage for difflib (GH-138670)
(cherry picked from commit 44991619ba04a98c5878ee5bbc38d2e47cbd22c7)

Co-authored-by: Jan-Eric Nitschke 
<[email protected]>

files:
M Lib/test/test_difflib.py

diff --git a/Lib/test/test_difflib.py b/Lib/test/test_difflib.py
index 6afd90af8442ad..943d7a659b1e1b 100644
--- a/Lib/test/test_difflib.py
+++ b/Lib/test/test_difflib.py
@@ -29,6 +29,16 @@ def test_one_delete(self):
                 ('delete', 40, 41, 40, 40),
                 ('equal', 41, 81, 40, 80)])
 
+    def test_opcode_caching(self):
+        sm = difflib.SequenceMatcher(None, 'b' * 100, 'a' + 'b' * 100)
+        opcode = sm.get_opcodes()
+        self.assertEqual(opcode,
+            [   ('insert', 0, 0, 0, 1),
+                ('equal', 0, 100, 1, 101)])
+        # Implementation detail: opcodes are cached;
+        # `get_opcodes()` returns the same object
+        self.assertIs(opcode, sm.get_opcodes())
+
     def test_bjunk(self):
         sm = difflib.SequenceMatcher(isjunk=lambda x: x == ' ',
                 a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40)
@@ -273,6 +283,15 @@ def 
test_make_file_usascii_charset_with_nonascii_input(self):
         self.assertIn('&#305;mpl&#305;c&#305;t', output)
 
 
+    def test_one_insert(self):
+        m = difflib.Differ().compare('b' * 2, 'a' + 'b' * 2)
+        self.assertEqual(list(m), ['+ a', '  b', '  b'])
+
+    def test_one_delete(self):
+        m = difflib.Differ().compare('a' + 'b' * 2, 'b' * 2)
+        self.assertEqual(list(m), ['- a', '  b', '  b'])
+
+
 class TestOutputFormat(unittest.TestCase):
     def test_tab_delimiter(self):
         args = ['one', 'two', 'Original', 'Current',
@@ -547,6 +566,26 @@ def test_longest_match_with_popular_chars(self):
         self.assertFalse(self.longer_match_exists(a, b, match.size))
 
 
+class TestCloseMatches(unittest.TestCase):
+    # Happy paths are tested in the doctests of `difflib.get_close_matches`.
+
+    def test_invalid_inputs(self):
+        self.assertRaises(ValueError, difflib.get_close_matches, "spam", 
['egg'], n=0)
+        self.assertRaises(ValueError, difflib.get_close_matches, "spam", 
['egg'], n=-1)
+        self.assertRaises(ValueError, difflib.get_close_matches, "spam", 
['egg'], cutoff=1.1)
+        self.assertRaises(ValueError, difflib.get_close_matches, "spam", 
['egg'], cutoff=-0.1)
+
+
+class TestRestore(unittest.TestCase):
+    # Happy paths are tested in the doctests of `difflib.restore`.
+
+    def test_invalid_input(self):
+        with self.assertRaises(ValueError):
+            ''.join(difflib.restore([], 0))
+        with self.assertRaises(ValueError):
+            ''.join(difflib.restore([], 3))
+
+
 def setUpModule():
     difflib.HtmlDiff._default_prefix = 0
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to