~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: John Arbash Meinel
  • Date: 2007-12-19 16:45:03 UTC
  • mto: This revision was merged to the branch mainline in revision 3131.
  • Revision ID: john@arbash-meinel.com-20071219164503-dxzdtrm0i3jet9ea
Cleanup the test cases (Andrew)

Show diffs side-by-side

added added

removed removed

Lines of Context:
744
744
        # This is what it currently gives:
745
745
        test_one('aBccDe', 'abccde', [(0,0), (5,5)])
746
746
 
 
747
    def assertDiffBlocks(self, a, b, expected_blocks):
 
748
        """Check that the sequence matcher returns the correct blocks.
 
749
 
 
750
        :param a: A sequence to match
 
751
        :param b: Another sequence to match
 
752
        :param expected_blocks: The expected output, not including the final
 
753
            matching block (len(a), len(b), 0)
 
754
        """
 
755
        matcher = self._PatienceSequenceMatcher(None, a, b)
 
756
        blocks = matcher.get_matching_blocks()
 
757
        last = blocks.pop()
 
758
        self.assertEqual((len(a), len(b), 0), last)
 
759
        self.assertEqual(expected_blocks, blocks)
 
760
 
747
761
    def test_matching_blocks(self):
748
 
        def chk_blocks(a, b, expected_blocks):
749
 
            # difflib always adds a signature of the total
750
 
            # length, with no matching entries at the end
751
 
            s = self._PatienceSequenceMatcher(None, a, b)
752
 
            blocks = s.get_matching_blocks()
753
 
            self.assertEquals((len(a), len(b), 0), blocks[-1])
754
 
            self.assertEquals(expected_blocks, blocks[:-1])
755
 
 
756
762
        # Some basic matching tests
757
 
        chk_blocks('', '', [])
758
 
        chk_blocks([], [], [])
759
 
        chk_blocks('abc', '', [])
760
 
        chk_blocks('', 'abc', [])
761
 
        chk_blocks('abcd', 'abcd', [(0, 0, 4)])
762
 
        chk_blocks('abcd', 'abce', [(0, 0, 3)])
763
 
        chk_blocks('eabc', 'abce', [(1, 0, 3)])
764
 
        chk_blocks('eabce', 'abce', [(1, 0, 4)])
765
 
        chk_blocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
766
 
        chk_blocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
767
 
        chk_blocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
768
 
        # This may check too much, but it checks to see that 
 
763
        self.assertDiffBlocks('', '', [])
 
764
        self.assertDiffBlocks([], [], [])
 
765
        self.assertDiffBlocks('abc', '', [])
 
766
        self.assertDiffBlocks('', 'abc', [])
 
767
        self.assertDiffBlocks('abcd', 'abcd', [(0, 0, 4)])
 
768
        self.assertDiffBlocks('abcd', 'abce', [(0, 0, 3)])
 
769
        self.assertDiffBlocks('eabc', 'abce', [(1, 0, 3)])
 
770
        self.assertDiffBlocks('eabce', 'abce', [(1, 0, 4)])
 
771
        self.assertDiffBlocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
 
772
        self.assertDiffBlocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
 
773
        self.assertDiffBlocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
 
774
        # This may check too much, but it checks to see that
769
775
        # a copied block stays attached to the previous section,
770
776
        # not the later one.
771
777
        # difflib would tend to grab the trailing longest match
772
778
        # which would make the diff not look right
773
 
        chk_blocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
774
 
                   [(0, 0, 6), (6, 11, 10)])
 
779
        self.assertDiffBlocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
 
780
                              [(0, 0, 6), (6, 11, 10)])
775
781
 
776
782
        # make sure it supports passing in lists
777
 
        chk_blocks(
 
783
        self.assertDiffBlocks(
778
784
                   ['hello there\n',
779
785
                    'world\n',
780
786
                    'how are you today?\n'],
784
790
 
785
791
        # non unique lines surrounded by non-matching lines
786
792
        # won't be found
787
 
        chk_blocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
 
793
        self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
788
794
 
789
795
        # But they only need to be locally unique
790
 
        chk_blocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
 
796
        self.assertDiffBlocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
791
797
 
792
798
        # non unique blocks won't be matched
793
 
        chk_blocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
 
799
        self.assertDiffBlocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
794
800
 
795
801
        # but locally unique ones will
796
 
        chk_blocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
 
802
        self.assertDiffBlocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
797
803
                                              (5,4,1), (7,5,2), (10,8,1)])
798
804
 
799
 
        chk_blocks('abbabbXd', 'cabbabxd', [(7,7,1)])
800
 
        chk_blocks('abbabbbb', 'cabbabbc', [])
801
 
        chk_blocks('bbbbbbbb', 'cbbbbbbc', [])
 
805
        self.assertDiffBlocks('abbabbXd', 'cabbabxd', [(7,7,1)])
 
806
        self.assertDiffBlocks('abbabbbb', 'cabbabbc', [])
 
807
        self.assertDiffBlocks('bbbbbbbb', 'cbbbbbbc', [])
802
808
 
803
809
    def test_matching_blocks_tuples(self):
804
 
        def chk_blocks(a, b, expected_blocks):
805
 
            # difflib always adds a signature of the total
806
 
            # length, with no matching entries at the end
807
 
            s = self._PatienceSequenceMatcher(None, a, b)
