[extensions-web] diffutils: Fix some more, add test cases
- From: Jasper St. Pierre <jstpierre src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [extensions-web] diffutils: Fix some more, add test cases
- Date: Sun, 19 Aug 2012 21:02:39 +0000 (UTC)
commit cc7630d7cc654d0e009426fbc0aa6157eec4f636
Author: Jasper St. Pierre <jstpierre mecheye net>
Date: Sun Aug 19 17:02:30 2012 -0400
diffutils: Fix some more, add test cases
We can't blindly be making changes like this unless we're sure we're
improving and not regressing. Add test cases, to be expanded later.
sweettooth/review/difftests.py | 50 ++++++++++++++++++++++++++++++++++++++++
sweettooth/review/diffutils.py | 25 ++++++++-----------
sweettooth/review/tests.py | 2 +
3 files changed, 63 insertions(+), 14 deletions(-)
---
diff --git a/sweettooth/review/difftests.py b/sweettooth/review/difftests.py
new file mode 100644
index 0000000..da8fac1
--- /dev/null
+++ b/sweettooth/review/difftests.py
@@ -0,0 +1,50 @@
+
+from unittest import TestCase
+from diffutils import get_chunks, new_chunk, new_line
+
+def strip_regions(chunks):
+ for chunk in chunks:
+ for L in chunk['lines']:
+ L['oldregion'] = None
+ L['newregion'] = None
+
+class DiffTest(TestCase):
+ def assertDiff(self, a, b, expected, test_regions=False):
+ resulted = list(get_chunks(a, b))
+ if not test_regions:
+ strip_regions(resulted)
+
+ self.assertEqual(resulted, expected)
+
+ def testFakeUnchanged(self):
+ a = ["a"]
+ b = ["a"]
+ self.assertDiff(a, b, [])
+
+ def testFakeDeleted(self):
+ a = ["a"]
+ b = None
+ self.assertDiff(a, b, [new_chunk(tag='delete', lines=[new_line(0, 0)])])
+
+ def testFakeInserted(self):
+ a = None
+ b = ["a"]
+ self.assertDiff(a, b, [new_chunk(tag='insert', lines=[new_line(0, 0)])])
+
+ def testBasicInsert(self):
+ a = ["a"]
+ b = ["a", "b"]
+ self.assertDiff(a, b, [new_chunk(tag='equal', lines=[new_line(0, 0)]),
+ new_chunk(tag='insert', lines=[new_line(None, 1)])])
+
+ def testBasicDelete(self):
+ a = ["a", "b"]
+ b = ["a"]
+ self.assertDiff(a, b, [new_chunk(tag='equal', lines=[new_line(0, 0)]),
+ new_chunk(tag='delete', lines=[new_line(1, None)])])
+
+ def testBasicReplace(self):
+ a = ["a", "b"]
+ b = ["a", "c"]
+ self.assertDiff(a, b, [new_chunk(tag='equal', lines=[new_line(0, 0)]),
+ new_chunk(tag='replace', lines=[new_line(1, 1)])])
diff --git a/sweettooth/review/diffutils.py b/sweettooth/review/diffutils.py
index 08635b5..a56a4b1 100644
--- a/sweettooth/review/diffutils.py
+++ b/sweettooth/review/diffutils.py
@@ -708,10 +708,7 @@ def new_chunk(lines, collapsable=False, tag='equal'):
}
def get_fake_chunk(numlines, tag):
- linenums = xrange(numlines)
- lines = [dict(oldlinenum=n+1, newlinenum=n+1,
- oldindex=n, newindex=n,
- oldregion=[], newregion=[]) for n in linenums]
+ lines = [new_line(oldindex=n, newindex=n) for n in xrange(numlines)]
return new_chunk(lines, tag=tag)
def get_linenum(idx):
@@ -720,18 +717,18 @@ def get_linenum(idx):
else:
return None
+def new_line(oldindex, newindex, oldregion=None, newregion=None):
+ oldlinenum, newlinenum = get_linenum(oldindex), get_linenum(newindex)
+ return dict(oldlinenum=oldlinenum, newlinenum=newlinenum,
+ oldindex=oldindex, newindex=newindex,
+ oldregion=oldregion, newregion=newregion)
+
def diff_line(old, new):
oldindex, oldline = old
newindex, newline = new
oldregion, newregion = get_line_changed_regions(oldline, newline)
- oldlinenum, newlinenum = get_linenum(oldindex), get_linenum(newindex)
-
- result = dict(oldlinenum=oldlinenum, newlinenum=newlinenum,
- oldindex=oldindex, newindex=newindex,
- oldregion=oldregion, newregion=newregion)
-
- return result
+ return new_line(oldindex, newindex, oldregion, newregion)
def get_chunks(a, b):
if a == b:
@@ -760,10 +757,10 @@ def get_chunks(a, b):
for tag, i1, i2, j1, j2 in differ.get_opcodes():
numlines = max(i2-i1, j2-j1)
- oldlines = izip_longest(xrange(i1, i2), a[i1:j2])
- newlines = izip_longest(xrange(j1, j2), b[j1:j2])
+ oldlines = zip(xrange(i1, i2), a[i1:i2])
+ newlines = zip(xrange(j1, j2), b[j1:j2])
- lines = [diff_line(old, new) for old, new in zip(oldlines, newlines)]
+ lines = [diff_line(old, new) for old, new in izip_longest(oldlines, newlines, fillvalue=(None, None))]
if tag == 'equal' and numlines > collapse_threshold:
last_range_start = numlines - context_num_lines
diff --git a/sweettooth/review/tests.py b/sweettooth/review/tests.py
index ef25a89..01f2cb5 100644
--- a/sweettooth/review/tests.py
+++ b/sweettooth/review/tests.py
@@ -7,6 +7,8 @@ from review.views import get_old_version, should_auto_approve
from testutils import BasicUserTestCase
+from difftests import DiffTests
+
class DiffViewTest(BasicUserTestCase, TestCase):
def test_get_zipfiles(self):
metadata = {"uuid": "test-metadata mecheye net",
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]