##// END OF EJS Templates
tests: finish updating test-bdiff to unittest (part 4 of 4)
Augie Fackler -
r30594:ea648e8f default
parent child Browse files
Show More
@@ -1,140 +1,141 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 85 # we should pick up abbbc. rather than bc.de as the longest match
86 86 ("a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n",
87 87 "a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n",
88 88 ['a\nb\nb\n',
89 89 diffreplace(6, 6, '', 'a\nb\nb\nb\nc\n.\n'),
90 90 'b\nc\n.\nd\ne\n',
91 91 diffreplace(16, 18, '.\n', ''),
92 92 'f\n']),
93 93 ]
94 94 for old, new, want in cases:
95 95 self.assertEqual(self.showdiff(old, new), want)
96 96
97 97 def test_fixws(self):
98 98 cases = [
99 99 (" \ta\r b\t\n", "ab\n", 1),
100 100 (" \ta\r b\t\n", " a b\n", 0),
101 101 ("", "", 1),
102 102 ("", "", 0),
103 103 ]
104 104 for a, b, allws in cases:
105 105 c = bdiff.fixws(a, allws)
106 106 self.assertEqual(
107 107 c, b, 'fixws(%r) want %r got %r (allws=%r)' % (a, b, c, allws))
108 108
109 def showdiff(a, b):
110 print('showdiff(\n %r,\n %r):' % (a, b))
111 bin = bdiff.bdiff(a, b)
112 pos = 0
113 q = 0
114 while pos < len(bin):
115 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
116 pos += 12
117 if p1:
118 print('', repr(a[q:p1]))
119 print('', p1, p2, repr(a[p1:p2]), '->', repr(bin[pos:pos + l]))
120 pos += l
121 q = p2
122 if q < len(a):
123 print('', repr(a[q:]))
109 def test_nice_diff_for_trivial_change(self):
110 self.assertEqual(self.showdiff(
111 ''.join('<%s\n-\n' % i for i in range(5)),
112 ''.join('>%s\n-\n' % i for i in range(5))),
113 [diffreplace(0, 3, '<0\n', '>0\n'),
114 '-\n',
115 diffreplace(5, 8, '<1\n', '>1\n'),
116 '-\n',
117 diffreplace(10, 13, '<2\n', '>2\n'),
118 '-\n',
119 diffreplace(15, 18, '<3\n', '>3\n'),
120 '-\n',
121 diffreplace(20, 23, '<4\n', '>4\n'),
122 '-\n'])
124 123
125 print("Nice diff for a trivial change:")
126 showdiff(
127 ''.join('<%s\n-\n' % i for i in range(5)),
128 ''.join('>%s\n-\n' % i for i in range(5)))
124 def test_prefer_appending(self):
125 # 1 line to 3 lines
126 self.assertEqual(self.showdiff('a\n', 'a\n' * 3),
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:")
131 showdiff('a\n', 'a\n' * 3)
132 print("Diff 1 to 5 lines - preference for appending:")
133 showdiff('a\n', 'a\n' * 5)
134 print("Diff 3 to 1 lines - preference for removing trailing lines:")
135 showdiff('a\n' * 3, 'a\n')
136 print("Diff 5 to 1 lines - preference for removing trailing lines:")
137 showdiff('a\n' * 5, 'a\n')
132 def test_prefer_removing_trailing(self):
133 # 3 lines to 1 line
134 self.assertEqual(self.showdiff('a\n' * 3, 'a\n'),
135 ['a\n', diffreplace(2, 6, 'a\na\n', '')])
136 # 5 lines to 1 line
137 self.assertEqual(self.showdiff('a\n' * 5, 'a\n'),
138 ['a\n', diffreplace(2, 10, 'a\na\na\na\n', '')])
138 139
139 140 if __name__ == '__main__':
140 141 silenttestrunner.main(__name__)
1 NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now