3
# Copyright (C) 2005 by Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
22
"""test suite for weave algorithm"""
 
 
26
from bzrlib.weave import Weave, WeaveFormatError
 
 
27
from bzrlib.weavefile import write_weave, read_weave
 
 
28
from pprint import pformat
 
 
36
    from sets import Set, ImmutableSet
 
 
38
    frozenset = ImmutableSet
 
 
43
# texts for use in testing
 
 
44
TEXT_0 = ["Hello world"]
 
 
45
TEXT_1 = ["Hello world",
 
 
50
class TestBase(testsweet.TestBase):
 
 
51
    def check_read_write(self, k):
 
 
52
        """Check the weave k can be written & re-read."""
 
 
53
        from tempfile import TemporaryFile
 
 
62
            self.log('serialized weave:')
 
 
64
            self.fail('read/write check failed')
 
 
74
class StoreText(TestBase):
 
 
75
    """Store and retrieve a simple text."""
 
 
78
        idx = k.add([], TEXT_0)
 
 
79
        self.assertEqual(k.get(idx), TEXT_0)
 
 
80
        self.assertEqual(idx, 0)
 
 
84
class AnnotateOne(TestBase):
 
 
88
        self.assertEqual(k.annotate(0),
 
 
92
class StoreTwo(TestBase):
 
 
96
        idx = k.add([], TEXT_0)
 
 
97
        self.assertEqual(idx, 0)
 
 
99
        idx = k.add([], TEXT_1)
 
 
100
        self.assertEqual(idx, 1)
 
 
102
        self.assertEqual(k.get(0), TEXT_0)
 
 
103
        self.assertEqual(k.get(1), TEXT_1)
 
 
105
        k.dump(self.TEST_LOG)
 
 
109
class DeltaAdd(TestBase):
 
 
110
    """Detection of changes prior to inserting new revision."""
 
 
113
        k.add([], ['line 1'])
 
 
115
        self.assertEqual(k._l,
 
 
121
        changes = list(k._delta(set([0]),
 
 
125
        self.log('raw changes: ' + pformat(changes))
 
 
127
        # currently there are 3 lines in the weave, and we insert after them
 
 
128
        self.assertEquals(changes,
 
 
129
                          [(3, 3, ['new line'])])
 
 
131
        changes = k._delta(set([0]),
 
 
135
        self.assertEquals(list(changes),
 
 
136
                          [(1, 1, ['top line'])])
 
 
138
        self.check_read_write(k)
 
 
141
class InvalidAdd(TestBase):
 
 
142
    """Try to use invalid version number during add."""
 
 
146
        self.assertRaises(ValueError,
 
 
152
class InsertLines(TestBase):
 
 
153
    """Store a revision that adds one line to the original.
 
 
155
    Look at the annotations to make sure that the first line is matched
 
 
156
    and not stored repeatedly."""
 
 
160
        k.add([], ['line 1'])
 
 
161
        k.add([0], ['line 1', 'line 2'])
 
 
163
        self.assertEqual(k.annotate(0),
 
 
166
        self.assertEqual(k.get(1),
 
 
170
        self.assertEqual(k.annotate(1),
 
 
174
        k.add([0], ['line 1', 'diverged line'])
 
 
176
        self.assertEqual(k.annotate(2),
 
 
178
                          (2, 'diverged line')])
 
 
180
        text3 = ['line 1', 'middle line', 'line 2']
 
 
184
        self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
 
 
186
        self.log("k._l=" + pformat(k._l))
 
 
188
        self.assertEqual(k.annotate(3),
 
 
193
        # now multiple insertions at different places
 
 
195
              ['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
 
 
197
        self.assertEqual(k.annotate(4), 
 
 
207
class DeleteLines(TestBase):
 
 
208
    """Deletion of lines from existing text.
 
 
210
    Try various texts all based on a common ancestor."""
 
 
214
        base_text = ['one', 'two', 'three', 'four']
 
 
218
        texts = [['one', 'two', 'three'],
 
 
219
                 ['two', 'three', 'four'],
 
 
221
                 ['one', 'two', 'three', 'four'],
 
 
227
        self.log('final weave:')
 
 
228
        self.log('k._l=' + pformat(k._l))
 
 
230
        for i in range(len(texts)):
 
 
231
            self.assertEqual(k.get(i+1),
 
 
237
class SuicideDelete(TestBase):
 
 
238
    """Invalid weave which tries to add and delete simultaneously."""
 
 
251
        ################################### SKIPPED
 
 
252
        # Weave.get doesn't trap this anymore
 
 
255
        self.assertRaises(WeaveFormatError,
 
 
261
class CannedDelete(TestBase):
 
 
262
    """Unpack canned weave with deleted lines."""
 
 
272
                'line to be deleted',
 
 
278
        self.assertEqual(k.get(0),
 
 
280
                          'line to be deleted',
 
 
284
        self.assertEqual(k.get(1),
 
 
291
class CannedReplacement(TestBase):
 
 
292
    """Unpack canned weave with deleted lines."""
 
 
302
                'line to be deleted',
 
 
311
        self.assertEqual(k.get(0),
 
 
313
                          'line to be deleted',
 
 
317
        self.assertEqual(k.get(1),
 
 
325
class BadWeave(TestBase):
 
 
326
    """Test that we trap an insert which should not occur."""
 
 
336
                '  added in version 1',
 
 
345
        ################################### SKIPPED
 
 
346
        # Weave.get doesn't trap this anymore
 
 
350
        self.assertRaises(WeaveFormatError,
 
 
355
class BadInsert(TestBase):
 
 
356
    """Test that we trap an insert which should not occur."""
 
 
368
                '  added in version 1',
 
 
376
        # this is not currently enforced by get
 
 
377
        return  ##########################################
 
 
379
        self.assertRaises(WeaveFormatError,
 
 
383
        self.assertRaises(WeaveFormatError,
 
 
388
class InsertNested(TestBase):
 
 
389
    """Insertion with nested instructions."""
 
 
401
                '  added in version 1',
 
 
410
        self.assertEqual(k.get(0),
 
 
414
        self.assertEqual(k.get(1),
 
 
416
                          '  added in version 1',
 
 
420
        self.assertEqual(k.get(2),
 
 
425
        self.assertEqual(k.get(3),
 
 
427
                          '  added in version 1',
 
 
434
class DeleteLines2(TestBase):
 
 
435
    """Test recording revisions that delete lines.
 
 
437
    This relies on the weave having a way to represent lines knocked
 
 
438
    out by a later revision."""
 
 
442
        k.add([], ["line the first",
 
 
447
        self.assertEqual(len(k.get(0)), 4)
 
 
449
        k.add([0], ["line the first",
 
 
452
        self.assertEqual(k.get(1),
 
 
456
        self.assertEqual(k.annotate(1),
 
 
457
                         [(0, "line the first"),
 
 
462
class IncludeVersions(TestBase):
 
 
463
    """Check texts that are stored across multiple revisions.
 
 
465
    Here we manually create a weave with particular encoding and make
 
 
466
    sure it unpacks properly.
 
 
468
    Text 0 includes nothing; text 1 includes text 0 and adds some
 
 
475
        k._v = [frozenset(), frozenset([0])]
 
 
483
        self.assertEqual(k.get(1),
 
 
487
        self.assertEqual(k.get(0),
 
 
490
        k.dump(self.TEST_LOG)
 
 
493
class DivergedIncludes(TestBase):
 
 
494
    """Weave with two diverged texts based on version 0.
 
 
510
                "alternative second line",
 
 
514
        self.assertEqual(k.get(0),
 
 
517
        self.assertEqual(k.get(1),
 
 
521
        self.assertEqual(k.get(2),
 
 
523
                          "alternative second line"])
 
 
525
        self.assertEqual(k.inclusions([2]),
 
 
530
class ReplaceLine(TestBase):
 
 
534
        text0 = ['cheddar', 'stilton', 'gruyere']
 
 
535
        text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
 
 
540
        self.log('k._l=' + pformat(k._l))
 
 
542
        self.assertEqual(k.get(0), text0)
 
 
543
        self.assertEqual(k.get(1), text1)
 
 
547
class Merge(TestBase):
 
 
548
    """Storage of versions that merge diverged parents"""
 
 
553
                 ['header', '', 'line from 1'],
 
 
554
                 ['header', '', 'line from 2', 'more from 2'],
 
 
555
                 ['header', '', 'line from 1', 'fixup line', 'line from 2'],
 
 
561
        k.add([0, 1, 2], texts[3])
 
 
563
        for i, t in enumerate(texts):
 
 
564
            self.assertEqual(k.get(i), t)
 
 
566
        self.assertEqual(k.annotate(3),
 
 
574
        self.assertEqual(k.inclusions([3]),
 
 
577
        self.log('k._l=' + pformat(k._l))
 
 
579
        self.check_read_write(k)
 
 
582
class Conflicts(TestBase):
 
 
583
    """Test detection of conflicting regions during a merge.
 
 
585
    A base version is inserted, then two descendents try to
 
 
586
    insert different lines in the same place.  These should be
 
 
587
    reported as a possible conflict and forwarded to the user."""
 
 
592
        k.add([], ['aaa', 'bbb'])
 
 
593
        k.add([0], ['aaa', '111', 'bbb'])
 
 
594
        k.add([1], ['aaa', '222', 'bbb'])
 
 
596
        merged = k.merge([1, 2])
 
 
598
        self.assertEquals([[['aaa']],
 
 
604
class NonConflict(TestBase):
 
 
605
    """Two descendants insert compatible changes.
 
 
607
    No conflict should be reported."""
 
 
612
        k.add([], ['aaa', 'bbb'])
 
 
613
        k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
 
 
614
        k.add([1], ['aaa', 'ccc', 'bbb', '222'])
 
 
620
class AutoMerge(TestBase):
 
 
624
        texts = [['header', 'aaa', 'bbb'],
 
 
625
                 ['header', 'aaa', 'line from 1', 'bbb'],
 
 
626
                 ['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
 
 
633
        self.log('k._l=' + pformat(k._l))
 
 
635
        m = list(k.mash_iter([0, 1, 2]))
 
 
641
                          'line from 2', 'more from 2'])
 
 
645
class Khayyam(TestBase):
 
 
646
    """Test changes to multi-line texts, and read/write"""
 
 
649
            """A Book of Verses underneath the Bough,
 
 
650
            A Jug of Wine, a Loaf of Bread, -- and Thou
 
 
651
            Beside me singing in the Wilderness --
 
 
652
            Oh, Wilderness were Paradise enow!""",
 
 
654
            """A Book of Verses underneath the Bough,
 
 
655
            A Jug of Wine, a Loaf of Bread, -- and Thou
 
 
656
            Beside me singing in the Wilderness --
 
 
657
            Oh, Wilderness were Paradise now!""",
 
 
659
            """A Book of poems underneath the tree,
 
 
660
            A Jug of Wine, a Loaf of Bread,
 
 
662
            Beside me singing in the Wilderness --
 
 
663
            Oh, Wilderness were Paradise now!
 
 
667
            """A Book of Verses underneath the Bough,
 
 
668
            A Jug of Wine, a Loaf of Bread,
 
 
670
            Beside me singing in the Wilderness --
 
 
671
            Oh, Wilderness were Paradise now!""",
 
 
673
        texts = [[l.strip() for l in t.split('\n')] for t in rawtexts]
 
 
678
            ver = k.add(list(parents), t)
 
 
681
        self.log("k._l=" + pformat(k._l))
 
 
683
        for i, t in enumerate(texts):
 
 
684
            self.assertEqual(k.get(i), t)
 
 
686
        self.check_read_write(k)
 
 
691
    from unittest import TestSuite, TestLoader
 
 
696
    suite.addTest(tl.loadTestsFromModule(testweave))
 
 
698
    return int(not testsweet.run_suite(suite)) # for shell 0=true
 
 
701
if __name__ == '__main__':
 
 
703
    sys.exit(testweave())