808
 
            blocks = s.get_matching_blocks()
809
 
            self.assertEquals((len(a), len(b), 0), blocks[-1])
810
 
            self.assertEquals(expected_blocks, blocks[:-1])
811
 
 
812
810
        # Some basic matching tests
813
 
        chk_blocks([], [], [])
814
 
        chk_blocks([('a',), ('b',), ('c,')], [], [])
815
 
        chk_blocks([], [('a',), ('b',), ('c,')], [])
816
 
        chk_blocks([('a',), ('b',), ('c,')],
817
 
                   [('a',), ('b',), ('c,')],
818
 
                   [(0, 0, 3)])
819
 
        chk_blocks([('a',), ('b',), ('c,')],
820
 
                   [('a',), ('b',), ('d,')],
821
 
                   [(0, 0, 2)])
822
 
        chk_blocks([('d',), ('b',), ('c,')],
823
 
                   [('a',), ('b',), ('c,')],
824
 
                   [(1, 1, 2)])
825
 
        chk_blocks([('d',), ('a',), ('b',), ('c,')],
826
 
                   [('a',), ('b',), ('c,')],
827
 
                   [(1, 0, 3)])
828
 
        chk_blocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
829
 
                   [('a', 'b'), ('c', 'X'), ('e', 'f')],
830
 
                   [(0, 0, 1), (2, 2, 1)])
831
 
        chk_blocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
832
 
                   [('a', 'b'), ('c', 'dX'), ('e', 'f')],
833
 
                   [(0, 0, 1), (2, 2, 1)])
 
811
        self.assertDiffBlocks([], [], [])
 
812
        self.assertDiffBlocks([('a',), ('b',), ('c,')], [], [])
 
813
        self.assertDiffBlocks([], [('a',), ('b',), ('c,')], [])
 
814
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
 
815
                              [('a',), ('b',), ('c,')],
 
816
                              [(0, 0, 3)])
 
817
        self.assertDiffBlocks([('a',), ('b',), ('c,')],
 
818
                              [('a',), ('b',), ('d,')],
 
819
                              [(0, 0, 2)])
 
820
        self.assertDiffBlocks([('d',), ('b',), ('c,')],
 
821
                              [('a',), ('b',), ('c,')],
 
822
                              [(1, 1, 2)])
 
823
        self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
 
824
                              [('a',), ('b',), ('c,')],
 
825
                              [(1, 0, 3)])
 
826
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
 
827
                              [('a', 'b'), ('c', 'X'), ('e', 'f')],
 
828
                              [(0, 0, 1), (2, 2, 1)])
 
829
        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
 
830
                              [('a', 'b'), ('c', 'dX'), ('e', 'f')],
 
831
                              [(0, 0, 1), (2, 2, 1)])
834
832
 
835
833
    def test_opcodes(self):
836
834
        def chk_ops(a, b, expected_codes):
948
946
    def test_multiple_ranges(self):
949
947
        # There was an earlier bug where we used a bad set of ranges,
950
948
        # this triggers that specific bug, to make sure it doesn't regress
951
 
        def chk_blocks(a, b, expected_blocks):
952
 
            # difflib always adds a signature of the total
953
 
            # length, with no matching entries at the end
954
 
            s = self._PatienceSequenceMatcher(None, a, b)
955
 
            blocks = s.get_matching_blocks()
956
 
            x = blocks.pop()
957
 
            self.assertEquals(x, (len(a), len(b), 0))
958
 
            self.assertEquals(expected_blocks, blocks)
959
 
 
960
 
        chk_blocks('abcdefghijklmnop'
961
 
                 , 'abcXghiYZQRSTUVWXYZijklmnop'
962
 
                 , [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
963
 
 
964
 
        chk_blocks('ABCd efghIjk  L'
965
 
                 , 'AxyzBCn mo pqrstuvwI1 2  L'
966
 
                 , [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
 
949
        self.assertDiffBlocks('abcdefghijklmnop',
 
950
                              'abcXghiYZQRSTUVWXYZijklmnop',
 
951
                              [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
 
952
 
 
953
        self.assertDiffBlocks('ABCd efghIjk  L',
 
954
                              'AxyzBCn mo pqrstuvwI1 2  L',
 
955
                              [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
967
956
 
968
957
        # These are rot13 code snippets.
969
 
        chk_blocks('''\
 
958
        self.assertDiffBlocks('''\
970
959
    trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
971
960
    """
972
961
    gnxrf_netf = ['svyr*']
1081
1070
 
1082
1071
    def test_unhashable(self):
1083
1072
        """We should get a proper exception here."""
1084
 
        # A sub-list is unhashable
 
1073
        # We need to be able to hash items in the sequence, lists are
 
1074
        # unhashable, and thus cannot be diffed
1085
1075
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1086
1076
                                         None, [[]], [])
1087
 
 
1088
 
    def test_no_length(self):
1089
 
        # Objects have no length
1090
 
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
1091
 
                                         None, [object()], [])
 
1077
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1078
                                         None, ['valid', []], [])
 
1079
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1080
                                         None, ['valid'], [[]])
 
1081
        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
 
1082
                                         None, ['valid'], ['valid', []])
1092
1083
 
1093
1084
 
1094
1085
class TestPatienceDiffLibFiles(TestCaseInTempDir):