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
22
"""test suite for weave algorithm"""
26
from bzrlib.weave import Weave, WeaveFormatError
27
from bzrlib.weavefile import write_weave, read_weave
28
from pprint import pformat
29
from bzrlib.intset import IntSet
36
from sets import Set, ImmutableSet
38
frozenset = ImmutableSet
43
# texts for use in testing
44
TEXT_0 = ["Hello world"]
45
TEXT_1 = ["Hello world",
50
class TestBase(testsweet.TestBase):
51
def check_read_write(self, k):
52
"""Check the weave k can be written & re-read."""
53
from tempfile import TemporaryFile
62
self.log('serialized weave:')
64
self.fail('read/write check failed')
74
class StoreText(TestBase):
75
"""Store and retrieve a simple text."""
78
idx = k.add([], TEXT_0)
79
self.assertEqual(k.get(idx), TEXT_0)
80
self.assertEqual(idx, 0)
84
class AnnotateOne(TestBase):
88
self.assertEqual(k.annotate(0),
92
class StoreTwo(TestBase):
96
idx = k.add([], TEXT_0)
97
self.assertEqual(idx, 0)
99
idx = k.add([], TEXT_1)
100
self.assertEqual(idx, 1)
102
self.assertEqual(k.get(0), TEXT_0)
103
self.assertEqual(k.get(1), TEXT_1)
105
k.dump(self.TEST_LOG)
109
class InvalidAdd(TestBase):
110
"""Try to use invalid version number during add."""
114
self.assertRaises(ValueError,
120
class InsertLines(TestBase):
121
"""Store a revision that adds one line to the original.
123
Look at the annotations to make sure that the first line is matched
124
and not stored repeatedly."""
128
k.add([], ['line 1'])
129
k.add([0], ['line 1', 'line 2'])
131
self.assertEqual(k.annotate(0),
134
self.assertEqual(k.get(1),
138
self.assertEqual(k.annotate(1),
142
k.add([0], ['line 1', 'diverged line'])
144
self.assertEqual(k.annotate(2),
146
(2, 'diverged line')])
148
text3 = ['line 1', 'middle line', 'line 2']
152
self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
154
self.log("k._l=" + pformat(k._l))
156
self.assertEqual(k.annotate(3),
161
# now multiple insertions at different places
163
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
165
self.assertEqual(k.annotate(4),
175
class DeleteLines(TestBase):
176
"""Deletion of lines from existing text.
178
Try various texts all based on a common ancestor."""
182
base_text = ['one', 'two', 'three', 'four']
186
texts = [['one', 'two', 'three'],
187
['two', 'three', 'four'],
189
['one', 'two', 'three', 'four'],
195
self.log('final weave:')
196
self.log('k._l=' + pformat(k._l))
198
for i in range(len(texts)):
199
self.assertEqual(k.get(i+1),
205
class SuicideDelete(TestBase):
206
"""Invalid weave which tries to add and delete simultaneously."""
219
################################### SKIPPED
220
# Weave.get doesn't trap this anymore
223
self.assertRaises(WeaveFormatError,
229
class CannedDelete(TestBase):
230
"""Unpack canned weave with deleted lines."""
240
'line to be deleted',
246
self.assertEqual(k.get(0),
248
'line to be deleted',
252
self.assertEqual(k.get(1),
259
class CannedReplacement(TestBase):
260
"""Unpack canned weave with deleted lines."""
270
'line to be deleted',
279
self.assertEqual(k.get(0),
281
'line to be deleted',
285
self.assertEqual(k.get(1),
293
class BadWeave(TestBase):
294
"""Test that we trap an insert which should not occur."""
304
' added in version 1',
313
################################### SKIPPED
314
# Weave.get doesn't trap this anymore
318
self.assertRaises(WeaveFormatError,
323
class BadInsert(TestBase):
324
"""Test that we trap an insert which should not occur."""
336
' added in version 1',
344
# this is not currently enforced by get
345
return ##########################################
347
self.assertRaises(WeaveFormatError,
351
self.assertRaises(WeaveFormatError,
356
class InsertNested(TestBase):
357
"""Insertion with nested instructions."""
369
' added in version 1',
378
self.assertEqual(k.get(0),
382
self.assertEqual(k.get(1),
384
' added in version 1',
388
self.assertEqual(k.get(2),
393
self.assertEqual(k.get(3),
395
' added in version 1',
402
class DeleteLines2(TestBase):
403
"""Test recording revisions that delete lines.
405
This relies on the weave having a way to represent lines knocked
406
out by a later revision."""
410
k.add([], ["line the first",
415
self.assertEqual(len(k.get(0)), 4)
417
k.add([0], ["line the first",
420
self.assertEqual(k.get(1),
424
self.assertEqual(k.annotate(1),
425
[(0, "line the first"),
430
class IncludeVersions(TestBase):
431
"""Check texts that are stored across multiple revisions.
433
Here we manually create a weave with particular encoding and make
434
sure it unpacks properly.
436
Text 0 includes nothing; text 1 includes text 0 and adds some
443
k._v = [frozenset(), frozenset([0])]
451
self.assertEqual(k.get(1),
455
self.assertEqual(k.get(0),
458
k.dump(self.TEST_LOG)
461
class DivergedIncludes(TestBase):
462
"""Weave with two diverged texts based on version 0.
478
"alternative second line",
482
self.assertEqual(k.get(0),
485
self.assertEqual(k.get(1),
489
self.assertEqual(k.get(2),
491
"alternative second line"])
493
self.assertEqual(list(k.inclusions([2])),
498
class ReplaceLine(TestBase):
502
text0 = ['cheddar', 'stilton', 'gruyere']
503
text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
508
self.log('k._l=' + pformat(k._l))
510
self.assertEqual(k.get(0), text0)
511
self.assertEqual(k.get(1), text1)
515
class Merge(TestBase):
516
"""Storage of versions that merge diverged parents"""
521
['header', '', 'line from 1'],
522
['header', '', 'line from 2', 'more from 2'],
523
['header', '', 'line from 1', 'fixup line', 'line from 2'],
529
k.add([0, 1, 2], texts[3])
531
for i, t in enumerate(texts):
532
self.assertEqual(k.get(i), t)
534
self.assertEqual(k.annotate(3),
542
self.assertEqual(list(k.inclusions([3])),
545
self.log('k._l=' + pformat(k._l))
547
self.check_read_write(k)
550
class Conflicts(TestBase):
551
"""Test detection of conflicting regions during a merge.
553
A base version is inserted, then two descendents try to
554
insert different lines in the same place. These should be
555
reported as a possible conflict and forwarded to the user."""
560
k.add([], ['aaa', 'bbb'])
561
k.add([0], ['aaa', '111', 'bbb'])
562
k.add([1], ['aaa', '222', 'bbb'])
564
merged = k.merge([1, 2])
566
self.assertEquals([[['aaa']],
572
class NonConflict(TestBase):
573
"""Two descendants insert compatible changes.
575
No conflict should be reported."""
580
k.add([], ['aaa', 'bbb'])
581
k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
582
k.add([1], ['aaa', 'ccc', 'bbb', '222'])
588
class AutoMerge(TestBase):
592
texts = [['header', 'aaa', 'bbb'],
593
['header', 'aaa', 'line from 1', 'bbb'],
594
['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
601
self.log('k._l=' + pformat(k._l))
603
m = list(k.mash_iter([0, 1, 2]))
609
'line from 2', 'more from 2'])
613
class Khayyam(TestBase):
614
"""Test changes to multi-line texts, and read/write"""
617
"""A Book of Verses underneath the Bough,
618
A Jug of Wine, a Loaf of Bread, -- and Thou
619
Beside me singing in the Wilderness --
620
Oh, Wilderness were Paradise enow!""",
622
"""A Book of Verses underneath the Bough,
623
A Jug of Wine, a Loaf of Bread, -- and Thou
624
Beside me singing in the Wilderness --
625
Oh, Wilderness were Paradise now!""",
627
"""A Book of poems underneath the tree,
628
A Jug of Wine, a Loaf of Bread,
630
Beside me singing in the Wilderness --
631
Oh, Wilderness were Paradise now!
635
"""A Book of Verses underneath the Bough,
636
A Jug of Wine, a Loaf of Bread,
638
Beside me singing in the Wilderness --
639
Oh, Wilderness were Paradise now!""",
641
texts = [[l.strip() for l in t.split('\n')] for t in rawtexts]
646
ver = k.add(list(parents), t)
649
self.log("k._l=" + pformat(k._l))
651
for i, t in enumerate(texts):
652
self.assertEqual(k.get(i), t)
654
self.check_read_write(k)
658
class MergeCases(TestBase):
659
def doMerge(self, base, a, b, mp):
660
from cStringIO import StringIO
661
from textwrap import dedent
667
w.add([], map(addcrlf, base))
668
w.add([0], map(addcrlf, a))
669
w.add([0], map(addcrlf, b))
671
self.log('weave is:')
674
self.log(tmpf.getvalue())
676
self.log('merge plan:')
677
p = list(w.plan_merge(1, 2))
678
for state, line in p:
680
self.log('%12s | %s' % (state, line[:-1]))
684
mt.writelines(w.weave_merge(p))
686
self.log(mt.getvalue())
688
mp = map(addcrlf, mp)
689
self.assertEqual(mt.readlines(), mp)
692
def testOneInsert(self):
698
def testSeparateInserts(self):
699
self.doMerge(['aaa', 'bbb', 'ccc'],
700
['aaa', 'xxx', 'bbb', 'ccc'],
701
['aaa', 'bbb', 'yyy', 'ccc'],
702
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
704
def testSameInsert(self):
705
self.doMerge(['aaa', 'bbb', 'ccc'],
706
['aaa', 'xxx', 'bbb', 'ccc'],
707
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
708
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
710
def testOverlappedInsert(self):
711
self.doMerge(['aaa', 'bbb'],
712
['aaa', 'xxx', 'yyy', 'bbb'],
713
['aaa', 'xxx', 'bbb'],
714
['aaa', '<<<<', 'xxx', 'yyy', '====', 'xxx', '>>>>', 'bbb'])
716
# really it ought to reduce this to
717
# ['aaa', 'xxx', 'yyy', 'bbb']
720
def testClashReplace(self):
721
self.doMerge(['aaa'],
724
['<<<<', 'xxx', '====', 'yyy', 'zzz', '>>>>'])
726
def testNonClashInsert(self):
727
self.doMerge(['aaa'],
730
['<<<<', 'xxx', 'aaa', '====', 'yyy', 'zzz', '>>>>'])
732
self.doMerge(['aaa'],
741
from unittest import TestSuite, TestLoader
746
suite.addTest(tl.loadTestsFromModule(testweave))
748
return int(not testsweet.run_suite(suite)) # for shell 0=true
751
if __name__ == '__main__':
753
sys.exit(testweave())