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