/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.1.55 by Martin Pool
doc
1
#! /usr/bin/python2.4
2
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
3
# Copyright (C) 2005 by Canonical Ltd
4
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.
9
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.
14
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
18
19
20
21
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
22
"""test suite for weave algorithm"""
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
23
24
25
from testsweet import TestBase
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
26
from weave import Weave, VerInfo, WeaveFormatError
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
27
0.1.52 by Martin Pool
Update tests for new weave representation
28
# XXX: If we do weaves this way, will a merge still behave the same
29
# way if it's done in a different order?  That's a pretty desirable
30
# property.
31
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
32
33
# texts for use in testing
0.1.3 by Martin Pool
Change storage of texts for testing
34
TEXT_0 = ["Hello world"]
35
TEXT_1 = ["Hello world",
36
          "A second line"]
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
37
38
39
class Easy(TestBase):
40
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
41
        k = Weave()
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
42
43
44
class StoreText(TestBase):
45
    """Store and retrieve a simple text."""
46
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
47
        k = Weave()
0.1.26 by Martin Pool
Refactor parameters to add command
48
        idx = k.add([], TEXT_0)
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
49
        self.assertEqual(k.get(idx), TEXT_0)
50
        self.assertEqual(idx, 0)
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
51
52
0.1.7 by Martin Pool
Add trivial annotate text
53
54
class AnnotateOne(TestBase):
55
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
56
        k = Weave()
0.1.26 by Martin Pool
Refactor parameters to add command
57
        k.add([], TEXT_0)
0.1.7 by Martin Pool
Add trivial annotate text
58
        self.assertEqual(k.annotate(0),
59
                         [(0, TEXT_0[0])])
60
61
0.1.5 by Martin Pool
Add test for storing two text versions.
62
class StoreTwo(TestBase):
63
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
64
        k = Weave()
0.1.5 by Martin Pool
Add test for storing two text versions.
65
0.1.26 by Martin Pool
Refactor parameters to add command
66
        idx = k.add([], TEXT_0)
0.1.5 by Martin Pool
Add test for storing two text versions.
67
        self.assertEqual(idx, 0)
68
0.1.26 by Martin Pool
Refactor parameters to add command
69
        idx = k.add([], TEXT_1)
0.1.5 by Martin Pool
Add test for storing two text versions.
70
        self.assertEqual(idx, 1)
71
72
        self.assertEqual(k.get(0), TEXT_0)
73
        self.assertEqual(k.get(1), TEXT_1)
74
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
75
        k.dump(self.TEST_LOG)
76
77
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
78
0.1.55 by Martin Pool
doc
79
class DeltaAdd(TestBase):
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
80
    """Detection of changes prior to inserting new revision."""
81
    def runTest(self):
82
        from pprint import pformat
83
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
84
        k = Weave()
0.1.26 by Martin Pool
Refactor parameters to add command
85
        k.add([], ['line 1'])
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
86
0.1.52 by Martin Pool
Update tests for new weave representation
87
        self.assertEqual(k._l,
88
                         [('{', 0),
89
                          'line 1',
90
                          ('}', 0),
91
                          ])
92
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
93
        changes = list(k._delta(set([0]),
94
                                ['line 1',
95
                                 'new line']))
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
96
97
        self.log('raw changes: ' + pformat(changes))
98
0.1.52 by Martin Pool
Update tests for new weave representation
99
        # currently there are 3 lines in the weave, and we insert after them
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
100
        self.assertEquals(changes,
0.1.52 by Martin Pool
Update tests for new weave representation
101
                          [(3, 3, ['new line'])])
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
102
0.1.24 by Martin Pool
Add another change for delta of new version.
103
        changes = k._delta(set([0]),
104
                           ['top line',
105
                            'line 1'])
106
        
107
        self.assertEquals(list(changes),
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
108
                          [(1, 1, ['top line'])])
0.1.24 by Martin Pool
Add another change for delta of new version.
109
110
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
111
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
112
class InvalidAdd(TestBase):
113
    """Try to use invalid version number during add."""
