/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge3.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011, 2016 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
from .. import (
 
18
from bzrlib import (
 
19
    errors,
19
20
    merge3,
20
21
    tests,
21
22
    )
22
 
from ..errors import BinaryFile
23
 
from ..sixish import (
24
 
    BytesIO,
25
 
    int2byte,
26
 
    )
27
 
 
 
23
from bzrlib.merge3 import Merge3
 
24
from bzrlib.errors import CantReprocessAndShowBase, BinaryFile
28
25
 
29
26
def split_lines(t):
30
 
    return BytesIO(t).readlines()
31
 
 
 
27
    from cStringIO import StringIO
 
28
    return StringIO(t).readlines()
32
29
 
33
30
############################################################
34
31
# test case data from the gnu diffutils manual
35
32
# common base
36
 
TZU = split_lines(b"""     The Nameless is the origin of Heaven and Earth;
 
33
TZU = split_lines("""     The Nameless is the origin of Heaven and Earth;
37
34
     The named is the mother of all things.
38
35
 
39
36
     Therefore let there always be non-being,
48
45
     The door of all subtleties!
49
46
""")
50
47
 
51
 
LAO = split_lines(b"""     The Way that can be told of is not the eternal Way;
 
48
LAO = split_lines("""     The Way that can be told of is not the eternal Way;
52
49
     The name that can be named is not the eternal name.
53
50
     The Nameless is the origin of Heaven and Earth;
54
51
     The Named is the mother of all things.
62
59
""")
63
60
 
64
61
 
65
 
TAO = split_lines(b"""     The Way that can be told of is not the eternal Way;
 
62
TAO = split_lines("""     The Way that can be told of is not the eternal Way;
66
63
     The name that can be named is not the eternal name.
67
64
     The Nameless is the origin of Heaven and Earth;
68
65
     The named is the mother of all things.
79
76
 
80
77
""")
81
78
 
82
 
MERGED_RESULT = split_lines(b"""     The Way that can be told of is not the eternal Way;
 
79
MERGED_RESULT = split_lines("""     The Way that can be told of is not the eternal Way;
83
80
     The name that can be named is not the eternal name.
84
81
     The Nameless is the origin of Heaven and Earth;
85
82
     The Named is the mother of all things.
98
95
>>>>>>> TAO
99
96
""")
100
97
 
101
 
 
102
98
class TestMerge3(tests.TestCase):
103
99
 
104
100
    def test_no_changes(self):
105
101
        """No conflicts because nothing changed"""
106
 
        m3 = merge3.Merge3([b'aaa', b'bbb'],
107
 
                           [b'aaa', b'bbb'],
108
 
                           [b'aaa', b'bbb'])
109
 
 
110
 
        self.assertEqual(m3.find_unconflicted(),
111
 
                         [(0, 2)])
112
 
 
113
 
        self.assertEqual(list(m3.find_sync_regions()),
114
 
                         [(0, 2,
115
 
                           0, 2,
116
 
                           0, 2),
117
 
                          (2, 2, 2, 2, 2, 2)])
118
 
 
119
 
        self.assertEqual(list(m3.merge_regions()),
120
 
                         [('unchanged', 0, 2)])
121
 
 
122
 
        self.assertEqual(list(m3.merge_groups()),
123
 
                         [('unchanged', [b'aaa', b'bbb'])])
 
102
        m3 = merge3.Merge3(['aaa', 'bbb'],
 
103
                           ['aaa', 'bbb'],
 
104
                           ['aaa', 'bbb'])
 
105
 
 
106
        self.assertEquals(m3.find_unconflicted(),
 
107
                          [(0, 2)])
 
108
 
 
109
        self.assertEquals(list(m3.find_sync_regions()),
 
110
                          [(0, 2,
 
111
                            0, 2,
 
112
                            0, 2),
 
113
                           (2,2, 2,2, 2,2)])
 
114
 
 
115
        self.assertEquals(list(m3.merge_regions()),
 
116
                          [('unchanged', 0, 2)])
 
117
 
 
118
        self.assertEquals(list(m3.merge_groups()),
 
119
                          [('unchanged', ['aaa', 'bbb'])])
124
120
 
125
121
    def test_front_insert(self):
126
 
        m3 = merge3.Merge3([b'zz'],
127
 
                           [b'aaa', b'bbb', b'zz'],
128
 
                           [b'zz'])
 
122
        m3 = merge3.Merge3(['zz'],
 
123
                           ['aaa', 'bbb', 'zz'],
 
124
                           ['zz'])
129
125
 
130
126
        # todo: should use a sentinal at end as from get_matching_blocks
131
127
        # to match without zz
132
 
        self.assertEqual(list(m3.find_sync_regions()),
133
 
                         [(0, 1, 2, 3, 0, 1),
134
 
                          (1, 1, 3, 3, 1, 1), ])
135
 
 
136
 
        self.assertEqual(list(m3.merge_regions()),
137
 
                         [('a', 0, 2),
138
 
                          ('unchanged', 0, 1)])
139
 
 
140
 
        self.assertEqual(list(m3.merge_groups()),
141
 
                         [('a', [b'aaa', b'bbb']),
142
 
                          ('unchanged', [b'zz'])])
 
128
        self.assertEquals(list(m3.find_sync_regions()),
 
129
                          [(0,1, 2,3, 0,1),
 
130
                           (1,1, 3,3, 1,1),])
 
131
 
 
132
        self.assertEquals(list(m3.merge_regions()),
 
133
                          [('a', 0, 2),
 
134
                           ('unchanged', 0, 1)])
 
135
 
 
136
        self.assertEquals(list(m3.merge_groups()),
 
137
                          [('a', ['aaa', 'bbb']),
 
138
                           ('unchanged', ['zz'])])
143
139
 
144
140
    def test_null_insert(self):
145
141
        m3 = merge3.Merge3([],
146
 
                           [b'aaa', b'bbb'],
 
142
                           ['aaa', 'bbb'],
147
143
                           [])
148
144
        # todo: should use a sentinal at end as from get_matching_blocks
149
145
        # to match without zz
150
 
        self.assertEqual(list(m3.find_sync_regions()),
151
 
                         [(0, 0, 2, 2, 0, 0)])
152
 
 
153
 
        self.assertEqual(list(m3.merge_regions()),
154
 
                         [('a', 0, 2)])
155
 
 
156
 
        self.assertEqual(list(m3.merge_lines()),
157
 
                         [b'aaa', b'bbb'])
 
146
        self.assertEquals(list(m3.find_sync_regions()),
 
147
                          [(0,0, 2,2, 0,0)])
 
148
 
 
149
        self.assertEquals(list(m3.merge_regions()),
 
150
                          [('a', 0, 2)])
 
151
 
 
152
        self.assertEquals(list(m3.merge_lines()),
 
153
                          ['aaa', 'bbb'])
158
154
 
159
155
    def test_no_conflicts(self):
160
156
        """No conflicts because only one side changed"""
161
 
        m3 = merge3.Merge3([b'aaa', b'bbb'],
162
 
                           [b'aaa', b'111', b'bbb'],
163
 
                           [b'aaa', b'bbb'])
164
 
 
165
 
        self.assertEqual(m3.find_unconflicted(),
166
 
                         [(0, 1), (1, 2)])
167
 
 
168
 
        self.assertEqual(list(m3.find_sync_regions()),
169
 
                         [(0, 1, 0, 1, 0, 1),
170
 
                          (1, 2, 2, 3, 1, 2),
171
 
                          (2, 2, 3, 3, 2, 2), ])
172
 
 
173
 
        self.assertEqual(list(m3.merge_regions()),
174
 
                         [('unchanged', 0, 1),
175
 
                          ('a', 1, 2),
176
 
                          ('unchanged', 1, 2), ])
 
157
        m3 = merge3.Merge3(['aaa', 'bbb'],
 
158
                           ['aaa', '111', 'bbb'],
 
159
                           ['aaa', 'bbb'])
 
160
 
 
161
        self.assertEquals(m3.find_unconflicted(),
 
162
                          [(0, 1), (1, 2)])
 
163
 
 
164
        self.assertEquals(list(m3.find_sync_regions()),
 
165
                          [(0,1, 0,1, 0,1),
 
166
                           (1,2, 2,3, 1,2),
 
167
                           (2,2, 3,3, 2,2),])
 
168
 
 
169
        self.assertEquals(list(m3.merge_regions()),
 
170
                          [('unchanged', 0, 1),
 
171
                           ('a', 1, 2),
 
172
                           ('unchanged', 1, 2),])
177
173
 
178
174
    def test_append_a(self):
179
 
        m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
180
 
                           [b'aaa\n', b'bbb\n', b'222\n'],
181
 
                           [b'aaa\n', b'bbb\n'])
 
175
        m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
 
176
                           ['aaa\n', 'bbb\n', '222\n'],
 
177
                           ['aaa\n', 'bbb\n'])
182
178
 
183
 
        self.assertEqual(b''.join(m3.merge_lines()),
184
 
                         b'aaa\nbbb\n222\n')
 
179
        self.assertEquals(''.join(m3.merge_lines()),
 
180
                          'aaa\nbbb\n222\n')
185
181
 
186
182
    def test_append_b(self):
187
 
        m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
188
 
                           [b'aaa\n', b'bbb\n'],
189
 
                           [b'aaa\n', b'bbb\n', b'222\n'])
 
