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