15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
from bzrlib.merge3 import Merge3
24
from bzrlib.errors import CantReprocessAndShowBase, BinaryFile
22
from ..errors import CantReprocessAndShowBase, BinaryFile
23
from ..sixish import (
26
27
def split_lines(t):
27
from cStringIO import StringIO
28
return StringIO(t).readlines()
28
return BytesIO(t).readlines()
30
30
############################################################
31
31
# test case data from the gnu diffutils manual
33
TZU = split_lines(""" The Nameless is the origin of Heaven and Earth;
33
TZU = split_lines(b""" The Nameless is the origin of Heaven and Earth;
34
34
The named is the mother of all things.
36
36
Therefore let there always be non-being,
45
45
The door of all subtleties!
48
LAO = split_lines(""" The Way that can be told of is not the eternal Way;
48
LAO = split_lines(b""" The Way that can be told of is not the eternal Way;
49
49
The name that can be named is not the eternal name.
50
50
The Nameless is the origin of Heaven and Earth;
51
51
The Named is the mother of all things.
79
MERGED_RESULT = split_lines(""" The Way that can be told of is not the eternal Way;
79
MERGED_RESULT = split_lines(b""" The Way that can be told of is not the eternal Way;
80
80
The name that can be named is not the eternal name.
81
81
The Nameless is the origin of Heaven and Earth;
82
82
The Named is the mother of all things.
106
self.assertEquals(m3.find_unconflicted(),
106
self.assertEqual(m3.find_unconflicted(),
109
self.assertEquals(list(m3.find_sync_regions()),
109
self.assertEqual(list(m3.find_sync_regions()),
115
self.assertEquals(list(m3.merge_regions()),
115
self.assertEqual(list(m3.merge_regions()),
116
116
[('unchanged', 0, 2)])
118
self.assertEquals(list(m3.merge_groups()),
118
self.assertEqual(list(m3.merge_groups()),
119
119
[('unchanged', ['aaa', 'bbb'])])
121
121
def test_front_insert(self):
126
126
# todo: should use a sentinal at end as from get_matching_blocks
127
127
# to match without zz
128
self.assertEquals(list(m3.find_sync_regions()),
128
self.assertEqual(list(m3.find_sync_regions()),
130
(1, 1, 3, 3, 1, 1),])
132
self.assertEquals(list(m3.merge_regions()),
132
self.assertEqual(list(m3.merge_regions()),
134
134
('unchanged', 0, 1)])
136
self.assertEquals(list(m3.merge_groups()),
136
self.assertEqual(list(m3.merge_groups()),
137
137
[('a', ['aaa', 'bbb']),
138
138
('unchanged', ['zz'])])
144
144
# todo: should use a sentinal at end as from get_matching_blocks
145
145
# to match without zz
146
self.assertEquals(list(m3.find_sync_regions()),
146
self.assertEqual(list(m3.find_sync_regions()),
147
[(0, 0, 2, 2, 0, 0)])
149
self.assertEquals(list(m3.merge_regions()),
149
self.assertEqual(list(m3.merge_regions()),
152
self.assertEquals(list(m3.merge_lines()),
152
self.assertEqual(list(m3.merge_lines()),
155
155
def test_no_conflicts(self):
158
158
['aaa', '111', 'bbb'],
161
self.assertEquals(m3.find_unconflicted(),
161
self.assertEqual(m3.find_unconflicted(),
162
162
[(0, 1), (1, 2)])
164
self.assertEquals(list(m3.find_sync_regions()),
164
self.assertEqual(list(m3.find_sync_regions()),
167
(2, 2, 3, 3, 2, 2),])
169
self.assertEquals(list(m3.merge_regions()),
169
self.assertEqual(list(m3.merge_regions()),
170
170
[('unchanged', 0, 1),
172
172
('unchanged', 1, 2),])
184
184
['aaa\n', 'bbb\n'],
185
185
['aaa\n', 'bbb\n', '222\n'])
187
self.assertEquals(''.join(m3.merge_lines()),
187
self.assertEqual(''.join(m3.merge_lines()),
188
188
'aaa\nbbb\n222\n')
190
190
def test_append_agreement(self):
192
192
['aaa\n', 'bbb\n', '222\n'],
193
193
['aaa\n', 'bbb\n', '222\n'])
195
self.assertEquals(''.join(m3.merge_lines()),
195
self.assertEqual(''.join(m3.merge_lines()),
196
196
'aaa\nbbb\n222\n')
198
198
def test_append_clash(self):
235
235
['aaa\n', '111\n', 'bbb\n'],
236
236
['aaa\n', '222\n', 'bbb\n'])
238
self.assertEquals(m3.find_unconflicted(),
238
self.assertEqual(m3.find_unconflicted(),
239
239
[(0, 1), (1, 2)])
241
self.assertEquals(list(m3.find_sync_regions()),
246
self.assertEquals(list(m3.merge_regions()),
248
('conflict', 1,1, 1,2, 1,2),
251
self.assertEquals(list(m3.merge_groups()),
241
self.assertEqual(list(m3.find_sync_regions()),
244
(2, 2, 3, 3, 3, 3),])
246
self.assertEqual(list(m3.merge_regions()),
247
[('unchanged', 0, 1),
248
('conflict', 1, 1, 1, 2, 1, 2),
249
('unchanged', 1, 2)])
251
self.assertEqual(list(m3.merge_groups()),
252
252
[('unchanged', ['aaa\n']),
253
253
('conflict', [], ['111\n'], ['222\n']),
254
254
('unchanged', ['bbb\n']),
257
ml = m3.merge_lines(name_a='a',
262
self.assertEquals(''.join(ml),
257
ml = m3.merge_lines(name_a=b'a',
262
self.assertEqual(b''.join(ml),
272
272
def test_replace_clash(self):
273
273
"""Both try to insert lines in the same place."""
274
m3 = merge3.Merge3(['aaa', '000', 'bbb'],
275
['aaa', '111', 'bbb'],
276
['aaa', '222', 'bbb'])
274
m3 = merge3.Merge3([b'aaa', b'000', b'bbb'],
275
[b'aaa', b'111', b'bbb'],
276
[b'aaa', b'222', b'bbb'])
278
self.assertEquals(m3.find_unconflicted(),
278
self.assertEqual(m3.find_unconflicted(),
279
279
[(0, 1), (2, 3)])
281
self.assertEquals(list(m3.find_sync_regions()),
281
self.assertEqual(list(m3.find_sync_regions()),
284
(3, 3, 3, 3, 3, 3),])
286
286
def test_replace_multi(self):
287
287
"""Replacement with regions of different size."""
288
m3 = merge3.Merge3(['aaa', '000', '000', 'bbb'],
289
['aaa', '111', '111', '111', 'bbb'],
290
['aaa', '222', '222', '222', '222', 'bbb'])
288
m3 = merge3.Merge3([b'aaa', b'000', b'000', b'bbb'],
289
[b'aaa', b'111', b'111', b'111', b'bbb'],
290
[b'aaa', b'222', b'222', b'222', b'222', b'bbb'])
292
self.assertEquals(m3.find_unconflicted(),
292
self.assertEqual(m3.find_unconflicted(),
293
293
[(0, 1), (3, 4)])
296
self.assertEquals(list(m3.find_sync_regions()),
296
self.assertEqual(list(m3.find_sync_regions()),
299
(4, 4, 5, 5, 6, 6),])
301
301
def test_merge_poem(self):
302
302
"""Test case from diff3 manual"""
304
304
ml = list(m3.merge_lines('LAO', 'TAO'))
305
305
self.log('merge result:')
306
306
self.log(''.join(ml))
307
self.assertEquals(ml, MERGED_RESULT)
307
self.assertEqual(ml, MERGED_RESULT)
309
309
def test_minimal_conflicts_common(self):
310
310
"""Reprocessing"""
439
439
merge_groups and merge_regions work with non-str input. Methods that
440
440
return lines like merge_lines fail.
442
base = [(x,x) for x in 'abcde']
443
a = [(x,x) for x in 'abcdef']
444
b = [(x,x) for x in 'Zabcde']
442
base = [(x, x) for x in 'abcde']
443
a = [(x, x) for x in 'abcdef']
444
b = [(x, x) for x in 'Zabcde']
445
445
m3 = merge3.Merge3(base, a, b, allow_objects=True)
446
446
self.assertEqual(
450
450
list(m3.merge_regions()))
451
451
self.assertEqual(
452
452
[('b', [('Z', 'Z')]),
453
('unchanged', [(x,x) for x in 'abcde']),
453
('unchanged', [(x, x) for x in 'abcde']),
454
454
('a', [('f', 'f')])],
455
455
list(m3.merge_groups()))