| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import unittest | 5 import unittest |
| 6 | 6 |
| 7 from webkitpy.common.html_diff import html_diff, html_diff_body | 7 from webkitpy.common.html_diff import HtmlDiffGenerator, html_diff |
| 8 | 8 |
| 9 | 9 |
| 10 class TestHtmlDiff(unittest.TestCase): | 10 class TestHtmlDiff(unittest.TestCase): |
| 11 | 11 |
| 12 def test_html_diff(self): | 12 def test_html_diff(self): |
| 13 self.assertEqual( | 13 self.assertEqual( |
| 14 html_diff('one\ntoo\nthree\n', 'one\ntwo\nthree\n'), | 14 html_diff('one\ntoo\nthree\n', 'one\ntwo\nthree\n'), |
| 15 ('<html>\n' | 15 ('<html>\n' |
| 16 '<head>\n' | 16 '<head>\n' |
| 17 '<style>\n' | 17 '<style>\n' |
| 18 'pre { white-space: pre-wrap; }\n' | 18 'table { white-space: pre-wrap; font-family: monospace; border-coll
apse: collapse; }\n' |
| 19 'th { color: #444; background: #eed; text-align: right; vertical-al
ign: baseline; padding: 1px 4px 1px 4px; }\n' |
| 19 '.del { background: #faa; }\n' | 20 '.del { background: #faa; }\n' |
| 20 '.add { background: #afa; }\n' | 21 '.add { background: #afa; }\n' |
| 21 '</style>\n' | 22 '</style>\n' |
| 22 '</head>\n' | 23 '</head>\n' |
| 23 '<body>\n' | 24 '<body><table>' |
| 24 '<pre>one\n' | 25 '<tr><th>1<th>1<td>one\n</tr>' |
| 25 '<span class="del">too\n' | 26 '<tr><th>2<th><td class="del">too\n</tr>' |
| 26 '</span><span class="add">two\n' | 27 '<tr><th><th>2<td class="add">two\n</tr>' |
| 27 '</span>three\n' | 28 '<tr><th>3<th>3<td>three\n</tr>' |
| 28 '</pre>\n' | 29 '</table></body>\n' |
| 29 '</body>\n' | |
| 30 '</html>\n')) | 30 '</html>\n')) |
| 31 | 31 |
| 32 def test_html_diff_same(self): | 32 def test_html_diff_same(self): |
| 33 self.assertEqual( | 33 self.assertEqual( |
| 34 html_diff_body(['one line\n'], ['one line\n']), | 34 HtmlDiffGenerator().generate_tbody(['one line\n'], ['one line\n']), |
| 35 'one line\n') | 35 '<tr><th>1<th>1<td>one line\n</tr>') |
| 36 self.assertEqual( | 36 self.assertEqual( |
| 37 html_diff_body(['<script>\n'], ['<script>\n']), | 37 HtmlDiffGenerator().generate_tbody(['<script>\n'], ['<script>\n']), |
| 38 '<script>\n') | 38 '<tr><th>1<th>1<td><script>\n</tr>') |
| 39 | 39 |
| 40 def test_html_diff_delete(self): | 40 def test_html_diff_delete(self): |
| 41 self.assertEqual( | 41 self.assertEqual( |
| 42 html_diff_body(['one line\n'], []), | 42 HtmlDiffGenerator().generate_tbody(['one line\n'], []), |
| 43 '<span class="del">one line\n</span>') | 43 '<tr><th>1<th><td class="del">one line\n</tr>') |
| 44 self.assertEqual( | 44 self.assertEqual( |
| 45 html_diff_body(['</pre>\n'], []), | 45 HtmlDiffGenerator().generate_tbody(['</pre>\n'], []), |
| 46 '<span class="del"></pre>\n</span>') | 46 '<tr><th>1<th><td class="del"></pre>\n</tr>') |
| 47 | 47 |
| 48 def test_html_diff_insert(self): | 48 def test_html_diff_insert(self): |
| 49 self.assertEqual( | 49 self.assertEqual( |
| 50 html_diff_body([], ['one line\n']), | 50 HtmlDiffGenerator().generate_tbody([], ['one line\n']), |
| 51 '<span class="add">one line\n</span>') | 51 '<tr><th><th>1<td class="add">one line\n</tr>') |
| 52 self.assertEqual( | 52 self.assertEqual( |
| 53 html_diff_body([], ['<!--\n']), | 53 HtmlDiffGenerator().generate_tbody([], ['<!--\n']), |
| 54 '<span class="add"><!--\n</span>') | 54 '<tr><th><th>1<td class="add"><!--\n</tr>') |
| 55 | 55 |
| 56 def test_html_diff_ending_newline(self): | 56 def test_html_diff_ending_newline(self): |
| 57 self.assertEqual( | 57 self.assertEqual( |
| 58 html_diff_body(['one line'], ['one line\n']), | 58 HtmlDiffGenerator().generate_tbody(['one line'], ['one line\n']), |
| 59 '<span class="del">one line</span><span class="add">one line\n</span
>') | 59 '<tr><th>1<th><td class="del">one line</tr><tr><th><th>1<td class="a
dd">one line\n</tr>') |
| 60 | 60 |
| 61 def test_html_diff_replace_multiple_lines(self): | 61 def test_html_diff_replace_multiple_lines(self): |
| 62 a_lines = [ | 62 a_lines = [ |
| 63 '1. Beautiful is better than ugly.\n', | 63 '1. Beautiful is better than ugly.\n', |
| 64 '2. Explicit is better than implicit.\n', | 64 '2. Explicit is better than implicit.\n', |
| 65 '3. Simple is better than complex.\n', | 65 '3. Simple is better than complex.\n', |
| 66 '4. Complex is better than complicated.\n', | 66 '4. Complex is better than complicated.\n', |
| 67 ] | 67 ] |
| 68 b_lines = [ | 68 b_lines = [ |
| 69 '1. Beautiful is better than ugly.\n', | 69 '1. Beautiful is better than ugly.\n', |
| 70 '3. Simple is better than complex.\n', | 70 '3. Simple is better than complex.\n', |
| 71 '4. Complicated is better than complex.\n', | 71 '4. Complicated is better than complex.\n', |
| 72 '5. Flat is better than nested.\n', | 72 '5. Flat is better than nested.\n', |
| 73 ] | 73 ] |
| 74 self.assertEqual(html_diff_body(a_lines, b_lines), ( | 74 self.assertEqual(HtmlDiffGenerator().generate_tbody(a_lines, b_lines), ( |
| 75 '1. Beautiful is better than ugly.\n' | 75 '<tr><th>1<th>1<td>1. Beautiful is better than ugly.\n</tr>' |
| 76 '<span class="del">2. Explicit is better than implicit.\n' | 76 '<tr><th>2<th><td class="del">2. Explicit is better than implicit.\n
</tr>' |
| 77 '3. Simple is better than complex.\n' | 77 '<tr><th>3<th><td class="del">3. Simple is better than complex.\n</t
r>' |
| 78 '4. Complex is better than complicated.\n' | 78 '<tr><th>4<th><td class="del">4. Complex is better than complicated.
\n</tr>' |
| 79 '</span><span class="add">3. Simple is better than complex.\n' | 79 '<tr><th><th>2<td class="add">3. Simple is better than complex.\n<
/tr>' |
| 80 '4. Complicated is better than complex.\n' | 80 '<tr><th><th>3<td class="add">4. Complicated is better than complex.
\n</tr>' |
| 81 '5. Flat is better than nested.\n' | 81 '<tr><th><th>4<td class="add">5. Flat is better than nested.\n</tr>'
)) |
| 82 '</span>')) | 82 |
| 83 def test_html_diff_context(self): |
| 84 a_lines = [ |
| 85 'line1\n', |
| 86 'line2\n', |
| 87 'line3\n', |
| 88 'line4\n', |
| 89 'line5\n', |
| 90 'line6\n', |
| 91 'line7\n', |
| 92 'line8\n', |
| 93 'line9a\n', |
| 94 'line10\n', |
| 95 'line11\n', |
| 96 'line12\n', |
| 97 'line13\n', |
| 98 'line14\n', |
| 99 'line15a\n', |
| 100 'line16\n', |
| 101 'line17\n', |
| 102 'line18\n', |
| 103 'line19\n', |
| 104 'line20\n', |
| 105 'line21\n', |
| 106 'line22\n', |
| 107 'line23\n', |
| 108 ] |
| 109 b_lines = [ |
| 110 'line1\n', |
| 111 'line2\n', |
| 112 'line3\n', |
| 113 'line4\n', |
| 114 'line5\n', |
| 115 'line6\n', |
| 116 'line7\n', |
| 117 'line8\n', |
| 118 'line9b\n', |
| 119 'line10\n', |
| 120 'line11\n', |
| 121 'line12\n', |
| 122 'line13\n', |
| 123 'line14\n', |
| 124 'line15b\n', |
| 125 'line16\n', |
| 126 'line17\n', |
| 127 'line18\n', |
| 128 'line19\n', |
| 129 'line20\n', |
| 130 'line21\n', |
| 131 'line22\n', |
| 132 'line23\n', |
| 133 ] |
| 134 self.assertEqual(HtmlDiffGenerator().generate_tbody(a_lines, b_lines), ( |
| 135 '<tr><td colspan=3>\n\n</tr>' |
| 136 '<tr><th>6<th>6<td>line6\n</tr>' |
| 137 '<tr><th>7<th>7<td>line7\n</tr>' |
| 138 '<tr><th>8<th>8<td>line8\n</tr>' |
| 139 '<tr><th>9<th><td class="del">line9a\n</tr>' |
| 140 '<tr><th><th>9<td class="add">line9b\n</tr>' |
| 141 '<tr><th>10<th>10<td>line10\n</tr>' |
| 142 '<tr><th>11<th>11<td>line11\n</tr>' |
| 143 '<tr><th>12<th>12<td>line12\n</tr>' |
| 144 '<tr><th>13<th>13<td>line13\n</tr>' |
| 145 '<tr><th>14<th>14<td>line14\n</tr>' |
| 146 '<tr><th>15<th><td class="del">line15a\n</tr>' |
| 147 '<tr><th><th>15<td class="add">line15b\n</tr>' |
| 148 '<tr><th>16<th>16<td>line16\n</tr>' |
| 149 '<tr><th>17<th>17<td>line17\n</tr>' |
| 150 '<tr><th>18<th>18<td>line18\n</tr>' |
| 151 '<tr><td colspan=3>\n\n</tr>')) |
| OLD | NEW |