tests: update more of test-bdiff.py to use unittest (part 2 of 4)
authorAugie Fackler <augie@google.com>
Thu, 15 Dec 2016 10:50:06 -0500
changeset 30592 0d8cada9998d
parent 30591 1b393a93a7df
child 30593 4286015285ec
tests: update more of test-bdiff.py to use unittest (part 2 of 4)
tests/test-bdiff.py
tests/test-bdiff.py.out
--- a/tests/test-bdiff.py	Thu Dec 15 10:10:15 2016 -0500
+++ b/tests/test-bdiff.py	Thu Dec 15 10:50:06 2016 -0500
@@ -1,4 +1,5 @@
 from __future__ import absolute_import, print_function
+import collections
 import struct
 import unittest
 
@@ -9,6 +10,11 @@
     mpatch,
 )
 
+class diffreplace(
+    collections.namedtuple('diffreplace', 'start end from_ to')):
+    def __repr__(self):
+        return 'diffreplace(%r, %r, %r, %r)' % self
+
 class BdiffTests(unittest.TestCase):
 
     def assert_bdiff_applies(self, a, b):
@@ -49,7 +55,45 @@
         for a, b in cases:
             self.assert_bdiff(a, b)
 
-#issue1295
+    def showdiff(self, a, b):
+        bin = bdiff.bdiff(a, b)
+        pos = 0
+        q = 0
+        actions = []
+        while pos < len(bin):
+            p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
+            pos += 12
+            if p1:
+                actions.append(a[q:p1])
+            actions.append(diffreplace(p1, p2, a[p1:p2], bin[pos:pos + l]))
+            pos += l
+            q = p2
+        if q < len(a):
+            actions.append(a[q:])
+        return actions
+
+    def test_issue1295(self):
+        cases = [
+            ("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\nx\n\nz\n",
+             ['x\n\nx\n\n', diffreplace(6, 6, '', 'y\n\n'), 'x\n\nx\n\nz\n']),
+            ("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\ny\n\nx\n\nz\n",
+             ['x\n\nx\n\n',
+              diffreplace(6, 6, '', 'y\n\n'),
+              'x\n\n',
+              diffreplace(9, 9, '', 'y\n\n'),
+              'x\n\nz\n']),
+            # we should pick up abbbc. rather than bc.de as the longest match
+            ("a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n",
+             "a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n",
+             ['a\nb\nb\n',
+              diffreplace(6, 6, '', 'a\nb\nb\nb\nc\n.\n'),
+              'b\nc\n.\nd\ne\n',
+              diffreplace(16, 18, '.\n', ''),
+              'f\n']),
+        ]
+        for old, new, want in cases:
+            self.assertEqual(self.showdiff(old, new), want)
+
 def showdiff(a, b):
     print('showdiff(\n  %r,\n  %r):' % (a, b))
     bin = bdiff.bdiff(a, b)
@@ -66,14 +110,6 @@
     if q < len(a):
         print('', repr(a[q:]))
 
-showdiff("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\nx\n\nz\n")
-showdiff("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\ny\n\nx\n\nz\n")
-# we should pick up abbbc. rather than bc.de as the longest match
-showdiff("a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n",
-         "a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n")
-
-print("done")
-
 def testfixws(a, b, allws):
     c = bdiff.fixws(a, allws)
     if c != b:
--- a/tests/test-bdiff.py.out	Thu Dec 15 10:10:15 2016 -0500
+++ b/tests/test-bdiff.py.out	Thu Dec 15 10:50:06 2016 -0500
@@ -1,26 +1,3 @@
-showdiff(
-  'x\n\nx\n\nx\n\nx\n\nz\n',
-  'x\n\nx\n\ny\n\nx\n\nx\n\nz\n'):
- 'x\n\nx\n\n'
- 6 6 '' -> 'y\n\n'
- 'x\n\nx\n\nz\n'
-showdiff(
-  'x\n\nx\n\nx\n\nx\n\nz\n',
-  'x\n\nx\n\ny\n\nx\n\ny\n\nx\n\nz\n'):
- 'x\n\nx\n\n'
- 6 6 '' -> 'y\n\n'
- 'x\n\n'
- 9 9 '' -> 'y\n\n'
- 'x\n\nz\n'
-showdiff(
-  'a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n',
-  'a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n'):
- 'a\nb\nb\n'
- 6 6 '' -> 'a\nb\nb\nb\nc\n.\n'
- 'b\nc\n.\nd\ne\n'
- 16 18 '.\n' -> ''
- 'f\n'
-done
 done
 Nice diff for a trivial change:
 showdiff(