744
744
# This is what it currently gives:
745
745
test_one('aBccDe', 'abccde', [(0,0), (5,5)])
747
def assertDiffBlocks(self, a, b, expected_blocks):
748
"""Check that the sequence matcher returns the correct blocks.
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)
755
matcher = self._PatienceSequenceMatcher(None, a, b)
756
blocks = matcher.get_matching_blocks()
758
self.assertEqual((len(a), len(b), 0), last)
759
self.assertEqual(expected_blocks, blocks)
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])
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)])
776
782
# make sure it supports passing in lists
783
self.assertDiffBlocks(
778
784
['hello there\n',
780
786
'how are you today?\n'],
785
791
# non unique lines surrounded by non-matching lines
787
chk_blocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
793
self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
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)])
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)])
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)])
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', [])
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])
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,')],
819
chk_blocks([('a',), ('b',), ('c,')],
820
[('a',), ('b',), ('d,')],
822
chk_blocks([('d',), ('b',), ('c,')],
823
[('a',), ('b',), ('c,')],
825
chk_blocks([('d',), ('a',), ('b',), ('c,')],
826
[('a',), ('b',), ('c,')],
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,')],
817
self.assertDiffBlocks([('a',), ('b',), ('c,')],
818
[('a',), ('b',), ('d,')],
820
self.assertDiffBlocks([('d',), ('b',), ('c,')],
821
[('a',), ('b',), ('c,')],
823
self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
824
[('a',), ('b',), ('c,')],
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)])
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()
957
self.assertEquals(x, (len(a), len(b), 0))
958
self.assertEquals(expected_blocks, blocks)
960
chk_blocks('abcdefghijklmnop'
961
, 'abcXghiYZQRSTUVWXYZijklmnop'
962
, [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
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)])
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)])
968
957
# These are rot13 code snippets.
958
self.assertDiffBlocks('''\
970
959
trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
972
961
gnxrf_netf = ['svyr*']