87
85
they have different names.
91
89
-- The Way of Lao-Tzu, tr. Wing-tsit Chan
96
class TestMerge3(tests.TestCase):
94
class TestMerge3(TestCase):
98
96
def test_no_changes(self):
99
97
"""No conflicts because nothing changed"""
100
m3 = merge3.Merge3(['aaa', 'bbb'],
98
m3 = Merge3(['aaa', 'bbb'],
104
self.assertEqual(m3.find_unconflicted(),
102
self.assertEquals(m3.find_unconflicted(),
107
self.assertEqual(list(m3.find_sync_regions()),
105
self.assertEquals(list(m3.find_sync_regions()),
111
109
(2,2, 2,2, 2,2)])
113
self.assertEqual(list(m3.merge_regions()),
111
self.assertEquals(list(m3.merge_regions()),
114
112
[('unchanged', 0, 2)])
116
self.assertEqual(list(m3.merge_groups()),
114
self.assertEquals(list(m3.merge_groups()),
117
115
[('unchanged', ['aaa', 'bbb'])])
119
117
def test_front_insert(self):
120
m3 = merge3.Merge3(['zz'],
121
['aaa', 'bbb', 'zz'],
119
['aaa', 'bbb', 'zz'],
124
122
# todo: should use a sentinal at end as from get_matching_blocks
125
123
# to match without zz
126
self.assertEqual(list(m3.find_sync_regions()),
124
self.assertEquals(list(m3.find_sync_regions()),
127
125
[(0,1, 2,3, 0,1),
128
126
(1,1, 3,3, 1,1),])
130
self.assertEqual(list(m3.merge_regions()),
128
self.assertEquals(list(m3.merge_regions()),
132
130
('unchanged', 0, 1)])
134
self.assertEqual(list(m3.merge_groups()),
132
self.assertEquals(list(m3.merge_groups()),
135
133
[('a', ['aaa', 'bbb']),
136
134
('unchanged', ['zz'])])
138
136
def test_null_insert(self):
139
m3 = merge3.Merge3([],
142
140
# todo: should use a sentinal at end as from get_matching_blocks
143
141
# to match without zz
144
self.assertEqual(list(m3.find_sync_regions()),
142
self.assertEquals(list(m3.find_sync_regions()),
145
143
[(0,0, 2,2, 0,0)])
147
self.assertEqual(list(m3.merge_regions()),
145
self.assertEquals(list(m3.merge_regions()),
150
self.assertEqual(list(m3.merge_lines()),
148
self.assertEquals(list(m3.merge_lines()),
153
151
def test_no_conflicts(self):
154
152
"""No conflicts because only one side changed"""
155
m3 = merge3.Merge3(['aaa', 'bbb'],
156
['aaa', '111', 'bbb'],
153
m3 = Merge3(['aaa', 'bbb'],
154
['aaa', '111', 'bbb'],
159
self.assertEqual(m3.find_unconflicted(),
157
self.assertEquals(m3.find_unconflicted(),
160
158
[(0, 1), (1, 2)])
162
self.assertEqual(list(m3.find_sync_regions()),
160
self.assertEquals(list(m3.find_sync_regions()),
163
161
[(0,1, 0,1, 0,1),
165
163
(2,2, 3,3, 2,2),])
167
self.assertEqual(list(m3.merge_regions()),
165
self.assertEquals(list(m3.merge_regions()),
168
166
[('unchanged', 0, 1),
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
self.assertEqual(''.join(m3.merge_lines()),
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
self.assertEqual(''.join(m3.merge_lines()),
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
self.assertEqual(''.join(m3.merge_lines()),
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',
203
201
start_marker='<<',
206
self.assertEqual(''.join(ml),
204
self.assertEquals(''.join(ml),
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',
224
222
start_marker='<<',
227
self.assertEqual(''.join(ml), 'aaa\n222\nbbb\n')
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
self.assertEqual(m3.find_unconflicted(),
234
self.assertEquals(m3.find_unconflicted(),
237
235
[(0, 1), (1, 2)])
239
self.assertEqual(list(m3.find_sync_regions()),
237
self.assertEquals(list(m3.find_sync_regions()),
240
238
[(0,1, 0,1, 0,1),
242
240
(2,2, 3,3, 3,3),])
244
self.assertEqual(list(m3.merge_regions()),
242
self.assertEquals(list(m3.merge_regions()),
245
243
[('unchanged', 0,1),
246
244
('conflict', 1,1, 1,2, 1,2),
247
245
('unchanged', 1,2)])
249
self.assertEqual(list(m3.merge_groups()),
247
self.assertEquals(list(m3.merge_groups()),
250
248
[('unchanged', ['aaa\n']),
251
249
('conflict', [], ['111\n'], ['222\n']),
252
250
('unchanged', ['bbb\n']),
270
268
def test_replace_clash(self):
271
269
"""Both try to insert lines in the same place."""
272
m3 = merge3.Merge3(['aaa', '000', 'bbb'],
273
['aaa', '111', 'bbb'],
274
['aaa', '222', 'bbb'])
270
m3 = Merge3(['aaa', '000', 'bbb'],
271
['aaa', '111', 'bbb'],
272
['aaa', '222', 'bbb'])
276
self.assertEqual(m3.find_unconflicted(),
274
self.assertEquals(m3.find_unconflicted(),
277
275
[(0, 1), (2, 3)])
279
self.assertEqual(list(m3.find_sync_regions()),
277
self.assertEquals(list(m3.find_sync_regions()),
280
278
[(0,1, 0,1, 0,1),
282
280
(3,3, 3,3, 3,3),])
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
self.assertEqual(m3.find_unconflicted(),
288
self.assertEquals(m3.find_unconflicted(),
291
289
[(0, 1), (3, 4)])
294
self.assertEqual(list(m3.find_sync_regions()),
292
self.assertEquals(list(m3.find_sync_regions()),
295
293
[(0,1, 0,1, 0,1),
297
295
(4,4, 5,5, 6,6),])
299
297
def test_merge_poem(self):
300
298
"""Test case from diff3 manual"""
301
m3 = merge3.Merge3(TZU, LAO, TAO)
299
m3 = Merge3(TZU, LAO, TAO)
302
300
ml = list(m3.merge_lines('LAO', 'TAO'))
303
301
self.log('merge result:')
304
302
self.log(''.join(ml))
305
self.assertEqual(ml, MERGED_RESULT)
303
self.assertEquals(ml, MERGED_RESULT)
307
305
def test_minimal_conflicts_common(self):
308
306
"""Reprocessing"""
309
307
base_text = ("a\n" * 20).splitlines(True)
310
308
this_text = ("a\n"*10+"b\n" * 10).splitlines(True)
311
309
other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True)
312
m3 = merge3.Merge3(base_text, other_text, this_text)
310
m3 = Merge3(base_text, other_text, this_text)
313
311
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
314
312
merged_text = "".join(list(m_lines))
315
313
optimal_text = ("a\n" * 10 + "<<<<<<< OTHER\nc\n"
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'])