~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_weave.py

  • Committer: Robert Collins
  • Date: 2006-03-03 01:47:22 UTC
  • mto: (1594.2.4 integration)
  • mto: This revision was merged to the branch mainline in revision 1596.
  • Revision ID: robertc@robertcollins.net-20060303014722-e890b9bf8628aebf
Add a revision store test adapter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 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 bzrlib import (
27
 
    errors,
28
 
    )
 
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.tests import TestCase
29
32
from bzrlib.osutils import sha_string
30
 
from bzrlib.tests import TestCase, TestCaseInTempDir
31
 
from bzrlib.weave import Weave, WeaveFormatError, WeaveError
32
 
from bzrlib.weavefile import write_weave, read_weave
33
33
 
34
34
 
35
35
# texts for use in testing
39
39
 
40
40
 
41
41
class TestBase(TestCase):
42
 
 
43
42
    def check_read_write(self, k):
44
43
        """Check the weave k can be written & re-read."""
45
44
        from tempfile import TemporaryFile
65
64
class WeaveContains(TestBase):
66
65
    """Weave __contains__ operator"""
67
66
    def runTest(self):
68
 
        k = Weave(get_scope=lambda:None)
 
67
        k = Weave()
69
68
        self.assertFalse('foo' in k)
70
 
        k.add_lines('foo', [], TEXT_1)
 
69
        k.add('foo', [], TEXT_1)
71
70
        self.assertTrue('foo' in k)
72
71
 
73
72
 
76
75
        k = Weave()
77
76
 
78
77
 
 
78
class StoreText(TestBase):
 
79
    """Store and retrieve a simple text."""
 
80
    def runTest(self):
 
81
        k = Weave()
 
82
        idx = k.add('text0', [], TEXT_0)
 
83
        self.assertEqual(k.get(idx), TEXT_0)
 
84
        self.assertEqual(idx, 0)
 
85
 
 
86
 
79
87
class AnnotateOne(TestBase):
80
88
    def runTest(self):
81
89
        k = Weave()
82
 
        k.add_lines('text0', [], TEXT_0)
83
 
        self.assertEqual(k.annotate('text0'),
84
 
                         [('text0', TEXT_0[0])])
85
 
 
 
90
        k.add('text0', [], TEXT_0)
 
91
        self.assertEqual(k.annotate(0),
 
92
                         [(0, TEXT_0[0])])
 
93
 
 
94
 
 
95
class StoreTwo(TestBase):
 
96
    def runTest(self):
 
97
        k = Weave()
 
98
 
 
99
        idx = k.add('text0', [], TEXT_0)
 
100
        self.assertEqual(idx, 0)
 
101
 
 
102
        idx = k.add('text1', [], TEXT_1)
 
103
        self.assertEqual(idx, 1)
 
104
 
 
105
        self.assertEqual(k.get(0), TEXT_0)
 
106
        self.assertEqual(k.get(1), TEXT_1)
 
107
 
 
108
 
 
109
class AddWithGivenSha(TestBase):
 
110
    def runTest(self):
 
111
        """Add with caller-supplied SHA-1"""
 
112
        k = Weave()
 
113
 
 
114
        t = 'text0'
 
115
        k.add('text0', [], [t], sha1=sha_string(t))
 
116
 
 
117
 
 
118
class GetSha1(TestBase):
 
119
    def test_get_sha1(self):
 
120
        k = Weave()
 
121
        k.add('text0', [], 'text0')
 
122
        self.assertEqual('34dc0e430c642a26c3dd1c2beb7a8b4f4445eb79',
 
123
                         k.get_sha1('text0'))
 
124
        self.assertRaises(errors.RevisionNotPresent,
 
125
                          k.get_sha1, 0)
 
126
        self.assertRaises(errors.RevisionNotPresent,
 
127
                          k.get_sha1, 'text1')
 
128
                        
86
129
 
87
130
class InvalidAdd(TestBase):
88
131
    """Try to use invalid version number during add."""
89
132
    def runTest(self):
90
133
        k = Weave()
91
134
 
