~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to testweave.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-23 23:52:10 UTC
  • Revision ID: mbp@sourcefrog.net-20050323235210-5464746b93c39ed0
more notes on darcs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
2
 
 
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.
7
 
 
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.
12
 
 
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
16
 
 
17
 
 
18
 
 
19
 
 
20
 
"""test suite for weave algorithm"""
21
 
 
22
 
 
23
 
from testsweet import TestBase
24
 
from weave import Weave, VerInfo, WeaveFormatError
25
 
 
26
 
 
27
 
# texts for use in testing
28
 
TEXT_0 = ["Hello world"]
29
 
TEXT_1 = ["Hello world",
30
 
          "A second line"]
31
 
 
32
 
 
33
 
class Easy(TestBase):
34
 
    def runTest(self):
35
 
        k = Weave()
36
 
 
37
 
 
38
 
class StoreText(TestBase):
39
 
    """Store and retrieve a simple text."""
40
 
    def runTest(self):
41
 
        k = Weave()
42
 
        idx = k.add([], TEXT_0)
43
 
        self.assertEqual(k.get(idx), TEXT_0)
44
 
        self.assertEqual(idx, 0)
45
 
 
46
 
 
47
 
 
48
 
class AnnotateOne(TestBase):
49
 
    def runTest(self):
50
 
        k = Weave()
51
 
        k.add([], TEXT_0)
52
 
        self.assertEqual(k.annotate(0),
53
 
                         [(0, TEXT_0[0])])
54
 
 
55
 
 
56
 
class StoreTwo(TestBase):
57
 
    def runTest(self):
58
 
        k = Weave()
59
 
 
60
 
        idx = k.add([], TEXT_0)
61
 
        self.assertEqual(idx, 0)
62
 
 
63
 
        idx = k.add([], TEXT_1)
64
 
        self.assertEqual(idx, 1)
65
 
 
66
 
        self.assertEqual(k.get(0), TEXT_0)
67
 
        self.assertEqual(k.get(1), TEXT_1)
68
 
 
69
 
        k.dump(self.TEST_LOG)
70
 
 
71
 
 
72
 
 
73
 
class Delta1(TestBase):
74
 
    """Detection of changes prior to inserting new revision."""
75
 
    def runTest(self):
76
 
        return ########################## SKIPPED
77
 
        from pprint import pformat
78
 
 
79
 
        k = Weave()
80
 
        k.add([], ['line 1'])
81
 
 
82
 
        changes = list(k._delta(set([0]),
83
 
                                ['line 1',
84
 
                                 'new line']))
85
 
 
86
 
        self.log('raw changes: ' + pformat(changes))
87
 
 
88
 
        # should be one inserted line after line 0q
89
 
        self.assertEquals(changes,
90
 
                          [(1, 1, ['new line'])])
91
 
 
92
 
        changes = k._delta(set([0]),
93
 
                           ['top line',
94
 
                            'line 1'])
95
 
        
96
 
        self.assertEquals(list(changes),
97
 
                          [(0, 0, ['top line'])])
98
 
 
99
 
 
100
 
 
101
 
class InvalidAdd(TestBase):
102
 
    """Try to use invalid version number during add."""
103
 
    def runTest(self):
104
 
        k = Weave()
105
 
 
106
 
        self.assertRaises(IndexError,
107
 
                          k.add,
108
 
                          [69],
109
 
                          ['new text!'])
110
 
 
111
 
 
112
 
class InsertLines(TestBase):
113
 
    """Store a revision that adds one line to the original.
114
 
 
115
 
    Look at the annotations to make sure that the first line is matched
116
 
    and not stored repeatedly."""
117
 
    def runTest(self):
118
 
        return ########################## SKIPPED
119
 
        
120
 
        k = Weave()
121
 
 
122
 
        k.add([], ['line 1'])
123
 
        k.add([0], ['line 1', 'line 2'])
124
 
 
125
 
        self.assertEqual(k.annotate(0),
126
 
                         [(0, 'line 1')])
