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 AppendA(TestBase):
 
 
113
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
114
                    ['aaa\n', 'bbb\n', '222\n'],
 
 
117
        self.assertEquals(''.join(m3.merge_lines()),
 
 
120
class AppendB(TestBase):
 
 
122
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
124
                    ['aaa\n', 'bbb\n', '222\n'])
 
 
126
        self.assertEquals(''.join(m3.merge_lines()),
 
 
129
class AppendAgreement(TestBase):
 
 
131
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
132
                    ['aaa\n', 'bbb\n', '222\n'],
 
 
133
                    ['aaa\n', 'bbb\n', '222\n'])
 
 
135
        self.assertEquals(''.join(m3.merge_lines()),
 
 
138
class AppendClash(TestBase):
 
 
140
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
141
                    ['aaa\n', 'bbb\n', '222\n'],
 
 
142
                    ['aaa\n', 'bbb\n', '333\n'])
 
 
144
        ml = m3.merge_lines(name_a='a',
 
 
149
        self.assertEquals(''.join(ml),
 
 
161
class InsertAgreement(TestBase):
 
 
163
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
164
                    ['aaa\n', '222\n', 'bbb\n'],
 
 
165
                    ['aaa\n', '222\n', 'bbb\n'])
 
 
167
        ml = m3.merge_lines(name_a='a',
 
 
172
        self.assertEquals(''.join(m3.merge_lines()),
 
 
178
class InsertClash(TestBase):
 
 
179
    """Both try to insert lines in the same place."""
 
 
181
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
182
                    ['aaa\n', '111\n', 'bbb\n'],
 
 
183
                    ['aaa\n', '222\n', 'bbb\n'])
 
 
185
        self.assertEquals(m3.find_unconflicted(),
 
 
188
        self.assertEquals(list(m3.find_sync_regions()),
 
 
193
        self.assertEquals(list(m3.merge_regions()),
 
 
195
                           ('conflict', 1,1, 1,2, 1,2),
 
 
198
        self.assertEquals(list(m3.merge_groups()),
 
 
199
                          [('unchanged', ['aaa\n']),
 
 
200
                           ('conflict', [], ['111\n'], ['222\n']),
 
 
201
                           ('unchanged', ['bbb\n']),
 
 
204
        ml = m3.merge_lines(name_a='a',
 
 
209
        self.assertEquals(''.join(ml),
 
 
221
class ReplaceClash(TestBase):
 
 
222
    """Both try to insert lines in the same place."""
 
 
224
        m3 = Merge3(['aaa', '000', 'bbb'],
 
 
225
                    ['aaa', '111', 'bbb'],
 
 
226
                    ['aaa', '222', 'bbb'])
 
 
228
        self.assertEquals(m3.find_unconflicted(),
 
 
231
        self.assertEquals(list(m3.find_sync_regions()),
 
 
238
class ReplaceMulti(TestBase):
 
 
239
    """Replacement with regions of different size."""
 
 
241
        m3 = Merge3(['aaa', '000', '000', 'bbb'],
 
 
242
                    ['aaa', '111', '111', '111', 'bbb'],
 
 
243
                    ['aaa', '222', '222', '222', '222', 'bbb'])
 
 
245
        self.assertEquals(m3.find_unconflicted(),
 
 
249
        self.assertEquals(list(m3.find_sync_regions()),
 
 
261
    from cStringIO import StringIO
 
 
262
    return StringIO(t).readlines()
 
 
266
############################################################
 
 
267
# test case from the gnu diffutils manual
 
 
270
TZU = split_lines("""     The Nameless is the origin of Heaven and Earth;
 
 
271
     The named is the mother of all things.
 
 
273
     Therefore let there always be non-being,
 
 
274
       so we may see their subtlety,
 
 
275
     And let there always be being,
 
 
276
       so we may see their outcome.
 
 
277
     The two are the same,
 
 
278
     But after they are produced,
 
 
279
       they have different names.
 
 
280
     They both may be called deep and profound.
 
 
281
     Deeper and more profound,
 
 
282
     The door of all subtleties!
 
 
285
LAO = split_lines("""     The Way that can be told of is not the eternal Way;
 
 
286
     The name that can be named is not the eternal name.
 
 
287
     The Nameless is the origin of Heaven and Earth;
 
 
288
     The Named is the mother of all things.
 
 
289
     Therefore let there always be non-being,
 
 
290
       so we may see their subtlety,
 
 
291
     And let there always be being,
 
 
292
       so we may see their outcome.
 
 
293
     The two are the same,
 
 
294
     But after they are produced,
 
 
295
       they have different names.
 
 
299
TAO = split_lines("""     The Way that can be told of is not the eternal Way;
 
 
300
     The name that can be named is not the eternal name.
 
 
301
     The Nameless is the origin of Heaven and Earth;
 
 
302
     The named is the mother of all things.
 
 
304
     Therefore let there always be non-being,
 
 
305
       so we may see their subtlety,
 
 
306
     And let there always be being,
 
 
307
       so we may see their result.
 
 
308
     The two are the same,
 
 
309
     But after they are produced,
 
 
310
       they have different names.
 
 
312
       -- The Way of Lao-Tzu, tr. Wing-tsit Chan
 
 
316
MERGED_RESULT = split_lines("""     The Way that can be told of is not the eternal Way;
 
 
317
     The name that can be named is not the eternal name.
 
 
318
     The Nameless is the origin of Heaven and Earth;
 
 
319
     The Named is the mother of all things.
 
 
320
     Therefore let there always be non-being,
 
 
321
       so we may see their subtlety,
 
 
322
     And let there always be being,
 
 
323
       so we may see their result.
 
 
324
     The two are the same,
 
 
325
     But after they are produced,
 
 
326
       they have different names.
 
 
330
       -- The Way of Lao-Tzu, tr. Wing-tsit Chan
 
 
337
class MergePoem(TestBase):
 
 
338
    """Test case from diff3 manual"""
 
 
340
        m3 = Merge3(TZU, LAO, TAO)
 
 
341
        ml = list(m3.merge_lines('LAO', 'TAO'))
 
 
342
        self.log('merge result:')
 
 
343
        self.log(''.join(ml))
 
 
344
        self.assertEquals(ml, MERGED_RESULT)