import difflib from test import support from test.support import findfile, force_colorized from test.support.import_helper import ensure_lazy_imports import unittest import doctest import sys class TestWithAscii(unittest.TestCase): def test_one_insert(self): sm = difflib.SequenceMatcher(None, 'b' * 100, 'a' + 'b' * 100) self.assertAlmostEqual(sm.ratio(), 0.995, places=3) self.assertEqual(list(sm.get_opcodes()), [ ('insert', 0, 0, 0, 1), ('equal', 0, 100, 1, 101)]) self.assertEqual(sm.bpopular, set()) sm = difflib.SequenceMatcher(None, 'b' * 100, 'b' * 50 + 'a' + 'b' * 50) self.assertAlmostEqual(sm.ratio(), 0.995, places=3) self.assertEqual(list(sm.get_opcodes()), [ ('equal', 0, 50, 0, 50), ('insert', 50, 50, 50, 51), ('equal', 50, 100, 51, 101)]) self.assertEqual(sm.bpopular, set()) def test_one_delete(self): sm = difflib.SequenceMatcher(None, 'a' * 40 + 'c' + 'b' * 40, 'a' * 40 + 'b' * 40) self.assertAlmostEqual(sm.ratio(), 0.994, places=3) self.assertEqual(list(sm.get_opcodes()), [ ('equal', 0, 40, 0, 40), ('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) self.assertEqual(sm.bjunk, set()) sm = difflib.SequenceMatcher(isjunk=lambda x: x == ' ', a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40 + ' ' * 20) self.assertEqual(sm.bjunk, {' '}) sm = difflib.SequenceMatcher(isjunk=lambda x: x in [' ', 'b'], a='a' * 40 + 'b' * 40, b='a' * 44 + 'b' * 40 + ' ' * 20) self.assertEqual(sm.bjunk, {' ', 'b'}) class TestAutojunk(unittest.TestCase): """Tests for the autojunk parameter added in SequenceMatcher and higher-level difflib APIs""" def test_one_insert_homogenous_sequence(self): # By default autojunk=True and the heuristic kicks in for a sequence # of length 200+ seq1 = 'b' * 200 seq2 = 'a' + 'b' * 200 sm = difflib.SequenceMatcher(None, seq1, seq2) self.assertAlmostEqual(sm.ratio(), 0, places=3) self.assertEqual(sm.bpopular, {'b'}) # Now turn the heuristic off sm = difflib.SequenceMatcher(None, seq1, seq2, autojunk=False) self.assertAlmostEqual(sm.ratio(), 0.9975, places=3) self.assertEqual(sm.bpopular, set()) def test_get_close_matches(self): word = 'a' + 'b' * 200 possibilities = ['b' * 200] # By default autojunk=True, so 'b' is junk -> ratio ~ 0 -> no matches self.assertEqual(difflib.get_close_matches(word, possibilities, cutoff=0.6), []) self.assertEqual(difflib.get_close_matches(word, possibilities, cutoff=0.6, autojunk=True), []) # With autojunk=False, ratio ~ 0.9975 -> match returned self.assertEqual(difflib.get_close_matches(word, possibilities, cutoff=0.6, autojunk=False), ['b' * 200]) def test_differ_and_ndiff(self): lines1 = ["x\n"] * 200 + ["a\n", "b\n", "c\n"] + ["x\n"] * 50 lines2 = ["a\n", "b\n", "c\n"] + ["x\n"] * 250 # Line-level autojunk propagation d_true = difflib.Differ(autojunk=True) d_false = difflib.Differ(autojunk=False) res_true = list(d_true.compare(lines1, lines2)) res_false = list(d_false.compare(lines1, lines2)) self.assertNotEqual(res_true, res_false) ndiff_true = list(difflib.ndiff(lines1, lines2, autojunk=True)) ndiff_false = list(difflib.ndiff(lines1, lines2, autojunk=False)) self.assertNotEqual(ndiff_true, ndiff_false) self.assertEqual(ndiff_true, res_true) self.assertEqual(ndiff_false, res_false) # Character-level autojunk propagation in Differ (_fancy_replace) line1 = "x" * 200 + "abc" + "x" * 50 + "\n" line2 = "abc" + "x" * 250 + "\n" fancy_true = list(difflib.Differ(autojunk=True).compare([line1], [line2])) fancy_false = list(difflib.Differ(autojunk=False).compare([line1], [line2])) self.assertNotEqual(fancy_true, fancy_false) def test_unified_and_context_diff(self): lines1 = ["x\n"] * 200 + ["a\n", "b\n", "c\n"] + ["x\n"] * 50 lines2 = ["a\n", "b\n", "c\n"] + ["x\n"] * 250 u_true = list(difflib.unified_diff(lines1, lines2, autojunk=True)) u_false = list(difflib.unified_diff(lines1, lines2, autojunk=False)) self.assertNotEqual(u_true, u_false) c_true = list(difflib.context_diff(lines1, lines2, autojunk=True)) c_false = list(difflib.context_diff(lines1, lines2, autojunk=False)) self.assertNotEqual(c_true, c_false) def test_htmldiff(self): lines1 = ["x\n"] * 200 + ["a\n", "b\n", "c\n"] + ["x\n"] * 50 lines2 = ["a\n", "b\n", "c\n"] + ["x\n"] * 250 old_prefix = difflib.HtmlDiff._default_prefix try: html_true = difflib.HtmlDiff(autojunk=True).make_file(lines1, lines2) html_false = difflib.HtmlDiff(autojunk=False).make_file(lines1, lines2) self.assertNotEqual(html_true, html_false) finally: difflib.HtmlDiff._default_prefix = old_prefix def test_autojunk_signatures(self): import inspect funcs = [ difflib.get_close_matches, difflib.unified_diff, difflib.context_diff, difflib.ndiff, ] for func in funcs: sig = inspect.signature(func) self.assertIn('autojunk', sig.parameters) param = sig.parameters['autojunk'] self.assertEqual(param.default, True) self.assertEqual(param.kind, inspect.Parameter.KEYWORD_ONLY) for cls in [difflib.Differ, difflib.HtmlDiff]: sig = inspect.signature(cls.__init__) self.assertIn('autojunk', sig.parameters) param = sig.parameters['autojunk'] self.assertEqual(param.default, True) class TestSFbugs(unittest.TestCase): def test_ratio_for_null_seqn(self): # Check clearing of SF bug 763023 s = difflib.SequenceMatcher(None, [], []) self.assertEqual(s.ratio(), 1) self.assertEqual(s.quick_ratio(), 1) self.assertEqual(s.real_quick_ratio(), 1) def test_comparing_empty_lists(self): # Check fix for bug #979794 group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes() self.assertRaises(StopIteration, next, group_gen) diff_gen = difflib.unified_diff([], []) self.assertRaises(StopIteration, next, diff_gen) def test_matching_blocks_cache(self): # Issue #21635 s = difflib.SequenceMatcher(None, "abxcd", "abcd") first = s.get_matching_blocks() second = s.get_matching_blocks() self.assertEqual(second[0].size, 2) self.assertEqual(second[1].size, 2) self.assertEqual(second[2].size, 0) def test_added_tab_hint(self): # Check fix for bug #1488943 diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"])) self.assertEqual("- \tI am a buggy", diff[0]) self.assertEqual("? \t --\n", diff[1]) self.assertEqual("+ \t\tI am a bug", diff[2]) self.assertEqual("? +\n", diff[3]) def test_hint_indented_properly_with_tabs(self): diff = list(difflib.Differ().compare(["\t \t \t^"], ["\t \t \t^\n"])) self.assertEqual("- \t \t \t^", diff[0]) self.assertEqual("+ \t \t \t^\n", diff[1]) self.assertEqual("? \t \t \t +\n", diff[2]) def test_mdiff_catch_stop_iteration(self): # Issue #33224 self.assertEqual( list(difflib._mdiff(["2"], ["3"], 1)), [((1, '\x00-2\x01'), (1, '\x00+3\x01'), True)], ) def test_mdiff_lopsided_replace(self): self.assertEqual( list(difflib._mdiff(["a\n"] * 4, ["b\n"])), [ ((1, '\x00-a\n\x01'), (1, '\x00+b\n\x01'), True), ((2, '\x00-a\n\x01'), ('', '\n'), True), ((3, '\x00-a\n\x01'), ('', '\n'), True), ((4, '\x00-a\n\x01'), ('', '\n'), True), ], ) self.assertEqual( list(difflib._mdiff(["a\n"], ["b\n"] * 4)), [ ((1, '\x00-a\n\x01'), (1, '\x00+b\n\x01'), True), (('', '\n'), (2, '\x00+b\n\x01'), True), (('', '\n'), (3, '\x00+b\n\x01'), True), (('', '\n'), (4, '\x00+b\n\x01'), True), ], ) patch914575_from1 = """ 1. Beautiful is beTTer than ugly. 2. Explicit is better than implicit. 3. Simple is better than complex. 4. Complex is better than complicated. """ patch914575_to1 = """ 1. Beautiful is better than ugly. 3. Simple is better than complex. 4. Complicated is better than complex. 5. Flat is better than nested. """ patch914575_nonascii_from1 = """ 1. Beautiful is beTTer than ugly. 2. Explicit is better than ımplıcıt. 3. Simple is better than complex. 4. Complex is better than complicated. """ patch914575_nonascii_to1 = """ 1. Beautiful is better than ügly. 3. Sımple is better than complex. 4. Complicated is better than cömplex. 5. Flat is better than nested. """ patch914575_from2 = """ \t\tLine 1: preceded by from:[tt] to:[ssss] \t\tLine 2: preceded by from:[sstt] to:[sssst] \t \tLine 3: preceded by from:[sstst] to:[ssssss] Line 4: \thas from:[sst] to:[sss] after : Line 5: has from:[t] to:[ss] at end\t """ patch914575_to2 = """ Line 1: preceded by from:[tt] to:[ssss] \tLine 2: preceded by from:[sstt] to:[sssst] Line 3: preceded by from:[sstst] to:[ssssss] Line 4: has from:[sst] to:[sss] after : Line 5: has from:[t] to:[ss] at end """ patch914575_from3 = """line 0 1234567890123456789012345689012345 line 1 line 2 line 3 line 4 changed line 5 changed line 6 changed line 7 line 8 subtracted line 9 1234567890123456789012345689012345 short line just fits in!! just fits in two lines yup!! the end""" patch914575_to3 = """line 0 1234567890123456789012345689012345 line 1 line 2 added line 3 line 4 chanGEd line 5a chanGed line 6a changEd line 7 line 8 line 9 1234567890 another long line that needs to be wrapped just fitS in!! just fits in two lineS yup!! the end""" class TestSFpatches(unittest.TestCase): def test_html_diff(self): # Check SF patch 914575 for generating HTML differences f1a = ((patch914575_from1 + '123\n'*10)*3) t1a = (patch914575_to1 + '123\n'*10)*3 f1b = '456\n'*10 + f1a t1b = '456\n'*10 + t1a f1a = f1a.splitlines() t1a = t1a.splitlines() f1b = f1b.splitlines() t1b = t1b.splitlines() f2 = patch914575_from2.splitlines() t2 = patch914575_to2.splitlines() f3 = patch914575_from3 t3 = patch914575_to3 i = difflib.HtmlDiff() j = difflib.HtmlDiff(tabsize=2) k = difflib.HtmlDiff(wrapcolumn=14) full = i.make_file(f1a,t1a,'from','to',context=False,numlines=5) tables = '\n'.join( [ '