/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/selftest/test_weave.py

[merge] robertc's integration, updated tests to check for retcode=3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011, 2016 Canonical Ltd
2
 
#
 
1
#! /usr/bin/python2.4
 
2
 
 
3
# Copyright (C) 2005 by Canonical Ltd
 
4
 
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
5
7
# the Free Software Foundation; either version 2 of the License, or
6
8
# (at your option) any later version.
7
 
#
 
9
 
8
10
# This program is distributed in the hope that it will be useful,
9
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
13
# GNU General Public License for more details.
12
 
#
 
14
 
13
15
# You should have received a copy of the GNU General Public License
14
16
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
18
 
17
19
 
18
20
# TODO: tests regarding version names
19
 
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
 
21
# TODO: rbc 20050108 test that join does not leave an inconsistent weave 
20
22
#       if it fails.
21
23
 
22
24
"""test suite for weave algorithm"""
23
25
 
24
26
from pprint import pformat
25
27
 
26
 
from .. import (
27
 
    errors,
28
 
    )
29
 
from ..osutils import sha_string
30
 
from ..sixish import (
31
 
    BytesIO,
32
 
    )
33
 
from . import TestCase, TestCaseInTempDir
34
 
from ..bzr.weave import Weave, WeaveFormatError, WeaveInvalidChecksum
35
 
from ..bzr.weavefile import write_weave, read_weave
 
28
import bzrlib.errors as errors
 
29
from bzrlib.weave import Weave, WeaveFormatError, WeaveError, reweave
 
30
from bzrlib.weavefile import write_weave, read_weave
 
31
from bzrlib.selftest import TestCase
 
32
from bzrlib.osutils import sha_string
36
33
 
37
34
 
38
35
# texts for use in testing
39
 
TEXT_0 = [b"Hello world"]
40
 
TEXT_1 = [b"Hello world",
41
 
          b"A second line"]
 
36
TEXT_0 = ["Hello world"]
 
37
TEXT_1 = ["Hello world",
 
38
          "A second line"]
 
39
 
42
40
 
43
41
 
44
42
class TestBase(TestCase):
45
 
 
46
43
    def check_read_write(self, k):
47
44
        """Check the weave k can be written & re-read."""
48
45
        from tempfile import TemporaryFile
67
64
 
68
65
class WeaveContains(TestBase):
69
66
    """Weave __contains__ operator"""
70
 
 
71
67
    def runTest(self):
72
 
        k = Weave(get_scope=lambda: None)
73
 
        self.assertFalse(b'foo' in k)
74
 
        k.add_lines(b'foo', [], TEXT_1)
75
 
        self.assertTrue(b'foo' in k)
 
68
        k = Weave()
 
69
        self.assertFalse('foo' in k)
 
70
        k.add('foo', [], TEXT_1)
 
71
        self.assertTrue('foo' in k)
76
72
 
77
73
 
78
74
class Easy(TestBase):
79
 
 
80
 
    def runTest(self):
81
 
        Weave()
 
75
    def runTest(self):
 
76
        k = Weave()
 
77
 
 
78
 
 
79
class StoreText(TestBase):
 
80
    """Store and retrieve a simple text."""
 
81
    def runTest(self):
 
82
        k = Weave()
 
83
        idx = k.add('text0', [], TEXT_0)
 
84
        self.assertEqual(k.get(idx), TEXT_0)
 
85
        self.assertEqual(idx, 0)
 
86
 
82
87
 
83
88
 
84
89
class AnnotateOne(TestBase):
85
 
 
86
 
    def runTest(self):
87
 
        k = Weave()
88
 
        k.add_lines(b'text0', [], TEXT_0)
89
 
        self.assertEqual(k.annotate(b'text0'),
90
 
                         [(b'text0', TEXT_0[0])])
 
90
    def runTest(self):
 
91
        k = Weave()
 
92
        k.add('text0', [], TEXT_0)
 
93
        self.assertEqual(k.annotate(0),
 
94
                         [(0, TEXT_0[0])])
 
95
 
 
96
 
 
97
class StoreTwo(TestBase):
 
98
    def runTest(self):
 
99
        k = Weave()
 
100
 
 
101
        idx = k.add('text0', [], TEXT_0)
 
102
        self.assertEqual(idx, 0)
 
103
 
 
104
        idx = k.add('text1', [], TEXT_1)
 
105
        self.assertEqual(idx, 1)
 
106
 
 
107
        self.assertEqual(k.get(0), TEXT_0)
 
108
        self.assertEqual(k.get(1), TEXT_1)
 
109
 
 
110
 
 
111
 
 
112
class AddWithGivenSha(TestBase):
 
113
    def runTest(self):
 
114
        """Add with caller-supplied SHA-1"""
 
115
        k = Weave()
 
116
 
 
117
        t = 'text0'
 
118
        k.add('text0', [], [t], sha1=sha_string(t))
 
119
 
91
120
 
92
121
 
93
122
class InvalidAdd(TestBase):
94
123
    """Try to use invalid version number during add."""
95
 
 
96
124
    def runTest(self):
97
125
        k = Weave()
98
126
 
99
 
        self.assertRaises(errors.RevisionNotPresent,
100
 
                          k.add_lines,
101
 
                          b'text0',
102
 
                          [b'69'],
103
 
                          [b'new text!'])
 
127
        self.assertRaises(IndexError,
 
128
                          k.add,
 
129
                          'text0',
 
130
                          [69],
 
131
                          ['new text!'])
104
132
 
105
133
 
106
134
class RepeatedAdd(TestBase):
107
135
    """Add the same version twice; harmless."""
108
 
 
109
 
    def test_duplicate_add(self):
 
136
    def runTest(self):
110
137
        k = Weave()
111
 
        idx = k.add_lines(b'text0', [], TEXT_0)
112
 
        idx2 = k.add_lines(b'text0', [], TEXT_0)
 
138
        idx = k.add('text0', [], TEXT_0)
 
139
        idx2 = k.add('text0', [], TEXT_0)
113
140
        self.assertEqual(idx, idx2)
114
141
 
115
142
 
 
143
 
116
144
class InvalidRepeatedAdd(TestBase):
117
 
 
118
145
    def runTest(self):
119
146
        k = Weave()
120
 
        k.add_lines(b'basis', [], TEXT_0)
121
 
        k.add_lines(b'text0', [], TEXT_0)
