Show More
@@ -1,141 +1,151 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 |
|
|||
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 | ] |
|
85 | ] | |
94 | for old, new, want in cases: |
|
86 | for old, new, want in cases: | |
95 | self.assertEqual(self.showdiff(old, new), want) |
|
87 | self.assertEqual(self.showdiff(old, new), want) | |
96 |
|
88 | |||
|
89 | def test_issue1295_varies_on_pure(self): | |||
|
90 | # we should pick up abbbc. rather than bc.de as the longest match | |||
|
91 | got = self.showdiff("a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n", | |||
|
92 | "a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n") | |||
|
93 | want_c = ['a\nb\nb\n', | |||
|
94 | diffreplace(6, 6, '', 'a\nb\nb\nb\nc\n.\n'), | |||
|
95 | 'b\nc\n.\nd\ne\n', | |||
|
96 | diffreplace(16, 18, '.\n', ''), | |||
|
97 | 'f\n'] | |||
|
98 | want_pure = [diffreplace(0, 0, '', 'a\nb\nb\n'), | |||
|
99 | 'a\nb\nb\nb\nc\n.\n', | |||
|
100 | diffreplace(12, 12, '', 'b\nc\n.\n'), | |||
|
101 | 'd\ne\n', | |||
|
102 | diffreplace(16, 18, '.\n', ''), 'f\n'] | |||
|
103 | self.assert_(got in (want_c, want_pure), | |||
|
104 | 'got: %r, wanted either %r or %r' % ( | |||
|
105 | got, want_c, want_pure)) | |||
|
106 | ||||
97 | def test_fixws(self): |
|
107 | def test_fixws(self): | |
98 | cases = [ |
|
108 | cases = [ | |
99 | (" \ta\r b\t\n", "ab\n", 1), |
|
109 | (" \ta\r b\t\n", "ab\n", 1), | |
100 | (" \ta\r b\t\n", " a b\n", 0), |
|
110 | (" \ta\r b\t\n", " a b\n", 0), | |
101 | ("", "", 1), |
|
111 | ("", "", 1), | |
102 | ("", "", 0), |
|
112 | ("", "", 0), | |
103 | ] |
|
113 | ] | |
104 | for a, b, allws in cases: |
|
114 | for a, b, allws in cases: | |
105 | c = bdiff.fixws(a, allws) |
|
115 | c = bdiff.fixws(a, allws) | |
106 | self.assertEqual( |
|
116 | self.assertEqual( | |
107 | c, b, 'fixws(%r) want %r got %r (allws=%r)' % (a, b, c, allws)) |
|
117 | c, b, 'fixws(%r) want %r got %r (allws=%r)' % (a, b, c, allws)) | |
108 |
|
118 | |||
109 | def test_nice_diff_for_trivial_change(self): |
|
119 | def test_nice_diff_for_trivial_change(self): | |
110 | self.assertEqual(self.showdiff( |
|
120 | self.assertEqual(self.showdiff( | |
111 | ''.join('<%s\n-\n' % i for i in range(5)), |
|
121 | ''.join('<%s\n-\n' % i for i in range(5)), | |
112 | ''.join('>%s\n-\n' % i for i in range(5))), |
|
122 | ''.join('>%s\n-\n' % i for i in range(5))), | |
113 | [diffreplace(0, 3, '<0\n', '>0\n'), |
|
123 | [diffreplace(0, 3, '<0\n', '>0\n'), | |
114 | '-\n', |
|
124 | '-\n', | |
115 | diffreplace(5, 8, '<1\n', '>1\n'), |
|
125 | diffreplace(5, 8, '<1\n', '>1\n'), | |
116 | '-\n', |
|
126 | '-\n', | |
117 | diffreplace(10, 13, '<2\n', '>2\n'), |
|
127 | diffreplace(10, 13, '<2\n', '>2\n'), | |
118 | '-\n', |
|
128 | '-\n', | |
119 | diffreplace(15, 18, '<3\n', '>3\n'), |
|
129 | diffreplace(15, 18, '<3\n', '>3\n'), | |
120 | '-\n', |
|
130 | '-\n', | |
121 | diffreplace(20, 23, '<4\n', '>4\n'), |
|
131 | diffreplace(20, 23, '<4\n', '>4\n'), | |
122 | '-\n']) |
|
132 | '-\n']) | |
123 |
|
133 | |||
124 | def test_prefer_appending(self): |
|
134 | def test_prefer_appending(self): | |
125 | # 1 line to 3 lines |
|
135 | # 1 line to 3 lines | |
126 | self.assertEqual(self.showdiff('a\n', 'a\n' * 3), |
|
136 | self.assertEqual(self.showdiff('a\n', 'a\n' * 3), | |
127 | ['a\n', diffreplace(2, 2, '', 'a\na\n')]) |
|
137 | ['a\n', diffreplace(2, 2, '', 'a\na\n')]) | |
128 | # 1 line to 5 lines |
|
138 | # 1 line to 5 lines | |
129 | self.assertEqual(self.showdiff('a\n', 'a\n' * 5), |
|
139 | self.assertEqual(self.showdiff('a\n', 'a\n' * 5), | |
130 | ['a\n', diffreplace(2, 2, '', 'a\na\na\na\n')]) |
|
140 | ['a\n', diffreplace(2, 2, '', 'a\na\na\na\n')]) | |
131 |
|
141 | |||
132 | def test_prefer_removing_trailing(self): |
|
142 | def test_prefer_removing_trailing(self): | |
133 | # 3 lines to 1 line |
|
143 | # 3 lines to 1 line | |
134 | self.assertEqual(self.showdiff('a\n' * 3, 'a\n'), |
|
144 | self.assertEqual(self.showdiff('a\n' * 3, 'a\n'), | |
135 | ['a\n', diffreplace(2, 6, 'a\na\n', '')]) |
|
145 | ['a\n', diffreplace(2, 6, 'a\na\n', '')]) | |
136 | # 5 lines to 1 line |
|
146 | # 5 lines to 1 line | |
137 | self.assertEqual(self.showdiff('a\n' * 5, 'a\n'), |
|
147 | self.assertEqual(self.showdiff('a\n' * 5, 'a\n'), | |
138 | ['a\n', diffreplace(2, 10, 'a\na\na\na\n', '')]) |
|
148 | ['a\n', diffreplace(2, 10, 'a\na\na\na\n', '')]) | |
139 |
|
149 | |||
140 | if __name__ == '__main__': |
|
150 | if __name__ == '__main__': | |
141 | silenttestrunner.main(__name__) |
|
151 | silenttestrunner.main(__name__) |
General Comments 0
You need to be logged in to leave comments.
Login now