/brz/remove-bazaar

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