122
 
        self.assertRaises(errors.RevisionAlreadyPresent,
123
 
                          k.add_lines,
124
 
                          b'text0',
 
147
        idx = k.add('text0', [], TEXT_0)
 
148
        self.assertRaises(WeaveError,
 
149
                          k.add,
 
150
                          'text0',
125
151
                          [],
126
 
                          [b'not the same text'])
127
 
        self.assertRaises(errors.RevisionAlreadyPresent,
128
 
                          k.add_lines,
129
 
                          b'text0',
130
 
                          [b'basis'],         # not the right parents
 
152
                          ['not the same text'])
 
153
        self.assertRaises(WeaveError,
 
154
                          k.add,
 
155
                          'text0',
 
156
                          [12],         # not the right parents
131
157
                          TEXT_0)
 
158
        
132
159
 
133
160
 
134
161
class InsertLines(TestBase):
136
163
 
137
164
    Look at the annotations to make sure that the first line is matched
138
165
    and not stored repeatedly."""
139
 
 
140
166
    def runTest(self):
141
167
        k = Weave()
142
168
 
143
 
        k.add_lines(b'text0', [], [b'line 1'])
144
 
        k.add_lines(b'text1', [b'text0'], [b'line 1', b'line 2'])
145
 
 
146
 
        self.assertEqual(k.annotate(b'text0'),
147
 
                         [(b'text0', b'line 1')])
148
 
 
149
 
        self.assertEqual(k.get_lines(1),
150
 
                         [b'line 1',
151
 
                          b'line 2'])
152
 
 
153
 
        self.assertEqual(k.annotate(b'text1'),
154
 
                         [(b'text0', b'line 1'),
155
 
                          (b'text1', b'line 2')])
156
 
 
157
 
        k.add_lines(b'text2', [b'text0'], [b'line 1', b'diverged line'])
158
 
 
159
 
        self.assertEqual(k.annotate(b'text2'),
160
 
                         [(b'text0', b'line 1'),
161
 
                          (b'text2', b'diverged line')])
162
 
 
163
 
        text3 = [b'line 1', b'middle line', b'line 2']
164
 
        k.add_lines(b'text3',
165
 
                    [b'text0', b'text1'],
166
 
                    text3)
167
 
 
168
 
        # self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]),
169
 
        # text3))))
 
169
        k.add('text0', [], ['line 1'])
 
170
        k.add('text1', [0], ['line 1', 'line 2'])
 
171
 
 
172
        self.assertEqual(k.annotate(0),
 
173
                         [(0, 'line 1')])
 
174
 
 
175
        self.assertEqual(k.get(1),
 
176
                         ['line 1',
 
177
                          'line 2'])
 
178
 
 
179
        self.assertEqual(k.annotate(1),
 
180
                         [(0, 'line 1'),
 
181
                          (1, 'line 2')])
 
182
 
 
183
        k.add('text2', [0], ['line 1', 'diverged line'])
 
184
 
 
185
        self.assertEqual(k.annotate(2),
 
186
                         [(0, 'line 1'),
 
187
                          (2, 'diverged line')])
 
188
 
 
189
        text3 = ['line 1', 'middle line', 'line 2']
 
190
        k.add('text3',
 
191
              [0, 1],
 
192
              text3)
 
193
 
 
194
        # self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
170
195
 
171
196
        self.log("k._weave=" + pformat(k._weave))
172
197
 
173
 
        self.assertEqual(k.annotate(b'text3'),
174
 
                         [(b'text0', b'line 1'),
175
 
                          (b'text3', b'middle line'),
176
 
                          (b'text1', b'line 2')])
 
198
        self.assertEqual(k.annotate(3),
 
199
                         [(0, 'line 1'),
 
200
                          (3, 'middle line'),
 
201
                          (1, 'line 2')])
177
202
 
178
203
        # now multiple insertions at different places
179
 
        k.add_lines(
180
 
            b'text4', [b'text0', b'text1', b'text3'],
181
 
            [b'line 1', b'aaa', b'middle line', b'bbb', b'line 2', b'ccc'])
182
 
 
183
 
        self.assertEqual(k.annotate(b'text4'),
184
 
                         [(b'text0', b'line 1'),
185
 
                          (b'text4', b'aaa'),
186
 
                          (b'text3', b'middle line'),
187
 
                          (b'text4', b'bbb'),
188
 
                          (b'text1', b'line 2'),
189
 
                          (b'text4', b'ccc')])
 
204
        k.add('text4',
 
205
              [0, 1, 3],
 
206
              ['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
 
207
 
 
208
        self.assertEqual(k.annotate(4), 
 
209
                         [(0, 'line 1'),
 
210
                          (4, 'aaa'),
 
211
                          (3, 'middle line'),
 
212
                          (4, 'bbb'),
 
213
                          (1, 'line 2'),
 
214
                          (4, 'ccc')])
 
215
 
190
216
 
191
217
 
192
218
class DeleteLines(TestBase):
193
219
    """Deletion of lines from existing text.
194
220
 
195
221
    Try various texts all based on a common ancestor."""
196
 
 
197
222
    def runTest(self):
198
223
        k = Weave()
199
224
 
200
 
        base_text = [b'one', b'two', b'three', b'four']
201
 
 
202
 
        k.add_lines(b'text0', [], base_text)
203
 
 
204
 
        texts = [[b'one', b'two', b'three'],
205
 
                 [b'two', b'three', b'four'],
206
 
                 [b'one', b'four'],
207
 
                 [b'one', b'two', b'three', b'four'],
 
225
        base_text = ['one', 'two', 'three', 'four']
 
226
 
 
227
        k.add('text0', [], base_text)
 
228
        
 
229
        texts = [['one', 'two', 'three'],
 
230
                 ['two', 'three', 'four'],
 
231
                 ['one', 'four'],
 
232
                 ['one', 'two', 'three', 'four'],
208
233
                 ]
209
234
 
210
235
        i = 1
211
236
        for t in texts:
212
 
            k.add_lines(b'text%d' % i, [b'text0'], t)
 
237
            ver = k.add('text%d' % i,
 
238
                        [0], t)
213
239
            i += 1
214
240
 
215
241
        self.log('final weave:')
216
242
        self.log('k._weave=' + pformat(k._weave))
217
243
 
218
244
        for i in range(len(texts)):
219
 
            self.assertEqual(k.get_lines(i + 1),
 
245
            self.assertEqual(k.get(i+1),
220
246
                             texts[i])
 
247
            
 
248
 
221
249
 
222
250
 
223
251
class SuicideDelete(TestBase):
224
252
    """Invalid weave which tries to add and delete simultaneously."""
225
 
 
226
253
    def runTest(self):
227
254
        k = Weave()
228
255
 
229
256
        k._parents = [(),
230
 
                      ]
231
 
        k._weave = [(b'{', 0),
232
 
                    b'first line',
233
 
                    (b'[', 0),
234
 
                    b'deleted in 0',
235
 
                    (b']', 0),
236
 
                    (b'}', 0),
237
 
                    ]
238
 
        # SKIPPED
 
257
                ]
 
258
        k._weave = [('{', 0),
 
259
                'first line',
 
260
                ('[', 0),
 
261
                'deleted in 0',
 
262
                (']', 0),
 
263
                ('}', 0),
 
264
                ]
 
265
        ################################### SKIPPED
239
266
        # Weave.get doesn't trap this anymore
240
 
        return
 
267
        return 
241
268
 
242
269
        self.assertRaises(WeaveFormatError,
243
 
                          k.get_lines,
244
 
                          0)
 
270
                          k.get,
 
271
                          0)        
 
272
 
245
273
 
246
274
 
247
275
class CannedDelete(TestBase):
248
276
    """Unpack canned weave with deleted lines."""
249
 
 
250
277
    def runTest(self):
251
278
        k = Weave()
252
279
 
253
280
        k._parents = [(),
254
 
                      frozenset([0]),
255
 
                      ]
256
 
        k._weave = [(b'{', 0),
257
 
                    b'first line',
258
 
                    (b'[', 1),
259
 
                    b'line to be deleted',
260
 
                    (b']', 1),
261
 
                    b'last line',
262
 
                    (b'}', 0),
263
 
                    ]
264
 
        k._sha1s = [
265
 
            sha_string(b'first lineline to be deletedlast line'),
266
 
            sha_string(b'first linelast line')]
267
 
 
268
 
        self.assertEqual(k.get_lines(0),
269
 
                         [b'first line',
270
 
                          b'line to be deleted',
271
 
                          b'last line',
272
 
                          ])
273
 
 
274
 
        self.assertEqual(k.get_lines(1),
275
 
                         [b'first line',
276
 
                          b'last line',
277
 
                          ])
 
281
                frozenset([0]),
 
282
                ]
 
283
        k._weave = [('{', 0),
 
284
                'first line',
 
285
                ('[', 1),
 
286
                'line to be deleted',
 
287
                (']', 1),
 
288
                'last line',
 
289
                ('}', 0),
 
290
                ]
 
291
 
 
292
        self.assertEqual(k.get(0),
 
293
                         ['first line',
 
294
                          'line to be deleted',
 
295
                          'last line',
 
296
                          ])
 
297
 
 
298
        self.assertEqual(k.get(1),
 
299
                         ['first line',
 
300
                          'last line',
 
301
                          ])
 
302
 
278
303
 
279
304
 
280
305
class CannedReplacement(TestBase):
281
306
    """Unpack canned weave with deleted lines."""
282
 
 
283
307
    def runTest(self):
284
308
        k = Weave()
285
309
 
286
310
        k._parents = [frozenset(),
287
 
                      frozenset([0]),
288
 
                      ]
289
 
        k._weave = [(b'{', 0),
290
 
                    b'first line',
291
 
                    (b'[', 1),
292
 
                    b'line to be deleted',
293
 
                    (b']', 1),
294
 
                    (b'{', 1),
295
 
                    b'replacement line',
296
 
                    (b'}', 1),
297
 
                    b'last line',
298
 
                    (b'}', 0),
299
 
                    ]
300
 
        k._sha1s = [
301
 
            sha_string(b'first lineline to be deletedlast line'),
302
 
            sha_string(b'first linereplacement linelast line')]
303
 
 
304
 
        self.assertEqual(k.get_lines(0),
305
 
                         [b'first line',
306
 
                          b'line to be deleted',
307
 
                          b'last line',
308
 
                          ])
309
 
 
310
 
        self.assertEqual(k.get_lines(1),
311
 
                         [b'first line',
312
 
                          b'replacement line',
313
 
                          b'last line',
314
 
                          ])
 
311
                frozenset([0]),
 
312
                ]
 
313
        k._weave = [('{', 0),
 
314
                'first line',
 
315
                ('[', 1),
 
316
                'line to be deleted',
 
317
                (']', 1),
 
318
                ('{', 1),
 
319
                'replacement line',                
 
320
                ('}', 1),
 
321
                'last line',
 
322
                ('}', 0),
 
323
                ]
 
324
 
 
325
        self.assertEqual(k.get(0),
 
326
                         ['first line',
 
327
                          'line to be deleted',
 
328
                          'last line',
 
329
                          ])
 
330
 
 
331
        self.assertEqual(k.get(1),
 
332
                         ['first line',
 
333
                          'replacement line',
 
334
                          'last line',
 
335
                          ])
 
336
 
315
337
 
316
338
 
317
339
class BadWeave(TestBase):
318
340
    """Test that we trap an insert which should not occur."""
319
 
 
320
341
    def runTest(self):
321
342
        k = Weave()
322
343
 
323
344
        k._parents = [frozenset(),
324
 
                      ]
325
 
        k._weave = [b'bad line',
326
 
                    (b'{', 0),
327
 
                    b'foo {',
328
 
                    (b'{', 1),
329
 
                    b'  added in version 1',
330
 
                    (b'{', 2),
331
 
                    b'  added in v2',
332
 
                    (b'}', 2),
333
 
                    b'  also from v1',
334
 
                    (b'}', 1),
335
 
                    b'}',
336
 
                    (b'}', 0)]
 
345
                ]
 
346
        k._weave = ['bad line',
 
347
                ('{', 0),
 
348
                'foo {',
 
349
                ('{', 1),
 
350
                '  added in version 1',
 
351
                ('{', 2),
 
352
                '  added in v2',
 
353
                ('}', 2),
 
354
                '  also from v1',
 
355
                ('}', 1),
 
356
                '}',
 
357
                ('}', 0)]
337
358
 
338
 
        # SKIPPED
 
359
        ################################### SKIPPED
339
360
        # Weave.get doesn't trap this anymore
340
 
        return
 
361
        return 
 
362
 
341
363
 
342
364
        self.assertRaises(WeaveFormatError,
343
365
                          k.get,
346
368
 
347
369
class BadInsert(TestBase):
348
370
    """Test that we trap an insert which should not occur."""
349
 
 
350
371
    def runTest(self):
351
372
        k = Weave()
352
373
 
353
374
        k._parents = [frozenset(),
354
 
                      frozenset([0]),
355
 
                      frozenset([0]),
356
 
                      frozenset([0, 1, 2]),
357
 
                      ]
358
 
        k._weave = [(b'{', 0),
359
 
                    b'foo {',
360
 
                    (b'{', 1),
361
 
                    b'  added in version 1',
362
 
                    (b'{', 1),
363
 
                    b'  more in 1',
364
 
                    (b'}', 1),
365
 
                    (b'}', 1),
366
 
                    (b'}', 0)]
 
375
                frozenset([0]),
 
376
                frozenset([0]),
 
377
                frozenset([0,1,2]),
 
378
                ]
 
379
        k._weave = [('{', 0),
 
380
                'foo {',
 
381
                ('{', 1),
 
382
                '  added in version 1',
 
383
                ('{', 1),
 
384
                '  more in 1',
 
385
                ('}', 1),
 
386
                ('}', 1),
 
387
                ('}', 0)]
 
388
 
367
389
 
368
390
        # this is not currently enforced by get
369
 
        return
 
391
        return  ##########################################
370
392
 
371
393
        self.assertRaises(WeaveFormatError,
372
394
                          k.get,
379
401
 
380
402
class InsertNested(TestBase):
381
403
    """Insertion with nested instructions."""
382
 
 
383
404
    def runTest(self):
384
405
        k = Weave()
385
406
 
386
407
        k._parents = [frozenset(),
387
 
                      frozenset([0]),
388
 
                      frozenset([0]),
389
 
                      frozenset([0, 1, 2]),
390
 
                      ]
391
 
        k._weave = [(b'{', 0),
392
 
                    b'foo {',
393
 
                    (b'{', 1),
394
 
                    b'  added in version 1',
395
 
                    (b'{', 2),
396
 
                    b'  added in v2',
397
 
                    (b'}', 2),
398
 
                    b'  also from v1',
399
 
                    (b'}', 1),
400
 
                    b'}',
401
 
                    (b'}', 0)]
402
 
 
403
 
        k._sha1s = [
404
 
            sha_string(b'foo {}'),
405
 
            sha_string(b'foo {  added in version 1  also from v1}'),
406
 
            sha_string(b'foo {  added in v2}'),
407
 
            sha_string(
408
 
                b'foo {  added in version 1  added in v2  also from v1}')
409
 
            ]
410
 
 
411
 
        self.assertEqual(k.get_lines(0),
412
 
                         [b'foo {',
413
 
                          b'}'])
414
 
 
415
 
        self.assertEqual(k.get_lines(1),
416
 
                         [b'foo {',
417
 
                          b'  added in version 1',
418
 
                          b'  also from v1',
419
 
                          b'}'])
420
 
 
421
 
        self.assertEqual(k.get_lines(2),
422
 
                         [b'foo {',
423
 
                          b'  added in v2',
424
 
                          b'}'])
425
 
 
426
 
        self.assertEqual(k.get_lines(3),
427
 
                         [b'foo {',
428
 
                          b'  added in version 1',
429
 
                          b'  added in v2',
430
 
                          b'  also from v1',
431
 
                          b'}'])
 
408
                frozenset([0]),
 
409
                frozenset([0]),
 
410
                frozenset([0,1,2]),
 
411
                ]
 
412
        k._weave = [('{', 0),
 
413
                'foo {',
 
414
                ('{', 1),
 
415
                '  added in version 1',
 
416
                ('{', 2),
 
417
                '  added in v2',
 
418
                ('}', 2),
 
419
                '  also from v1',
 
420
                ('}', 1),
 
421
                '}',
 
422
                ('}', 0)]
 
423
 
 
424
        self.assertEqual(k.get(0),
 
425
                         ['foo {',
 
426
                          '}'])
 
427
 
 
428
        self.assertEqual(k.get(1),
 
429
                         ['foo {',
 
430
                          '  added in version 1',
 
431
                          '  also from v1',
 
432
                          '}'])
 
433
                       
 
434
        self.assertEqual(k.get(2),
 
435
                         ['foo {',
 
436
                          '  added in v2',
 
437
                          '}'])
 
438
 
 
439
        self.assertEqual(k.get(3),
 
440
                         ['foo {',
 
441
                          '  added in version 1',
 
442
                          '  added in v2',
 
443
                          '  also from v1',
 
444
                          '}'])
 
445
                         
432
446
 
433
447
 
434
448
class DeleteLines2(TestBase):
436
450
 
437
451
    This relies on the weave having a way to represent lines knocked
438
452
    out by a later revision."""
439
 
 
440
453
    def runTest(self):
441
454
        k = Weave()
442
455
 
443
 
        k.add_lines(b'text0', [], [b"line the first",
444
 
                                   b"line 2",
445
 
                                   b"line 3",
446
 
                                   b"fine"])
447
 
 
448
 
        self.assertEqual(len(k.get_lines(0)), 4)
449
 
 
450
 
        k.add_lines(b'text1', [b'text0'], [b"line the first",
451
 
                                           b"fine"])
452
 
 
453
 
        self.assertEqual(k.get_lines(1),
454
 
                         [b"line the first",
455
 
                          b"fine"])
456
 
 
457
 
        self.assertEqual(k.annotate(b'text1'),
458
 
                         [(b'text0', b"line the first"),
459
 
                          (b'text0', b"fine")])
 
456
        k.add('text0', [], ["line the first",
 
457
                   "line 2",
 
458
                   "line 3",
 
459
                   "fine"])
 
460
 
 
461
        self.assertEqual(len(k.get(0)), 4)
 
462
 
 
463
        k.add('text1', [0], ["line the first",
 
464
                   "fine"])
 
465
 
 
466
        self.assertEqual(k.get(1),
 
467
                         ["line the first",
 
468
                          "fine"])
 
469
 
 
470
        self.assertEqual(k.annotate(1),
 
471
                         [(0, "line the first"),
 
472
                          (0, "fine")])
 
473
 
460
474
 
461
475
 
462
476
class IncludeVersions(TestBase):
473
487
        k = Weave()
474
488
 
475
489
        k._parents = [frozenset(), frozenset([0])]
476
 
        k._weave = [(b'{', 0),
477
 
                    b"first line",
478
 
                    (b'}', 0),
479
 
                    (b'{', 1),
480
 
                    b"second line",
481
 
                    (b'}', 1)]
482
 
 
483
 
        k._sha1s = [sha_string(b'first line'), sha_string(
484
 
            b'first linesecond line')]
485
 
 
486
 
        self.assertEqual(k.get_lines(1),
487
 
                         [b"first line",
488
 
                          b"second line"])
489
 
 
490
 
        self.assertEqual(k.get_lines(0),
491
 
                         [b"first line"])
 
490
        k._weave = [('{', 0),
 
491
                "first line",
 
492
                ('}', 0),
 
493
                ('{', 1),
 
494
                "second line",
 
495
                ('}', 1)]
 
496
 
 
497
        self.assertEqual(k.get(1),
 
498
                         ["first line",
 
499
                          "second line"])
 
500
 
 
501
        self.assertEqual(k.get(0),
 
502
                         ["first line"])
492
503
 
493
504
 
494
505
class DivergedIncludes(TestBase):
495
506
    """Weave with two diverged texts based on version 0.
496
507
    """
497
 
 
498
508
    def runTest(self):
499
 
        # FIXME make the weave, dont poke at it.
500
509
        k = Weave()
501
510
 
502
 
        k._names = [b'0', b'1', b'2']
503
 
        k._name_map = {b'0': 0, b'1': 1, b'2': 2}
504
511
        k._parents = [frozenset(),
505
 
                      frozenset([0]),
506
 
                      frozenset([0]),
507
 
                      ]
508
 
        k._weave = [(b'{', 0),
509
 
                    b"first line",
510
 
                    (b'}', 0),
511
 
                    (b'{', 1),
512
 
                    b"second line",
513
 
                    (b'}', 1),
514
 
                    (b'{', 2),
515
 
                    b"alternative second line",
516
 
                    (b'}', 2),
517
 
                    ]
518
 
 
519
 
        k._sha1s = [
520
 
            sha_string(b'first line'),
521
 
            sha_string(b'first linesecond line'),
522
 
            sha_string(b'first linealternative second line')]
523
 
 
524
 
        self.assertEqual(k.get_lines(0),
525
 
                         [b"first line"])
526
 
 
527
 
        self.assertEqual(k.get_lines(1),
528
 
                         [b"first line",
529
 
                          b"second line"])
530
 
 
531
 
        self.assertEqual(k.get_lines(b'2'),
532
 
                         [b"first line",
533
 
                          b"alternative second line"])
534
 
 
535
 
        self.assertEqual(list(k.get_ancestry([b'2'])),
536
 
                         [b'0', b'2'])
 
512
                frozenset([0]),
 
513
                frozenset([0]),
 
514
                ]
 