114
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
115
        k = Weave()
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
116
117
        self.assertRaises(IndexError,
118
                          k.add,
119
                          [69],
120
                          ['new text!'])
121
122
0.1.26 by Martin Pool
Refactor parameters to add command
123
class InsertLines(TestBase):
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
124
    """Store a revision that adds one line to the original.
125
126
    Look at the annotations to make sure that the first line is matched
127
    and not stored repeatedly."""
128
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
129
        k = Weave()
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
130
0.1.26 by Martin Pool
Refactor parameters to add command
131
        k.add([], ['line 1'])
132
        k.add([0], ['line 1', 'line 2'])
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
133
134
        self.assertEqual(k.annotate(0),
135
                         [(0, 'line 1')])
136
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
137
        self.assertEqual(k.get(1),
138
                         ['line 1',
139
                          'line 2'])
140
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
141
        self.assertEqual(k.annotate(1),
142
                         [(0, 'line 1'),
143
                          (1, 'line 2')])
144
0.1.28 by Martin Pool
More tests for insertion of lines in new versions.
145
        k.add([0], ['line 1', 'diverged line'])
146
147
        self.assertEqual(k.annotate(2),
148
                         [(0, 'line 1'),
149
                          (2, 'diverged line')])
150
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
151
        text3 = ['line 1', 'middle line', 'line 2']
0.1.28 by Martin Pool
More tests for insertion of lines in new versions.
152
        k.add([0, 1],
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
153
              text3)
154
155
        from pprint import pformat
156
157
        self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
158
159
        self.log("k._l=" + pformat(k._l))
0.1.28 by Martin Pool
More tests for insertion of lines in new versions.
160
161
        self.assertEqual(k.annotate(3),
162
                         [(0, 'line 1'),
163
                          (3, 'middle line'),
164
                          (1, 'line 2')])
165
0.1.31 by Martin Pool
Fix insertion of multiple regions, calculating the right line offset as we go.
166
        # now multiple insertions at different places
