tests: update more of test-bdiff.py to use unittest (part 2 of 4)

This commit is contained in:
Augie Fackler 2016-12-15 10:50:06 -05:00
parent 133f8468d3
commit d751e0686d
2 changed files with 45 additions and 32 deletions

View File

@ -1,4 +1,5 @@
from __future__ import absolute_import, print_function from __future__ import absolute_import, print_function
import collections
import struct import struct
import unittest import unittest
@ -9,6 +10,11 @@ from mercurial import (
mpatch, mpatch,
) )
class diffreplace(
collections.namedtuple('diffreplace', 'start end from_ to')):
def __repr__(self):
return 'diffreplace(%r, %r, %r, %r)' % self
class BdiffTests(unittest.TestCase): class BdiffTests(unittest.TestCase):
def assert_bdiff_applies(self, a, b): def assert_bdiff_applies(self, a, b):
@ -49,7 +55,45 @@ class BdiffTests(unittest.TestCase):
for a, b in cases: for a, b in cases:
self.assert_bdiff(a, b) 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): def showdiff(a, b):
print('showdiff(\n %r,\n %r):' % (a, b)) print('showdiff(\n %r,\n %r):' % (a, b))
bin = bdiff.bdiff(a, b) bin = bdiff.bdiff(a, b)
@ -66,14 +110,6 @@ def showdiff(a, b):
if q < len(a): if q < len(a):
print('', repr(a[q:])) 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): def testfixws(a, b, allws):
c = bdiff.fixws(a, allws) c = bdiff.fixws(a, allws)
if c != b: if c != b:

View File

@ -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 done
Nice diff for a trivial change: Nice diff for a trivial change:
showdiff( showdiff(