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, TestCase
 
 
19
from bzrlib.merge3 import Merge3
 
 
22
    from cStringIO import StringIO
 
 
23
    return StringIO(t).readlines()
 
 
25
############################################################
 
 
26
# test case data from the gnu diffutils manual
 
 
28
TZU = split_lines("""     The Nameless is the origin of Heaven and Earth;
 
 
29
     The named is the mother of all things.
 
 
31
     Therefore let there always be non-being,
 
 
32
       so we may see their subtlety,
 
 
33
     And let there always be being,
 
 
34
       so we may see their outcome.
 
 
36
     But after they are produced,
 
 
37
       they have different names.
 
 
38
     They both may be called deep and profound.
 
 
39
     Deeper and more profound,
 
 
40
     The door of all subtleties!
 
 
43
LAO = split_lines("""     The Way that can be told of is not the eternal Way;
 
 
44
     The name that can be named is not the eternal name.
 
 
45
     The Nameless is the origin of Heaven and Earth;
 
 
46
     The Named is the mother of all things.
 
 
47
     Therefore let there always be non-being,
 
 
48
       so we may see their subtlety,
 
 
49
     And let there always be being,
 
 
50
       so we may see their outcome.
 
 
52
     But after they are produced,
 
 
53
       they have different names.
 
 
57
TAO = split_lines("""     The Way that can be told of is not the eternal Way;
 
 
58
     The name that can be named is not the eternal name.
 
 
59
     The Nameless is the origin of Heaven and Earth;
 
 
60
     The named is the mother of all things.
 
 
62
     Therefore let there always be non-being,
 
 
63
       so we may see their subtlety,
 
 
64
     And let there always be being,
 
 
65
       so we may see their result.
 
 
67
     But after they are produced,
 
 
68
       they have different names.
 
 
70
       -- The Way of Lao-Tzu, tr. Wing-tsit Chan
 
 
74
MERGED_RESULT = split_lines("""     The Way that can be told of is not the eternal Way;
 
 
75
     The name that can be named is not the eternal name.
 
 
76
     The Nameless is the origin of Heaven and Earth;
 
 
77
     The Named is the mother of all things.
 
 
78
     Therefore let there always be non-being,
 
 
79
       so we may see their subtlety,
 
 
80
     And let there always be being,
 
 
81
       so we may see their result.
 
 
83
     But after they are produced,
 
 
84
       they have different names.
 
 
88
       -- The Way of Lao-Tzu, tr. Wing-tsit Chan
 
 
93
class TestMerge3(TestCase):
 
 
95
    def test_no_changes(self):
 
 
96
        """No conflicts because nothing changed"""
 
 
97
        m3 = Merge3(['aaa', 'bbb'],
 
 
101
        self.assertEquals(m3.find_unconflicted(),
 
 
104
        self.assertEquals(list(m3.find_sync_regions()),
 
 
110
        self.assertEquals(list(m3.merge_regions()),
 
 
111
                          [('unchanged', 0, 2)])
 
 
113
        self.assertEquals(list(m3.merge_groups()),
 
 
114
                          [('unchanged', ['aaa', 'bbb'])])
 
 
116
    def test_front_insert(self):
 
 
118
                    ['aaa', 'bbb', 'zz'],
 
 
121
        # todo: should use a sentinal at end as from get_matching_blocks
 
 
122
        # to match without zz
 
 
123
        self.assertEquals(list(m3.find_sync_regions()),
 
 
127
        self.assertEquals(list(m3.merge_regions()),
 
 
129
                           ('unchanged', 0, 1)])
 
 
131
        self.assertEquals(list(m3.merge_groups()),
 
 
132
                          [('a', ['aaa', 'bbb']),
 
 
133
                           ('unchanged', ['zz'])])
 
 
135
    def test_null_insert(self):
 
 
139
        # todo: should use a sentinal at end as from get_matching_blocks
 
 
140
        # to match without zz
 
 
141
        self.assertEquals(list(m3.find_sync_regions()),
 
 
144
        self.assertEquals(list(m3.merge_regions()),
 
 
147
        self.assertEquals(list(m3.merge_lines()),
 
 
150
    def test_no_conflicts(self):
 
 
151
        """No conflicts because only one side changed"""
 
 
152
        m3 = Merge3(['aaa', 'bbb'],
 
 
153
                    ['aaa', '111', 'bbb'],
 
 
156
        self.assertEquals(m3.find_unconflicted(),
 
 
159
        self.assertEquals(list(m3.find_sync_regions()),
 
 
164
        self.assertEquals(list(m3.merge_regions()),
 
 
165
                          [('unchanged', 0, 1),
 
 
167
                           ('unchanged', 1, 2),])
 
 
169
    def test_append_a(self):
 
 
170
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
171
                    ['aaa\n', 'bbb\n', '222\n'],
 
 
174
        self.assertEquals(''.join(m3.merge_lines()),
 
 
177
    def test_append_b(self):
 
 
178
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
180
                    ['aaa\n', 'bbb\n', '222\n'])
 
 
182
        self.assertEquals(''.join(m3.merge_lines()),
 
 
185
    def test_append_agreement(self):
 
 
186
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
187
                    ['aaa\n', 'bbb\n', '222\n'],
 
 
188
                    ['aaa\n', 'bbb\n', '222\n'])
 
 
190
        self.assertEquals(''.join(m3.merge_lines()),
 
 
193
    def test_append_clash(self):
 
 
194
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
195
                    ['aaa\n', 'bbb\n', '222\n'],
 
 
196
                    ['aaa\n', 'bbb\n', '333\n'])
 
 
198
        ml = m3.merge_lines(name_a='a',
 
 
203
        self.assertEquals(''.join(ml),
 
 
214
    def test_insert_agreement(self):
 
 
215
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
216
                    ['aaa\n', '222\n', 'bbb\n'],
 
 
217
                    ['aaa\n', '222\n', 'bbb\n'])
 
 
219
        ml = m3.merge_lines(name_a='a',
 
 
224
        self.assertEquals(''.join(m3.merge_lines()),
 
 
227
    def test_insert_clash(self):
 
 
228
        """Both try to insert lines in the same place."""
 
 
229
        m3 = Merge3(['aaa\n', 'bbb\n'],
 
 
230
                    ['aaa\n', '111\n', 'bbb\n'],
 
 
231
                    ['aaa\n', '222\n', 'bbb\n'])
 
 
233
        self.assertEquals(m3.find_unconflicted(),
 
 
236
        self.assertEquals(list(m3.find_sync_regions()),
 
 
241
        self.assertEquals(list(m3.merge_regions()),
 
 
243
                           ('conflict', 1,1, 1,2, 1,2),
 
 
246
        self.assertEquals(list(m3.merge_groups()),
 
 
247
                          [('unchanged', ['aaa\n']),
 
 
248
                           ('conflict', [], ['111\n'], ['222\n']),
 
 
249
                           ('unchanged', ['bbb\n']),
 
 
252
        ml = m3.merge_lines(name_a='a',
 
 
257
        self.assertEquals(''.join(ml),
 
 
267
    def test_replace_clash(self):
 
 
268
        """Both try to insert lines in the same place."""
 
 
269
        m3 = Merge3(['aaa', '000', 'bbb'],
 
 
270
                    ['aaa', '111', 'bbb'],
 
 
271
                    ['aaa', '222', 'bbb'])
 
 
273
        self.assertEquals(m3.find_unconflicted(),
 
 
276
        self.assertEquals(list(m3.find_sync_regions()),
 
 
281
    def test_replace_multi(self):
 
 
282
        """Replacement with regions of different size."""
 
 
283
        m3 = Merge3(['aaa', '000', '000', 'bbb'],
 
 
284
                    ['aaa', '111', '111', '111', 'bbb'],
 
 
285
                    ['aaa', '222', '222', '222', '222', 'bbb'])
 
 
287
        self.assertEquals(m3.find_unconflicted(),
 
 
291
        self.assertEquals(list(m3.find_sync_regions()),
 
 
296
    def test_merge_poem(self):
 
 
297
        """Test case from diff3 manual"""
 
 
298
        m3 = Merge3(TZU, LAO, TAO)
 
 
299
        ml = list(m3.merge_lines('LAO', 'TAO'))
 
 
300
        self.log('merge result:')
 
 
301
        self.log(''.join(ml))
 
 
302
        self.assertEquals(ml, MERGED_RESULT)