17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
# TODO: tests regarding version names
21
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
22
24
"""test suite for weave algorithm"""
26
from pprint import pformat
26
from bzrlib.weave import Weave, WeaveFormatError
28
import bzrlib.errors as errors
29
from bzrlib.weave import Weave, WeaveFormatError, WeaveError, reweave
27
30
from bzrlib.weavefile import write_weave, read_weave
28
from pprint import pformat
35
from sets import Set, ImmutableSet
37
frozenset = ImmutableSet
31
from bzrlib.tests import TestCase
32
from bzrlib.osutils import sha_string
42
35
# texts for use in testing
113
127
self.assertRaises(IndexError,
134
class RepeatedAdd(TestBase):
135
"""Add the same version twice; harmless."""
138
idx = k.add('text0', [], TEXT_0)
139
idx2 = k.add('text0', [], TEXT_0)
140
self.assertEqual(idx, idx2)
144
class InvalidRepeatedAdd(TestBase):
147
idx = k.add('text0', [], TEXT_0)
148
self.assertRaises(WeaveError,
152
['not the same text'])
153
self.assertRaises(WeaveError,
156
[12], # not the right parents
119
161
class InsertLines(TestBase):
120
162
"""Store a revision that adds one line to the original.
522
567
['header', '', 'line from 1', 'fixup line', 'line from 2'],
528
k.add([0, 1, 2], texts[3])
570
k.add('text0', [], texts[0])
571
k.add('text1', [0], texts[1])
572
k.add('text2', [0], texts[2])
573
k.add('merge', [0, 1, 2], texts[3])
530
575
for i, t in enumerate(texts):
531
576
self.assertEqual(k.get(i), t)
666
w.add([], map(addcrlf, base))
667
w.add([0], map(addcrlf, a))
668
w.add([0], map(addcrlf, b))
714
w.add('text0', [], map(addcrlf, base))
715
w.add('text1', [0], map(addcrlf, a))
716
w.add('text2', [0], map(addcrlf, b))
670
718
self.log('weave is:')
671
719
tmpf = StringIO()
710
758
self.doMerge(['aaa', 'bbb'],
711
759
['aaa', 'xxx', 'yyy', 'bbb'],
712
760
['aaa', 'xxx', 'bbb'],
713
['aaa', '<<<<', 'xxx', 'yyy', '====', 'xxx', '>>>>', 'bbb'])
761
['aaa', '<<<<<<<', 'xxx', 'yyy', '=======', 'xxx',
715
764
# really it ought to reduce this to
716
765
# ['aaa', 'xxx', 'yyy', 'bbb']
720
769
self.doMerge(['aaa'],
723
['<<<<', 'xxx', '====', 'yyy', 'zzz', '>>>>'])
772
['<<<<<<<', 'xxx', '=======', 'yyy', 'zzz',
725
775
def testNonClashInsert(self):
726
776
self.doMerge(['aaa'],
729
['<<<<', 'xxx', 'aaa', '====', 'yyy', 'zzz', '>>>>'])
779
['<<<<<<<', 'xxx', 'aaa', '=======', 'yyy', 'zzz',
731
782
self.doMerge(['aaa'],
748
799
self.doMerge(['aaa', 'bbb', 'ccc'],
749
800
['aaa', 'ddd', 'ccc'],
751
['<<<<', 'aaa', '====', '>>>>', 'ccc'])
757
from unittest import TestSuite, TestLoader
762
suite.addTest(tl.loadTestsFromModule(testweave))
764
return int(not testsweet.run_suite(suite)) # for shell 0=true
767
if __name__ == '__main__':
769
sys.exit(testweave())
802
['<<<<<<<<', 'aaa', '=======', '>>>>>>>', 'ccc'])
805
class JoinWeavesTests(TestBase):
807
super(JoinWeavesTests, self).setUp()
808
self.weave1 = Weave()
809
self.lines1 = ['hello\n']
810
self.lines3 = ['hello\n', 'cruel\n', 'world\n']
811
self.weave1.add('v1', [], self.lines1)
812
self.weave1.add('v2', [0], ['hello\n', 'world\n'])
813
self.weave1.add('v3', [1], self.lines3)
815
def test_join_empty(self):
816
"""Join two empty weaves."""
817
eq = self.assertEqual
821
eq(w1.numversions(), 0)
823
def test_join_empty_to_nonempty(self):
824
"""Join empty weave onto nonempty."""
825
self.weave1.join(Weave())
826
self.assertEqual(len(self.weave1), 3)
828
def test_join_unrelated(self):
829
"""Join two weaves with no history in common."""
831
wb.add('b1', [], ['line from b\n'])
834
eq = self.assertEqual
836
eq(sorted(list(w1.iter_names())),
837
['b1', 'v1', 'v2', 'v3'])
839
def test_join_related(self):
840
wa = self.weave1.copy()
841
wb = self.weave1.copy()
842
wa.add('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
843
wb.add('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
844
eq = self.assertEquals
849
eq(wa.get_lines('b1'),
850
['hello\n', 'pale blue\n', 'world\n'])
852
def test_join_parent_disagreement(self):
853
"""Cannot join weaves with different parents for a version."""
856
wa.add('v1', [], ['hello\n'])
858
wb.add('v1', ['v0'], ['hello\n'])
859
self.assertRaises(WeaveError,
862
def test_join_text_disagreement(self):
863
"""Cannot join weaves with different texts for a version."""
866
wa.add('v1', [], ['hello\n'])
867
wb.add('v1', [], ['not\n', 'hello\n'])
868
self.assertRaises(WeaveError,
871
def test_join_unordered(self):
872
"""Join weaves where indexes differ.
874
The source weave contains a different version at index 0."""
875
wa = self.weave1.copy()
877
wb.add('x1', [], ['line from x1\n'])
878
wb.add('v1', [], ['hello\n'])
879
wb.add('v2', ['v1'], ['hello\n', 'world\n'])
881
eq = self.assertEquals
882
eq(sorted(wa.iter_names()), ['v1', 'v2', 'v3', 'x1',])
883
eq(wa.get_text('x1'), 'line from x1\n')