167
        k.add([0, 1, 3],
168
              ['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
169
170
        self.assertEqual(k.annotate(4), 
171
                         [(0, 'line 1'),
172
                          (4, 'aaa'),
173
                          (3, 'middle line'),
174
                          (4, 'bbb'),
175
                          (1, 'line 2'),
176
                          (4, 'ccc')])
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
177
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
178
0.1.48 by Martin Pool
Basic parsing of delete instructions.
179
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
180
class DeleteLines(TestBase):
181
    """Deletion of lines from existing text.
182
183
    Try various texts all based on a common ancestor."""
184
    def runTest(self):
185
        k = Weave()
186
187
        base_text = ['one', 'two', 'three', 'four']
188
189
        k.add([], base_text)
190
        
191
        texts = [['one', 'two', 'three'],
192
                 ['two', 'three', 'four'],
193
                 ['one', 'four'],
194
                 ['one', 'two', 'three', 'four'],
195
                 ]
196
197
        for t in texts:
198
            ver = k.add([0], t)
199
200
        from pprint import pformat
201
        self.log('final weave:')
202
        self.log('k._l=' + pformat(k._l))
203
204
        for i in range(len(texts)):
205
            self.assertEqual(k.get(i+1),
206
                             texts[i])
207
            
208
209
210
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
211
class SuicideDelete(TestBase):
0.1.55 by Martin Pool
doc
212
    """Invalid weave which tries to add and delete simultaneously."""
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
213
    def runTest(self):
214
        k = Weave()
215
216
        k._v = [VerInfo([]),
217
                ]
218
        k._l = [('{', 0),
219
                'first line',
220
                ('[', 0),
221
                'deleted in 0',
222
                (']', 0),
223
                ('}', 0),
224
                ]
225
226
        self.assertRaises(WeaveFormatError,
227
                          k.get,
228
                          0)        
229
230
231
0.1.48 by Martin Pool
Basic parsing of delete instructions.
232
class CannedDelete(TestBase):
233
    """Unpack canned weave with deleted lines."""
234
    def runTest(self):
235
        k = Weave()
236
237
        k._v = [VerInfo([]),
238
                VerInfo([0]),
239
                ]
240
        k._l = [('{', 0),
241
                'first line',
242
                ('[', 1),
243
                'line to be deleted',
244
                (']', 1),
245
                'last line',
246
                ('}', 0),
247
                ]
248
249
        self.assertEqual(k.get(0),
250
                         ['first line',
251
                          'line to be deleted',
252
                          'last line',
253
                          ])
254
0.1.50 by Martin Pool
Basic implementation of deletion markers
255
        self.assertEqual(k.get(1),
256
                         ['first line',
257
                          'last line',
258
                          ])
259
0.1.48 by Martin Pool
Basic parsing of delete instructions.
260
261
0.1.51 by Martin Pool
Add test for replacement lines
262
class CannedReplacement(TestBase):
263
    """Unpack canned weave with deleted lines."""
264
    def runTest(self):
265
        k = Weave()
266
267
        k._v = [VerInfo([]),
268
                VerInfo([0]),
269
                ]
270
        k._l = [('{', 0),
271
                'first line',
272
                ('[', 1),
273
                'line to be deleted',
274
                (']', 1),
275
                ('{', 1),
276
                'replacement line',                
277
                ('}', 1),
278
                'last line',
279
                ('}', 0),
280
                ]
281
282
        self.assertEqual(k.get(0),
283
                         ['first line',
284
                          'line to be deleted',
285
                          'last line',
286
                          ])
287
288
        self.assertEqual(k.get(1),
289
                         ['first line',
290
                          'replacement line',
291
                          'last line',
292
                          ])
293
294
295
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
296
class BadWeave(TestBase):
297
    """Test that we trap an insert which should not occur."""
298
    def runTest(self):
299
        k = Weave()
300
301
        k._v = [VerInfo([]),
302
                ]
303
        k._l = ['bad line',
304
                ('{', 0),
305
                'foo {',
306
                ('{', 1),
307
                '  added in version 1',
308
                ('{', 2),
309
                '  added in v2',
310
                ('}', 2),
311
                '  also from v1',
312
                ('}', 1),
313
                '}',
314
                ('}', 0)]
315
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
316
        self.assertRaises(WeaveFormatError,
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
317
                          k.get,
318
                          0)
319
320
321
class BadInsert(TestBase):
322
    """Test that we trap an insert which should not occur."""
323
    def runTest(self):
324
        k = Weave()
325
326
        k._v = [VerInfo([]),
327
                VerInfo([0]),
328
                VerInfo([0]),
329
                VerInfo([0,1,2]),
330
                ]
331
        k._l = [('{', 0),
332
                'foo {',
333
                ('{', 1),
334
                '  added in version 1',
335
                ('{', 1),
336
                '  more in 1',
337
                ('}', 1),
338
                ('}', 1),
339
                ('}', 0)]
340
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
341
        self.assertRaises(WeaveFormatError,
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
342
                          k.get,
343
                          0)
344
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
345
        self.assertRaises(WeaveFormatError,
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
346
                          k.get,
347
                          1)
348
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
349
350
class InsertNested(TestBase):
351
    """Insertion with nested instructions."""
352
    def runTest(self):
353
        k = Weave()
354
355
        k._v = [VerInfo([]),
356
                VerInfo([0]),
357
                VerInfo([0]),
0.1.44 by Martin Pool
More tests for nested insert instructions
358
                VerInfo([0,1,2]),
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
359
                ]
360
        k._l = [('{', 0),
361
                'foo {',
362
                ('{', 1),
363
                '  added in version 1',
0.1.42 by Martin Pool
More tests for nested insert instructions
364
                ('{', 2),
365
                '  added in v2',
366
                ('}', 2),
367
                '  also from v1',
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
368
                ('}', 1),
369
                '}',
370
                ('}', 0)]
371
372
        self.assertEqual(k.get(0),
373
                         ['foo {',
374
                          '}'])
375
376
        self.assertEqual(k.get(1),
377
                         ['foo {',
378
                          '  added in version 1',
0.1.42 by Martin Pool
More tests for nested insert instructions
379
                          '  also from v1',
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
380
                          '}'])
381
                       
0.1.44 by Martin Pool
More tests for nested insert instructions
382
        self.assertEqual(k.get(2),
383
                         ['foo {',
384
                          '  added in v2',
385
                          '}'])
386
387
        self.assertEqual(k.get(3),
388
                         ['foo {',
389
                          '  added in version 1',
390
                          '  added in v2',
391
                          '  also from v1',
392
                          '}'])
393
                         
0.1.45 by Martin Pool
doc
394
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
395
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
396
class DeleteLines2(TestBase):
0.1.30 by Martin Pool
Start adding tests for line deletion
397
    """Test recording revisions that delete lines.
398
399
    This relies on the weave having a way to represent lines knocked
400
    out by a later revision."""
401
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
402
        k = Weave()
0.1.30 by Martin Pool
Start adding tests for line deletion
403
404
        k.add([], ["line the first",
405
                   "line 2",
406
                   "line 3",
407
                   "fine"])
408
409
        self.assertEqual(len(k.get(0)), 4)
410
411
        k.add([0], ["line the first",
412
                   "fine"])
413
414
        self.assertEqual(k.get(1),
415
                         ["line the first",
416
                          "fine"])
417
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
418
        self.assertEqual(k.annotate(1),
419
                         [(0, "line the first"),
420
                          (0, "fine")])
421
0.1.30 by Martin Pool
Start adding tests for line deletion
422
0.1.26 by Martin Pool
Refactor parameters to add command
423
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
424
class IncludeVersions(TestBase):
425
    """Check texts that are stored across multiple revisions.
426
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
427
    Here we manually create a weave with particular encoding and make
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
428
    sure it unpacks properly.
429
430
    Text 0 includes nothing; text 1 includes text 0 and adds some
431
    lines.
432
    """
433
434
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
435
        k = Weave()
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
436
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
437
        k._v = [VerInfo(), VerInfo(included=[0])]
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
438
        k._l = [('{', 0),
439
                "first line",
440
                ('}', 0),
441
                ('{', 1),
442
                "second line",
443
                ('}', 1)]
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
444
445
        self.assertEqual(k.get(1),
446
                         ["first line",
447
                          "second line"])
448
449
        self.assertEqual(k.get(0),
450
                         ["first line"])
451
452
        k.dump(self.TEST_LOG)
453
0.1.5 by Martin Pool
Add test for storing two text versions.
454
0.1.14 by Martin Pool
Another test for version inclusion
455
class DivergedIncludes(TestBase):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
456
    """Weave with two diverged texts based on version 0.
0.1.14 by Martin Pool
Another test for version inclusion
457
    """
458
    def runTest(self):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
459
        k = Weave()
0.1.14 by Martin Pool
Another test for version inclusion
460
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
461
        k._v = [VerInfo(),
462
                VerInfo(included=[0]),
463
                VerInfo(included=[0]),
464
                ]
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
465
        k._l = [('{', 0),
466
                "first line",
467
                ('}', 0),
468
                ('{', 1),
469
                "second line",
470
                ('}', 1),
471
                ('{', 2),
472
                "alternative second line",
473
                ('}', 2),                
474
                ]
0.1.14 by Martin Pool
Another test for version inclusion
475
476
        self.assertEqual(k.get(0),
477
                         ["first line"])
478
479
        self.assertEqual(k.get(1),
480
                         ["first line",
481
                          "second line"])
482
483
        self.assertEqual(k.get(2),
484
                         ["first line",
485
                          "alternative second line"])
486
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
487
def testweave():
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
488
    import testsweet
489
    from unittest import TestSuite, TestLoader
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
490
    import testweave
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
491
492
    tl = TestLoader()
493
    suite = TestSuite()
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
494
    suite.addTest(tl.loadTestsFromModule(testweave))
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
495
    
0.1.15 by Martin Pool
Fix inverted shell return code for testknit
496
    return int(not testsweet.run_suite(suite)) # for shell 0=true
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
497
498
499
if __name__ == '__main__':
500
    import sys
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
501
    sys.exit(testweave())
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
502