1
# Copyright (C) 2005-2011 Canonical Ltd
1
# Copyright (C) 2004, 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.tests import TestCaseInTempDir, TestCase
19
from bzrlib.merge3 import Merge3
22
20
from bzrlib.errors import CantReprocessAndShowBase, BinaryFile
24
22
def split_lines(t):
170
168
('unchanged', 1, 2),])
172
170
def test_append_a(self):
173
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
174
['aaa\n', 'bbb\n', '222\n'],
171
m3 = Merge3(['aaa\n', 'bbb\n'],
172
['aaa\n', 'bbb\n', '222\n'],
177
175
self.assertEquals(''.join(m3.merge_lines()),
178
176
'aaa\nbbb\n222\n')
180
178
def test_append_b(self):
181
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
183
['aaa\n', 'bbb\n', '222\n'])
179
m3 = Merge3(['aaa\n', 'bbb\n'],
181
['aaa\n', 'bbb\n', '222\n'])
185
183
self.assertEquals(''.join(m3.merge_lines()),
186
184
'aaa\nbbb\n222\n')
188
186
def test_append_agreement(self):
189
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
190
['aaa\n', 'bbb\n', '222\n'],
191
['aaa\n', 'bbb\n', '222\n'])
187
m3 = Merge3(['aaa\n', 'bbb\n'],
188
['aaa\n', 'bbb\n', '222\n'],
189
['aaa\n', 'bbb\n', '222\n'])
193
191
self.assertEquals(''.join(m3.merge_lines()),
194
192
'aaa\nbbb\n222\n')
196
194
def test_append_clash(self):
197
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
198
['aaa\n', 'bbb\n', '222\n'],
199
['aaa\n', 'bbb\n', '333\n'])
195
m3 = Merge3(['aaa\n', 'bbb\n'],
196
['aaa\n', 'bbb\n', '222\n'],
197
['aaa\n', 'bbb\n', '333\n'])
201
199
ml = m3.merge_lines(name_a='a',
217
215
def test_insert_agreement(self):
218
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
219
['aaa\n', '222\n', 'bbb\n'],
220
['aaa\n', '222\n', 'bbb\n'])
216
m3 = Merge3(['aaa\n', 'bbb\n'],
217
['aaa\n', '222\n', 'bbb\n'],
218
['aaa\n', '222\n', 'bbb\n'])
222
220
ml = m3.merge_lines(name_a='a',
227
225
self.assertEquals(''.join(ml), 'aaa\n222\nbbb\n')
230
228
def test_insert_clash(self):
231
229
"""Both try to insert lines in the same place."""
232
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
233
['aaa\n', '111\n', 'bbb\n'],
234
['aaa\n', '222\n', 'bbb\n'])
230
m3 = Merge3(['aaa\n', 'bbb\n'],
231
['aaa\n', '111\n', 'bbb\n'],
232
['aaa\n', '222\n', 'bbb\n'])
236
234
self.assertEquals(m3.find_unconflicted(),
237
235
[(0, 1), (1, 2)])
284
282
def test_replace_multi(self):
285
283
"""Replacement with regions of different size."""
286
m3 = merge3.Merge3(['aaa', '000', '000', 'bbb'],
287
['aaa', '111', '111', '111', 'bbb'],
288
['aaa', '222', '222', '222', '222', 'bbb'])
284
m3 = Merge3(['aaa', '000', '000', 'bbb'],
285
['aaa', '111', '111', '111', 'bbb'],
286
['aaa', '222', '222', '222', '222', 'bbb'])
290
288
self.assertEquals(m3.find_unconflicted(),
291
289
[(0, 1), (3, 4)])
358
356
base_text = ("a\n" * 20).splitlines(True)
359
357
this_text = ("a\n"*10+"b\n" * 10).splitlines(True)
360
358
other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True)
361
m3 = merge3.Merge3(base_text, other_text, this_text)
362
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True,
359
m3 = Merge3(base_text, other_text, this_text)
360
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True,
363
361
base_marker='|||||||')
364
362
self.assertRaises(CantReprocessAndShowBase, list, m_lines)
366
364
def test_binary(self):
367
self.assertRaises(BinaryFile, merge3.Merge3, ['\x00'], ['a'], ['b'])
369
def test_dos_text(self):
373
m3 = merge3.Merge3(base_text.splitlines(True),
374
other_text.splitlines(True),
375
this_text.splitlines(True))
376
m_lines = m3.merge_lines('OTHER', 'THIS')
377
self.assertEqual('<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n'
378
'>>>>>>> THIS\r\n'.splitlines(True), list(m_lines))
380
def test_mac_text(self):
384
m3 = merge3.Merge3(base_text.splitlines(True),
385
other_text.splitlines(True),
386
this_text.splitlines(True))
387
m_lines = m3.merge_lines('OTHER', 'THIS')
388
self.assertEqual('<<<<<<< OTHER\rc\r=======\rb\r'
389
'>>>>>>> THIS\r'.splitlines(True), list(m_lines))
391
def test_merge3_cherrypick(self):
394
other_text = "a\nb\nc\n"
395
# When cherrypicking, lines in base are not part of the conflict
396
m3 = merge3.Merge3(base_text.splitlines(True),
397
this_text.splitlines(True),
398
other_text.splitlines(True), is_cherrypick=True)
399
m_lines = m3.merge_lines()
400
self.assertEqualDiff('a\n<<<<<<<\n=======\nc\n>>>>>>>\n',
403
# This is not symmetric
404
m3 = merge3.Merge3(base_text.splitlines(True),
405
other_text.splitlines(True),
406
this_text.splitlines(True), is_cherrypick=True)
407
m_lines = m3.merge_lines()
408
self.assertEqualDiff('a\n<<<<<<<\nb\nc\n=======\n>>>>>>>\n',
411
def test_merge3_cherrypick_w_mixed(self):
412
base_text = 'a\nb\nc\nd\ne\n'
413
this_text = 'a\nb\nq\n'
414
other_text = 'a\nb\nc\nd\nf\ne\ng\n'
415
# When cherrypicking, lines in base are not part of the conflict
416
m3 = merge3.Merge3(base_text.splitlines(True),
417
this_text.splitlines(True),
418
other_text.splitlines(True), is_cherrypick=True)
419
m_lines = m3.merge_lines()
420
self.assertEqualDiff('a\n'
433
def test_allow_objects(self):
434
"""Objects other than strs may be used with Merge3 when
437
merge_groups and merge_regions work with non-str input. Methods that
438
return lines like merge_lines fail.
440
base = [(x,x) for x in 'abcde']
441
a = [(x,x) for x in 'abcdef']
442
b = [(x,x) for x in 'Zabcde']
443
m3 = merge3.Merge3(base, a, b, allow_objects=True)
448
list(m3.merge_regions()))
450
[('b', [('Z', 'Z')]),
451
('unchanged', [(x,x) for x in 'abcde']),
452
('a', [('f', 'f')])],
453
list(m3.merge_groups()))
365
self.assertRaises(BinaryFile, Merge3, ['\x00'], ['a'], ['b'])