183
        m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
 
184
                           ['aaa\n', 'bbb\n'],
 
185
                           ['aaa\n', 'bbb\n', '222\n'])
190
186
 
191
 
        self.assertEqual(b''.join(m3.merge_lines()),
192
 
                         b'aaa\nbbb\n222\n')
 
187
        self.assertEquals(''.join(m3.merge_lines()),
 
188
                          'aaa\nbbb\n222\n')
193
189
 
194
190
    def test_append_agreement(self):
195
 
        m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
196
 
                           [b'aaa\n', b'bbb\n', b'222\n'],
197
 
                           [b'aaa\n', b'bbb\n', b'222\n'])
 
191
        m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
 
192
                           ['aaa\n', 'bbb\n', '222\n'],
 
193
                           ['aaa\n', 'bbb\n', '222\n'])
198
194
 
199
 
        self.assertEqual(b''.join(m3.merge_lines()),
200
 
                         b'aaa\nbbb\n222\n')
 
195
        self.assertEquals(''.join(m3.merge_lines()),
 
196
                          'aaa\nbbb\n222\n')
201
197
 
202
198
    def test_append_clash(self):
203
 
        m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
204
 
                           [b'aaa\n', b'bbb\n', b'222\n'],
205
 
                           [b'aaa\n', b'bbb\n', b'333\n'])
 