515
        k._weave = [('{', 0),
 
516
                "first line",
 
517
                ('}', 0),
 
518
                ('{', 1),
 
519
                "second line",
 
520
                ('}', 1),
 
521
                ('{', 2),
 
522
                "alternative second line",
 
523
                ('}', 2),                
 
524
                ]
 
525
 
 
526
        self.assertEqual(k.get(0),
 
527
                         ["first line"])
 
528
 
 
529
        self.assertEqual(k.get(1),
 
530
                         ["first line",
 
531
                          "second line"])
 
532
 
 
533
        self.assertEqual(k.get(2),
 
534
                         ["first line",
 
535
                          "alternative second line"])
 
536
 
 
537
        self.assertEqual(list(k.inclusions([2])),
 
538
                         [0, 2])
 
539
 
537
540
 
538
541
 
539
542
class ReplaceLine(TestBase):
540
543
    def runTest(self):
541
544
        k = Weave()
542
545
 
543
 
        text0 = [b'cheddar', b'stilton', b'gruyere']
544
 
        text1 = [b'cheddar', b'blue vein', b'neufchatel', b'chevre']
545
 
 
546
 
        k.add_lines(b'text0', [], text0)
547
 
        k.add_lines(b'text1', [b'text0'], text1)
 
546
        text0 = ['cheddar', 'stilton', 'gruyere']
 
