Mercurial > hg
comparison tests/test-bdiff.py @ 30592:0d8cada9998d
tests: update more of test-bdiff.py to use unittest (part 2 of 4)
author | Augie Fackler <augie@google.com> |
---|---|
date | Thu, 15 Dec 2016 10:50:06 -0500 |
parents | 1b393a93a7df |
children | 4286015285ec |
comparison
equal
deleted
inserted
replaced
30591:1b393a93a7df | 30592:0d8cada9998d |
---|---|
1 from __future__ import absolute_import, print_function | 1 from __future__ import absolute_import, print_function |
2 import collections | |
2 import struct | 3 import struct |
3 import unittest | 4 import unittest |
4 | 5 |
5 import silenttestrunner | 6 import silenttestrunner |
6 | 7 |
7 from mercurial import ( | 8 from mercurial import ( |
8 bdiff, | 9 bdiff, |
9 mpatch, | 10 mpatch, |
10 ) | 11 ) |
12 | |
13 class diffreplace( | |
14 collections.namedtuple('diffreplace', 'start end from_ to')): | |
15 def __repr__(self): | |
16 return 'diffreplace(%r, %r, %r, %r)' % self | |
11 | 17 |
12 class BdiffTests(unittest.TestCase): | 18 class BdiffTests(unittest.TestCase): |
13 | 19 |
14 def assert_bdiff_applies(self, a, b): | 20 def assert_bdiff_applies(self, a, b): |
15 d = bdiff.bdiff(a, b) | 21 d = bdiff.bdiff(a, b) |
47 ("a\nb", "a\nb"), | 53 ("a\nb", "a\nb"), |
48 ] | 54 ] |
49 for a, b in cases: | 55 for a, b in cases: |
50 self.assert_bdiff(a, b) | 56 self.assert_bdiff(a, b) |
51 | 57 |
52 #issue1295 | 58 def showdiff(self, a, b): |
59 bin = bdiff.bdiff(a, b) | |
60 pos = 0 | |
61 q = 0 | |
62 actions = [] | |
63 while pos < len(bin): | |
64 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) | |
65 pos += 12 | |
66 if p1: | |
67 actions.append(a[q:p1]) | |
68 actions.append(diffreplace(p1, p2, a[p1:p2], bin[pos:pos + l])) | |
69 pos += l | |
70 q = p2 | |
71 if q < len(a): | |
72 actions.append(a[q:]) | |
73 return actions | |
74 | |
75 def test_issue1295(self): | |
76 cases = [ | |
77 ("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\nx\n\nz\n", | |
78 ['x\n\nx\n\n', diffreplace(6, 6, '', 'y\n\n'), 'x\n\nx\n\nz\n']), | |
79 ("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\ny\n\nx\n\nz\n", | |
80 ['x\n\nx\n\n', | |
81 diffreplace(6, 6, '', 'y\n\n'), | |
82 'x\n\n', | |
83 diffreplace(9, 9, '', 'y\n\n'), | |
84 'x\n\nz\n']), | |
85 # we should pick up abbbc. rather than bc.de as the longest match | |
86 ("a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n", | |
87 "a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n", | |
88 ['a\nb\nb\n', | |
89 diffreplace(6, 6, '', 'a\nb\nb\nb\nc\n.\n'), | |
90 'b\nc\n.\nd\ne\n', | |
91 diffreplace(16, 18, '.\n', ''), | |
92 'f\n']), | |
93 ] | |
94 for old, new, want in cases: | |
95 self.assertEqual(self.showdiff(old, new), want) | |
96 | |
53 def showdiff(a, b): | 97 def showdiff(a, b): |
54 print('showdiff(\n %r,\n %r):' % (a, b)) | 98 print('showdiff(\n %r,\n %r):' % (a, b)) |
55 bin = bdiff.bdiff(a, b) | 99 bin = bdiff.bdiff(a, b) |
56 pos = 0 | 100 pos = 0 |
57 q = 0 | 101 q = 0 |
63 print('', p1, p2, repr(a[p1:p2]), '->', repr(bin[pos:pos + l])) | 107 print('', p1, p2, repr(a[p1:p2]), '->', repr(bin[pos:pos + l])) |
64 pos += l | 108 pos += l |
65 q = p2 | 109 q = p2 |
66 if q < len(a): | 110 if q < len(a): |
67 print('', repr(a[q:])) | 111 print('', repr(a[q:])) |
68 | |
69 showdiff("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\nx\n\nz\n") | |
70 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") | |
71 # we should pick up abbbc. rather than bc.de as the longest match | |
72 showdiff("a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n", | |
73 "a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n") | |
74 | |
75 print("done") | |
76 | 112 |
77 def testfixws(a, b, allws): | 113 def testfixws(a, b, allws): |
78 c = bdiff.fixws(a, allws) | 114 c = bdiff.fixws(a, allws) |
79 if c != b: | 115 if c != b: |
80 print("*** fixws", repr(a), repr(b), allws) | 116 print("*** fixws", repr(a), repr(b), allws) |