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 case for knit/weave algorithm"""
23
from testsweet import TestBase
24
from knit import Knit, VerInfo
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
from pprint import pformat
81
changes = list(k._delta(set([0]),
85
self.log('raw changes: ' + pformat(changes))
87
# should be one inserted line after line 0q
88
self.assertEquals(changes,
89
[(1, 1, ['new line'])])
91
changes = k._delta(set([0]),
95
self.assertEquals(list(changes),
96
[(0, 0, ['top line'])])
100
class InvalidAdd(TestBase):
101
"""Try to use invalid version number during add."""
105
self.assertRaises(IndexError,
111
class InsertLines(TestBase):
112
"""Store a revision that adds one line to the original.
114
Look at the annotations to make sure that the first line is matched
115
and not stored repeatedly."""
119
k.add([], ['line 1'])
120
k.add([0], ['line 1', 'line 2'])
122
self.assertEqual(k.annotate(0),
125
self.assertEqual(k.get(1),
129
self.assertEqual(k.annotate(1),
133
k.add([0], ['line 1', 'diverged line'])
135
self.assertEqual(k.annotate(2),
137
(2, 'diverged line')])
140
['line 1', 'middle line', 'line 2'])
142
self.assertEqual(k.annotate(3),
149
class IncludeVersions(TestBase):
150
"""Check texts that are stored across multiple revisions.
152
Here we manually create a knit with particular encoding and make
153
sure it unpacks properly.
155
Text 0 includes nothing; text 1 includes text 0 and adds some
162
k._v = [VerInfo(), VerInfo(included=[0])]
163
k._l = [(0, "first line"),
166
self.assertEqual(k.get(1),
170
self.assertEqual(k.get(0),
173
k.dump(self.TEST_LOG)
176
class DivergedIncludes(TestBase):
177
"""Knit with two diverged texts based on version 0.
183
VerInfo(included=[0]),
184
VerInfo(included=[0]),
186
k._l = [(0, "first line"),
188
(2, "alternative second line"),]
190
self.assertEqual(k.get(0),
193
self.assertEqual(k.get(1),
197
self.assertEqual(k.get(2),
199
"alternative second line"])
203
from unittest import TestSuite, TestLoader
208
suite.addTest(tl.loadTestsFromModule(testknit))
210
return int(not testsweet.run_suite(suite)) # for shell 0=true
213
if __name__ == '__main__':