Show More
@@ -1,140 +1,141 b'' | |||||
1 | from __future__ import absolute_import, print_function |
|
1 | from __future__ import absolute_import, print_function | |
2 | import collections |
|
2 | import collections | |
3 | import struct |
|
3 | import struct | |
4 | import unittest |
|
4 | import unittest | |
5 |
|
5 | |||
6 | import silenttestrunner |
|
6 | import silenttestrunner | |
7 |
|
7 | |||
8 | from mercurial import ( |
|
8 | from mercurial import ( | |
9 | bdiff, |
|
9 | bdiff, | |
10 | mpatch, |
|
10 | mpatch, | |
11 | ) |
|
11 | ) | |
12 |
|
12 | |||
13 | class diffreplace( |
|
13 | class diffreplace( | |
14 | collections.namedtuple('diffreplace', 'start end from_ to')): |
|
14 | collections.namedtuple('diffreplace', 'start end from_ to')): | |
15 | def __repr__(self): |
|
15 | def __repr__(self): | |
16 | return 'diffreplace(%r, %r, %r, %r)' % self |
|
16 | return 'diffreplace(%r, %r, %r, %r)' % self | |
17 |
|
17 | |||
18 | class BdiffTests(unittest.TestCase): |
|
18 | class BdiffTests(unittest.TestCase): | |
19 |
|
19 | |||
20 | def assert_bdiff_applies(self, a, b): |
|
20 | def assert_bdiff_applies(self, a, b): | |
21 | d = bdiff.bdiff(a, b) |
|
21 | d = bdiff.bdiff(a, b) | |
22 | c = a |
|
22 | c = a | |
23 | if d: |
|
23 | if d: | |
24 | c = mpatch.patches(a, [d]) |
|
24 | c = mpatch.patches(a, [d]) | |
25 | self.assertEqual( |
|
25 | self.assertEqual( | |
26 | c, b, ("bad diff+patch result from\n %r to\n " |
|
26 | c, b, ("bad diff+patch result from\n %r to\n " | |
27 | "%r: \nbdiff: %r\npatched: %r" % (a, b, d, c[:200]))) |
|
27 | "%r: \nbdiff: %r\npatched: %r" % (a, b, d, c[:200]))) | |
28 |
|
28 | |||
29 | def assert_bdiff(self, a, b): |
|
29 | def assert_bdiff(self, a, b): | |
30 | self.assert_bdiff_applies(a, b) |
|
30 | self.assert_bdiff_applies(a, b) | |
31 | self.assert_bdiff_applies(b, a) |
|
31 | self.assert_bdiff_applies(b, a) | |
32 |
|
32 | |||
33 | def test_bdiff_basic(self): |
|
33 | def test_bdiff_basic(self): | |
34 | cases = [ |
|
34 | cases = [ | |
35 | ("a\nc\n\n\n\n", "a\nb\n\n\n"), |
|
35 | ("a\nc\n\n\n\n", "a\nb\n\n\n"), | |
36 | ("a\nb\nc\n", "a\nc\n"), |
|
36 | ("a\nb\nc\n", "a\nc\n"), | |
37 | ("", ""), |
|
37 | ("", ""), | |
38 | ("a\nb\nc", "a\nb\nc"), |
|
38 | ("a\nb\nc", "a\nb\nc"), | |
39 | ("a\nb\nc\nd\n", "a\nd\n"), |
|
39 | ("a\nb\nc\nd\n", "a\nd\n"), | |
40 | ("a\nb\nc\nd\n", "a\nc\ne\n"), |
|
40 | ("a\nb\nc\nd\n", "a\nc\ne\n"), | |
41 | ("a\nb\nc\n", "a\nc\n"), |
|
41 | ("a\nb\nc\n", "a\nc\n"), | |
42 | ("a\n", "c\na\nb\n"), |
|
42 | ("a\n", "c\na\nb\n"), | |
43 | ("a\n", ""), |
|
43 | ("a\n", ""), | |
44 | ("a\n", "b\nc\n"), |
|
44 | ("a\n", "b\nc\n"), | |
45 | ("a\n", "c\na\n"), |
|
45 | ("a\n", "c\na\n"), | |
46 | ("", "adjfkjdjksdhfksj"), |
|
46 | ("", "adjfkjdjksdhfksj"), | |
47 | ("", "ab"), |
|
47 | ("", "ab"), | |
48 | ("", "abc"), |
|
48 | ("", "abc"), | |
49 | ("a", "a"), |
|
49 | ("a", "a"), | |
50 | ("ab", "ab"), |
|
50 | ("ab", "ab"), | |
51 | ("abc", "abc"), |
|
51 | ("abc", "abc"), | |
52 | ("a\n", "a\n"), |
|
52 | ("a\n", "a\n"), | |
53 | ("a\nb", "a\nb"), |
|
53 | ("a\nb", "a\nb"), | |
54 | ] |
|
54 | ] | |
55 | for a, b in cases: |
|
55 | for a, b in cases: | |
56 | self.assert_bdiff(a, b) |
|
56 | self.assert_bdiff(a, b) | |
57 |
|
57 | |||
58 | def showdiff(self, a, b): |
|
58 | def showdiff(self, a, b): | |
59 | bin = bdiff.bdiff(a, b) |
|
59 | bin = bdiff.bdiff(a, b) | |
60 | pos = 0 |
|
60 | pos = 0 | |
61 | q = 0 |
|
61 | q = 0 | |
62 | actions = [] |
|
62 | actions = [] | |
63 | while pos < len(bin): |
|
63 | while pos < len(bin): | |
64 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) |
|
64 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) | |
65 | pos += 12 |
|
65 | pos += 12 | |
66 | if p1: |
|
66 | if p1: | |
67 | actions.append(a[q:p1]) |
|
67 | actions.append(a[q:p1]) | |
68 | actions.append(diffreplace(p1, p2, a[p1:p2], bin[pos:pos + l])) |
|
68 | actions.append(diffreplace(p1, p2, a[p1:p2], bin[pos:pos + l])) | |
69 | pos += l |
|
69 | pos += l | |
70 | q = p2 |
|
70 | q = p2 | |
71 | if q < len(a): |
|
71 | if q < len(a): | |
72 | actions.append(a[q:]) |
|
72 | actions.append(a[q:]) | |
73 | return actions |
|
73 | return actions | |
74 |
|
74 | |||
75 | def test_issue1295(self): |
|
75 | def test_issue1295(self): | |
76 | cases = [ |
|
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", |
|
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']), |
|
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", |
|
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', |
|
80 | ['x\n\nx\n\n', | |
81 | diffreplace(6, 6, '', 'y\n\n'), |
|
81 | diffreplace(6, 6, '', 'y\n\n'), | |
82 | 'x\n\n', |
|
82 | 'x\n\n', | |
83 | diffreplace(9, 9, '', 'y\n\n'), |
|
83 | diffreplace(9, 9, '', 'y\n\n'), | |
84 | 'x\n\nz\n']), |
|
84 | 'x\n\nz\n']), | |
85 | # we should pick up abbbc. rather than bc.de as the longest match |
|
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", |
|
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", |
|
87 | "a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n", | |
88 | ['a\nb\nb\n', |
|
88 | ['a\nb\nb\n', | |
89 | diffreplace(6, 6, '', 'a\nb\nb\nb\nc\n.\n'), |
|
89 | diffreplace(6, 6, '', 'a\nb\nb\nb\nc\n.\n'), | |
90 | 'b\nc\n.\nd\ne\n', |
|
90 | 'b\nc\n.\nd\ne\n', | |
91 | diffreplace(16, 18, '.\n', ''), |
|
91 | diffreplace(16, 18, '.\n', ''), | |
92 | 'f\n']), |
|
92 | 'f\n']), | |
93 | ] |
|
93 | ] | |
94 | for old, new, want in cases: |
|
94 | for old, new, want in cases: | |
95 | self.assertEqual(self.showdiff(old, new), want) |
|
95 | self.assertEqual(self.showdiff(old, new), want) | |
96 |
|
96 | |||
97 | def test_fixws(self): |
|
97 | def test_fixws(self): | |
98 | cases = [ |
|
98 | cases = [ | |
99 | (" \ta\r b\t\n", "ab\n", 1), |
|
99 | (" \ta\r b\t\n", "ab\n", 1), | |
100 | (" \ta\r b\t\n", " a b\n", 0), |
|
100 | (" \ta\r b\t\n", " a b\n", 0), | |
101 | ("", "", 1), |
|
101 | ("", "", 1), | |
102 | ("", "", 0), |
|
102 | ("", "", 0), | |
103 | ] |
|
103 | ] | |
104 | for a, b, allws in cases: |
|
104 | for a, b, allws in cases: | |
105 | c = bdiff.fixws(a, allws) |
|
105 | c = bdiff.fixws(a, allws) | |
106 | self.assertEqual( |
|
106 | self.assertEqual( | |
107 | c, b, 'fixws(%r) want %r got %r (allws=%r)' % (a, b, c, allws)) |
|
107 | c, b, 'fixws(%r) want %r got %r (allws=%r)' % (a, b, c, allws)) | |
108 |
|
108 | |||
109 | def showdiff(a, b): |
|
109 | def test_nice_diff_for_trivial_change(self): | |
110 | print('showdiff(\n %r,\n %r):' % (a, b)) |
|
110 | self.assertEqual(self.showdiff( | |
111 | bin = bdiff.bdiff(a, b) |
|
111 | ''.join('<%s\n-\n' % i for i in range(5)), | |
112 | pos = 0 |
|
112 | ''.join('>%s\n-\n' % i for i in range(5))), | |
113 | q = 0 |
|
113 | [diffreplace(0, 3, '<0\n', '>0\n'), | |
114 | while pos < len(bin): |
|
114 | '-\n', | |
115 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) |
|
115 | diffreplace(5, 8, '<1\n', '>1\n'), | |
116 | pos += 12 |
|
116 | '-\n', | |
117 | if p1: |
|
117 | diffreplace(10, 13, '<2\n', '>2\n'), | |
118 | print('', repr(a[q:p1])) |
|
118 | '-\n', | |
119 | print('', p1, p2, repr(a[p1:p2]), '->', repr(bin[pos:pos + l])) |
|
119 | diffreplace(15, 18, '<3\n', '>3\n'), | |
120 | pos += l |
|
120 | '-\n', | |
121 | q = p2 |
|
121 | diffreplace(20, 23, '<4\n', '>4\n'), | |
122 | if q < len(a): |
|
122 | '-\n']) | |
123 | print('', repr(a[q:])) |
|
|||
124 |
|
123 | |||
125 | print("Nice diff for a trivial change:") |
|
124 | def test_prefer_appending(self): | |
126 | showdiff( |
|
125 | # 1 line to 3 lines | |
127 | ''.join('<%s\n-\n' % i for i in range(5)), |
|
126 | self.assertEqual(self.showdiff('a\n', 'a\n' * 3), | |
128 | ''.join('>%s\n-\n' % i for i in range(5))) |
|
127 | ['a\n', diffreplace(2, 2, '', 'a\na\n')]) | |
|
128 | # 1 line to 5 lines | |||
|
129 | self.assertEqual(self.showdiff('a\n', 'a\n' * 5), | |||
|
130 | ['a\n', diffreplace(2, 2, '', 'a\na\na\na\n')]) | |||
129 |
|
131 | |||
130 | print("Diff 1 to 3 lines - preference for appending:") |
|
132 | def test_prefer_removing_trailing(self): | |
131 | showdiff('a\n', 'a\n' * 3) |
|
133 | # 3 lines to 1 line | |
132 | print("Diff 1 to 5 lines - preference for appending:") |
|
134 | self.assertEqual(self.showdiff('a\n' * 3, 'a\n'), | |
133 | showdiff('a\n', 'a\n' * 5) |
|
135 | ['a\n', diffreplace(2, 6, 'a\na\n', '')]) | |
134 | print("Diff 3 to 1 lines - preference for removing trailing lines:") |
|
136 | # 5 lines to 1 line | |
135 |
showdiff('a\n' * |
|
137 | self.assertEqual(self.showdiff('a\n' * 5, 'a\n'), | |
136 | print("Diff 5 to 1 lines - preference for removing trailing lines:") |
|
138 | ['a\n', diffreplace(2, 10, 'a\na\na\na\n', '')]) | |
137 | showdiff('a\n' * 5, 'a\n') |
|
|||
138 |
|
139 | |||
139 | if __name__ == '__main__': |
|
140 | if __name__ == '__main__': | |
140 | silenttestrunner.main(__name__) |
|
141 | silenttestrunner.main(__name__) |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now