~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tools/testweave.py

merge merge tweaks from aaron, which includes latest .dev

Show diffs side-by-side

added added

removed removed

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