/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_knit.py

  • Committer: Aaron Bentley
  • Date: 2006-05-20 17:51:13 UTC
  • mfrom: (1718 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1727.
  • Revision ID: aaron.bentley@utoronto.ca-20060520175113-4549e0023f9210bf
Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
 
23
23
from bzrlib.errors import KnitError, RevisionAlreadyPresent
24
 
from bzrlib.knit import KnitVersionedFile, KnitPlainFactory, KnitAnnotateFactory
 
24
from bzrlib.knit import (
 
25
    KnitVersionedFile,
 
26
    KnitPlainFactory,
 
27
    KnitAnnotateFactory,
 
28
    WeaveToKnit)
25
29
from bzrlib.osutils import split_lines
26
 
from bzrlib.tests import TestCaseInTempDir
 
30
from bzrlib.tests import TestCaseWithTransport
27
31
from bzrlib.transport import TransportLogger
28
32
from bzrlib.transport.local import LocalTransport
29
33
from bzrlib.transport.memory import MemoryTransport
30
 
 
31
 
 
32
 
class KnitTests(TestCaseInTempDir):
33
 
 
34
 
    def add_stock_one_and_one_a(self, k):
35
 
        k.add_lines('text-1', [], split_lines(TEXT_1))
36
 
        k.add_lines('text-1a', ['text-1'], split_lines(TEXT_1A))
37
 
 
38
 
    def test_knit_constructor(self):
39
 
        """Construct empty k"""
40
 
        self.make_test_knit()
 
34
from bzrlib.weave import Weave
 
35
 
 
36
 
 
37
class KnitTests(TestCaseWithTransport):
 
38
    """Class containing knit test helper routines."""
41
39
 
42
40
    def make_test_knit(self, annotate=False):
43
41
        if not annotate:
46
44
            factory = None
47
45
        return KnitVersionedFile('test', LocalTransport('.'), access_mode='w', factory=factory, create=True)
48
46
 
 
47
 
 
48
class BasicKnitTests(KnitTests):
 
49
 
 
50
    def add_stock_one_and_one_a(self, k):
 
51
        k.add_lines('text-1', [], split_lines(TEXT_1))
 
52
        k.add_lines('text-1a', ['text-1'], split_lines(TEXT_1A))
 
53
 
 
54
    def test_knit_constructor(self):
 
55
        """Construct empty k"""
 
56
        self.make_test_knit()
 
57
 
49
58
    def test_knit_add(self):
50
59
        """Store one text in knit and retrieve"""
51
60
        k = self.make_test_knit()
54
63
        self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
55
64
 
56
65
    def test_knit_reload(self):
57
 
        """Store and reload a knit"""
 
66
        # test that the content in a reloaded knit is correct
58
67
        k = self.make_test_knit()
59
68
        k.add_lines('text-1', [], split_lines(TEXT_1))
60
69
        del k
89
98
        k = KnitVersionedFile('test', LocalTransport('.'), delta=False, create=True)
90
99
        k.add_lines('text-1', [], ['a\n',    'b'  ])
91
100
        k.add_lines('text-2', ['text-1'], ['a\rb\n', 'b\n'])
 
101
        # reopening ensures maximum room for confusion
 
102
        k = KnitVersionedFile('test', LocalTransport('.'), delta=False, create=True)
92
103
        self.assertEquals(k.get_lines('text-1'), ['a\n',    'b'  ])
93
104
        self.assertEquals(k.get_lines('text-2'), ['a\rb\n', 'b\n'])
94
105
 
334
345
        self.assertEqual(k1.delta, k2.delta)
335
346
        # the generic test checks for empty content and file class
336
347
 
 
348
    def test_knit_format(self):
 
349
        # this tests that a new knit index file has the expected content
 
350
        # and that is writes the data we expect as records are added.
 
351
        knit = self.make_test_knit(True)
 
352
        self.assertFileEqual("# bzr knit index 8\n", 'test.kndx')
 
353
        knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
 
354
        self.assertFileEqual(
 
355
            "# bzr knit index 8\n"
 
356
            "\n"
 
357
            "revid fulltext 0 84 .a_ghost :",
 
358
            'test.kndx')
 
359
        knit.add_lines_with_ghosts('revid2', ['revid'], ['a\n'])
 
360
        self.assertFileEqual(
 
361
            "# bzr knit index 8\n"
 
362
            "\nrevid fulltext 0 84 .a_ghost :"
 
363
            "\nrevid2 line-delta 84 82 0 :",
 
364
            'test.kndx')
 
365
        # we should be able to load this file again
 
366
        knit = KnitVersionedFile('test', LocalTransport('.'), access_mode='r')
 
367
        self.assertEqual(['revid', 'revid2'], knit.versions())
 
368
        # write a short write to the file and ensure that its ignored
 
369
        indexfile = file('test.kndx', 'at')
 
370
        indexfile.write('\nrevid3 line-delta 166 82 1 2 3 4 5 .phwoar:demo ')
 
371
        indexfile.close()
 
372
        # we should be able to load this file again
 
373
        knit = KnitVersionedFile('test', LocalTransport('.'), access_mode='w')
 
374
        self.assertEqual(['revid', 'revid2'], knit.versions())
 
375
        # and add a revision with the same id the failed write had
 
376
        knit.add_lines('revid3', ['revid2'], ['a\n'])
 
377
        # and when reading it revid3 should now appear.
 
378
        knit = KnitVersionedFile('test', LocalTransport('.'), access_mode='r')
 
379
        self.assertEqual(['revid', 'revid2', 'revid3'], knit.versions())
 
380
        self.assertEqual(['revid2'], knit.get_parents('revid3'))
 
381
 
 
382
    def test_plan_merge(self):
 
383
        my_knit = self.make_test_knit(annotate=True)
 
384
        my_knit.add_lines('text1', [], split_lines(TEXT_1))
 
385
        my_knit.add_lines('text1a', ['text1'], split_lines(TEXT_1A))
 
386
        my_knit.add_lines('text1b', ['text1'], split_lines(TEXT_1B))
 
387
        plan = list(my_knit.plan_merge('text1a', 'text1b'))
 
388
        for plan_line, expected_line in zip(plan, AB_MERGE):
 
389
            self.assertEqual(plan_line, expected_line)
 
390
 
337
391
 
338
392
TEXT_1 = """\
339
393
Banana cup cakes:
353
407
- self-raising flour
354
408
"""
355
409
 
 
410
TEXT_1B = """\
 
411
Banana cup cake recipe
 
412
 
 
413
- bananas (do not use plantains!!!)
 
414
- broken tea cups
 
415
- flour
 
416
"""
 
417
 
356
418
delta_1_1a = """\
357
419
0,1,2
358
420
Banana cup cake recipe
371
433
- mushrooms
372
434
"""
373
435
 
 
436
AB_MERGE_TEXT="""unchanged|Banana cup cake recipe
 
437
new-a|(serves 6)
 
438
unchanged|
 
439
killed-b|- bananas
 
440
killed-b|- eggs
 
441
new-b|- bananas (do not use plantains!!!)
 
442
unchanged|- broken tea cups
 
443
new-a|- self-raising flour
 
444
new-b|- flour
 
445
"""
 
446
AB_MERGE=[tuple(l.split('|')) for l in AB_MERGE_TEXT.splitlines(True)]
 
447
 
 
448
 
374
449
def line_delta(from_lines, to_lines):
375
450
    """Generate line-based delta from one text to another"""
376
451
    s = difflib.SequenceMatcher(None, from_lines, to_lines)
399
474
        i = i + c
400
475
        offset = offset + (b - a) + c
401
476
    return out
 
477
 
 
478
 
 
479
class TestWeaveToKnit(KnitTests):
 
480
 
 
481
    def test_weave_to_knit_matches(self):
 
482
        # check that the WeaveToKnit is_compatible function
 
483
        # registers True for a Weave to a Knit.
 
484
        w = Weave()
 
485
        k = self.make_test_knit()
 
486
        self.failUnless(WeaveToKnit.is_compatible(w, k))
 
487
        self.failIf(WeaveToKnit.is_compatible(k, w))
 
488
        self.failIf(WeaveToKnit.is_compatible(w, w))
 
489
        self.failIf(WeaveToKnit.is_compatible(k, k))