199
        m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
 
200
                           ['aaa\n', 'bbb\n', '222\n'],
 
201
                           ['aaa\n', 'bbb\n', '333\n'])
206
202
 
207
 
        ml = m3.merge_lines(name_a=b'a',
208
 
                            name_b=b'b',
209
 
                            start_marker=b'<<',
210
 
                            mid_marker=b'--',
211
 
                            end_marker=b'>>')
212
 
        self.assertEqual(b''.join(ml),
213
 
                         b'''\
 
203
        ml = m3.merge_lines(name_a='a',
 
204
                            name_b='b',
 
205
                            start_marker='<<',
 
206
                            mid_marker='--',
 
207
                            end_marker='>>')
 
208
        self.assertEquals(''.join(ml),
 
209
'''\
214
210
aaa
215
211
bbb
216
212
<< a
221
217
''')
222
218
 
223
219
    def test_insert_agreement(self):
224
 
        m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
225
 
                           [b'aaa\n', b'222\n', b'bbb\n'],
226
 
                           [b'aaa\n', b'222\n', b'bbb\n'])
227
 
 
228
 
        ml = m3.merge_lines(name_a=b'a',
229
 
                            name_b=b'b',
230
 
                            start_marker=b'<<',
231
 
                            mid_marker=b'--',
232
 
                            end_marker=b'>>')
233
 
        self.assertEqual(b''.join(ml), b'aaa\n222\nbbb\n')
 
220
        m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
 
221
                           ['aaa\n', '222\n', 'bbb\n'],
 
222
                           ['aaa\n', '222\n', 'bbb\n'])
 
223
 
 
224
        ml = m3.merge_lines(name_a='a',
 
225
                            name_b='b',
 
226
                            start_marker='<<',
 
227
                            mid_marker='--',
 
228
                            end_marker='>>')
 
229
        self.assertEquals(''.join(ml), 'aaa\n222\nbbb\n')
 
230
 
234
231
 
235
232
    def test_insert_clash(self):
236
233
        """Both try to insert lines in the same place."""
237
 
        m3 = merge3.Merge3([b'aaa\n', b'bbb\n'],
238
 
                           [b'aaa\n', b'111\n', b'bbb\n'],
239
 
                           [b'aaa\n', b'222\n', b'bbb\n'])
240
 
 
241
 
        self.assertEqual(m3.find_unconflicted(),
242
 
                         [(0, 1), (1, 2)])
243
 
 
244
 
        self.assertEqual(list(m3.find_sync_regions()),
245
 
                         [(0, 1, 0, 1, 0, 1),
246
 
                          (1, 2, 2, 3, 2, 3),
247
 
                          (2, 2, 3, 3, 3, 3), ])
248
 
 
249
 
        self.assertEqual(list(m3.merge_regions()),
250
 
                         [('unchanged', 0, 1),
251
 
                          ('conflict', 1, 1, 1, 2, 1, 2),
252
 
                          ('unchanged', 1, 2)])
253
 
 
254
 
        self.assertEqual(list(m3.merge_groups()),
255
 
                         [('unchanged', [b'aaa\n']),
256
 
                          ('conflict', [], [b'111\n'], [b'222\n']),
257
 
                          ('unchanged', [b'bbb\n']),
258
 
                          ])
259
 
 
260
 
        ml = m3.merge_lines(name_a=b'a',
261
 
                            name_b=b'b',
262
 
                            start_marker=b'<<',
263
 
                            mid_marker=b'--',
264
 
                            end_marker=b'>>')
265
 
        self.assertEqual(b''.join(ml),
266
 
                         b'''aaa
 
234
        m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
 
235
                           ['aaa\n', '111\n', 'bbb\n'],
 
236
                           ['aaa\n', '222\n', 'bbb\n'])
 
