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 weave import Weave, WeaveFormatError
27
from pprint import pformat
35
from sets import Set, ImmutableSet
37
frozenset = ImmutableSet
42
# texts for use in testing
43
TEXT_0 = ["Hello world"]
44
TEXT_1 = ["Hello world",
49
class TestBase(testsweet.TestBase):
50
def check_read_write(self, k):
51
"""Check the weave k can be written & re-read."""
52
from tempfile import TemporaryFile
53
from weavefile import write_weave, read_weave
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 DeltaAdd(TestBase):
110
"""Detection of changes prior to inserting new revision."""
113
k.add([], ['line 1'])
115
self.assertEqual(k._l,
121
changes = list(k._delta(set([0]),
125
self.log('raw changes: ' + pformat(changes))
127
# currently there are 3 lines in the weave, and we insert after them
128
self.assertEquals(changes,
129
[(3, 3, ['new line'])])
131
changes = k._delta(set([0]),
135
self.assertEquals(list(changes),
136
[(1, 1, ['top line'])])
138
self.check_read_write(k)
141
class InvalidAdd(TestBase):
142
"""Try to use invalid version number during add."""
146
self.assertRaises(IndexError,
152
class InsertLines(TestBase):
153
"""Store a revision that adds one line to the original.
155
Look at the annotations to make sure that the first line is matched
156
and not stored repeatedly."""
160
k.add([], ['line 1'])
161
k.add([0], ['line 1', 'line 2'])
163
self.assertEqual(k.annotate(0),
166
self.assertEqual(k.get(1),
170
self.assertEqual(k.annotate(1),
174
k.add([0], ['line 1', 'diverged line'])
176
self.assertEqual(k.annotate(2),
178
(2, 'diverged line')])
180
text3 = ['line 1', 'middle line', 'line 2']
184
self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
186
self.log("k._l=" + pformat(k._l))
188
self.assertEqual(k.annotate(3),
193
# now multiple insertions at different places
195
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
197
self.assertEqual(k.annotate(4),
207
class DeleteLines(TestBase):
208
"""Deletion of lines from existing text.
210
Try various texts all based on a common ancestor."""
214
base_text = ['one', 'two', 'three', 'four']
218
texts = [['one', 'two', 'three'],
219
['two', 'three', 'four'],
221
['one', 'two', 'three', 'four'],
227
self.log('final weave:')
228
self.log('k._l=' + pformat(k._l))
230
for i in range(len(texts)):
231
self.assertEqual(k.get(i+1),
237
class SuicideDelete(TestBase):
238
"""Invalid weave which tries to add and delete simultaneously."""
252
self.assertRaises(WeaveFormatError,
258
class CannedDelete(TestBase):
259
"""Unpack canned weave with deleted lines."""
269
'line to be deleted',
275
self.assertEqual(k.get(0),
277
'line to be deleted',
281
self.assertEqual(k.get(1),
288
class CannedReplacement(TestBase):
289
"""Unpack canned weave with deleted lines."""
299
'line to be deleted',
308
self.assertEqual(k.get(0),
310
'line to be deleted',
314
self.assertEqual(k.get(1),
322
class BadWeave(TestBase):
323
"""Test that we trap an insert which should not occur."""
333
' added in version 1',
342
self.assertRaises(WeaveFormatError,
347
class BadInsert(TestBase):
348
"""Test that we trap an insert which should not occur."""
360
' added in version 1',
367
self.assertRaises(WeaveFormatError,
371
self.assertRaises(WeaveFormatError,
376
class InsertNested(TestBase):
377
"""Insertion with nested instructions."""
389
' added in version 1',
398
self.assertEqual(k.get(0),
402
self.assertEqual(k.get(1),
404
' added in version 1',
408
self.assertEqual(k.get(2),
413
self.assertEqual(k.get(3),
415
' added in version 1',
422
class DeleteLines2(TestBase):
423
"""Test recording revisions that delete lines.
425
This relies on the weave having a way to represent lines knocked
426
out by a later revision."""
430
k.add([], ["line the first",
435
self.assertEqual(len(k.get(0)), 4)
437
k.add([0], ["line the first",
440
self.assertEqual(k.get(1),
444
self.assertEqual(k.annotate(1),
445
[(0, "line the first"),
450
class IncludeVersions(TestBase):
451
"""Check texts that are stored across multiple revisions.
453
Here we manually create a weave with particular encoding and make
454
sure it unpacks properly.
456
Text 0 includes nothing; text 1 includes text 0 and adds some
463
k._v = [frozenset(), frozenset([0])]
471
self.assertEqual(k.get(1),
475
self.assertEqual(k.get(0),
478
k.dump(self.TEST_LOG)
481
class DivergedIncludes(TestBase):
482
"""Weave with two diverged texts based on version 0.
498
"alternative second line",
502
self.assertEqual(k.get(0),
505
self.assertEqual(k.get(1),
509
self.assertEqual(k.get(2),
511
"alternative second line"])
513
self.assertEqual(k.inclusions([2]),
518
class ReplaceLine(TestBase):
522
text0 = ['cheddar', 'stilton', 'gruyere']
523
text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
528
self.log('k._l=' + pformat(k._l))
530
self.assertEqual(k.get(0), text0)
531
self.assertEqual(k.get(1), text1)
535
class Merge(TestBase):
536
"""Storage of versions that merge diverged parents"""
541
['header', '', 'line from 1'],
542
['header', '', 'line from 2', 'more from 2'],
543
['header', '', 'line from 1', 'fixup line', 'line from 2'],
549
k.add([0, 1, 2], texts[3])
551
for i, t in enumerate(texts):
552
self.assertEqual(k.get(i), t)
554
self.assertEqual(k.annotate(3),
562
self.assertEqual(k.inclusions([3]),
565
self.log('k._l=' + pformat(k._l))
567
self.check_read_write(k)
570
class Conflicts(TestBase):
571
"""Test detection of conflicting regions during a merge.
573
A base version is inserted, then two descendents try to
574
insert different lines in the same place. These should be
575
reported as a possible conflict and forwarded to the user."""
580
k.add([], ['aaa', 'bbb'])
581
k.add([0], ['aaa', '111', 'bbb'])
582
k.add([1], ['aaa', '222', 'bbb'])
584
merged = k.merge([1, 2])
586
self.assertEquals([[['aaa']],
592
class NonConflict(TestBase):
593
"""Two descendants insert compatible changes.
595
No conflict should be reported."""
600
k.add([], ['aaa', 'bbb'])
601
k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
602
k.add([1], ['aaa', 'ccc', 'bbb', '222'])
608
class AutoMerge(TestBase):
612
texts = [['header', 'aaa', 'bbb'],
613
['header', 'aaa', 'line from 1', 'bbb'],
614
['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
621
self.log('k._l=' + pformat(k._l))
623
m = list(k.mash_iter([0, 1, 2]))
629
'line from 2', 'more from 2'])
633
class Khayyam(TestBase):
634
"""Test changes to multi-line texts, and read/write"""
637
"""A Book of Verses underneath the Bough,
638
A Jug of Wine, a Loaf of Bread, -- and Thou
639
Beside me singing in the Wilderness --
640
Oh, Wilderness were Paradise enow!""",
642
"""A Book of Verses underneath the Bough,
643
A Jug of Wine, a Loaf of Bread, -- and Thou
644
Beside me singing in the Wilderness --
645
Oh, Wilderness were Paradise now!""",
647
"""A Book of poems underneath the tree,
648
A Jug of Wine, a Loaf of Bread,
650
Beside me singing in the Wilderness --
651
Oh, Wilderness were Paradise now!
655
"""A Book of Verses underneath the Bough,
656
A Jug of Wine, a Loaf of Bread,
658
Beside me singing in the Wilderness --
659
Oh, Wilderness were Paradise now!""",
661
texts = [[l.strip() for l in t.split('\n')] for t in rawtexts]
666
ver = k.add(list(parents), t)
669
self.log("k._l=" + pformat(k._l))
671
for i, t in enumerate(texts):
672
self.assertEqual(k.get(i), t)
674
self.check_read_write(k)
679
from unittest import TestSuite, TestLoader
684
suite.addTest(tl.loadTestsFromModule(testweave))
686
return int(not testsweet.run_suite(suite)) # for shell 0=true
689
if __name__ == '__main__':
691
sys.exit(testweave())