13
15
# You should have received a copy of the GNU General Public License
 
14
16
# along with this program; if not, write to the Free Software
 
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
20
# TODO: tests regarding version names
 
19
 
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
 
 
21
# TODO: rbc 20050108 test that join does not leave an inconsistent weave 
 
22
24
"""test suite for weave algorithm"""
 
 
84
95
                         [('text0', TEXT_0[0])])
 
 
98
class StoreTwo(TestBase):
 
 
102
        idx = k.add_lines('text0', [], TEXT_0)
 
 
103
        self.assertEqual(idx, 0)
 
 
105
        idx = k.add_lines('text1', [], TEXT_1)
 
 
106
        self.assertEqual(idx, 1)
 
 
108
        self.assertEqual(k.get_lines(0), TEXT_0)
 
 
109
        self.assertEqual(k.get_lines(1), TEXT_1)
 
 
112
class GetSha1(TestBase):
 
 
113
    def test_get_sha1(self):
 
 
115
        k.add_lines('text0', [], 'text0')
 
 
116
        self.assertEqual('34dc0e430c642a26c3dd1c2beb7a8b4f4445eb79',
 
 
118
        self.assertRaises(errors.RevisionNotPresent,
 
 
120
        self.assertRaises(errors.RevisionNotPresent,
 
87
124
class InvalidAdd(TestBase):
 
88
125
    """Try to use invalid version number during add."""
 
89
126
    def runTest(self):
 
 
170
206
              ['text0', 'text1', 'text3'],
 
171
207
              ['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
 
173
 
        self.assertEqual(k.annotate('text4'),
 
 
209
        self.assertEqual(k.annotate('text4'), 
 
174
210
                         [('text0', 'line 1'),
 
175
211
                          ('text4', 'aaa'),
 
176
212
                          ('text3', 'middle line'),
 
 
609
645
            A Jug of Wine, a Loaf of Bread, -- and Thou
 
610
646
            Beside me singing in the Wilderness --
 
611
647
            Oh, Wilderness were Paradise enow!""",
 
613
649
            """A Book of Verses underneath the Bough,
 
614
650
            A Jug of Wine, a Loaf of Bread, -- and Thou
 
615
651
            Beside me singing in the Wilderness --
 
 
657
693
        self.weave1.add_lines('v1', [], self.lines1)
 
658
694
        self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
 
659
695
        self.weave1.add_lines('v3', ['v2'], self.lines3)
 
 
697
    def test_join_empty(self):
 
 
698
        """Join two empty weaves."""
 
 
699
        eq = self.assertEqual
 
 
705
    def test_join_empty_to_nonempty(self):
 
 
706
        """Join empty weave onto nonempty."""
 
 
707
        self.weave1.join(Weave())
 
 
708
        self.assertEqual(len(self.weave1), 3)
 
 
710
    def test_join_unrelated(self):
 
 
711
        """Join two weaves with no history in common."""
 
 
713
        wb.add_lines('b1', [], ['line from b\n'])
 
 
716
        eq = self.assertEqual
 
 
718
        eq(sorted(w1.versions()),
 
 
719
           ['b1', 'v1', 'v2', 'v3'])
 
 
721
    def test_join_related(self):
 
 
722
        wa = self.weave1.copy()
 
 
723
        wb = self.weave1.copy()
 
 
724
        wa.add_lines('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
 
 
725
        wb.add_lines('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
 
 
726
        eq = self.assertEquals
 
 
731
        eq(wa.get_lines('b1'),
 
 
732
           ['hello\n', 'pale blue\n', 'world\n'])
 
 
734
    def test_join_parent_disagreement(self):
 
 
735
        #join reconciles differening parents into a union.
 
 
738
        wa.add_lines('v1', [], ['hello\n'])
 
 
739
        wb.add_lines('v0', [], [])
 
 
740
        wb.add_lines('v1', ['v0'], ['hello\n'])
 
 
742
        self.assertEqual(['v0'], wa.get_parents('v1'))
 
 
744
    def test_join_text_disagreement(self):
 
 
745
        """Cannot join weaves with different texts for a version."""
 
 
748
        wa.add_lines('v1', [], ['hello\n'])
 
 
749
        wb.add_lines('v1', [], ['not\n', 'hello\n'])
 
 
750
        self.assertRaises(WeaveError,
 
 
753
    def test_join_unordered(self):
 
 
754
        """Join weaves where indexes differ.
 
 
756
        The source weave contains a different version at index 0."""
 
 
757
        wa = self.weave1.copy()
 
 
759
        wb.add_lines('x1', [], ['line from x1\n'])
 
 
760
        wb.add_lines('v1', [], ['hello\n'])
 
 
761
        wb.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
 
 
763
        eq = self.assertEquals
 
 
764
        eq(sorted(wa.versions()), ['v1', 'v2', 'v3', 'x1',])
 
 
765
        eq(wa.get_text('x1'), 'line from x1\n')
 
661
767
    def test_written_detection(self):
 
662
768
        # Test detection of weave file corruption.
 
 
707
813
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
 
710
 
class TestWeave(TestCase):
 
712
 
    def test_allow_reserved_false(self):
 
713
 
        w = Weave('name', allow_reserved=False)
 
714
 
        # Add lines is checked at the WeaveFile level, not at the Weave level
 
715
 
        w.add_lines('name:', [], TEXT_1)
 
716
 
        # But get_lines is checked at this level
 
717
 
        self.assertRaises(errors.ReservedId, w.get_lines, 'name:')
 
719
 
    def test_allow_reserved_true(self):
 
720
 
        w = Weave('name', allow_reserved=True)
 
721
 
        w.add_lines('name:', [], TEXT_1)
 
722
 
        self.assertEqual(TEXT_1, w.get_lines('name:'))
 
725
816
class InstrumentedWeave(Weave):
 
726
817
    """Keep track of how many times functions are called."""
 
728
819
    def __init__(self, weave_name=None):
 
729
820
        self._extract_count = 0
 
730
821
        Weave.__init__(self, weave_name=weave_name)
 
 
734
825
        return Weave._extract(self, versions)
 
 
828
class JoinOptimization(TestCase):
 
 
829
    """Test that Weave.join() doesn't extract all texts, only what must be done."""
 
 
832
        w1 = InstrumentedWeave()
 
 
833
        w2 = InstrumentedWeave()
 
 
836
        txt1 = ['a\n', 'b\n']
 
 
837
        txt2 = ['a\n', 'c\n']
 
 
838
        txt3 = ['a\n', 'b\n', 'c\n']
 
 
840
        w1.add_lines('txt0', [], txt0) # extract 1a
 
 
841
        w2.add_lines('txt0', [], txt0) # extract 1b
 
 
842
        w1.add_lines('txt1', ['txt0'], txt1)# extract 2a
 
 
843
        w2.add_lines('txt2', ['txt0'], txt2)# extract 2b
 
 
844
        w1.join(w2) # extract 3a to add txt2 
 
 
845
        w2.join(w1) # extract 3b to add txt1 
 
 
847
        w1.add_lines('txt3', ['txt1', 'txt2'], txt3) # extract 4a 
 
 
848
        w2.add_lines('txt3', ['txt2', 'txt1'], txt3) # extract 4b
 
 
849
        # These secretly have inverted parents
 
 
851
        # This should not have to do any extractions
 
 
852
        w1.join(w2) # NO extract, texts already present with same parents
 
 
853
        w2.join(w1) # NO extract, texts already present with same parents
 
 
855
        self.assertEqual(4, w1._extract_count)
 
 
856
        self.assertEqual(4, w2._extract_count)
 
 
858
    def test_double_parent(self):
 
 
859
        # It should not be considered illegal to add
 
 
860
        # a revision with the same parent twice
 
 
861
        w1 = InstrumentedWeave()
 
 
862
        w2 = InstrumentedWeave()
 
 
865
        txt1 = ['a\n', 'b\n']
 
 
866
        txt2 = ['a\n', 'c\n']
 
 
867
        txt3 = ['a\n', 'b\n', 'c\n']
 
 
869
        w1.add_lines('txt0', [], txt0)
 
 
870
        w2.add_lines('txt0', [], txt0)
 
 
871
        w1.add_lines('txt1', ['txt0'], txt1)
 
 
872
        w2.add_lines('txt1', ['txt0', 'txt0'], txt1)
 
 
873
        # Same text, effectively the same, because the
 
 
874
        # parent is only repeated
 
 
875
        w1.join(w2) # extract 3a to add txt2 
 
 
876
        w2.join(w1) # extract 3b to add txt1 
 
737
879
class TestNeedsReweave(TestCase):
 
738
880
    """Internal corner cases for when reweave is needed."""