237
 
 
238
        self.assertEquals(m3.find_unconflicted(),
 
239
                          [(0, 1), (1, 2)])
 
240
 
 
241
        self.assertEquals(list(m3.find_sync_regions()),
 
242
                          [(0,1, 0,1, 0,1),
 
243
                           (1,2, 2,3, 2,3),
 
244
                           (2,2, 3,3, 3,3),])
 
245
 
 
246
        self.assertEquals(list(m3.merge_regions()),
 
247
                          [('unchanged', 0,1),
 
248
                           ('conflict', 1,1, 1,2, 1,2),
 
249
                           ('unchanged', 1,2)])
 
250
 
 
251
        self.assertEquals(list(m3.merge_groups()),
 
252
                          [('unchanged', ['aaa\n']),
 
253
                           ('conflict', [], ['111\n'], ['222\n']),
 
254
                           ('unchanged', ['bbb\n']),
 
255
                           ])
 
256
 
 
257
        ml = m3.merge_lines(name_a='a',
 
258
                            name_b='b',
 
259
                            start_marker='<<',
 
260
                            mid_marker='--',
 
261
                            end_marker='>>')
 
262
        self.assertEquals(''.join(ml),
 
263
'''aaa
267
264
<< a
268
265
111
269
266
--
274
271
 
275
272
    def test_replace_clash(self):
276
273
        """Both try to insert lines in the same place."""
277
 
        m3 = merge3.Merge3([b'aaa', b'000', b'bbb'],
278
 
                           [b'aaa', b'111', b'bbb'],
279
 
                           [b'aaa', b'222', b'bbb'])
280
 
 
281
 
        self.assertEqual(m3.find_unconflicted(),
282
 
                         [(0, 1), (2, 3)])
283
 
 
284
 
        self.assertEqual(list(m3.find_sync_regions()),
285
 
                         [(0, 1, 0, 1, 0, 1),
286
 
                          (2, 3, 2, 3, 2, 3),
287
 
                          (3, 3, 3, 3, 3, 3), ])
 
274
        m3 = merge3.Merge3(['aaa', '000', 'bbb'],
 
275
                           ['aaa', '111', 'bbb'],
 
276
                           ['aaa', '222', 'bbb'])
 
277
 
 
278
        self.assertEquals(m3.find_unconflicted(),
 
279
                          [(0, 1), (2, 3)])
 
280
 
 
281
        self.assertEquals(list(m3.find_sync_regions()),
 
282
                          [(0,1, 0,1, 0,1),
 
283
                           (2,3, 2,3, 2,3),
 
284
                           (3,3, 3,3, 3,3),])
288
285
 
289
286
    def test_replace_multi(self):
290
287
        """Replacement with regions of different size."""
291
 
        m3 = merge3.Merge3([b'aaa', b'000', b'000', b'bbb'],
292
 
                           [b'aaa', b'111', b'111', b'111', b'bbb'],
293
 
                           [b'aaa', b'222', b'222', b'222', b'222', b'bbb'])
294
 
 
295
 
        self.assertEqual(m3.find_unconflicted(),
296
 
                         [(0, 1), (3, 4)])
297
 
 
298
 
        self.assertEqual(list(m3.find_sync_regions()),
299
 
                         [(0, 1, 0, 1, 0, 1),
300
 
                          (3, 4, 4, 5, 5, 6),
301
 
                          (4, 4, 5, 5, 6, 6), ])
 
288
        m3 = merge3.Merge3(['aaa', '000', '000', 'bbb'],
 
289
                           ['aaa', '111', '111', '111', 'bbb'],
 
290
                           ['aaa', '222', '222', '222', '222', 'bbb'])
 
291
 
 
292
        self.assertEquals(m3.find_unconflicted(),
 
293
                          [(0, 1), (3, 4)])
 
294
 
 
295
 
 
296
        self.assertEquals(list(m3.find_sync_regions()),
 
297
                          [(0,1, 0,1, 0,1),
 
298
                           (3,4, 4,5, 5,6),
 
299
                           (4,4, 5,5, 6,6),])
302
300
 
303
301
    def test_merge_poem(self):
304
302
        """Test case from diff3 manual"""
305
303
        m3 = merge3.Merge3(TZU, LAO, TAO)
306
 
        ml = list(m3.merge_lines(b'LAO', b'TAO'))
 
304
        ml = list(m3.merge_lines('LAO', 'TAO'))
307
305
        self.log('merge result:')
308
 
        self.log(b''.join(ml))
309
 
        self.assertEqual(ml, MERGED_RESULT)
 
306
        self.log(''.join(ml))
 
307
        self.assertEquals(ml, MERGED_RESULT)
310
308
 
311
309
    def test_minimal_conflicts_common(self):
312
310
        """Reprocessing"""
313
 
        base_text = (b"a\n" * 20).splitlines(True)
314
 
        this_text = (b"a\n" * 10 + b"b\n" * 10).splitlines(True)
315
 
        other_text = (b"a\n" * 10 + b"c\n" + b"b\n" *
316
 
                      8 + b"c\n").splitlines(True)
 
311
        base_text = ("a\n" * 20).splitlines(True)
 
312
        this_text = ("a\n"*10+"b\n" * 10).splitlines(True)
 
313
        other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True)
317
314
        m3 = merge3.Merge3(base_text, other_text, this_text)
318
 
        m_lines = m3.merge_lines(b'OTHER', b'THIS', reprocess=True)
319
 
        merged_text = b"".join(list(m_lines))
320
 
        optimal_text = (b"a\n" * 10 + b"<<<<<<< OTHER\nc\n"
321
 
                        + 8 * b"b\n" + b"c\n=======\n"
322
 
                        + 10 * b"b\n" + b">>>>>>> THIS\n")
 
315
        m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
 
316
        merged_text = "".join(list(m_lines))
 
317
        optimal_text = ("a\n" * 10 + "<<<<<<< OTHER\nc\n"
 
318
            + 8* "b\n" + "c\n=======\n"
 
319
            + 10*"b\n" + ">>>>>>> THIS\n")
323
320
        self.assertEqualDiff(optimal_text, merged_text)
324
321
 
325
322
    def test_minimal_conflicts_unique(self):
326
323
        def add_newline(s):
327
324
            """Add a newline to each entry in the string"""
328
 
            return [(int2byte(x) + b'\n') for x in bytearray(s)]
 
325
            return [(x+'\n') for x in s]
329
326
 
330
 
        base_text = add_newline(b"abcdefghijklm")
331
 
        this_text = add_newline(b"abcdefghijklmNOPQRSTUVWXYZ")
332
 
        other_text = add_newline(b"abcdefghijklm1OPQRSTUVWXY2")
 
327
        base_text = add_newline("abcdefghijklm")
 
328
        this_text = add_newline("abcdefghijklmNOPQRSTUVWXYZ")
 
329
        other_text = add_newline("abcdefghijklm1OPQRSTUVWXY2")
333
330
        m3 = merge3.Merge3(base_text, other_text, this_text)
334
 
        m_lines = m3.merge_lines(b'OTHER', b'THIS', reprocess=True)
335
 
        merged_text = b"".join(list(m_lines))
336
 
        optimal_text = b''.join(add_newline(b"abcdefghijklm")
337
 
                                + [b"<<<<<<< OTHER\n1\n=======\nN\n>>>>>>> THIS\n"]
338
 
                                + add_newline(b'OPQRSTUVWXY')
339
 
                                + [b"<<<<<<< OTHER\n2\n=======\nZ\n>>>>>>> THIS\n"]
340
 
                                )
 
331
        m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
 
332
        merged_text = "".join(list(m_lines))
 
333
        optimal_text = ''.join(add_newline("abcdefghijklm")
 
334
            + ["<<<<<<< OTHER\n1\n=======\nN\n>>>>>>> THIS\n"]
 
335
            + add_newline('OPQRSTUVWXY')
 
336
            + ["<<<<<<< OTHER\n2\n=======\nZ\n>>>>>>> THIS\n"]
 
337
            )
341
338
        self.assertEqualDiff(optimal_text, merged_text)
342
339
 
343
340
    def test_minimal_conflicts_nonunique(self):
344
341
        def add_newline(s):
345
342
            """Add a newline to each entry in the string"""
346
 
            return [(int2byte(x) + b'\n') for x in bytearray(s)]
 
343
            return [(x+'\n') for x in s]
347
344
 
348
 
        base_text = add_newline(b"abacddefgghij")
349
 
        this_text = add_newline(b"abacddefgghijkalmontfprz")
350
 
        other_text = add_newline(b"abacddefgghijknlmontfprd")
 
345
        base_text = add_newline("abacddefgghij")
 
346
        this_text = add_newline("abacddefgghijkalmontfprz")
 
347
        other_text = add_newline("abacddefgghijknlmontfprd")
351
348
        m3 = merge3.Merge3(base_text, other_text, this_text)
352
 
        m_lines = m3.merge_lines(b'OTHER', b'THIS', reprocess=True)
353
 
        merged_text = b"".join(list(m_lines))
354
 
        optimal_text = b''.join(add_newline(b"abacddefgghijk")
355
 
                                + [b"<<<<<<< OTHER\nn\n=======\na\n>>>>>>> THIS\n"]
356
 
                                + add_newline(b'lmontfpr')
357
 
                                + [b"<<<<<<< OTHER\nd\n=======\nz\n>>>>>>> THIS\n"]
358
 
                                )
 
349
        m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
 
350
        merged_text = "".join(list(m_lines))
 
351
        optimal_text = ''.join(add_newline("abacddefgghijk")
 
352
            + ["<<<<<<< OTHER\nn\n=======\na\n>>>>>>> THIS\n"]
 
353
            + add_newline('lmontfpr')
 
354
            + ["<<<<<<< OTHER\nd\n=======\nz\n>>>>>>> THIS\n"]
 
355
            )
359
356
        self.assertEqualDiff(optimal_text, merged_text)
360
357
 
361
358
    def test_reprocess_and_base(self):
362
359
        """Reprocessing and showing base breaks correctly"""
363
 
        base_text = (b"a\n" * 20).splitlines(True)
364
 
        this_text = (b"a\n" * 10 + b"b\n" * 10).splitlines(True)
365
 
        other_text = (b"a\n" * 10 + b"c\n" + b"b\n" *
366
 
                      8 + b"c\n").splitlines(True)
 
360
        base_text = ("a\n" * 20).splitlines(True)
 
361
        this_text = ("a\n"*10+"b\n" * 10).splitlines(True)
 
362
        other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True)
367
363
        m3 = merge3.Merge3(base_text, other_text, this_text)
368
 
        m_lines = m3.merge_lines(b'OTHER', b'THIS', reprocess=True,
369
 
                                 base_marker=b'|||||||')
370
 
        self.assertRaises(merge3.CantReprocessAndShowBase, list, m_lines)
 
364
        m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True,
 
365
                                 base_marker='|||||||')
 
366
        self.assertRaises(CantReprocessAndShowBase, list, m_lines)
371
367
 
372
368
    def test_binary(self):
373
 
        self.assertRaises(BinaryFile, merge3.Merge3, [b'\x00'], [b'a'], [b'b'])
 
369
        self.assertRaises(BinaryFile, merge3.Merge3, ['\x00'], ['a'], ['b'])
374
370
 
375
371
    def test_dos_text(self):
376
 
        base_text = b'a\r\n'
377
 
        this_text = b'b\r\n'
378
 
        other_text = b'c\r\n'
 
372
        base_text = 'a\r\n'
 
373
        this_text = 'b\r\n'
 
374
        other_text = 'c\r\n'
379
375
        m3 = merge3.Merge3(base_text.splitlines(True),
380
376
                           other_text.splitlines(True),
381
377
                           this_text.splitlines(True))
382
 
        m_lines = m3.merge_lines(b'OTHER', b'THIS')
383
 
        self.assertEqual(b'<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n'
384
 
                         b'>>>>>>> THIS\r\n'.splitlines(True), list(m_lines))
 
378
        m_lines = m3.merge_lines('OTHER', 'THIS')
 
379
        self.assertEqual('<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n'
 
380
            '>>>>>>> THIS\r\n'.splitlines(True), list(m_lines))
385
381
 
386
382
    def test_mac_text(self):
387
 
        base_text = b'a\r'
388
 
        this_text = b'b\r'
389
 
        other_text = b'c\r'
 
383
        base_text = 'a\r'
 
384
        this_text = 'b\r'
 
385
        other_text = 'c\r'
390
386
        m3 = merge3.Merge3(base_text.splitlines(True),
391
387
                           other_text.splitlines(True),
392
388
                           this_text.splitlines(True))
393
 
        m_lines = m3.merge_lines(b'OTHER', b'THIS')
394
 
        self.assertEqual(b'<<<<<<< OTHER\rc\r=======\rb\r'
395
 
                         b'>>>>>>> THIS\r'.splitlines(True), list(m_lines))
 
389
        m_lines = m3.merge_lines('OTHER', 'THIS')
 
390
        self.assertEqual('<<<<<<< OTHER\rc\r=======\rb\r'
 
391
            '>>>>>>> THIS\r'.splitlines(True), list(m_lines))
396
392
 
397
393
    def test_merge3_cherrypick(self):
398
 
        base_text = b"a\nb\n"
399
 
        this_text = b"a\n"
400
 
        other_text = b"a\nb\nc\n"
 
394
        base_text = "a\nb\n"
 
395
        this_text = "a\n"
 
396
        other_text = "a\nb\nc\n"
401
397
        # When cherrypicking, lines in base are not part of the conflict
402
398
        m3 = merge3.Merge3(base_text.splitlines(True),
403
399
                           this_text.splitlines(True),
404
400
                           other_text.splitlines(True), is_cherrypick=True)
405
401
        m_lines = m3.merge_lines()
406
 
        self.assertEqualDiff(b'a\n<<<<<<<\n=======\nc\n>>>>>>>\n',
407
 
                             b''.join(m_lines))
 
402
        self.assertEqualDiff('a\n<<<<<<<\n=======\nc\n>>>>>>>\n',
 
403
                             ''.join(m_lines))
408
404
 
409
405
        # This is not symmetric
410
406
        m3 = merge3.Merge3(base_text.splitlines(True),
411
407
                           other_text.splitlines(True),
412
408
                           this_text.splitlines(True), is_cherrypick=True)
413
409
        m_lines = m3.merge_lines()
414
 
        self.assertEqualDiff(b'a\n<<<<<<<\nb\nc\n=======\n>>>>>>>\n',
415
 
                             b''.join(m_lines))
 
410
        self.assertEqualDiff('a\n<<<<<<<\nb\nc\n=======\n>>>>>>>\n',
 
411
                             ''.join(m_lines))
416
412
 
417
413
    def test_merge3_cherrypick_w_mixed(self):
418
 
        base_text = b'a\nb\nc\nd\ne\n'
419
 
        this_text = b'a\nb\nq\n'
420
 
        other_text = b'a\nb\nc\nd\nf\ne\ng\n'
 
414
        base_text = 'a\nb\nc\nd\ne\n'
 
415
        this_text = 'a\nb\nq\n'
 
416
        other_text = 'a\nb\nc\nd\nf\ne\ng\n'
421
417
        # When cherrypicking, lines in base are not part of the conflict
422
418
        m3 = merge3.Merge3(base_text.splitlines(True),
423
419
                           this_text.splitlines(True),
424
420
                           other_text.splitlines(True), is_cherrypick=True)
425
421
        m_lines = m3.merge_lines()
426
 
        self.assertEqualDiff(b'a\n'
427
 
                             b'b\n'
428
 
                             b'<<<<<<<\n'
429
 
                             b'q\n'
430
 
                             b'=======\n'
431
 
                             b'f\n'
432
 
                             b'>>>>>>>\n'
433
 
                             b'<<<<<<<\n'
434
 
                             b'=======\n'
435
 
                             b'g\n'
436
 
                             b'>>>>>>>\n',
437
 
                             b''.join(m_lines))
 
422
        self.assertEqualDiff('a\n'
 
423
                             'b\n'
 
424
                             '<<<<<<<\n'
 
425
                             'q\n'
 
426
                             '=======\n'
 
427
                             'f\n'
 
428
                             '>>>>>>>\n'
 
429
                             '<<<<<<<\n'
 
430
                             '=======\n'
 
431
                             'g\n'
 
432
                             '>>>>>>>\n',
 
433
                             ''.join(m_lines))
438
434
 
439
435
    def test_allow_objects(self):
440
436
        """Objects other than strs may be used with Merge3 when
441
437
        allow_objects=True.
442
 
 
 
438
        
443
439
        merge_groups and merge_regions work with non-str input.  Methods that
444
440
        return lines like merge_lines fail.
445
441
        """
446
 
        base = [(x, x) for x in 'abcde']
447
 
        a = [(x, x) for x in 'abcdef']
448
 
        b = [(x, x) for x in 'Zabcde']
 
442
        base = [(x,x) for x in 'abcde']
 
443
        a = [(x,x) for x in 'abcdef']
 
444
        b = [(x,x) for x in 'Zabcde']
449
445
        m3 = merge3.Merge3(base, a, b, allow_objects=True)
450
446
        self.assertEqual(
451
447
            [('b', 0, 1),
454
450
            list(m3.merge_regions()))
455
451
        self.assertEqual(
456
452
            [('b', [('Z', 'Z')]),
457
 
             ('unchanged', [(x, x) for x in 'abcde']),
 
453
             ('unchanged', [(x,x) for x in 'abcde']),
458
454
             ('a', [('f', 'f')])],
459
455
            list(m3.merge_groups()))
 
456