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
TZU = split_lines(""" The Nameless is the origin of Heaven and Earth;
211
The named is the mother of all things.
213
Therefore let there always be non-being,
214
so we may see their subtlety,
215
And let there always be being,
216
so we may see their outcome.
217
The two are the same,
218
But after they are produced,
219
they have different names.
220
They both may be called deep and profound.
221
Deeper and more profound,
222
The door of all subtleties!
225
LAO = split_lines(""" The Way that can be told of is not the eternal Way;
226
The name that can be named is not the eternal name.
227
The Nameless is the origin of Heaven and Earth;
228
The Named is the mother of all things.
229
Therefore let there always be non-being,
230
so we may see their subtlety,
231
And let there always be being,
232
so we may see their outcome.
233
The two are the same,
234
But after they are produced,
235
they have different names.
239
TAO = split_lines(""" The Way that can be told of is not the eternal Way;
240
The name that can be named is not the eternal name.
241
The Nameless is the origin of Heaven and Earth;
242
The named is the mother of all things.
244
Therefore let there always be non-being,
245
so we may see their subtlety,
246
And let there always be being,
247
so we may see their result.
248
The two are the same,
249
But after they are produced,
250
they have different names.
252
-- The Way of Lao-Tzu, tr. Wing-tsit Chan
256
MERGED_RESULT = split_lines(""" The Way that can be told of is not the eternal Way;
257
The name that can be named is not the eternal name.
258
The Nameless is the origin of Heaven and Earth;
259
The Named is the mother of all things.
260
Therefore let there always be non-being,
261
so we may see their subtlety,
262
And let there always be being,
263
so we may see their result.
264
The two are the same,
265
But after they are produced,
266
they have different names.
270
-- The Way of Lao-Tzu, tr. Wing-tsit Chan
277
class MergePoem(TestBase):
278
"""Test case from diff3 manual"""
280
m3 = Merge3(TZU, LAO, TAO)
281
ml = list(m3.merge_lines('LAO', 'TAO'))
282
self.log('merge result:')
283
self.log(''.join(ml))
284
self.assertEquals(ml, MERGED_RESULT)