79
79
"""Store and retrieve a simple text."""
82
idx = k.add('text0', [], TEXT_0)
83
self.assertEqual(k.get(idx), TEXT_0)
82
idx = k.add_lines('text0', [], TEXT_0)
83
self.assertEqual(k.get_lines(idx), TEXT_0)
84
84
self.assertEqual(idx, 0)
87
87
class AnnotateOne(TestBase):
90
k.add('text0', [], TEXT_0)
91
self.assertEqual(k.annotate(0),
90
k.add_lines('text0', [], TEXT_0)
91
self.assertEqual(k.annotate('text0'),
92
[('text0', TEXT_0[0])])
95
95
class StoreTwo(TestBase):
99
idx = k.add('text0', [], TEXT_0)
99
idx = k.add_lines('text0', [], TEXT_0)
100
100
self.assertEqual(idx, 0)
102
idx = k.add('text1', [], TEXT_1)
102
idx = k.add_lines('text1', [], TEXT_1)
103
103
self.assertEqual(idx, 1)
105
self.assertEqual(k.get(0), TEXT_0)
106
self.assertEqual(k.get(1), TEXT_1)
109
class AddWithGivenSha(TestBase):
111
"""Add with caller-supplied SHA-1"""
115
k.add('text0', [], [t], sha1=sha_string(t))
105
self.assertEqual(k.get_lines(0), TEXT_0)
106
self.assertEqual(k.get_lines(1), TEXT_1)
118
109
class GetSha1(TestBase):
119
110
def test_get_sha1(self):
121
k.add('text0', [], 'text0')
112
k.add_lines('text0', [], 'text0')
122
113
self.assertEqual('34dc0e430c642a26c3dd1c2beb7a8b4f4445eb79',
123
114
k.get_sha1('text0'))
124
115
self.assertRaises(errors.RevisionNotPresent,
143
134
"""Add the same version twice; harmless."""
144
135
def runTest(self):
146
idx = k.add('text0', [], TEXT_0)
147
idx2 = k.add('text0', [], TEXT_0)
137
idx = k.add_lines('text0', [], TEXT_0)
138
idx2 = k.add_lines('text0', [], TEXT_0)
148
139
self.assertEqual(idx, idx2)
151
142
class InvalidRepeatedAdd(TestBase):
152
143
def runTest(self):
154
idx = k.add('text0', [], TEXT_0)
145
k.add_lines('basis', [], TEXT_0)
146
idx = k.add_lines('text0', [], TEXT_0)
155
147
self.assertRaises(errors.RevisionAlreadyPresent,
159
151
['not the same text'])
160
152
self.assertRaises(errors.RevisionAlreadyPresent,
163
[12], # not the right parents
155
['basis'], # not the right parents
172
164
def runTest(self):
175
k.add('text0', [], ['line 1'])
176
k.add('text1', [0], ['line 1', 'line 2'])
178
self.assertEqual(k.annotate(0),
181
self.assertEqual(k.get(1),
167
k.add_lines('text0', [], ['line 1'])
168
k.add_lines('text1', ['text0'], ['line 1', 'line 2'])
170
self.assertEqual(k.annotate('text0'),
171
[('text0', 'line 1')])
173
self.assertEqual(k.get_lines(1),
185
self.assertEqual(k.annotate(1),
189
k.add('text2', [0], ['line 1', 'diverged line'])
191
self.assertEqual(k.annotate(2),
193
(2, 'diverged line')])
177
self.assertEqual(k.annotate('text1'),
178
[('text0', 'line 1'),
179
('text1', 'line 2')])
181
k.add_lines('text2', ['text0'], ['line 1', 'diverged line'])
183
self.assertEqual(k.annotate('text2'),
184
[('text0', 'line 1'),
185
('text2', 'diverged line')])
195
187
text3 = ['line 1', 'middle line', 'line 2']
200
192
# self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
202
194
self.log("k._weave=" + pformat(k._weave))
204
self.assertEqual(k.annotate(3),
196
self.assertEqual(k.annotate('text3'),
197
[('text0', 'line 1'),
198
('text3', 'middle line'),
199
('text1', 'line 2')])
209
201
# now multiple insertions at different places
203
['text0', 'text1', 'text3'],
212
204
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
214
self.assertEqual(k.annotate(4),
206
self.assertEqual(k.annotate('text4'),
207
[('text0', 'line 1'),
209
('text3', 'middle line'),
223
215
class DeleteLines(TestBase):
230
222
base_text = ['one', 'two', 'three', 'four']
232
k.add('text0', [], base_text)
224
k.add_lines('text0', [], base_text)
234
226
texts = [['one', 'two', 'three'],
235
227
['two', 'three', 'four'],
242
ver = k.add('text%d' % i,
234
ver = k.add_lines('text%d' % i,
246
238
self.log('final weave:')
247
239
self.log('k._weave=' + pformat(k._weave))
249
241
for i in range(len(texts)):
250
self.assertEqual(k.get(i+1),
242
self.assertEqual(k.get_lines(i+1),
293
285
k._sha1s = [sha_string('first lineline to be deletedlast line')
294
286
, sha_string('first linelast line')]
296
self.assertEqual(k.get(0),
288
self.assertEqual(k.get_lines(0),
298
290
'line to be deleted',
302
self.assertEqual(k.get(1),
294
self.assertEqual(k.get_lines(1),
327
319
k._sha1s = [sha_string('first lineline to be deletedlast line')
328
320
, sha_string('first linereplacement linelast line')]
330
self.assertEqual(k.get(0),
322
self.assertEqual(k.get_lines(0),
332
324
'line to be deleted',
336
self.assertEqual(k.get(1),
328
self.assertEqual(k.get_lines(1),
338
330
'replacement line',
431
423
, sha_string('foo { added in version 1 added in v2 also from v1}')
434
self.assertEqual(k.get(0),
426
self.assertEqual(k.get_lines(0),
438
self.assertEqual(k.get(1),
430
self.assertEqual(k.get_lines(1),
440
432
' added in version 1',
444
self.assertEqual(k.get(2),
436
self.assertEqual(k.get_lines(2),
449
self.assertEqual(k.get(3),
441
self.assertEqual(k.get_lines(3),
451
443
' added in version 1',
462
454
def runTest(self):
465
k.add('text0', [], ["line the first",
457
k.add_lines('text0', [], ["line the first",
470
self.assertEqual(len(k.get(0)), 4)
462
self.assertEqual(len(k.get_lines(0)), 4)
472
k.add('text1', [0], ["line the first",
464
k.add_lines('text1', ['text0'], ["line the first",
475
self.assertEqual(k.get(1),
467
self.assertEqual(k.get_lines(1),
476
468
["line the first",
479
self.assertEqual(k.annotate(1),
480
[(0, "line the first"),
471
self.assertEqual(k.annotate('text1'),
472
[('text0', "line the first"),
484
476
class IncludeVersions(TestBase):
505
497
k._sha1s = [sha_string('first line')
506
498
, sha_string('first linesecond line')]
508
self.assertEqual(k.get(1),
500
self.assertEqual(k.get_lines(1),
512
self.assertEqual(k.get(0),
504
self.assertEqual(k.get_lines(0),
540
533
, sha_string('first linesecond line')
541
534
, sha_string('first linealternative second line')]
543
self.assertEqual(k.get(0),
536
self.assertEqual(k.get_lines(0),
546
self.assertEqual(k.get(1),
539
self.assertEqual(k.get_lines(1),
550
self.assertEqual(k.get(2),
543
self.assertEqual(k.get_lines('2'),
552
545
"alternative second line"])
554
self.assertEqual(list(k.inclusions([2])),
547
self.assertEqual(list(k.get_ancestry(['2'])),
562
555
text0 = ['cheddar', 'stilton', 'gruyere']
563
556
text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
565
k.add('text0', [], text0)
566
k.add('text1', [0], text1)
558
k.add_lines('text0', [], text0)
559
k.add_lines('text1', ['text0'], text1)
568
561
self.log('k._weave=' + pformat(k._weave))
570
self.assertEqual(k.get(0), text0)
571
self.assertEqual(k.get(1), text1)
563
self.assertEqual(k.get_lines(0), text0)
564
self.assertEqual(k.get_lines(1), text1)
574
567
class Merge(TestBase):
582
575
['header', '', 'line from 1', 'fixup line', 'line from 2'],
585
k.add('text0', [], texts[0])
586
k.add('text1', [0], texts[1])
587
k.add('text2', [0], texts[2])
588
k.add('merge', [0, 1, 2], texts[3])
578
k.add_lines('text0', [], texts[0])
579
k.add_lines('text1', ['text0'], texts[1])
580
k.add_lines('text2', ['text0'], texts[2])
581
k.add_lines('merge', ['text0', 'text1', 'text2'], texts[3])
590
583
for i, t in enumerate(texts):
591
self.assertEqual(k.get(i), t)
584
self.assertEqual(k.get_lines(i), t)
593
self.assertEqual(k.annotate(3),
586
self.assertEqual(k.annotate('merge'),
587
[('text0', 'header'),
589
('text1', 'line from 1'),
590
('merge', 'fixup line'),
591
('text2', 'line from 2'),
601
self.assertEqual(list(k.inclusions([3])),
594
self.assertEqual(list(k.get_ancestry(['merge'])),
602
595
['text0', 'text1', 'text2', 'merge'])
604
597
self.log('k._weave=' + pformat(k._weave))
619
k.add([], ['aaa', 'bbb'])
620
k.add([0], ['aaa', '111', 'bbb'])
621
k.add([1], ['aaa', '222', 'bbb'])
612
k.add_lines([], ['aaa', 'bbb'])
613
k.add_lines([0], ['aaa', '111', 'bbb'])
614
k.add_lines([1], ['aaa', '222', 'bbb'])
623
616
merged = k.merge([1, 2])
638
k.add([], ['aaa', 'bbb'])
639
k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
640
k.add([1], ['aaa', 'ccc', 'bbb', '222'])
631
k.add_lines([], ['aaa', 'bbb'])
632
k.add_lines([0], ['111', 'aaa', 'ccc', 'bbb'])
633
k.add_lines([1], ['aaa', 'ccc', 'bbb', '222'])
643
636
class Khayyam(TestBase):
678
ver = k.add('text%d' % i,
671
ver = k.add_lines('text%d' % i,
679
672
list(parents), t)
673
parents.add('text%d' % i)
683
676
self.log("k._weave=" + pformat(k._weave))
685
678
for i, t in enumerate(texts):
686
self.assertEqual(k.get(i), t)
679
self.assertEqual(k.get_lines(i), t)
688
681
self.check_read_write(k)
700
w.add('text0', [], map(addcrlf, base))
701
w.add('text1', [0], map(addcrlf, a))
702
w.add('text2', [0], map(addcrlf, b))
693
w.add_lines('text0', [], map(addcrlf, base))
694
w.add_lines('text1', ['text0'], map(addcrlf, a))
695
w.add_lines('text2', ['text0'], map(addcrlf, b))
704
697
self.log('weave is:')
705
698
tmpf = StringIO()
794
787
self.weave1 = Weave()
795
788
self.lines1 = ['hello\n']
796
789
self.lines3 = ['hello\n', 'cruel\n', 'world\n']
797
self.weave1.add('v1', [], self.lines1)
798
self.weave1.add('v2', [0], ['hello\n', 'world\n'])
799
self.weave1.add('v3', [1], self.lines3)
790
self.weave1.add_lines('v1', [], self.lines1)
791
self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
792
self.weave1.add_lines('v3', ['v2'], self.lines3)
801
794
def test_join_empty(self):
802
795
"""Join two empty weaves."""
814
807
def test_join_unrelated(self):
815
808
"""Join two weaves with no history in common."""
817
wb.add('b1', [], ['line from b\n'])
810
wb.add_lines('b1', [], ['line from b\n'])
820
813
eq = self.assertEqual
822
eq(sorted(list(w1.iter_names())),
815
eq(sorted(w1.versions()),
823
816
['b1', 'v1', 'v2', 'v3'])
825
818
def test_join_related(self):
826
819
wa = self.weave1.copy()
827
820
wb = self.weave1.copy()
828
wa.add('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
829
wb.add('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
821
wa.add_lines('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
822
wb.add_lines('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
830
823
eq = self.assertEquals
839
832
#join reconciles differening parents into a union.
842
wa.add('v1', [], ['hello\n'])
844
wb.add('v1', ['v0'], ['hello\n'])
835
wa.add_lines('v1', [], ['hello\n'])
836
wb.add_lines('v0', [], [])
837
wb.add_lines('v1', ['v0'], ['hello\n'])
846
839
self.assertEqual(['v0'], wa.get_parents('v1'))
849
842
"""Cannot join weaves with different texts for a version."""
852
wa.add('v1', [], ['hello\n'])
853
wb.add('v1', [], ['not\n', 'hello\n'])
845
wa.add_lines('v1', [], ['hello\n'])
846
wb.add_lines('v1', [], ['not\n', 'hello\n'])
854
847
self.assertRaises(WeaveError,
860
853
The source weave contains a different version at index 0."""
861
854
wa = self.weave1.copy()
863
wb.add('x1', [], ['line from x1\n'])
864
wb.add('v1', [], ['hello\n'])
865
wb.add('v2', ['v1'], ['hello\n', 'world\n'])
856
wb.add_lines('x1', [], ['line from x1\n'])
857
wb.add_lines('v1', [], ['hello\n'])
858
wb.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
867
860
eq = self.assertEquals
868
eq(sorted(wa.iter_names()), ['v1', 'v2', 'v3', 'x1',])
861
eq(sorted(wa.versions()), ['v1', 'v2', 'v3', 'x1',])
869
862
eq(wa.get_text('x1'), 'line from x1\n')
871
864
def test_written_detection(self):
877
870
from cStringIO import StringIO
880
w.add('v1', [], ['hello\n'])
881
w.add('v2', ['v1'], ['hello\n', 'there\n'])
873
w.add_lines('v1', [], ['hello\n'])
874
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
883
876
tmpf = StringIO()
884
877
write_weave(w, tmpf)
901
894
self.assertEqual('hello\n', w.get_text('v1'))
902
895
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
903
896
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
904
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
905
897
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
907
899
# Change the sha checksum
915
907
self.assertEqual('hello\n', w.get_text('v1'))
916
908
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
917
909
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
918
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
919
910
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
943
934
txt2 = ['a\n', 'c\n']
944
935
txt3 = ['a\n', 'b\n', 'c\n']
946
w1.add('txt0', [], txt0) # extract 1a
947
w2.add('txt0', [], txt0) # extract 1b
948
w1.add('txt1', [0], txt1)# extract 2a
949
w2.add('txt2', [0], txt2)# extract 2b
937
w1.add_lines('txt0', [], txt0) # extract 1a
938
w2.add_lines('txt0', [], txt0) # extract 1b
939
w1.add_lines('txt1', ['txt0'], txt1)# extract 2a
940
w2.add_lines('txt2', ['txt0'], txt2)# extract 2b
950
941
w1.join(w2) # extract 3a to add txt2
951
942
w2.join(w1) # extract 3b to add txt1
953
w1.add('txt3', [1, 2], txt3) # extract 4a
954
w2.add('txt3', [1, 2], txt3) # extract 4b
944
w1.add_lines('txt3', ['txt1', 'txt2'], txt3) # extract 4a
945
w2.add_lines('txt3', ['txt2', 'txt1'], txt3) # extract 4b
955
946
# These secretly have inverted parents
957
948
# This should not have to do any extractions
972
963
txt2 = ['a\n', 'c\n']
973
964
txt3 = ['a\n', 'b\n', 'c\n']
975
w1.add('txt0', [], txt0)
976
w2.add('txt0', [], txt0)
977
w1.add('txt1', [0], txt1)
978
w2.add('txt1', [0,0], txt1)
966
w1.add_lines('txt0', [], txt0)
967
w2.add_lines('txt0', [], txt0)
968
w1.add_lines('txt1', ['txt0'], txt1)
969
w2.add_lines('txt1', ['txt0', 'txt0'], txt1)
979
970
# Same text, effectively the same, because the
980
971
# parent is only repeated
981
972
w1.join(w2) # extract 3a to add txt2
982
973
w2.join(w1) # extract 3b to add txt1
985
class MismatchedTexts(TestCase):
986
"""Test that merging two weaves with different texts fails."""
988
def test_reweave(self):
992
w1.add('txt0', [], ['a\n'])
993
w2.add('txt0', [], ['a\n'])
994
w1.add('txt1', [0], ['a\n', 'b\n'])
995
w2.add('txt1', [0], ['a\n', 'c\n'])
997
self.assertRaises(errors.WeaveTextDiffers, w1.reweave, w2)
1000
976
class TestNeedsRweave(TestCase):
1001
977
"""Internal corner cases for when reweave is needed."""