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),
163
class BadWeave(TestBase):
164
"""Test that we trap an insert which should not occur."""
174
' added in version 1',
183
self.assertRaises(WeaveFormatError,
188
class BadInsert(TestBase):
189
"""Test that we trap an insert which should not occur."""
201
' added in version 1',
208
self.assertRaises(WeaveFormatError,
212
self.assertRaises(WeaveFormatError,
217
class InsertNested(TestBase):
218
"""Insertion with nested instructions."""
230
' added in version 1',
239
self.assertEqual(k.get(0),
243
self.assertEqual(k.get(1),
245
' added in version 1',
249
self.assertEqual(k.get(2),
254
self.assertEqual(k.get(3),
256
' added in version 1',
263
class DeleteLines(TestBase):
264
"""Test recording revisions that delete lines.
266
This relies on the weave having a way to represent lines knocked
267
out by a later revision."""
271
k.add([], ["line the first",
276
self.assertEqual(len(k.get(0)), 4)
278
return ################################## SKIPPED
280
k.add([0], ["line the first",
283
self.assertEqual(k.get(1),
289
class IncludeVersions(TestBase):
290
"""Check texts that are stored across multiple revisions.
292
Here we manually create a weave with particular encoding and make
293
sure it unpacks properly.
295
Text 0 includes nothing; text 1 includes text 0 and adds some
302
k._v = [VerInfo(), VerInfo(included=[0])]
310
self.assertEqual(k.get(1),
314
self.assertEqual(k.get(0),
317
k.dump(self.TEST_LOG)
320
class DivergedIncludes(TestBase):
321
"""Weave with two diverged texts based on version 0.
327
VerInfo(included=[0]),
328
VerInfo(included=[0]),
337
"alternative second line",
341
self.assertEqual(k.get(0),
344
self.assertEqual(k.get(1),
348
self.assertEqual(k.get(2),
350
"alternative second line"])
354
from unittest import TestSuite, TestLoader
359
suite.addTest(tl.loadTestsFromModule(testweave))
361
return int(not testsweet.run_suite(suite)) # for shell 0=true
364
if __name__ == '__main__':
366
sys.exit(testweave())