@@ -29,6 +29,16 @@ def test_one_delete(self):
29
29
('delete' , 40 , 41 , 40 , 40 ),
30
30
('equal' , 41 , 81 , 40 , 80 )])
31
31
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
+
32
42
def test_bjunk (self ):
33
43
sm = difflib .SequenceMatcher (isjunk = lambda x : x == ' ' ,
34
44
a = 'a' * 40 + 'b' * 40 , b = 'a' * 44 + 'b' * 40 )
@@ -293,6 +303,15 @@ def test_close_matches_aligned(self):
293
303
'+ kitten\n ' ,
294
304
'+ puppy\n ' ])
295
305
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
+
296
315
class TestOutputFormat (unittest .TestCase ):
297
316
def test_tab_delimiter (self ):
298
317
args = [['one' ], ['two' ], 'Original' , 'Current' ,
@@ -601,6 +620,26 @@ def test_longest_match_with_popular_chars(self):
601
620
self .assertFalse (self .longer_match_exists (a , b , match .size ))
602
621
603
622
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
+
604
643
def setUpModule ():
605
644
difflib .HtmlDiff ._default_prefix = 0
606
645
0 commit comments