Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 1 | import difflib |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 2 | from test.test_support import run_unittest, findfile |
Neal Norwitz | e7dfe21 | 2003-07-01 14:59:46 +0000 | [diff] [blame] | 3 | import unittest |
Raymond Hettinger | 43d790c | 2003-07-16 04:34:56 +0000 | [diff] [blame] | 4 | import doctest |
Gustavo Niemeyer | 54814881 | 2006-01-31 18:34:13 +0000 | [diff] [blame] | 5 | import sys |
Neal Norwitz | e7dfe21 | 2003-07-01 14:59:46 +0000 | [diff] [blame] | 6 | |
| 7 | class TestSFbugs(unittest.TestCase): |
| 8 | |
| 9 | def test_ratio_for_null_seqn(self): |
| 10 | # Check clearing of SF bug 763023 |
| 11 | s = difflib.SequenceMatcher(None, [], []) |
| 12 | self.assertEqual(s.ratio(), 1) |
| 13 | self.assertEqual(s.quick_ratio(), 1) |
| 14 | self.assertEqual(s.real_quick_ratio(), 1) |
| 15 | |
Brett Cannon | d2c5b4b | 2004-07-10 23:54:07 +0000 | [diff] [blame] | 16 | def test_comparing_empty_lists(self): |
| 17 | # Check fix for bug #979794 |
| 18 | group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes() |
| 19 | self.assertRaises(StopIteration, group_gen.next) |
| 20 | diff_gen = difflib.unified_diff([], []) |
| 21 | self.assertRaises(StopIteration, diff_gen.next) |
| 22 | |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 23 | patch914575_from1 = """ |
| 24 | 1. Beautiful is beTTer than ugly. |
| 25 | 2. Explicit is better than implicit. |
| 26 | 3. Simple is better than complex. |
| 27 | 4. Complex is better than complicated. |
| 28 | """ |
| 29 | |
| 30 | patch914575_to1 = """ |
| 31 | 1. Beautiful is better than ugly. |
| 32 | 3. Simple is better than complex. |
| 33 | 4. Complicated is better than complex. |
| 34 | 5. Flat is better than nested. |
| 35 | """ |
| 36 | |
| 37 | patch914575_from2 = """ |
| 38 | \t\tLine 1: preceeded by from:[tt] to:[ssss] |
| 39 | \t\tLine 2: preceeded by from:[sstt] to:[sssst] |
| 40 | \t \tLine 3: preceeded by from:[sstst] to:[ssssss] |
| 41 | Line 4: \thas from:[sst] to:[sss] after : |
| 42 | Line 5: has from:[t] to:[ss] at end\t |
| 43 | """ |
| 44 | |
| 45 | patch914575_to2 = """ |
| 46 | Line 1: preceeded by from:[tt] to:[ssss] |
| 47 | \tLine 2: preceeded by from:[sstt] to:[sssst] |
| 48 | Line 3: preceeded by from:[sstst] to:[ssssss] |
| 49 | Line 4: has from:[sst] to:[sss] after : |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 50 | Line 5: has from:[t] to:[ss] at end |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 51 | """ |
| 52 | |
| 53 | patch914575_from3 = """line 0 |
| 54 | 1234567890123456789012345689012345 |
| 55 | line 1 |
| 56 | line 2 |
| 57 | line 3 |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 58 | line 4 changed |
| 59 | line 5 changed |
| 60 | line 6 changed |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 61 | line 7 |
| 62 | line 8 subtracted |
| 63 | line 9 |
| 64 | 1234567890123456789012345689012345 |
| 65 | short line |
| 66 | just fits in!! |
| 67 | just fits in two lines yup!! |
| 68 | the end""" |
| 69 | |
| 70 | patch914575_to3 = """line 0 |
| 71 | 1234567890123456789012345689012345 |
| 72 | line 1 |
| 73 | line 2 added |
| 74 | line 3 |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 75 | line 4 chanGEd |
| 76 | line 5a chanGed |
| 77 | line 6a changEd |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 78 | line 7 |
| 79 | line 8 |
| 80 | line 9 |
| 81 | 1234567890 |
| 82 | another long line that needs to be wrapped |
| 83 | just fitS in!! |
| 84 | just fits in two lineS yup!! |
| 85 | the end""" |
| 86 | |
| 87 | class TestSFpatches(unittest.TestCase): |
| 88 | |
| 89 | def test_html_diff(self): |
| 90 | # Check SF patch 914575 for generating HTML differences |
| 91 | f1a = ((patch914575_from1 + '123\n'*10)*3) |
| 92 | t1a = (patch914575_to1 + '123\n'*10)*3 |
| 93 | f1b = '456\n'*10 + f1a |
| 94 | t1b = '456\n'*10 + t1a |
| 95 | f1a = f1a.splitlines() |
| 96 | t1a = t1a.splitlines() |
| 97 | f1b = f1b.splitlines() |
| 98 | t1b = t1b.splitlines() |
| 99 | f2 = patch914575_from2.splitlines() |
| 100 | t2 = patch914575_to2.splitlines() |
| 101 | f3 = patch914575_from3 |
| 102 | t3 = patch914575_to3 |
| 103 | i = difflib.HtmlDiff() |
| 104 | j = difflib.HtmlDiff(tabsize=2) |
| 105 | k = difflib.HtmlDiff(wrapcolumn=14) |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 106 | |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 107 | full = i.make_file(f1a,t1a,'from','to',context=False,numlines=5) |
| 108 | tables = '\n'.join( |
| 109 | [ |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 110 | '<h2>Context (first diff within numlines=5(default))</h2>', |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 111 | i.make_table(f1a,t1a,'from','to',context=True), |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 112 | '<h2>Context (first diff after numlines=5(default))</h2>', |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 113 | i.make_table(f1b,t1b,'from','to',context=True), |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 114 | '<h2>Context (numlines=6)</h2>', |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 115 | i.make_table(f1a,t1a,'from','to',context=True,numlines=6), |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 116 | '<h2>Context (numlines=0)</h2>', |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 117 | i.make_table(f1a,t1a,'from','to',context=True,numlines=0), |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 118 | '<h2>Same Context</h2>', |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 119 | i.make_table(f1a,f1a,'from','to',context=True), |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 120 | '<h2>Same Full</h2>', |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 121 | i.make_table(f1a,f1a,'from','to',context=False), |
| 122 | '<h2>Empty Context</h2>', |
| 123 | i.make_table([],[],'from','to',context=True), |
| 124 | '<h2>Empty Full</h2>', |
| 125 | i.make_table([],[],'from','to',context=False), |
| 126 | '<h2>tabsize=2</h2>', |
| 127 | j.make_table(f2,t2), |
| 128 | '<h2>tabsize=default</h2>', |
| 129 | i.make_table(f2,t2), |
| 130 | '<h2>Context (wrapcolumn=14,numlines=0)</h2>', |
| 131 | k.make_table(f3.splitlines(),t3.splitlines(),context=True,numlines=0), |
| 132 | '<h2>wrapcolumn=14,splitlines()</h2>', |
| 133 | k.make_table(f3.splitlines(),t3.splitlines()), |
| 134 | '<h2>wrapcolumn=14,splitlines(True)</h2>', |
| 135 | k.make_table(f3.splitlines(True),t3.splitlines(True)), |
| 136 | ]) |
| 137 | actual = full.replace('</body>','\n%s\n</body>' % tables) |
| 138 | # temporarily uncomment next three lines to baseline this test |
| 139 | #f = open('test_difflib_expect.html','w') |
| 140 | #f.write(actual) |
| 141 | #f.close() |
| 142 | expect = open(findfile('test_difflib_expect.html')).read() |
Tim Peters | 48bd7f3 | 2004-08-29 22:38:38 +0000 | [diff] [blame] | 143 | |
| 144 | |
Martin v. Löwis | e064b41 | 2004-08-29 16:34:40 +0000 | [diff] [blame] | 145 | self.assertEqual(actual,expect) |
| 146 | |
Gustavo Niemeyer | 54814881 | 2006-01-31 18:34:13 +0000 | [diff] [blame] | 147 | def test_recursion_limit(self): |
| 148 | # Check if the problem described in patch #1413711 exists. |
| 149 | limit = sys.getrecursionlimit() |
| 150 | old = [(i%2 and "K:%d" or "V:A:%d") % i for i in range(limit*2)] |
| 151 | new = [(i%2 and "K:%d" or "V:B:%d") % i for i in range(limit*2)] |
| 152 | difflib.SequenceMatcher(None, old, new).get_opcodes() |
| 153 | |
| 154 | |
Neal Norwitz | ce4a9c9 | 2006-04-09 08:36:46 +0000 | [diff] [blame^] | 155 | def test_main(): |
| 156 | difflib.HtmlDiff._default_prefix = 0 |
| 157 | Doctests = doctest.DocTestSuite(difflib) |
| 158 | run_unittest(TestSFpatches, TestSFbugs, Doctests) |
Raymond Hettinger | 43d790c | 2003-07-16 04:34:56 +0000 | [diff] [blame] | 159 | |
Neal Norwitz | ce4a9c9 | 2006-04-09 08:36:46 +0000 | [diff] [blame^] | 160 | if __name__ == '__main__': |
| 161 | test_main() |