92
 
        self.assertRaises(errors.RevisionNotPresent,
93
 
                          k.add_lines,
 
135
        self.assertRaises(IndexError,
 
136
                          k.add,
94
137
                          'text0',
95
 
                          ['69'],
 
138
                          [69],
96
139
                          ['new text!'])
97
140
 
98
141
 
99
142
class RepeatedAdd(TestBase):
100
143
    """Add the same version twice; harmless."""
101
 
 
102
 
    def test_duplicate_add(self):
 
144
    def runTest(self):
103
145
        k = Weave()
104
 
        idx = k.add_lines('text0', [], TEXT_0)
105
 
        idx2 = k.add_lines('text0', [], TEXT_0)
 
146
        idx = k.add('text0', [], TEXT_0)
 
147
        idx2 = k.add('text0', [], TEXT_0)
106
148
        self.assertEqual(idx, idx2)
107
149
 
108
150
 
109
151
class InvalidRepeatedAdd(TestBase):
110
152
    def runTest(self):
111
153
        k = Weave()
112
 
        k.add_lines('basis', [], TEXT_0)
113
 
        idx = k.add_lines('text0', [], TEXT_0)
 
154
        idx = k.add('text0', [], TEXT_0)
114
155
        self.assertRaises(errors.RevisionAlreadyPresent,
115
 
                          k.add_lines,
 
156
                          k.add,
116
157
                          'text0',
117
158
                          [],
118
159
                          ['not the same text'])
119
160
        self.assertRaises(errors.RevisionAlreadyPresent,
120
 
                          k.add_lines,
 
161
                          k.add,
121
162
                          'text0',
122
 
                          ['basis'],         # not the right parents
 
163
                          [12],         # not the right parents
123
164
                          TEXT_0)
124
 
 
 
165
        
125
166
 
126
167
class InsertLines(TestBase):
127
168
    """Store a revision that adds one line to the original.
131
172
    def runTest(self):
132
173
        k = Weave()
133
174
 
134
 
        k.add_lines('text0', [], ['line 1'])
135
 
        k.add_lines('text1', ['text0'], ['line 1', 'line 2'])
136
 
 
137
 
        self.assertEqual(k.annotate('text0'),
138
 
                         [('text0', 'line 1')])
139
 
 
140
 
        self.assertEqual(k.get_lines(1),
 
175
        k.add('text0', [], ['line 1'])
 
176
        k.add('text1', [0], ['line 1', 'line 2'])
 
177
 
 
178
        self.assertEqual(k.annotate(0),
 
179
                         [(0, 'line 1')])
 
180
 
 
181
        self.assertEqual(k.get(1),
141
182
                         ['line 1',
142
183
                          'line 2'])
143
184
 
144
 
        self.assertEqual(k.annotate('text1'),
145
 
                         [('text0', 'line 1'),
146
 
                          ('text1', 'line 2')])
147
 
 
148
 
        k.add_lines('text2', ['text0'], ['line 1', 'diverged line'])
149
 
 
150
 
        self.assertEqual(k.annotate('text2'),
151
 
                         [('text0', 'line 1'),
152
 
                          ('text2', 'diverged line')])
 
185
        self.assertEqual(k.annotate(1),
 
186
                         [(0, 'line 1'),
 
187
                          (1, 'line 2')])
 
188
 
 
189
        k.add('text2', [0], ['line 1', 'diverged line'])
 
190
 
 
191
        self.assertEqual(k.annotate(2),
 
192
                         [(0, 'line 1'),
 
193
                          (2, 'diverged line')])
153
194
 
154
195
        text3 = ['line 1', 'middle line', 'line 2']
155
 
        k.add_lines('text3',
156
 
              ['text0', 'text1'],
 
196
        k.add('text3',
 
197
              [0, 1],
157
198
              text3)
158
199
 
159
200
        # self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
160
201
 
161
202
        self.log("k._weave=" + pformat(k._weave))
162
203
 
163
 
        self.assertEqual(k.annotate('text3'),
164
 
                         [('text0', 'line 1'),
165
 
                          ('text3', 'middle line'),
166
 
                          ('text1', 'line 2')])
 
204
        self.assertEqual(k.annotate(3),
 
205
                         [(0, 'line 1'),
 
206
                          (3, 'middle line'),
 
207
                          (1, 'line 2')])
167
208
 
168
209
        # now multiple insertions at different places
169
 
        k.add_lines('text4',
170
 
              ['text0', 'text1', 'text3'],
 
210
        k.add('text4',
 
211
              [0, 1, 3],
171
212
              ['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
172
213
 
173
 
        self.assertEqual(k.annotate('text4'),
174
 
                         [('text0', 'line 1'),
175
 
                          ('text4', 'aaa'),
176
 
                          ('text3', 'middle line'),
177
 
                          ('text4', 'bbb'),
178
 
                          ('text1', 'line 2'),
179
 
                          ('text4', 'ccc')])
 
214
        self.assertEqual(k.annotate(4), 
 
215
                         [(0, 'line 1'),
 
216
                          (4, 'aaa'),
 
217
                          (3, 'middle line'),
 
218
                          (4, 'bbb'),
 
219
                          (1, 'line 2'),
 
220
                          (4, 'ccc')])
180
221
 
181
222
 
182
223
class DeleteLines(TestBase):
188
229
 
189
230
        base_text = ['one', 'two', 'three', 'four']
190
231
 
191
 
        k.add_lines('text0', [], base_text)
192
 
 
 
232
        k.add('text0', [], base_text)
 
233
        
193
234
        texts = [['one', 'two', 'three'],
194
235
                 ['two', 'three', 'four'],
195
236
                 ['one', 'four'],
198
239
 
199
240
        i = 1
200
241
        for t in texts:
201
 
            ver = k.add_lines('text%d' % i,
202
 
                        ['text0'], t)
 
242
            ver = k.add('text%d' % i,
 
243
                        [0], t)
203
244
            i += 1
204
245
 
205
246
        self.log('final weave:')
206
247
        self.log('k._weave=' + pformat(k._weave))
207
248
 
208
249
        for i in range(len(texts)):
209
 
            self.assertEqual(k.get_lines(i+1),
 
250
            self.assertEqual(k.get(i+1),
210
251
                             texts[i])
211
252
 
212
253
 
226
267
                ]
227
268
        ################################### SKIPPED
228
269
        # Weave.get doesn't trap this anymore
229
 
        return
 
270
        return 
230
271
 
231
272
        self.assertRaises(WeaveFormatError,
232
 
                          k.get_lines,
233
 
                          0)
 
273
                          k.get,
 
274
                          0)        
234
275
 
235
276
 
236
277
class CannedDelete(TestBase):
252
293
        k._sha1s = [sha_string('first lineline to be deletedlast line')
253
294
                  , sha_string('first linelast line')]
254
295
 
255
 
        self.assertEqual(k.get_lines(0),
 
296
        self.assertEqual(k.get(0),
256
297
                         ['first line',
257
298
                          'line to be deleted',
258
299
                          'last line',
259
300
                          ])
260
301
 
261
 
        self.assertEqual(k.get_lines(1),
 
302
        self.assertEqual(k.get(1),
262
303
                         ['first line',
263
304
                          'last line',
264
305
                          ])
278
319
                'line to be deleted',
279
320
                (']', 1),
280
321
                ('{', 1),
281
 
                'replacement line',
 
322
                'replacement line',                
282
323
                ('}', 1),
283
324
                'last line',
284
325
                ('}', 0),
286
327
        k._sha1s = [sha_string('first lineline to be deletedlast line')
287
328
                  , sha_string('first linereplacement linelast line')]
288
329
 
289
 
        self.assertEqual(k.get_lines(0),
 
330
        self.assertEqual(k.get(0),
290
331
                         ['first line',
291
332
                          'line to be deleted',
292
333
                          'last line',
293
334
                          ])
294
335
 
295
 
        self.assertEqual(k.get_lines(1),
 
336
        self.assertEqual(k.get(1),
296
337
                         ['first line',
297
338
                          'replacement line',
298
339
                          'last line',
321
362
 
322
363
        ################################### SKIPPED
323
364
        # Weave.get doesn't trap this anymore
324
 
        return
 
365
        return 
325
366
 
326
367
 
327
368
        self.assertRaises(WeaveFormatError,
390
431
                  , sha_string('foo {  added in version 1  added in v2  also from v1}')
391
432
                  ]
392
433
 
393
 
        self.assertEqual(k.get_lines(0),
394
 
                         ['foo {',
395
 
                          '}'])
396
 
 
397
 
        self.assertEqual(k.get_lines(1),
398
 
                         ['foo {',
399
 
                          '  added in version 1',
400
 
                          '  also from v1',
401
 
                          '}'])
402
 
 
403
 
        self.assertEqual(k.get_lines(2),
404
 
                         ['foo {',
405
 
                          '  added in v2',
406
 
                          '}'])
407
 
 
408
 
        self.assertEqual(k.get_lines(3),
409
 
                         ['foo {',
410
 
                          '  added in version 1',
411
 
                          '  added in v2',
412
 
                          '  also from v1',
413
 
                          '}'])
414
 
 
 
434
        self.assertEqual(k.get(0),
 
435
                         ['foo {',
 
436
                          '}'])
 
437
 
 
438
        self.assertEqual(k.get(1),
 
439
                         ['foo {',
 
440
                          '  added in version 1',
 
441
                          '  also from v1',
 
442
                          '}'])
 
443
                       
 
444
        self.assertEqual(k.get(2),
 
445
                         ['foo {',
 
446
                          '  added in v2',
 
447
                          '}'])
 
448
 
 
449
        self.assertEqual(k.get(3),
 
450
                         ['foo {',
 
451
                          '  added in version 1',
 
452
                          '  added in v2',
 
453
                          '  also from v1',
 
454
                          '}'])
 
455
                         
415
456
 
416
457
class DeleteLines2(TestBase):
417
458
    """Test recording revisions that delete lines.
421
462
    def runTest(self):
422
463
        k = Weave()
423
464
 
424
 
        k.add_lines('text0', [], ["line the first",
 
465
        k.add('text0', [], ["line the first",
425
466
                   "line 2",
426
467
                   "line 3",
427
468
                   "fine"])
428
469
 
429
 
        self.assertEqual(len(k.get_lines(0)), 4)
 
470
        self.assertEqual(len(k.get(0)), 4)
430
471
 
431
 
        k.add_lines('text1', ['text0'], ["line the first",
 
472
        k.add('text1', [0], ["line the first",
432
473
                   "fine"])
433
474
 
434
 
        self.assertEqual(k.get_lines(1),
 
475
        self.assertEqual(k.get(1),
435
476
                         ["line the first",
436
477
                          "fine"])
437
478
 
438
 
        self.assertEqual(k.annotate('text1'),
439
 
                         [('text0', "line the first"),
440
 
                          ('text0', "fine")])
 
479
        self.assertEqual(k.annotate(1),
 
480
                         [(0, "line the first"),
 
481
                          (0, "fine")])
441
482
 
442
483
 
443
484
class IncludeVersions(TestBase):
464
505
        k._sha1s = [sha_string('first line')
465
506
                  , sha_string('first linesecond line')]
466
507
 
467
 
        self.assertEqual(k.get_lines(1),
 
508
        self.assertEqual(k.get(1),
468
509
                         ["first line",
469
510
                          "second line"])
470
511
 
471
 
        self.assertEqual(k.get_lines(0),
 
512
        self.assertEqual(k.get(0),
472
513
                         ["first line"])
473
514
 
474
515
 
480
521
        k = Weave()
481
522
 
482
523
        k._names = ['0', '1', '2']
483
 
        k._name_map = {'0':0, '1':1, '2':2}
484
524
        k._parents = [frozenset(),
485
525
                frozenset([0]),
486
526
                frozenset([0]),
493
533
                ('}', 1),
494
534
                ('{', 2),
495
535
                "alternative second line",
496
 
                ('}', 2),
 
536
                ('}', 2),                
497
537
                ]
498
538
 
499
539
        k._sha1s = [sha_string('first line')
500
540
                  , sha_string('first linesecond line')
501
541
                  , sha_string('first linealternative second line')]
502
542
 
503
 
        self.assertEqual(k.get_lines(0),
 
543
        self.assertEqual(k.get(0),
504
544
                         ["first line"])
505
545
 
506
 
        self.assertEqual(k.get_lines(1),
 
546
        self.assertEqual(k.get(1),
507
547
                         ["first line",
508
548
                          "second line"])
509
549
 
510
 
        self.assertEqual(k.get_lines('2'),
 
550
        self.assertEqual(k.get(2),
511
551
                         ["first line",
512
552
                          "alternative second line"])
513
553
 
514
 
        self.assertEqual(list(k.get_ancestry(['2'])),
 
554
        self.assertEqual(list(k.inclusions([2])),
515
555
                         ['0', '2'])
516
556
 
517
557
 
521
561
 
522
562
        text0 = ['cheddar', 'stilton', 'gruyere']
523
563
        text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
524
 
 
525
 
        k.add_lines('text0', [], text0)
526
 
        k.add_lines('text1', ['text0'], text1)
 
564
        
 
565
        k.add('text0', [], text0)
 
566
        k.add('text1', [0], text1)
527
567
 
528
568
        self.log('k._weave=' + pformat(k._weave))
529
569
 
530
 
        self.assertEqual(k.get_lines(0), text0)
531
 
        self.assertEqual(k.get_lines(1), text1)
 
570
        self.assertEqual(k.get(0), text0)
 
571
        self.assertEqual(k.get(1), text1)
532
572
 
533
573
 
534
574
class Merge(TestBase):
542
582
                 ['header', '', 'line from 1', 'fixup line', 'line from 2'],
543
583
                 ]
544
584
 
545
 
        k.add_lines('text0', [], texts[0])
546
 
        k.add_lines('text1', ['text0'], texts[1])
547
 
        k.add_lines('text2', ['text0'], texts[2])
548
 
        k.add_lines('merge', ['text0', 'text1', 'text2'], texts[3])
 
585
        k.add('text0', [], texts[0])
 
586
        k.add('text1', [0], texts[1])
 
587
        k.add('text2', [0], texts[2])
 
588
        k.add('merge', [0, 1, 2], texts[3])
549
589
 
550
590
        for i, t in enumerate(texts):
551
 
            self.assertEqual(k.get_lines(i), t)
 
591
            self.assertEqual(k.get(i), t)
552
592
 
553
 
        self.assertEqual(k.annotate('merge'),
554
 
                         [('text0', 'header'),
555
 
                          ('text1', ''),
556
 
                          ('text1', 'line from 1'),
557
 
                          ('merge', 'fixup line'),
558
 
                          ('text2', 'line from 2'),
 
593
        self.assertEqual(k.annotate(3),
 
594
                         [(0, 'header'),
 
595
                          (1, ''),
 
596
                          (1, 'line from 1'),
 
597
                          (3, 'fixup line'),
 
598
                          (2, 'line from 2'),
559
599
                          ])
560
600
 
561
 
        self.assertEqual(list(k.get_ancestry(['merge'])),
 
601
        self.assertEqual(list(k.inclusions([3])),
562
602
                         ['text0', 'text1', 'text2', 'merge'])
563
603
 
564
604
        self.log('k._weave=' + pformat(k._weave))
576
616
        return  # NOT RUN
577
617
        k = Weave()
578
618
 
579
 
        k.add_lines([], ['aaa', 'bbb'])
580
 
        k.add_lines([0], ['aaa', '111', 'bbb'])
581
 
        k.add_lines([1], ['aaa', '222', 'bbb'])
 
619
        k.add([], ['aaa', 'bbb'])
 
620
        k.add([0], ['aaa', '111', 'bbb'])
 
621
        k.add([1], ['aaa', '222', 'bbb'])
582
622
 
583
623
        merged = k.merge([1, 2])
584
624
 
595
635
        return  # NOT RUN
596
636
        k = Weave()
597
637
 
598
 
        k.add_lines([], ['aaa', 'bbb'])
599
 
        k.add_lines([0], ['111', 'aaa', 'ccc', 'bbb'])
600
 
        k.add_lines([1], ['aaa', 'ccc', 'bbb', '222'])
 
638
        k.add([], ['aaa', 'bbb'])
 
639
        k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
 
640
        k.add([1], ['aaa', 'ccc', 'bbb', '222'])
601
641
 
602
642
 
603
643
class Khayyam(TestBase):
609
649
            A Jug of Wine, a Loaf of Bread, -- and Thou
610
650
            Beside me singing in the Wilderness --
611
651
            Oh, Wilderness were Paradise enow!""",
612
 
 
 
652
            
613
653
            """A Book of Verses underneath the Bough,
614
654
            A Jug of Wine, a Loaf of Bread, -- and Thou
615
655
            Beside me singing in the Wilderness --
635
675
        parents = set()
636
676
        i = 0
637
677
        for t in texts:
638
 
            ver = k.add_lines('text%d' % i,
 
678
            ver = k.add('text%d' % i,
639
679
                        list(parents), t)
640
 
            parents.add('text%d' % i)
 
680
            parents.add(ver)
641
681
            i += 1
642
682
 
643
683
        self.log("k._weave=" + pformat(k._weave))
644
684
 
645
685
        for i, t in enumerate(texts):
646
 
            self.assertEqual(k.get_lines(i), t)
 
686
            self.assertEqual(k.get(i), t)
647
687
 
648
688
        self.check_read_write(k)
649
689
 
650
690
 
 
691
class MergeCases(TestBase):
 
692
    def doMerge(self, base, a, b, mp):
 
693
        from cStringIO import StringIO
 
694
        from textwrap import dedent
 
695
 
 
696
        def addcrlf(x):
 
697
            return x + '\n'
 
698
        
 
699
        w = Weave()
 
700
        w.add('text0', [], map(addcrlf, base))
 
701
        w.add('text1', [0], map(addcrlf, a))
 
702
        w.add('text2', [0], map(addcrlf, b))
 
703
 
 
704
        self.log('weave is:')
 
705
        tmpf = StringIO()
 
706
        write_weave(w, tmpf)
 
707
        self.log(tmpf.getvalue())
 
708
 
 
709
        self.log('merge plan:')
 
710
        p = list(w.plan_merge(1, 2))
 
711
        for state, line in p:
 
712
            if line:
 
713
                self.log('%12s | %s' % (state, line[:-1]))
 
714
 
 
715
        self.log('merge:')
 
716
        mt = StringIO()
 
717
        mt.writelines(w.weave_merge(p))
 
718
        mt.seek(0)
 
719
        self.log(mt.getvalue())
 
720
 
 
721
        mp = map(addcrlf, mp)
 
722
        self.assertEqual(mt.readlines(), mp)
 
723
        
 
724
        
 
725
    def testOneInsert(self):
 
726
        self.doMerge([],
 
727
                     ['aa'],
 
728
                     [],
 
729
                     ['aa'])
 
730
 
 
731
    def testSeparateInserts(self):
 
732
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
733
                     ['aaa', 'xxx', 'bbb', 'ccc'],
 
734
                     ['aaa', 'bbb', 'yyy', 'ccc'],
 
735
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
 
736
 
 
737
    def testSameInsert(self):
 
738
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
739
                     ['aaa', 'xxx', 'bbb', 'ccc'],
 
740
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
 
741
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
 
742
 
 
743
    def testOverlappedInsert(self):
 
744
        self.doMerge(['aaa', 'bbb'],
 
745
                     ['aaa', 'xxx', 'yyy', 'bbb'],
 
746
                     ['aaa', 'xxx', 'bbb'],
 
747
                     ['aaa', '<<<<<<< ', 'xxx', 'yyy', '=======', 'xxx', 
 
748
                      '>>>>>>> ', 'bbb'])
 
749
 
 
750
        # really it ought to reduce this to 
 
751
        # ['aaa', 'xxx', 'yyy', 'bbb']
 
752
 
 
753
 
 
754
    def testClashReplace(self):
 
755
        self.doMerge(['aaa'],
 
756
                     ['xxx'],
 
757
                     ['yyy', 'zzz'],
 
758
                     ['<<<<<<< ', 'xxx', '=======', 'yyy', 'zzz', 
 
759
                      '>>>>>>> '])
 
760
 
 
761
    def testNonClashInsert(self):
 
762
        self.doMerge(['aaa'],
 
763
                     ['xxx', 'aaa'],
 
764
                     ['yyy', 'zzz'],
 
765
                     ['<<<<<<< ', 'xxx', 'aaa', '=======', 'yyy', 'zzz', 
 
766
                      '>>>>>>> '])
 
767
 
 
768
        self.doMerge(['aaa'],
 
769
                     ['aaa'],
 
770
                     ['yyy', 'zzz'],
 
771
                     ['yyy', 'zzz'])
 
772
 
 
773
 
 
774
    def testDeleteAndModify(self):
 
775
        """Clashing delete and modification.
 
776
 
 
777
        If one side modifies a region and the other deletes it then
 
778
        there should be a conflict with one side blank.
 
779
        """
 
780
 
 
781
        #######################################
 
782
        # skippd, not working yet
 
783
        return
 
784
        
 
785
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
786
                     ['aaa', 'ddd', 'ccc'],
 
787
                     ['aaa', 'ccc'],
 
788
                     ['<<<<<<<< ', 'aaa', '=======', '>>>>>>> ', 'ccc'])
 
789
 
 
790
 
651
791
class JoinWeavesTests(TestBase):
652
792
    def setUp(self):
653
793
        super(JoinWeavesTests, self).setUp()
654
794
        self.weave1 = Weave()
655
795
        self.lines1 = ['hello\n']
656
796
        self.lines3 = ['hello\n', 'cruel\n', 'world\n']
657
 
        self.weave1.add_lines('v1', [], self.lines1)
658
 
        self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
659
 
        self.weave1.add_lines('v3', ['v2'], self.lines3)
 
797
        self.weave1.add('v1', [], self.lines1)
 
798
        self.weave1.add('v2', [0], ['hello\n', 'world\n'])
 
799
        self.weave1.add('v3', [1], self.lines3)
 
800
        
 
801
    def test_join_empty(self):
 
802
        """Join two empty weaves."""
 
803
        eq = self.assertEqual
 
804
        w1 = Weave()
 
805
        w2 = Weave()
 
806
        w1.join(w2)
 
807
        eq(w1.numversions(), 0)
 
808
        
 
809
    def test_join_empty_to_nonempty(self):
 
810
        """Join empty weave onto nonempty."""
 
811
        self.weave1.join(Weave())
 
812
        self.assertEqual(len(self.weave1), 3)
 
813
 
 
814
    def test_join_unrelated(self):
 
815
        """Join two weaves with no history in common."""
 
816
        wb = Weave()
 
817
        wb.add('b1', [], ['line from b\n'])
 
818
        w1 = self.weave1
 
819
        w1.join(wb)
 
820
        eq = self.assertEqual
 
821
        eq(len(w1), 4)
 
822
        eq(sorted(list(w1.iter_names())),
 
823
           ['b1', 'v1', 'v2', 'v3'])
 
824
 
 
825
    def test_join_related(self):
 
826
        wa = self.weave1.copy()
 
827
        wb = self.weave1.copy()
 
828
        wa.add('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
 
829
        wb.add('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
 
830
        eq = self.assertEquals
 
831
        eq(len(wa), 4)
 
832
        eq(len(wb), 4)
 
833
        wa.join(wb)
 
834
        eq(len(wa), 5)
 
835
        eq(wa.get_lines('b1'),
 
836
           ['hello\n', 'pale blue\n', 'world\n'])
 
837
 
 
838
    def test_join_parent_disagreement(self):
 
839
        #join reconciles differening parents into a union.
 
840
        wa = Weave()
 
841
        wb = Weave()
 
842
        wa.add('v1', [], ['hello\n'])
 
843
        wb.add('v0', [], [])
 
844
        wb.add('v1', ['v0'], ['hello\n'])
 
845
        wa.join(wb)
 
846
        self.assertEqual(['v0'], wa.get_parents('v1'))
 
847
 
 
848
    def test_join_text_disagreement(self):
 
849
        """Cannot join weaves with different texts for a version."""
 
850
        wa = Weave()
 
851
        wb = Weave()
 
852
        wa.add('v1', [], ['hello\n'])
 
853
        wb.add('v1', [], ['not\n', 'hello\n'])
 
854
        self.assertRaises(WeaveError,
 
855
                          wa.join, wb)
 
856
 
 
857
    def test_join_unordered(self):
 
858
        """Join weaves where indexes differ.
 
859
        
 
860
        The source weave contains a different version at index 0."""
 
861
        wa = self.weave1.copy()
 
862
        wb = Weave()
 
863
        wb.add('x1', [], ['line from x1\n'])
 
864
        wb.add('v1', [], ['hello\n'])
 
865
        wb.add('v2', ['v1'], ['hello\n', 'world\n'])
 
866
        wa.join(wb)
 
867
        eq = self.assertEquals
 
868
        eq(sorted(wa.iter_names()), ['v1', 'v2', 'v3', 'x1',])
 
869
        eq(wa.get_text('x1'), 'line from x1\n')
660
870
 
661
871
    def test_written_detection(self):
662
872
        # Test detection of weave file corruption.
667
877
        from cStringIO import StringIO
668
878
 
669
879
        w = Weave()
670
 
        w.add_lines('v1', [], ['hello\n'])
671
 
        w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
 
880
        w.add('v1', [], ['hello\n'])
 
881
        w.add('v2', ['v1'], ['hello\n', 'there\n'])
672
882
 
673
883
        tmpf = StringIO()
674
884
        write_weave(w, tmpf)
691
901
        self.assertEqual('hello\n', w.get_text('v1'))
692
902
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
693
903
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
 
904
        self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
694
905
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
695
906
 
696
907
        # Change the sha checksum
704
915
        self.assertEqual('hello\n', w.get_text('v1'))
705
916
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
706
917
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
 
918
        self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
707
919
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
708
920
 
709
921
 
710
 
class TestWeave(TestCase):
711
 
 
712
 
    def test_allow_reserved_false(self):
713
 
        w = Weave('name', allow_reserved=False)
714
 
        # Add lines is checked at the WeaveFile level, not at the Weave level
715
 
        w.add_lines('name:', [], TEXT_1)
716
 
        # But get_lines is checked at this level
717
 
        self.assertRaises(errors.ReservedId, w.get_lines, 'name:')
718
 
 
719
 
    def test_allow_reserved_true(self):
720
 
        w = Weave('name', allow_reserved=True)
721
 
        w.add_lines('name:', [], TEXT_1)
722
 
        self.assertEqual(TEXT_1, w.get_lines('name:'))
723
 
 
724
 
 
725
922
class InstrumentedWeave(Weave):
726
923
    """Keep track of how many times functions are called."""
727
 
 
 
924
    
728
925
    def __init__(self, weave_name=None):
729
926
        self._extract_count = 0
730
927
        Weave.__init__(self, weave_name=weave_name)
734
931
        return Weave._extract(self, versions)
735
932
 
736
933
 
737
 
class TestNeedsReweave(TestCase):
738
 
    """Internal corner cases for when reweave is needed."""
739
 
 
740
 
    def test_compatible_parents(self):
 
934
class JoinOptimization(TestCase):
 
935
    """Test that Weave.join() doesn't extract all texts, only what must be done."""
 
936
 
 
937
    def test_join(self):
 
938
        w1 = InstrumentedWeave()
 
939
        w2 = InstrumentedWeave()
 
940
 
 
941
        txt0 = ['a\n']
 
942
        txt1 = ['a\n', 'b\n']
 
943
        txt2 = ['a\n', 'c\n']
 
944
        txt3 = ['a\n', 'b\n', 'c\n']
 
945
 
 
946
        w1.add('txt0', [], txt0) # extract 1a
 
947
        w2.add('txt0', [], txt0) # extract 1b
 
948
        w1.add('txt1', [0], txt1)# extract 2a
 
949
        w2.add('txt2', [0], txt2)# extract 2b
 
950
        w1.join(w2) # extract 3a to add txt2 
 
951
        w2.join(w1) # extract 3b to add txt1 
 
952
 
 
953
        w1.add('txt3', [1, 2], txt3) # extract 4a 
 
954
        w2.add('txt3', [1, 2], txt3) # extract 4b
 
955
        # These secretly have inverted parents
 
956
 
 
957
        # This should not have to do any extractions
 
958
        w1.join(w2) # NO extract, texts already present with same parents
 
959
        w2.join(w1) # NO extract, texts already present with same parents
 
960
 
 
961
        self.assertEqual(4, w1._extract_count)
 
962
        self.assertEqual(4, w2._extract_count)
 
963
 
 
964
    def test_double_parent(self):
 
965
        # It should not be considered illegal to add
 
966
        # a revision with the same parent twice
 
967
        w1 = InstrumentedWeave()
 
968
        w2 = InstrumentedWeave()
 
969
 
 
970
        txt0 = ['a\n']
 
971
        txt1 = ['a\n', 'b\n']
 
972
        txt2 = ['a\n', 'c\n']
 
973
        txt3 = ['a\n', 'b\n', 'c\n']
 
974
 
 
975
        w1.add('txt0', [], txt0)
 
976
        w2.add('txt0', [], txt0)
 
977
        w1.add('txt1', [0], txt1)
 
978
        w2.add('txt1', [0,0], txt1)
 
979
        # Same text, effectively the same, because the
 
980
        # parent is only repeated
 
981
        w1.join(w2) # extract 3a to add txt2 
 
982
        w2.join(w1) # extract 3b to add txt1 
 
983
 
 
984
 
 
985
class MismatchedTexts(TestCase):
 
986
    """Test that merging two weaves with different texts fails."""
 
987
 
 
988
    def test_reweave(self):
741
989
        w1 = Weave('a')
742
 
        my_parents = set([1, 2, 3])
743
 
        # subsets are ok
744
 
        self.assertTrue(w1._compatible_parents(my_parents, set([3])))
745
 
        # same sets
746
 
        self.assertTrue(w1._compatible_parents(my_parents, set(my_parents)))
747
 
        # same empty corner case
748
 
        self.assertTrue(w1._compatible_parents(set(), set()))
749
 
        # other cannot contain stuff my_parents does not
750
 
        self.assertFalse(w1._compatible_parents(set(), set([1])))
751
 
        self.assertFalse(w1._compatible_parents(my_parents, set([1, 2, 3, 4])))
752
 
        self.assertFalse(w1._compatible_parents(my_parents, set([4])))
753
 
 
754
 
 
755
 
class TestWeaveFile(TestCaseInTempDir):
756
 
 
757
 
    def test_empty_file(self):
758
 
        f = open('empty.weave', 'wb+')
759
 
        try:
760
 
            self.assertRaises(errors.WeaveFormatError,
761
 
                              read_weave, f)
762
 
        finally:
763
 
            f.close()
 
990
        w2 = Weave('b')
 
991
 
 
992
        w1.add('txt0', [], ['a\n'])
 
993
        w2.add('txt0', [], ['a\n'])
 
994
        w1.add('txt1', [0], ['a\n', 'b\n'])
 
995
        w2.add('txt1', [0], ['a\n', 'c\n'])
 
996
 
 
997
        self.assertRaises(errors.WeaveTextDiffers, w1.reweave, w2)
 
998
 
 
999