20
20
# TODO: tests regarding version names
21
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
24
24
"""test suite for weave algorithm"""
26
from pprint import pformat
28
from bzrlib.weave import Weave, WeaveFormatError
28
import bzrlib.errors as errors
29
from bzrlib.weave import Weave, WeaveFormatError, WeaveError, reweave
29
30
from bzrlib.weavefile import write_weave, read_weave
30
from pprint import pformat
37
from sets import Set, ImmutableSet
39
frozenset = ImmutableSet
31
from bzrlib.tests import TestCase
32
from bzrlib.osutils import sha_string
44
35
# texts for use in testing
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
130
161
class InsertLines(TestBase):
131
162
"""Store a revision that adds one line to the original.
729
758
self.doMerge(['aaa', 'bbb'],
730
759
['aaa', 'xxx', 'yyy', 'bbb'],
731
760
['aaa', 'xxx', 'bbb'],
732
['aaa', '<<<<', 'xxx', 'yyy', '====', 'xxx', '>>>>', 'bbb'])
761
['aaa', '<<<<<<<', 'xxx', 'yyy', '=======', 'xxx',
734
764
# really it ought to reduce this to
735
765
# ['aaa', 'xxx', 'yyy', 'bbb']
739
769
self.doMerge(['aaa'],
742
['<<<<', 'xxx', '====', 'yyy', 'zzz', '>>>>'])
772
['<<<<<<<', 'xxx', '=======', 'yyy', 'zzz',
744
775
def testNonClashInsert(self):
745
776
self.doMerge(['aaa'],
748
['<<<<', 'xxx', 'aaa', '====', 'yyy', 'zzz', '>>>>'])
779
['<<<<<<<', 'xxx', 'aaa', '=======', 'yyy', 'zzz',
750
782
self.doMerge(['aaa'],
767
799
self.doMerge(['aaa', 'bbb', 'ccc'],
768
800
['aaa', 'ddd', 'ccc'],
770
['<<<<', 'aaa', '====', '>>>>', 'ccc'])
776
from unittest import TestSuite, TestLoader
781
suite.addTest(tl.loadTestsFromModule(testweave))
783
return int(not testsweet.run_suite(suite)) # for shell 0=true
786
if __name__ == '__main__':
788
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')