Child: [ddf08c] (diff)

Download this file

test_diff.py    123 lines (112 with data), 4.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import unittest
from allura.lib.diff import HtmlSideBySideDiff
class TestHtmlSideBySideDiff(unittest.TestCase):
def setUp(self):
self.diff = HtmlSideBySideDiff()
def test_render_change(self):
html = self.diff._render_change(
'aline', 'aline <span class="diff-add">bline</span>',
1, 2,'aclass', 'bclass')
expected = '''
<tr>
<td class="lineno">1</td>
<td class="aclass"><pre>aline</pre></td>
<td class="lineno">2</td>
<td class="bclass"><pre>aline <span class="diff-add">bline</span></pre></td>
</tr>'''.strip()
self.assertEqual(html, expected)
def test_render_change_default_args(self):
html = self.diff._render_change('aline', 'bline')
expected = '''
<tr>
<td class="lineno"></td>
<td><pre>aline</pre></td>
<td class="lineno"></td>
<td><pre>bline</pre></td>
</tr>'''.strip()
self.assertEqual(html, expected)
def test_preprocess(self):
d = self.diff
self.assertEquals(d._preprocess(None), None)
self.assertEquals(d._preprocess('<br>&nbsp;'), '&lt;br&gt;&amp;nbsp;')
self.assertEquals(d._preprocess('\ttabbed'), ' tabbed')
# test non default tab size
d = HtmlSideBySideDiff(2)
self.assertEquals(d._preprocess('\ttabbed'), ' tabbed')
def test_replace_marks(self):
line, flag = self.diff._replace_marks('\0+line added\1')
self.assertEquals(line, 'line added')
self.assertEquals(flag, 'diff-add')
line, flag = self.diff._replace_marks('\0-line removed\1')
self.assertEquals(line, 'line removed')
self.assertEquals(flag, 'diff-rem')
line, flag = self.diff._replace_marks('\0^line changed\1')
self.assertEquals(line, '<span class="diff-chg">line changed</span>')
self.assertEquals(flag, 'diff-chg')
line, flag = self.diff._replace_marks('chunk \0+add\1ed')
self.assertEquals(line, 'chunk <span class="diff-add">add</span>ed')
self.assertEquals(flag, 'diff-chg')
line, flag = self.diff._replace_marks('chunk \0-remov\1ed')
self.assertEquals(line, 'chunk <span class="diff-rem">remov</span>ed')
self.assertEquals(flag, 'diff-chg')
line, flag = self.diff._replace_marks('chunk \0^chang\1ed')
self.assertEquals(line, 'chunk <span class="diff-chg">chang</span>ed')
self.assertEquals(flag, 'diff-chg')
def test_make_line(self):
# context separation
d = (None, None, None)
expected = '''
<tr>
<td class="lineno"></td>
<td class="diff-gap"><pre>...</pre></td>
<td class="lineno"></td>
<td class="diff-gap"><pre>...</pre></td>
</tr>'''.strip()
self.assertEquals(self.diff._make_line(d), expected)
# no change
d = ((1, 'aline'), (1, 'aline'), False)
expected = '''
<tr>
<td class="lineno">1</td>
<td><pre>aline</pre></td>
<td class="lineno">1</td>
<td><pre>aline</pre></td>
</tr>'''.strip()
self.assertEquals(self.diff._make_line(d), expected)
# has change
d = ((1, '\0^a\1line'), (1, '\0^b\1line'), True)
expected = '''
<tr>
<td class="lineno">1</td>
<td class="diff-chg"><pre><span class="diff-chg">a</span>line</pre></td>
<td class="lineno">1</td>
<td class="diff-chg"><pre><span class="diff-chg">b</span>line</pre></td>
</tr>'''.strip()
self.assertEquals(self.diff._make_line(d), expected)
def test_make_table(self):
a = 'line 1\nline 2'.split('\n')
b = 'changed line 1\nchanged line 2'.split('\n')
expected = '''
<table class="side-by-side-diff">
<thead>
<th class="lineno"></th>
<th>file a</th>
<th class="lineno"></th>
<th>file b</th>
</thead>
<tr>
<td class="lineno">1</td>
<td class="diff-rem"><pre>line 1</pre></td>
<td class="lineno">1</td>
<td class="diff-add"><pre>changed line 1</pre></td>
</tr>
<tr>
<td class="lineno">2</td>
<td class="diff-rem"><pre>line 2</pre></td>
<td class="lineno">2</td>
<td class="diff-add"><pre>changed line 2</pre></td>
</tr>
</table>
'''.strip()
html = self.diff.make_table(a, b, 'file a', 'file b')
self.assertEquals(html, expected)