1
# Copyright (C) 2005 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
"""test suite for weave algorithm"""
23
from testsweet import TestBase
24
from weave import Weave, VerInfo, WeaveFormatError
27
# texts for use in testing
28
TEXT_0 = ["Hello world"]
29
TEXT_1 = ["Hello world",
38
class StoreText(TestBase):
39
"""Store and retrieve a simple text."""
42
idx = k.add([], TEXT_0)
43
self.assertEqual(k.get(idx), TEXT_0)
44
self.assertEqual(idx, 0)
48
class AnnotateOne(TestBase):
52
self.assertEqual(k.annotate(0),
56
class StoreTwo(TestBase):
60
idx = k.add([], TEXT_0)
61
self.assertEqual(idx, 0)
63
idx = k.add([], TEXT_1)
64
self.assertEqual(idx, 1)
66
self.assertEqual(k.get(0), TEXT_0)
67
self.assertEqual(k.get(1), TEXT_1)
73
class Delta1(TestBase):
74
"""Detection of changes prior to inserting new revision."""
76
return ########################## SKIPPED
77
from pprint import pformat
82
changes = list(k._delta(set([0]),
86
self.log('raw changes: ' + pformat(changes))
88
# should be one inserted line after line 0q
89
self.assertEquals(changes,
90
[(1, 1, ['new line'])])
92
changes = k._delta(set([0]),
96
self.assertEquals(list(changes),
97
[(0, 0, ['top line'])])
101
class InvalidAdd(TestBase):
102
"""Try to use invalid version number during add."""
106
self.assertRaises(IndexError,
112
class InsertLines(TestBase):
113
"""Store a revision that adds one line to the original.
115
Look at the annotations to make sure that the first line is matched
116
and not stored repeatedly."""
118
return ########################## SKIPPED
122
k.add([], ['line 1'])
123
k.add([0], ['line 1', 'line 2'])
125
self.assertEqual(k.annotate(0),
128
self.assertEqual(k.get(1),
132
self.assertEqual(k.annotate(1),
136
k.add([0], ['line 1', 'diverged line'])
138
self.assertEqual(k.annotate(2),
140
(2, 'diverged line')])
143
['line 1', 'middle line', 'line 2'])
145
self.assertEqual(k.annotate(3),
150
# now multiple insertions at different places
152
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
154
self.assertEqual(k.annotate(4),
164
class SuicideDelete(TestBase):
178
self.assertRaises(WeaveFormatError,
184
class CannedDelete(TestBase):
185
"""Unpack canned weave with deleted lines."""
195
'line to be deleted',
201
self.assertEqual(k.get(0),
203
'line to be deleted',
207
self.assertEqual(k.get(1),
214
class CannedReplacement(TestBase):
215
"""Unpack canned weave with deleted lines."""
225
'line to be deleted',
234
self.assertEqual(k.get(0),
236
'line to be deleted',
240
self.assertEqual(k.get(1),
248
class BadWeave(TestBase):
249
"""Test that we trap an insert which should not occur."""
259
' added in version 1',
268
self.assertRaises(WeaveFormatError,
273
class BadInsert(TestBase):
274
"""Test that we trap an insert which should not occur."""
286
' added in version 1',
293
self.assertRaises(WeaveFormatError,
297
self.assertRaises(WeaveFormatError,
302
class InsertNested(TestBase):
303
"""Insertion with nested instructions."""
315
' added in version 1',
324
self.assertEqual(k.get(0),
328
self.assertEqual(k.get(1),
330
' added in version 1',
334
self.assertEqual(k.get(2),
339
self.assertEqual(k.get(3),
341
' added in version 1',
348
class DeleteLines(TestBase):
349
"""Test recording revisions that delete lines.
351
This relies on the weave having a way to represent lines knocked
352
out by a later revision."""
356
k.add([], ["line the first",
361
self.assertEqual(len(k.get(0)), 4)
363
return ################################## SKIPPED
365
k.add([0], ["line the first",
368
self.assertEqual(k.get(1),
374
class IncludeVersions(TestBase):
375
"""Check texts that are stored across multiple revisions.
377
Here we manually create a weave with particular encoding and make
378
sure it unpacks properly.
380
Text 0 includes nothing; text 1 includes text 0 and adds some
387
k._v = [VerInfo(), VerInfo(included=[0])]
395
self.assertEqual(k.get(1),
399
self.assertEqual(k.get(0),
402
k.dump(self.TEST_LOG)
405
class DivergedIncludes(TestBase):
406
"""Weave with two diverged texts based on version 0.
412
VerInfo(included=[0]),
413
VerInfo(included=[0]),
422
"alternative second line",
426
self.assertEqual(k.get(0),
429
self.assertEqual(k.get(1),
433
self.assertEqual(k.get(2),
435
"alternative second line"])
439
from unittest import TestSuite, TestLoader
444
suite.addTest(tl.loadTestsFromModule(testweave))
446
return int(not testsweet.run_suite(suite)) # for shell 0=true
449
if __name__ == '__main__':
451
sys.exit(testweave())