1
# Copyright (C) 2004, 2005 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.selftest import InTempDir, TestBase
19
from bzrlib.merge3 import Merge3
25
class NoChanges(TestBase):
26
"""No conflicts because nothing changed"""
28
m3 = Merge3(['aaa', 'bbb'],
32
self.assertEquals(m3.find_unconflicted(),
35
self.assertEquals(list(m3.find_sync_regions()),
41
self.assertEquals(list(m3.merge_regions()),
42
[('unchanged', 0, 2)])
44
self.assertEquals(list(m3.merge_groups()),
45
[('unchanged', ['aaa', 'bbb'])])
48
class FrontInsert(TestBase):
54
# todo: should use a sentinal at end as from get_matching_blocks
56
self.assertEquals(list(m3.find_sync_regions()),
60
self.assertEquals(list(m3.merge_regions()),
64
self.assertEquals(list(m3.merge_groups()),
65
[('a', ['aaa', 'bbb']),
66
('unchanged', ['zz'])])
70
class NullInsert(TestBase):
76
# todo: should use a sentinal at end as from get_matching_blocks
78
self.assertEquals(list(m3.find_sync_regions()),
81
self.assertEquals(list(m3.merge_regions()),
84
self.assertEquals(list(m3.merge_lines()),
89
class NoConflicts(TestBase):
90
"""No conflicts because only one side changed"""
92
m3 = Merge3(['aaa', 'bbb'],
93
['aaa', '111', 'bbb'],
96
self.assertEquals(m3.find_unconflicted(),
99
self.assertEquals(list(m3.find_sync_regions()),
104
self.assertEquals(list(m3.merge_regions()),
105
[('unchanged', 0, 1),
107
('unchanged', 1, 2),])
111
class InsertAgreement(TestBase):
113
m3 = Merge3(['aaa\n', 'bbb\n'],
114
['aaa\n', '222\n', 'bbb\n'],
115
['aaa\n', '222\n', 'bbb\n'])
117
self.assertEquals(''.join(m3.merge_lines()),
122
class InsertClash(TestBase):
123
"""Both try to insert lines in the same place."""
125
m3 = Merge3(['aaa\n', 'bbb\n'],
126
['aaa\n', '111\n', 'bbb\n'],
127
['aaa\n', '222\n', 'bbb\n'])
129
self.assertEquals(m3.find_unconflicted(),
132
self.assertEquals(list(m3.find_sync_regions()),
137
self.assertEquals(list(m3.merge_regions()),
139
('conflict', 1,1, 1,2, 1,2),
142
self.assertEquals(list(m3.merge_groups()),
143
[('unchanged', ['aaa\n']),
144
('conflict', [], ['111\n'], ['222\n']),
145
('unchanged', ['bbb\n']),
148
ml = m3.merge_lines(name_a='a',
153
self.assertEquals(''.join(ml),
165
class ReplaceClash(TestBase):
166
"""Both try to insert lines in the same place."""
168
m3 = Merge3(['aaa', '000', 'bbb'],
169
['aaa', '111', 'bbb'],
170
['aaa', '222', 'bbb'])
172
self.assertEquals(m3.find_unconflicted(),
175
self.assertEquals(list(m3.find_sync_regions()),
182
class ReplaceMulti(TestBase):
183
"""Replacement with regions of different size."""
185
m3 = Merge3(['aaa', '000', '000', 'bbb'],
186
['aaa', '111', '111', '111', 'bbb'],
187
['aaa', '222', '222', '222', '222', 'bbb'])
189
self.assertEquals(m3.find_unconflicted(),
193
self.assertEquals(list(m3.find_sync_regions()),
205
from cStringIO import StringIO
206
return StringIO(t).readlines()
210
############################################################
211
# test case from the gnu diffutils manual
214
TZU = split_lines(""" The Nameless is the origin of Heaven and Earth;
215
The named is the mother of all things.
217
Therefore let there always be non-being,
218
so we may see their subtlety,
219
And let there always be being,
220
so we may see their outcome.
221
The two are the same,
222
But after they are produced,
223
they have different names.
224
They both may be called deep and profound.
225
Deeper and more profound,
226
The door of all subtleties!
229
LAO = split_lines(""" The Way that can be told of is not the eternal Way;
230
The name that can be named is not the eternal name.
231
The Nameless is the origin of Heaven and Earth;
232
The Named is the mother of all things.
233
Therefore let there always be non-being,
234
so we may see their subtlety,
235
And let there always be being,
236
so we may see their outcome.
237
The two are the same,
238
But after they are produced,
239
they have different names.
243
TAO = split_lines(""" The Way that can be told of is not the eternal Way;
244
The name that can be named is not the eternal name.
245
The Nameless is the origin of Heaven and Earth;
246
The named is the mother of all things.
248
Therefore let there always be non-being,
249
so we may see their subtlety,
250
And let there always be being,
251
so we may see their result.
252
The two are the same,
253
But after they are produced,
254
they have different names.
256
-- The Way of Lao-Tzu, tr. Wing-tsit Chan
260
MERGED_RESULT = split_lines(""" The Way that can be told of is not the eternal Way;
261
The name that can be named is not the eternal name.
262
The Nameless is the origin of Heaven and Earth;
263
The Named is the mother of all things.
264
Therefore let there always be non-being,
265
so we may see their subtlety,
266
And let there always be being,
267
so we may see their result.
268
The two are the same,
269
But after they are produced,
270
they have different names.
274
-- The Way of Lao-Tzu, tr. Wing-tsit Chan
281
class MergePoem(TestBase):
282
"""Test case from diff3 manual"""
284
m3 = Merge3(TZU, LAO, TAO)
285
ml = list(m3.merge_lines('LAO', 'TAO'))
286
self.log('merge result:')
287
self.log(''.join(ml))
288
self.assertEquals(ml, MERGED_RESULT)