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