547
        text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
 
548
        
 
549
        k.add('text0', [], text0)
 
550
        k.add('text1', [0], text1)
548
551
 
549
552
        self.log('k._weave=' + pformat(k._weave))
550
553
 
551
 
        self.assertEqual(k.get_lines(0), text0)
552
 
        self.assertEqual(k.get_lines(1), text1)
 
554
        self.assertEqual(k.get(0), text0)
 
555
        self.assertEqual(k.get(1), text1)
 
556
 
553
557
 
554
558
 
555
559
class Merge(TestBase):
556
560
    """Storage of versions that merge diverged parents"""
557
 
 
558
561
    def runTest(self):
559
562
        k = Weave()
560
563
 
561
 
        texts = [
562
 
            [b'header'],
563
 
            [b'header', b'', b'line from 1'],
564
 
            [b'header', b'', b'line from 2', b'more from 2'],
565
 
            [b'header', b'', b'line from 1', b'fixup line', b'line from 2'],
566
 
            ]
 
564
        texts = [['header'],
 
565
                 ['header', '', 'line from 1'],
 
566
                 ['header', '', 'line from 2', 'more from 2'],
 
567
                 ['header', '', 'line from 1', 'fixup line', 'line from 2'],
 
568
                 ]
567
569
 
568
 
        k.add_lines(b'text0', [], texts[0])
