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