incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [7/11] git commit: [#5389] ticket:229 tests for HtmlSideBySideDiff + minor fixes
Date Mon, 17 Dec 2012 23:01:39 GMT
[#5389] ticket:229 tests for HtmlSideBySideDiff + minor fixes


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/d0338fc2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/d0338fc2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/d0338fc2

Branch: refs/heads/master
Commit: d0338fc204b35818f2d44a0748517436295d0a6b
Parents: 3bda7bd
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Fri Dec 7 14:12:00 2012 +0000
Committer: Cory Johns <johnsca@geek.net>
Committed: Mon Dec 17 22:21:17 2012 +0000

----------------------------------------------------------------------
 Allura/allura/lib/diff.py              |   10 +-
 Allura/allura/tests/unit/test_utils.py |  119 +++++++++++++++++++++++++++
 2 files changed, 124 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/d0338fc2/Allura/allura/lib/diff.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/diff.py b/Allura/allura/lib/diff.py
index 78a736b..50d83a3 100644
--- a/Allura/allura/lib/diff.py
+++ b/Allura/allura/lib/diff.py
@@ -9,7 +9,7 @@ class HtmlSideBySideDiff(object):
     <th colspan="2">%s</th>
     <th colspan="2">%s</th>
   </thead>
-  %s
+%s
 </table>
 '''.strip()
 
@@ -22,7 +22,7 @@ class HtmlSideBySideDiff(object):
 </tr>'''.strip()
 
     def __init__(self, tabsize=4):
-        self._tabsize = 4
+        self._tabsize = tabsize
 
     def _render_change(self, aline, bline, anum=None, bnum=None, astyle=None, bstyle=None):
         astyle = (' class="%s"' % astyle) if astyle else ''
@@ -43,13 +43,13 @@ class HtmlSideBySideDiff(object):
         # this is needed to be able to highlight entire <td> in the table,
         # rather then highlighting only chunk inside the <span>
         flag = ''
-        if line.startswith('\0+'):
+        if line.startswith('\0+') and line.endswith('\1'):
             line = line.lstrip('\0+').rstrip('\1')
             flag = 'diff-add'
-        elif line.startswith('\0-'):
+        elif line.startswith('\0-') and line.endswith('\1'):
             line = line.lstrip('\0-').rstrip('\1')
             flag = 'diff-rem'
-        elif '\0^' in line:
+        elif '\0^' in line or '\0+' in line or '\0-' in line:
             flag = 'diff-chg'
 
         # replace all other marks with <span>'s

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/d0338fc2/Allura/allura/tests/unit/test_utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/test_utils.py b/Allura/allura/tests/unit/test_utils.py
index bfda6d2..55b4b45 100644
--- a/Allura/allura/tests/unit/test_utils.py
+++ b/Allura/allura/tests/unit/test_utils.py
@@ -3,6 +3,8 @@ from mock import Mock
 
 from alluratest.controller import setup_unit_test
 from allura.lib.utils import generate_code_stats, chunked_list
+from allura.lib.diff import HtmlSideBySideDiff
+
 
 class TestCodeStats(unittest.TestCase):
 
@@ -35,3 +37,120 @@ class TestUtils(unittest.TestCase):
         self.assertEqual(len(chunks), 4)
         self.assertEqual(len(chunks[0]), 3)
         self.assertEqual([el for sublist in chunks for el in sublist], l)
+
+
+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 colspan="2">file a</th>
+    <th colspan="2">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)


Mime
View raw message