569
 
        k.add_lines(b'text1', [b'text0'], texts[1])
570
 
        k.add_lines(b'text2', [b'text0'], texts[2])
571
 
        k.add_lines(b'merge', [b'text0', b'text1', b'text2'], texts[3])
 
570
        k.add('text0', [], texts[0])
 
571
        k.add('text1', [0], texts[1])
 
572
        k.add('text2', [0], texts[2])
 
573
        k.add('merge', [0, 1, 2], texts[3])
572
574
 
573
575
        for i, t in enumerate(texts):
574
 
            self.assertEqual(k.get_lines(i), t)
 
576
            self.assertEqual(k.get(i), t)
575
577
 
576
 
        self.assertEqual(k.annotate(b'merge'),
577
 
                         [(b'text0', b'header'),
578
 
                          (b'text1', b''),
579
 
                          (b'text1', b'line from 1'),
580
 
                          (b'merge', b'fixup line'),
581
 
                          (b'text2', b'line from 2'),
 
578
        self.assertEqual(k.annotate(3),
 
579
                         [(0, 'header'),
 
580
                          (1, ''),
 
581
                          (1, 'line from 1'),
 
582
                          (3, 'fixup line'),
 
583
                          (2, 'line from 2'),
582
584
                          ])
583
585
 
584
 
        self.assertEqual(list(k.get_ancestry([b'merge'])),
585
 
                         [b'text0', b'text1', b'text2', b'merge'])
 
586
        self.assertEqual(list(k.inclusions([3])),
 
587
                         [0, 1, 2, 3])
586
588
 
587
589
        self.log('k._weave=' + pformat(k._weave))
588
590
 
595
597
    A base version is inserted, then two descendents try to
596
598
    insert different lines in the same place.  These should be
597
599
    reported as a possible conflict and forwarded to the user."""
598
 
 
599
600
    def runTest(self):
600
601
        return  # NOT RUN
601
602
        k = Weave()
602
603
 
603
 
        k.add_lines([], [b'aaa', b'bbb'])
604
 
        k.add_lines([0], [b'aaa', b'111', b'bbb'])
605
 
        k.add_lines([1], [b'aaa', b'222', b'bbb'])
606
 
 
607
 
        k.merge([1, 2])
608
 
 
609
 
        self.assertEqual([[[b'aaa']],
610
 
                          [[b'111'], [b'222']],
611
 
                          [[b'bbb']]])
 
604
        k.add([], ['aaa', 'bbb'])
 
605
        k.add([0], ['aaa', '111', 'bbb'])
 
606
        k.add([1], ['aaa', '222', 'bbb'])
 
607
 
 
608
        merged = k.merge([1, 2])
 
609
 
 
610
        self.assertEquals([[['aaa']],
 
611
                           [['111'], ['222']],
 
612
                           [['bbb']]])
 
613
 
612
614
 
613
615
 
614
616
class NonConflict(TestBase):
615
617
    """Two descendants insert compatible changes.
616
618
 
617
619
    No conflict should be reported."""
618
 
 
619
620
    def runTest(self):
620
621
        return  # NOT RUN
621
622
        k = Weave()
622
623
 
623
 
        k.add_lines([], [b'aaa', b'bbb'])
624
 
        k.add_lines([0], [b'111', b'aaa', b'ccc', b'bbb'])
625
 
        k.add_lines([1], [b'aaa', b'ccc', b'bbb', b'222'])
 
624
        k.add([], ['aaa', 'bbb'])
 
625
        k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
 
626
        k.add([1], ['aaa', 'ccc', 'bbb', '222'])
 
627
 
 
628
    
 
629
    
 
630
 
 
631
 
 
632
class AutoMerge(TestBase):
 
633
    def runTest(self):
 
634
        k = Weave()
 
635
 
 
636
        texts = [['header', 'aaa', 'bbb'],
 
637
                 ['header', 'aaa', 'line from 1', 'bbb'],
 
638
                 ['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
 
639
                 ]
 
640
 
 
641
        k.add('text0', [], texts[0])
 
642
        k.add('text1', [0], texts[1])
 
643
        k.add('text2', [0], texts[2])
 
644
 
 
645
        self.log('k._weave=' + pformat(k._weave))
 
646
 
 
647
        m = list(k.mash_iter([0, 1, 2]))
 
648
 
 
649
        self.assertEqual(m,
 
650
                         ['header', 'aaa',
 
651
                          'line from 1',
 
652
                          'bbb',
 
653
                          'line from 2', 'more from 2'])
 
654
        
626
655
 
627
656
 
628
657
class Khayyam(TestBase):
629
658
    """Test changes to multi-line texts, and read/write"""
630
 
 
631
 
    def test_multi_line_merge(self):
 
659
    def runTest(self):
632
660
        rawtexts = [
633
 
            b"""A Book of Verses underneath the Bough,
 
661
            """A Book of Verses underneath the Bough,
634
662
            A Jug of Wine, a Loaf of Bread, -- and Thou
635
663
            Beside me singing in the Wilderness --
636
664
            Oh, Wilderness were Paradise enow!""",
637
 
 
638
 
            b"""A Book of Verses underneath the Bough,
 
665
            
 
666
            """A Book of Verses underneath the Bough,
639
667
            A Jug of Wine, a Loaf of Bread, -- and Thou
640
668
            Beside me singing in the Wilderness --
641
669
            Oh, Wilderness were Paradise now!""",
642
670
 
643
 
            b"""A Book of poems underneath the tree,
 
671
            """A Book of poems underneath the tree,
644
672
            A Jug of Wine, a Loaf of Bread,
645
673
            and Thou
646
674
            Beside me singing in the Wilderness --
648
676
 
649
677
            -- O. Khayyam""",
650
678
 
651
 
            b"""A Book of Verses underneath the Bough,
 
679
            """A Book of Verses underneath the Bough,
652
680
            A Jug of Wine, a Loaf of Bread,
653
681
            and Thou
654
682
            Beside me singing in the Wilderness --
655
683
            Oh, Wilderness were Paradise now!""",
656
684
            ]
657
 
        texts = [[l.strip() for l in t.split(b'\n')] for t in rawtexts]
 
685
        texts = [[l.strip() for l in t.split('\n')] for t in rawtexts]
658
686
 
659
687
        k = Weave()
660
688
        parents = set()
661
689
        i = 0
662
690
        for t in texts:
663
 
            k.add_lines(b'text%d' % i, list(parents), t)
664
 
            parents.add(b'text%d' % i)
 
691
            ver = k.add('text%d' % i,
 
692
                        list(parents), t)
 
693
            parents.add(ver)
665
694
            i += 1
666
695
 
667
696
        self.log("k._weave=" + pformat(k._weave))
668
697
 
669
698
        for i, t in enumerate(texts):
670
 
            self.assertEqual(k.get_lines(i), t)
 
699
            self.assertEqual(k.get(i), t)
671
700
 
672
701
        self.check_read_write(k)
673
702
 
674
703
 
 
704
 
 
705
class MergeCases(TestBase):
 
706
    def doMerge(self, base, a, b, mp):
 
707
        from cStringIO import StringIO
 
708
        from textwrap import dedent
 
709
 
 
710
        def addcrlf(x):
 
711
            return x + '\n'
 
712
        
 
713
        w = Weave()
 
714
        w.add('text0', [], map(addcrlf, base))
 
715
        w.add('text1', [0], map(addcrlf, a))
 
716
        w.add('text2', [0], map(addcrlf, b))
 
717
 
 
718
        self.log('weave is:')
 
719
        tmpf = StringIO()
 
720
        write_weave(w, tmpf)
 
721
        self.log(tmpf.getvalue())
 
722
 
 
723
        self.log('merge plan:')
 
724
        p = list(w.plan_merge(1, 2))
 
725
        for state, line in p:
 
726
            if line:
 
727
                self.log('%12s | %s' % (state, line[:-1]))
 
728
 
 
729
        self.log('merge:')
 
730
        mt = StringIO()
 
731
        mt.writelines(w.weave_merge(p))
 
732
        mt.seek(0)
 
733
        self.log(mt.getvalue())
 
734
 
 
735
        mp = map(addcrlf, mp)
 
736
        self.assertEqual(mt.readlines(), mp)
 
737
        
 
738
        
 
739
    def testOneInsert(self):
 
740
        self.doMerge([],
 
741
                     ['aa'],
 
742
                     [],
 
743
                     ['aa'])
 
744
 
 
745
    def testSeparateInserts(self):
 
746
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
747
                     ['aaa', 'xxx', 'bbb', 'ccc'],
 
748
                     ['aaa', 'bbb', 'yyy', 'ccc'],
 
749
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
 
750
 
 
751
    def testSameInsert(self):
 
752
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
753
                     ['aaa', 'xxx', 'bbb', 'ccc'],
 
754
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
 
755
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
 
756
 
 
757
    def testOverlappedInsert(self):
 
758
        self.doMerge(['aaa', 'bbb'],
 
759
                     ['aaa', 'xxx', 'yyy', 'bbb'],
 
760
                     ['aaa', 'xxx', 'bbb'],
 
761
                     ['aaa', '<<<<<<<', 'xxx', 'yyy', '=======', 'xxx', 
 
762
                      '>>>>>>>', 'bbb'])
 
763
 
 
764
        # really it ought to reduce this to 
 
765
        # ['aaa', 'xxx', 'yyy', 'bbb']
 
766
 
 
767
 
 
768
    def testClashReplace(self):
 
769
        self.doMerge(['aaa'],
 
770
                     ['xxx'],
 
771
                     ['yyy', 'zzz'],
 
772
                     ['<<<<<<<', 'xxx', '=======', 'yyy', 'zzz', 
 
773
                      '>>>>>>>'])
 
774
 
 
775
    def testNonClashInsert(self):
 
776
        self.doMerge(['aaa'],
 
777
                     ['xxx', 'aaa'],
 
778
                     ['yyy', 'zzz'],
 
779
                     ['<<<<<<<', 'xxx', 'aaa', '=======', 'yyy', 'zzz', 
 
780
                      '>>>>>>>'])
 
781
 
 
782
        self.doMerge(['aaa'],
 
783
                     ['aaa'],
 
784
                     ['yyy', 'zzz'],
 
785
                     ['yyy', 'zzz'])
 
786
 
 
787
 
 
788
    def testDeleteAndModify(self):
 
789
        """Clashing delete and modification.
 
790
 
 
791
        If one side modifies a region and the other deletes it then
 
792
        there should be a conflict with one side blank.
 
793
        """
 
794
 
 
795
        #######################################
 
796
        # skippd, not working yet
 
797
        return
 
798
        
 
799
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
800
                     ['aaa', 'ddd', 'ccc'],
 
801
                     ['aaa', 'ccc'],
 
802
                     ['<<<<<<<<', 'aaa', '=======', '>>>>>>>', 'ccc'])
 
803
 
 
804
 
675
805
class JoinWeavesTests(TestBase):
676
 
 
677
806
    def setUp(self):
678
807
        super(JoinWeavesTests, self).setUp()
679
808
        self.weave1 = Weave()
680
 
        self.lines1 = [b'hello\n']
681
 
        self.lines3 = [b'hello\n', b'cruel\n', b'world\n']
682
 
        self.weave1.add_lines(b'v1', [], self.lines1)
683
 
        self.weave1.add_lines(b'v2', [b'v1'], [b'hello\n', b'world\n'])
684
 
        self.weave1.add_lines(b'v3', [b'v2'], self.lines3)
685
 
 
686
 
    def test_written_detection(self):
687
 
        # Test detection of weave file corruption.
688
 
        #
689
 
        # Make sure that we can detect if a weave file has
690
 
        # been corrupted. This doesn't test all forms of corruption,
691
 
        # but it at least helps verify the data you get, is what you want.
692
 
 
693
 
        w = Weave()
694
 
        w.add_lines(b'v1', [], [b'hello\n'])
695
 
        w.add_lines(b'v2', [b'v1'], [b'hello\n', b'there\n'])
696
 
 
697
 
        tmpf = BytesIO()
698
 
        write_weave(w, tmpf)
699
 
 
700
 
        # Because we are corrupting, we need to make sure we have the exact
701
 
        # text
702
 
        self.assertEqual(
703
 
            b'# bzr weave file v5\n'
704
 
            b'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
705
 
            b'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
706
 
            b'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n',
707
 
            tmpf.getvalue())
708
 
 
709
 
        # Change a single letter
710
 
        tmpf = BytesIO(
711
 
            b'# bzr weave file v5\n'
712
 
            b'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
713
 
            b'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
714
 
            b'w\n{ 0\n. hello\n}\n{ 1\n. There\n}\nW\n')
715
 
 
716
 
        w = read_weave(tmpf)
717
 
 
718
 
        self.assertEqual(b'hello\n', w.get_text(b'v1'))
719
 
        self.assertRaises(WeaveInvalidChecksum, w.get_text, b'v2')
720
 
        self.assertRaises(WeaveInvalidChecksum, w.get_lines, b'v2')
721
 
        self.assertRaises(WeaveInvalidChecksum, w.check)
722
 
 
723
 
        # Change the sha checksum
724
 
        tmpf = BytesIO(
725
 
            b'# bzr weave file v5\n'
726
 
            b'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
727
 
            b'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
728
 
            b'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n')
729
 
 
730
 
        w = read_weave(tmpf)
731
 
 
732
 
        self.assertEqual(b'hello\n', w.get_text(b'v1'))
733
 
        self.assertRaises(WeaveInvalidChecksum, w.get_text, b'v2')
734
 
        self.assertRaises(WeaveInvalidChecksum, w.get_lines, b'v2')
735
 
        self.assertRaises(WeaveInvalidChecksum, w.check)
736
 
 
737
 
 
738
 
class TestWeave(TestCase):
739
 
 
740
 
    def test_allow_reserved_false(self):
741
 
        w = Weave('name', allow_reserved=False)
742
 
        # Add lines is checked at the WeaveFile level, not at the Weave level
743
 
        w.add_lines(b'name:', [], TEXT_1)
744
 
        # But get_lines is checked at this level
745
 
        self.assertRaises(errors.ReservedId, w.get_lines, b'name:')
746
 
 
747
 
    def test_allow_reserved_true(self):
748
 
        w = Weave('name', allow_reserved=True)
749
 
        w.add_lines(b'name:', [], TEXT_1)
750
 
        self.assertEqual(TEXT_1, w.get_lines(b'name:'))
751
 
 
752
 
 
753
 
class InstrumentedWeave(Weave):
754
 
    """Keep track of how many times functions are called."""
755
 
 
756
 
    def __init__(self, weave_name=None):
757
 
        self._extract_count = 0
758
 
        Weave.__init__(self, weave_name=weave_name)
759
 
 
760
 
    def _extract(self, versions):
761
 
        self._extract_count += 1
762
 
        return Weave._extract(self, versions)
763
 
 
764
 
 
765
 
class TestNeedsReweave(TestCase):
766
 
    """Internal corner cases for when reweave is needed."""
767
 
 
768
 
    def test_compatible_parents(self):
769
 
        w1 = Weave('a')
770
 
        my_parents = {1, 2, 3}
771
 
        # subsets are ok
772
 
        self.assertTrue(w1._compatible_parents(my_parents, {3}))
773
 
        # same sets
774
 
        self.assertTrue(w1._compatible_parents(my_parents, set(my_parents)))
775
 
        # same empty corner case
776
 
        self.assertTrue(w1._compatible_parents(set(), set()))
777
 
        # other cannot contain stuff my_parents does not
778
 
        self.assertFalse(w1._compatible_parents(set(), {1}))
779
 
        self.assertFalse(w1._compatible_parents(my_parents, {1, 2, 3, 4}))
780
 
        self.assertFalse(w1._compatible_parents(my_parents, {4}))
781
 
 
782
 
 
783
 
class TestWeaveFile(TestCaseInTempDir):
784
 
 
785
 
    def test_empty_file(self):
786
 
        with open('empty.weave', 'wb+') as f:
787
 
            self.assertRaises(WeaveFormatError, read_weave, f)
 
809
        self.lines1 = ['hello\n']
 
810
        self.lines3 = ['hello\n', 'cruel\n', 'world\n']
 
811
        self.weave1.add('v1', [], self.lines1)
 
812
        self.weave1.add('v2', [0], ['hello\n', 'world\n'])
 
813
        self.weave1.add('v3', [1], self.lines3)
 
814
        
 
815
    def test_join_empty(self):
 
816
        """Join two empty weaves."""
 
817
        eq = self.assertEqual
 
818
        w1 = Weave()
 
819
        w2 = Weave()
 
820
        w1.join(w2)
 
821
        eq(w1.numversions(), 0)
 
822
        
 
823
    def test_join_empty_to_nonempty(self):
 
824
        """Join empty weave onto nonempty."""
 
825
        self.weave1.join(Weave())
 
826
        self.assertEqual(len(self.weave1), 3)
 
827
 
 
828
    def test_join_unrelated(self):
 
829
        """Join two weaves with no history in common."""
 
830
        wb = Weave()
 
831
        wb.add('b1', [], ['line from b\n'])
 
832
        w1 = self.weave1
 
833
        w1.join(wb)
 
834
        eq = self.assertEqual
 
835
        eq(len(w1), 4)
 
836
        eq(sorted(list(w1.iter_names())),
 
837
           ['b1', 'v1', 'v2', 'v3'])
 
838
 
 
839
    def test_join_related(self):
 
840
        wa = self.weave1.copy()
 
841
        wb = self.weave1.copy()
 
842
        wa.add('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
 
843
        wb.add('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
 
844
        eq = self.assertEquals
 
845
        eq(len(wa), 4)
 
846
        eq(len(wb), 4)
 
847
        wa.join(wb)
 
848
        eq(len(wa), 5)
 
849
        eq(wa.get_lines('b1'),
 
850
           ['hello\n', 'pale blue\n', 'world\n'])
 
851
 
 
852
    def test_join_parent_disagreement(self):
 
853
        """Cannot join weaves with different parents for a version."""
 
854
        wa = Weave()
 
855
        wb = Weave()
 
856
        wa.add('v1', [], ['hello\n'])
 
857
        wb.add('v0', [], [])
 
858
        wb.add('v1', ['v0'], ['hello\n'])
 
859
        self.assertRaises(WeaveError,
 
860
                          wa.join, wb)
 
861
 
 
862
    def test_join_text_disagreement(self):
 
863
        """Cannot join weaves with different texts for a version."""
 
864
        wa = Weave()
 
865
        wb = Weave()
 
866
        wa.add('v1', [], ['hello\n'])
 
867
        wb.add('v1', [], ['not\n', 'hello\n'])
 
868
        self.assertRaises(WeaveError,
 
869
                          wa.join, wb)
 
870
 
 
871
    def test_join_unordered(self):
 
872
        """Join weaves where indexes differ.
 
873
        
 
874
        The source weave contains a different version at index 0."""
 
875
        wa = self.weave1.copy()
 
876
        wb = Weave()
 
877
        wb.add('x1', [], ['line from x1\n'])
 
878
        wb.add('v1', [], ['hello\n'])
 
879
        wb.add('v2', ['v1'], ['hello\n', 'world\n'])
 
880
        wa.join(wb)
 
881
        eq = self.assertEquals
 
882
        eq(sorted(wa.iter_names()), ['v1', 'v2', 'v3', 'x1',])
 
883
        eq(wa.get_text('x1'), 'line from x1\n')