3
# Copyright (C) 2005 by Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
# TODO: tests regarding version names
24
"""test suite for weave algorithm"""
28
from bzrlib.weave import Weave, WeaveFormatError
29
from bzrlib.weavefile import write_weave, read_weave
30
from bzrlib.selftest import TestCase
31
from pprint import pformat
38
from sets import Set, ImmutableSet
40
frozenset = ImmutableSet
45
# texts for use in testing
46
TEXT_0 = ["Hello world"]
47
TEXT_1 = ["Hello world",
52
class TestBase(TestCase):
53
def check_read_write(self, k):
54
"""Check the weave k can be written & re-read."""
55
from tempfile import TemporaryFile
64
self.log('serialized weave:')
68
self.log('parents: %s' % (k._parents == k2._parents))
69
self.log(' %r' % k._parents)
70
self.log(' %r' % k2._parents)
74
self.fail('read/write check failed')
84
class StoreText(TestBase):
85
"""Store and retrieve a simple text."""
88
idx = k.add('text0', [], TEXT_0)
89
self.assertEqual(k.get(idx), TEXT_0)
90
self.assertEqual(idx, 0)
94
class AnnotateOne(TestBase):
97
k.add('text0', [], TEXT_0)
98
self.assertEqual(k.annotate(0),
102
class StoreTwo(TestBase):
106
idx = k.add('text0', [], TEXT_0)
107
self.assertEqual(idx, 0)
109
idx = k.add('text1', [], TEXT_1)
110
self.assertEqual(idx, 1)
112
self.assertEqual(k.get(0), TEXT_0)
113
self.assertEqual(k.get(1), TEXT_1)
117
class InvalidAdd(TestBase):
118
"""Try to use invalid version number during add."""
122
self.assertRaises(IndexError,
129
class InsertLines(TestBase):
130
"""Store a revision that adds one line to the original.
132
Look at the annotations to make sure that the first line is matched
133
and not stored repeatedly."""
137
k.add('text0', [], ['line 1'])
138
k.add('text1', [0], ['line 1', 'line 2'])
140
self.assertEqual(k.annotate(0),
143
self.assertEqual(k.get(1),
147
self.assertEqual(k.annotate(1),
151
k.add('text2', [0], ['line 1', 'diverged line'])
153
self.assertEqual(k.annotate(2),
155
(2, 'diverged line')])
157
text3 = ['line 1', 'middle line', 'line 2']
162
# self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
164
self.log("k._weave=" + pformat(k._weave))
166
self.assertEqual(k.annotate(3),
171
# now multiple insertions at different places
174
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
176
self.assertEqual(k.annotate(4),
186
class DeleteLines(TestBase):
187
"""Deletion of lines from existing text.
189
Try various texts all based on a common ancestor."""
193
base_text = ['one', 'two', 'three', 'four']
195
k.add('text0', [], base_text)
197
texts = [['one', 'two', 'three'],
198
['two', 'three', 'four'],
200
['one', 'two', 'three', 'four'],
205
ver = k.add('text%d' % i,
209
self.log('final weave:')
210
self.log('k._weave=' + pformat(k._weave))
212
for i in range(len(texts)):
213
self.assertEqual(k.get(i+1),
219
class SuicideDelete(TestBase):
220
"""Invalid weave which tries to add and delete simultaneously."""
226
k._weave = [('{', 0),
233
################################### SKIPPED
234
# Weave.get doesn't trap this anymore
237
self.assertRaises(WeaveFormatError,
243
class CannedDelete(TestBase):
244
"""Unpack canned weave with deleted lines."""
251
k._weave = [('{', 0),
254
'line to be deleted',
260
self.assertEqual(k.get(0),
262
'line to be deleted',
266
self.assertEqual(k.get(1),
273
class CannedReplacement(TestBase):
274
"""Unpack canned weave with deleted lines."""
278
k._parents = [frozenset(),
281
k._weave = [('{', 0),
284
'line to be deleted',
293
self.assertEqual(k.get(0),
295
'line to be deleted',
299
self.assertEqual(k.get(1),
307
class BadWeave(TestBase):
308
"""Test that we trap an insert which should not occur."""
312
k._parents = [frozenset(),
314
k._weave = ['bad line',
318
' added in version 1',
327
################################### SKIPPED
328
# Weave.get doesn't trap this anymore
332
self.assertRaises(WeaveFormatError,
337
class BadInsert(TestBase):
338
"""Test that we trap an insert which should not occur."""
342
k._parents = [frozenset(),
347
k._weave = [('{', 0),
350
' added in version 1',
358
# this is not currently enforced by get
359
return ##########################################
361
self.assertRaises(WeaveFormatError,
365
self.assertRaises(WeaveFormatError,
370
class InsertNested(TestBase):
371
"""Insertion with nested instructions."""
375
k._parents = [frozenset(),
380
k._weave = [('{', 0),
383
' added in version 1',
392
self.assertEqual(k.get(0),
396
self.assertEqual(k.get(1),
398
' added in version 1',
402
self.assertEqual(k.get(2),
407
self.assertEqual(k.get(3),
409
' added in version 1',
416
class DeleteLines2(TestBase):
417
"""Test recording revisions that delete lines.
419
This relies on the weave having a way to represent lines knocked
420
out by a later revision."""
424
k.add('text0', [], ["line the first",
429
self.assertEqual(len(k.get(0)), 4)
431
k.add('text1', [0], ["line the first",
434
self.assertEqual(k.get(1),
438
self.assertEqual(k.annotate(1),
439
[(0, "line the first"),
444
class IncludeVersions(TestBase):
445
"""Check texts that are stored across multiple revisions.
447
Here we manually create a weave with particular encoding and make
448
sure it unpacks properly.
450
Text 0 includes nothing; text 1 includes text 0 and adds some
457
k._parents = [frozenset(), frozenset([0])]
458
k._weave = [('{', 0),
465
self.assertEqual(k.get(1),
469
self.assertEqual(k.get(0),
473
class DivergedIncludes(TestBase):
474
"""Weave with two diverged texts based on version 0.
479
k._parents = [frozenset(),
483
k._weave = [('{', 0),
490
"alternative second line",
494
self.assertEqual(k.get(0),
497
self.assertEqual(k.get(1),
501
self.assertEqual(k.get(2),
503
"alternative second line"])
505
self.assertEqual(list(k.inclusions([2])),
510
class ReplaceLine(TestBase):
514
text0 = ['cheddar', 'stilton', 'gruyere']
515
text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
517
k.add('text0', [], text0)
518
k.add('text1', [0], text1)
520
self.log('k._weave=' + pformat(k._weave))
522
self.assertEqual(k.get(0), text0)
523
self.assertEqual(k.get(1), text1)
527
class Merge(TestBase):
528
"""Storage of versions that merge diverged parents"""
533
['header', '', 'line from 1'],
534
['header', '', 'line from 2', 'more from 2'],
535
['header', '', 'line from 1', 'fixup line', 'line from 2'],
538
k.add('text0', [], texts[0])
539
k.add('text1', [0], texts[1])
540
k.add('text2', [0], texts[2])
541
k.add('merge', [0, 1, 2], texts[3])
543
for i, t in enumerate(texts):
544
self.assertEqual(k.get(i), t)
546
self.assertEqual(k.annotate(3),
554
self.assertEqual(list(k.inclusions([3])),
557
self.log('k._weave=' + pformat(k._weave))
559
self.check_read_write(k)
562
class Conflicts(TestBase):
563
"""Test detection of conflicting regions during a merge.
565
A base version is inserted, then two descendents try to
566
insert different lines in the same place. These should be
567
reported as a possible conflict and forwarded to the user."""
572
k.add([], ['aaa', 'bbb'])
573
k.add([0], ['aaa', '111', 'bbb'])
574
k.add([1], ['aaa', '222', 'bbb'])
576
merged = k.merge([1, 2])
578
self.assertEquals([[['aaa']],
584
class NonConflict(TestBase):
585
"""Two descendants insert compatible changes.
587
No conflict should be reported."""
592
k.add([], ['aaa', 'bbb'])
593
k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
594
k.add([1], ['aaa', 'ccc', 'bbb', '222'])
600
class AutoMerge(TestBase):
604
texts = [['header', 'aaa', 'bbb'],
605
['header', 'aaa', 'line from 1', 'bbb'],
606
['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
609
k.add('text0', [], texts[0])
610
k.add('text1', [0], texts[1])
611
k.add('text2', [0], texts[2])
613
self.log('k._weave=' + pformat(k._weave))
615
m = list(k.mash_iter([0, 1, 2]))
621
'line from 2', 'more from 2'])
625
class Khayyam(TestBase):
626
"""Test changes to multi-line texts, and read/write"""
629
"""A Book of Verses underneath the Bough,
630
A Jug of Wine, a Loaf of Bread, -- and Thou
631
Beside me singing in the Wilderness --
632
Oh, Wilderness were Paradise enow!""",
634
"""A Book of Verses underneath the Bough,
635
A Jug of Wine, a Loaf of Bread, -- and Thou
636
Beside me singing in the Wilderness --
637
Oh, Wilderness were Paradise now!""",
639
"""A Book of poems underneath the tree,
640
A Jug of Wine, a Loaf of Bread,
642
Beside me singing in the Wilderness --
643
Oh, Wilderness were Paradise now!
647
"""A Book of Verses underneath the Bough,
648
A Jug of Wine, a Loaf of Bread,
650
Beside me singing in the Wilderness --
651
Oh, Wilderness were Paradise now!""",
653
texts = [[l.strip() for l in t.split('\n')] for t in rawtexts]
659
ver = k.add('text%d' % i,
664
self.log("k._weave=" + pformat(k._weave))
666
for i, t in enumerate(texts):
667
self.assertEqual(k.get(i), t)
669
self.check_read_write(k)
673
class MergeCases(TestBase):
674
def doMerge(self, base, a, b, mp):
675
from cStringIO import StringIO
676
from textwrap import dedent
682
w.add('text0', [], map(addcrlf, base))
683
w.add('text1', [0], map(addcrlf, a))
684
w.add('text2', [0], map(addcrlf, b))
686
self.log('weave is:')
689
self.log(tmpf.getvalue())
691
self.log('merge plan:')
692
p = list(w.plan_merge(1, 2))
693
for state, line in p:
695
self.log('%12s | %s' % (state, line[:-1]))
699
mt.writelines(w.weave_merge(p))
701
self.log(mt.getvalue())
703
mp = map(addcrlf, mp)
704
self.assertEqual(mt.readlines(), mp)
707
def testOneInsert(self):
713
def testSeparateInserts(self):
714
self.doMerge(['aaa', 'bbb', 'ccc'],
715
['aaa', 'xxx', 'bbb', 'ccc'],
716
['aaa', 'bbb', 'yyy', 'ccc'],
717
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
719
def testSameInsert(self):
720
self.doMerge(['aaa', 'bbb', 'ccc'],
721
['aaa', 'xxx', 'bbb', 'ccc'],
722
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
723
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
725
def testOverlappedInsert(self):
726
self.doMerge(['aaa', 'bbb'],
727
['aaa', 'xxx', 'yyy', 'bbb'],
728
['aaa', 'xxx', 'bbb'],
729
['aaa', '<<<<', 'xxx', 'yyy', '====', 'xxx', '>>>>', 'bbb'])
731
# really it ought to reduce this to
732
# ['aaa', 'xxx', 'yyy', 'bbb']
735
def testClashReplace(self):
736
self.doMerge(['aaa'],
739
['<<<<', 'xxx', '====', 'yyy', 'zzz', '>>>>'])
741
def testNonClashInsert(self):
742
self.doMerge(['aaa'],
745
['<<<<', 'xxx', 'aaa', '====', 'yyy', 'zzz', '>>>>'])
747
self.doMerge(['aaa'],
753
def testDeleteAndModify(self):
754
"""Clashing delete and modification.
756
If one side modifies a region and the other deletes it then
757
there should be a conflict with one side blank.
760
#######################################
761
# skippd, not working yet
764
self.doMerge(['aaa', 'bbb', 'ccc'],
765
['aaa', 'ddd', 'ccc'],
767
['<<<<', 'aaa', '====', '>>>>', 'ccc'])
771
if __name__ == '__main__':
774
sys.exit(unittest.main())