127
 
 
128
 
        self.assertEqual(k.get(1),
129
 
                         ['line 1',
130
 
                          'line 2'])
131
 
 
132
 
        self.assertEqual(k.annotate(1),
133
 
                         [(0, 'line 1'),
134
 
                          (1, 'line 2')])
135
 
 
136
 
        k.add([0], ['line 1', 'diverged line'])
137
 
 
138
 
        self.assertEqual(k.annotate(2),
139
 
                         [(0, 'line 1'),
140
 
                          (2, 'diverged line')])
141
 
 
142
 
        k.add([0, 1],
143
 
              ['line 1', 'middle line', 'line 2'])
144
 
 
145
 
        self.assertEqual(k.annotate(3),
146
 
                         [(0, 'line 1'),
147
 
                          (3, 'middle line'),
148
 
                          (1, 'line 2')])
149
 
 
150
 
        # now multiple insertions at different places
151
 
        k.add([0, 1, 3],
152
 
              ['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
153
 
 
154
 
        self.assertEqual(k.annotate(4), 
155
 
                         [(0, 'line 1'),
156
 
                          (4, 'aaa'),
157
 
                          (3, 'middle line'),
158
 
                          (4, 'bbb'),
159
 
                          (1, 'line 2'),
160
 
                          (4, 'ccc')])
161
 
 
162
 
 
163
 
class BadWeave(TestBase):
164
 
    """Test that we trap an insert which should not occur."""
165
 
    def runTest(self):
166
 
        k = Weave()
167
 
 
168
 
        k._v = [VerInfo([]),
169
 
                ]
170
 
        k._l = ['bad line',
171
 
                ('{', 0),
172
 
                'foo {',
173
 
                ('{', 1),
174
 
                '  added in version 1',
175
 
                ('{', 2),
176
 
                '  added in v2',
177
 
                ('}', 2),
178
 
                '  also from v1',
179
 
                ('}', 1),
180
 
                '}',
181
 
                ('}', 0)]
182
 
 
183
 
        self.assertRaises(WeaveFormatError,
184
 
                          k.get,
185
 
                          0)
186
 
 
187
 
 
188
 
class BadInsert(TestBase):
189
 
    """Test that we trap an insert which should not occur."""
190
 
    def runTest(self):
191
 
        k = Weave()
192
 
 
193
 
        k._v = [VerInfo([]),
194
 
                VerInfo([0]),
195
 
                VerInfo([0]),
196
 
                VerInfo([0,1,2]),
197
 
                ]
198
 
        k._l = [('{', 0),
199
 
                'foo {',
200
 
                ('{', 1),
201
 
                '  added in version 1',
202
 
                ('{', 1),
203
 
                '  more in 1',
204
 
                ('}', 1),
205
 
                ('}', 1),
206
 
                ('}', 0)]
207
 
 
208
 
        self.assertRaises(WeaveFormatError,
209
 
                          k.get,
210
 
                          0)
211
 
 
212
 
        self.assertRaises(WeaveFormatError,
213
 
                          k.get,
214
 
                          1)
215
 
 
216
 
 
217
 
class InsertNested(TestBase):
218
 
    """Insertion with nested instructions."""
219
 
    def runTest(self):
220
 
        k = Weave()
221
 
 
222
 
        k._v = [VerInfo([]),
223
 
                VerInfo([0]),
224
 
                VerInfo([0]),
225
 
                VerInfo([0,1,2]),
226
 
                ]
227
 
        k._l = [('{', 0),
228
 
                'foo {',
229
 
                ('{', 1),
230
 
                '  added in version 1',
231
 
                ('{', 2),
232
 
                '  added in v2',
233
 
                ('}', 2),
234
 
                '  also from v1',
235
 
                ('}', 1),
236
 
                '}',
237
 
                ('}', 0)]
238
 
 
239
 
        self.assertEqual(k.get(0),
240
 
                         ['foo {',
241
 
                          '}'])
242
 
 
243
 
        self.assertEqual(k.get(1),
244
 
                         ['foo {',
245
 
                          '  added in version 1',
246
 
                          '  also from v1',
247
 
                          '}'])
248
 
                       
249
 
        self.assertEqual(k.get(2),
250
 
                         ['foo {',
251
 
                          '  added in v2',
252
 
                          '}'])
253
 
 
254
 
        self.assertEqual(k.get(3),
255
 
                         ['foo {',
256
 
                          '  added in version 1',
257
 
                          '  added in v2',
258
 
                          '  also from v1',
259
 
                          '}'])
260
 
                         
261
 
 
262
 
 
263
 
class DeleteLines(TestBase):
264
 
    """Test recording revisions that delete lines.
265
 
 
266
 
    This relies on the weave having a way to represent lines knocked
267
 
    out by a later revision."""
268
 
    def runTest(self):
269
 
        k = Weave()
270
 
 
271
 
        k.add([], ["line the first",
272
 
                   "line 2",
273
 
                   "line 3",
274
 
                   "fine"])
275
 
 
276
 
        self.assertEqual(len(k.get(0)), 4)
277
 
 
278
 
        return ################################## SKIPPED
279
 
 
280
 
        k.add([0], ["line the first",
281
 
                   "fine"])
282
 
 
283
 
        self.assertEqual(k.get(1),
284
 
                         ["line the first",
285
 
                          "fine"])
286
 
 
287
 
 
288
 
 
289
 
class IncludeVersions(TestBase):
290
 
    """Check texts that are stored across multiple revisions.
291
 
 
292
 
    Here we manually create a weave with particular encoding and make
293
 
    sure it unpacks properly.
294
 
 
295
 
    Text 0 includes nothing; text 1 includes text 0 and adds some
296
 
    lines.
297
 
    """
298
 
 
299
 
    def runTest(self):
300
 
        k = Weave()
301
 
 
302
 
        k._v = [VerInfo(), VerInfo(included=[0])]
303
 
        k._l = [('{', 0),
304
 
                "first line",
305
 
                ('}', 0),
306
 
                ('{', 1),
307
 
                "second line",
308
 
                ('}', 1)]
309
 
 
310
 
        self.assertEqual(k.get(1),
311
 
                         ["first line",
312
 
                          "second line"])
313
 
 
314
 
        self.assertEqual(k.get(0),
315
 
                         ["first line"])
316
 
 
317
 
        k.dump(self.TEST_LOG)
318
 
 
319
 
 
320
 
class DivergedIncludes(TestBase):
321
 
    """Weave with two diverged texts based on version 0.
322
 
    """
323
 
    def runTest(self):
324
 
        k = Weave()
325
 
 
326
 
        k._v = [VerInfo(),
327
 
                VerInfo(included=[0]),
328
 
                VerInfo(included=[0]),
329
 
                ]
330
 
        k._l = [('{', 0),
331
 
                "first line",
332
 
                ('}', 0),
333
 
                ('{', 1),
334
 
                "second line",
335
 
                ('}', 1),
336
 
                ('{', 2),
337
 
                "alternative second line",
338
 
                ('}', 2),                
339
 
                ]
340
 
 
341
 
        self.assertEqual(k.get(0),
342
 
                         ["first line"])
343
 
 
344
 
        self.assertEqual(k.get(1),
345
 
                         ["first line",
346
 
                          "second line"])
347
 
 
348
 
        self.assertEqual(k.get(2),
349
 
                         ["first line",
350
 
                          "alternative second line"])
351
 
 
352
 
def testweave():
353
 
    import testsweet
354
 
    from unittest import TestSuite, TestLoader
355
 
    import testweave
356
 
 
357
 
    tl = TestLoader()
358
 
    suite = TestSuite()
359
 
    suite.addTest(tl.loadTestsFromModule(testweave))
360
 
    
361
 
    return int(not testsweet.run_suite(suite)) # for shell 0=true
362
 
 
363
 
 
364
 
if __name__ == '__main__':
365
 
    import sys
366
 
    sys.exit(testweave())
367