[PATCH 2 of 5 pure-fix] tests: update more of test-bdiff.py to use unittest (part 2 of 4)

Augie Fackler raf at durin42.com
Thu Dec 15 11:32:34 EST 2016


# HG changeset patch
# User Augie Fackler <augie at google.com>
# Date 1481817006 18000
#      Thu Dec 15 10:50:06 2016 -0500
# Node ID 609ce0f260cb7203f547abb770fb7efae08eb448
# Parent  0aae265cd9708228fc3615b994ef8cdb31060c74
tests: update more of test-bdiff.py to use unittest (part 2 of 4)

diff --git a/tests/test-bdiff.py b/tests/test-bdiff.py
--- a/tests/test-bdiff.py
+++ b/tests/test-bdiff.py
@@ -1,4 +1,5 @@
 from __future__ import absolute_import, print_function
+import collections
 import silenttestrunner
 import struct
 import unittest
@@ -8,6 +9,11 @@ from mercurial import (
     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):
@@ -48,7 +54,45 @@ class BdiffTests(unittest.TestCase):
         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)
@@ -65,14 +109,6 @@ def showdiff(a, b):
     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:
diff --git a/tests/test-bdiff.py.out b/tests/test-bdiff.py.out
--- a/tests/test-bdiff.py.out
+++ b/tests/test-bdiff.py.out
@@ -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(


More information about the Mercurial-devel mailing list