166
131
def runTest(self):
169
k.add('text0', [], ['line 1'])
170
k.add('text1', [0], ['line 1', 'line 2'])
172
self.assertEqual(k.annotate(0),
175
self.assertEqual(k.get(1),
134
k.add_lines('text0', [], ['line 1'])
135
k.add_lines('text1', ['text0'], ['line 1', 'line 2'])
137
self.assertEqual(k.annotate('text0'),
138
[('text0', 'line 1')])
140
self.assertEqual(k.get_lines(1),
179
self.assertEqual(k.annotate(1),
183
k.add('text2', [0], ['line 1', 'diverged line'])
185
self.assertEqual(k.annotate(2),
187
(2, 'diverged line')])
144
self.assertEqual(k.annotate('text1'),
145
[('text0', 'line 1'),
146
('text1', 'line 2')])
148
k.add_lines('text2', ['text0'], ['line 1', 'diverged line'])
150
self.assertEqual(k.annotate('text2'),
151
[('text0', 'line 1'),
152
('text2', 'diverged line')])
189
154
text3 = ['line 1', 'middle line', 'line 2']
194
159
# self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
196
161
self.log("k._weave=" + pformat(k._weave))
198
self.assertEqual(k.annotate(3),
163
self.assertEqual(k.annotate('text3'),
164
[('text0', 'line 1'),
165
('text3', 'middle line'),
166
('text1', 'line 2')])
203
168
# now multiple insertions at different places
170
['text0', 'text1', 'text3'],
206
171
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
208
self.assertEqual(k.annotate(4),
173
self.assertEqual(k.annotate('text4'),
174
[('text0', 'line 1'),
176
('text3', 'middle line'),
218
182
class DeleteLines(TestBase):
624
k.add([], ['aaa', 'bbb'])
625
k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
626
k.add([1], ['aaa', 'ccc', 'bbb', '222'])
632
class AutoMerge(TestBase):
636
texts = [['header', 'aaa', 'bbb'],
637
['header', 'aaa', 'line from 1', 'bbb'],
638
['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
641
k.add('text0', [], texts[0])
642
k.add('text1', [0], texts[1])
643
k.add('text2', [0], texts[2])
645
self.log('k._weave=' + pformat(k._weave))
647
m = list(k.mash_iter([0, 1, 2]))
653
'line from 2', 'more from 2'])
598
k.add_lines([], ['aaa', 'bbb'])
599
k.add_lines([0], ['111', 'aaa', 'ccc', 'bbb'])
600
k.add_lines([1], ['aaa', 'ccc', 'bbb', '222'])
657
603
class Khayyam(TestBase):
658
604
"""Test changes to multi-line texts, and read/write"""
606
def test_multi_line_merge(self):
661
608
"""A Book of Verses underneath the Bough,
662
609
A Jug of Wine, a Loaf of Bread, -- and Thou
663
610
Beside me singing in the Wilderness --
664
611
Oh, Wilderness were Paradise enow!""",
666
613
"""A Book of Verses underneath the Bough,
667
614
A Jug of Wine, a Loaf of Bread, -- and Thou
668
615
Beside me singing in the Wilderness --
691
ver = k.add('text%d' % i,
638
ver = k.add_lines('text%d' % i,
692
639
list(parents), t)
640
parents.add('text%d' % i)
696
643
self.log("k._weave=" + pformat(k._weave))
698
645
for i, t in enumerate(texts):
699
self.assertEqual(k.get(i), t)
646
self.assertEqual(k.get_lines(i), t)
701
648
self.check_read_write(k)
705
class MergeCases(TestBase):
706
def doMerge(self, base, a, b, mp):
707
from cStringIO import StringIO
708
from textwrap import dedent
714
w.add('text0', [], map(addcrlf, base))
715
w.add('text1', [0], map(addcrlf, a))
716
w.add('text2', [0], map(addcrlf, b))
718
self.log('weave is:')
721
self.log(tmpf.getvalue())
723
self.log('merge plan:')
724
p = list(w.plan_merge(1, 2))
725
for state, line in p:
727
self.log('%12s | %s' % (state, line[:-1]))
731
mt.writelines(w.weave_merge(p))
733
self.log(mt.getvalue())
735
mp = map(addcrlf, mp)
736
self.assertEqual(mt.readlines(), mp)
739
def testOneInsert(self):
745
def testSeparateInserts(self):
746
self.doMerge(['aaa', 'bbb', 'ccc'],
747
['aaa', 'xxx', 'bbb', 'ccc'],
748
['aaa', 'bbb', 'yyy', 'ccc'],
749
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
751
def testSameInsert(self):
752
self.doMerge(['aaa', 'bbb', 'ccc'],
753
['aaa', 'xxx', 'bbb', 'ccc'],
754
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
755
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
757
def testOverlappedInsert(self):
758
self.doMerge(['aaa', 'bbb'],
759
['aaa', 'xxx', 'yyy', 'bbb'],
760
['aaa', 'xxx', 'bbb'],
761
['aaa', '<<<<<<<', 'xxx', 'yyy', '=======', 'xxx',
764
# really it ought to reduce this to
765
# ['aaa', 'xxx', 'yyy', 'bbb']
768
def testClashReplace(self):
769
self.doMerge(['aaa'],
772
['<<<<<<<', 'xxx', '=======', 'yyy', 'zzz',
775
def testNonClashInsert(self):
776
self.doMerge(['aaa'],
779
['<<<<<<<', 'xxx', 'aaa', '=======', 'yyy', 'zzz',
782
self.doMerge(['aaa'],
788
def testDeleteAndModify(self):
789
"""Clashing delete and modification.
791
If one side modifies a region and the other deletes it then
792
there should be a conflict with one side blank.
795
#######################################
796
# skippd, not working yet
799
self.doMerge(['aaa', 'bbb', 'ccc'],
800
['aaa', 'ddd', 'ccc'],
802
['<<<<<<<<', 'aaa', '=======', '>>>>>>>', 'ccc'])
805
651
class JoinWeavesTests(TestBase):
807
653
super(JoinWeavesTests, self).setUp()
808
654
self.weave1 = Weave()
809
655
self.lines1 = ['hello\n']
810
656
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')
657
self.weave1.add_lines('v1', [], self.lines1)
658
self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
659
self.weave1.add_lines('v3', ['v2'], self.lines3)
661
def test_written_detection(self):
662
# Test detection of weave file corruption.
664
# Make sure that we can detect if a weave file has
665
# been corrupted. This doesn't test all forms of corruption,
666
# but it at least helps verify the data you get, is what you want.
667
from cStringIO import StringIO
670
w.add_lines('v1', [], ['hello\n'])
671
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
676
# Because we are corrupting, we need to make sure we have the exact text
677
self.assertEquals('# bzr weave file v5\n'
678
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
679
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
680
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n',
683
# Change a single letter
684
tmpf = StringIO('# bzr weave file v5\n'
685
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
686
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
687
'w\n{ 0\n. hello\n}\n{ 1\n. There\n}\nW\n')
691
self.assertEqual('hello\n', w.get_text('v1'))
692
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
693
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
694
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
696
# Change the sha checksum
697
tmpf = StringIO('# bzr weave file v5\n'
698
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
699
'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
700
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n')
704
self.assertEqual('hello\n', w.get_text('v1'))
705
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
706
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
707
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
710
class TestWeave(TestCase):
712
def test_allow_reserved_false(self):
713
w = Weave('name', allow_reserved=False)
714
# Add lines is checked at the WeaveFile level, not at the Weave level
715
w.add_lines('name:', [], TEXT_1)
716
# But get_lines is checked at this level
717
self.assertRaises(errors.ReservedId, w.get_lines, 'name:')
719
def test_allow_reserved_true(self):
720
w = Weave('name', allow_reserved=True)
721
w.add_lines('name:', [], TEXT_1)
722
self.assertEqual(TEXT_1, w.get_lines('name:'))
725
class InstrumentedWeave(Weave):
726
"""Keep track of how many times functions are called."""
728
def __init__(self, weave_name=None):
729
self._extract_count = 0
730
Weave.__init__(self, weave_name=weave_name)
732
def _extract(self, versions):
733
self._extract_count += 1
734
return Weave._extract(self, versions)
737
class TestNeedsReweave(TestCase):
738
"""Internal corner cases for when reweave is needed."""
740
def test_compatible_parents(self):
742
my_parents = set([1, 2, 3])
744
self.assertTrue(w1._compatible_parents(my_parents, set([3])))
746
self.assertTrue(w1._compatible_parents(my_parents, set(my_parents)))
747
# same empty corner case
748
self.assertTrue(w1._compatible_parents(set(), set()))
749
# other cannot contain stuff my_parents does not
750
self.assertFalse(w1._compatible_parents(set(), set([1])))
751
self.assertFalse(w1._compatible_parents(my_parents, set([1, 2, 3, 4])))
752
self.assertFalse(w1._compatible_parents(my_parents, set([4])))
755
class TestWeaveFile(TestCaseInTempDir):
757
def test_empty_file(self):
758
f = open('empty.weave', 'wb+')
760
self.assertRaises(errors.WeaveFormatError,