Rev 3084: Cleanup the test cases (Andrew) in http://bzr.arbash-meinel.com/branches/bzr/0.93-dev/patience_tuples

John Arbash Meinel john at arbash-meinel.com
Wed Dec 19 16:45:26 GMT 2007


At http://bzr.arbash-meinel.com/branches/bzr/0.93-dev/patience_tuples

------------------------------------------------------------
revno: 3084
revision-id:john at arbash-meinel.com-20071219164503-dxzdtrm0i3jet9ea
parent: john at arbash-meinel.com-20071219154004-wjlljom34fzjw8ws
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: patience_tuples
timestamp: Wed 2007-12-19 10:45:03 -0600
message:
  Cleanup the test cases (Andrew)
modified:
  bzrlib/tests/test_diff.py      testdiff.py-20050727164403-d1a3496ebb12e339
-------------- next part --------------
=== modified file 'bzrlib/tests/test_diff.py'
--- a/bzrlib/tests/test_diff.py	2007-12-04 16:28:28 +0000
+++ b/bzrlib/tests/test_diff.py	2007-12-19 16:45:03 +0000
@@ -744,37 +744,43 @@
         # This is what it currently gives:
         test_one('aBccDe', 'abccde', [(0,0), (5,5)])
 
+    def assertDiffBlocks(self, a, b, expected_blocks):
+        """Check that the sequence matcher returns the correct blocks.
+
+        :param a: A sequence to match
+        :param b: Another sequence to match
+        :param expected_blocks: The expected output, not including the final
+            matching block (len(a), len(b), 0)
+        """
+        matcher = self._PatienceSequenceMatcher(None, a, b)
+        blocks = matcher.get_matching_blocks()
+        last = blocks.pop()
+        self.assertEqual((len(a), len(b), 0), last)
+        self.assertEqual(expected_blocks, blocks)
+
     def test_matching_blocks(self):
-        def chk_blocks(a, b, expected_blocks):
-            # difflib always adds a signature of the total
-            # length, with no matching entries at the end
-            s = self._PatienceSequenceMatcher(None, a, b)
-            blocks = s.get_matching_blocks()
-            self.assertEquals((len(a), len(b), 0), blocks[-1])
-            self.assertEquals(expected_blocks, blocks[:-1])
-
         # Some basic matching tests
-        chk_blocks('', '', [])
-        chk_blocks([], [], [])
-        chk_blocks('abc', '', [])
-        chk_blocks('', 'abc', [])
-        chk_blocks('abcd', 'abcd', [(0, 0, 4)])
-        chk_blocks('abcd', 'abce', [(0, 0, 3)])
-        chk_blocks('eabc', 'abce', [(1, 0, 3)])
-        chk_blocks('eabce', 'abce', [(1, 0, 4)])
-        chk_blocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
-        chk_blocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
-        chk_blocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
-        # This may check too much, but it checks to see that 
+        self.assertDiffBlocks('', '', [])
+        self.assertDiffBlocks([], [], [])
+        self.assertDiffBlocks('abc', '', [])
+        self.assertDiffBlocks('', 'abc', [])
+        self.assertDiffBlocks('abcd', 'abcd', [(0, 0, 4)])
+        self.assertDiffBlocks('abcd', 'abce', [(0, 0, 3)])
+        self.assertDiffBlocks('eabc', 'abce', [(1, 0, 3)])
+        self.assertDiffBlocks('eabce', 'abce', [(1, 0, 4)])
+        self.assertDiffBlocks('abcde', 'abXde', [(0, 0, 2), (3, 3, 2)])
+        self.assertDiffBlocks('abcde', 'abXYZde', [(0, 0, 2), (3, 5, 2)])
+        self.assertDiffBlocks('abde', 'abXYZde', [(0, 0, 2), (2, 5, 2)])
+        # This may check too much, but it checks to see that
         # a copied block stays attached to the previous section,
         # not the later one.
         # difflib would tend to grab the trailing longest match
         # which would make the diff not look right
-        chk_blocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
-                   [(0, 0, 6), (6, 11, 10)])
+        self.assertDiffBlocks('abcdefghijklmnop', 'abcdefxydefghijklmnop',
+                              [(0, 0, 6), (6, 11, 10)])
 
         # make sure it supports passing in lists
-        chk_blocks(
+        self.assertDiffBlocks(
                    ['hello there\n',
                     'world\n',
                     'how are you today?\n'],
@@ -784,53 +790,45 @@
 
         # non unique lines surrounded by non-matching lines
         # won't be found
-        chk_blocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
+        self.assertDiffBlocks('aBccDe', 'abccde', [(0,0,1), (5,5,1)])
 
         # But they only need to be locally unique
-        chk_blocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
+        self.assertDiffBlocks('aBcDec', 'abcdec', [(0,0,1), (2,2,1), (4,4,2)])
 
         # non unique blocks won't be matched
-        chk_blocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
+        self.assertDiffBlocks('aBcdEcdFg', 'abcdecdfg', [(0,0,1), (8,8,1)])
 
         # but locally unique ones will
-        chk_blocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
+        self.assertDiffBlocks('aBcdEeXcdFg', 'abcdecdfg', [(0,0,1), (2,2,2),
                                               (5,4,1), (7,5,2), (10,8,1)])
 
-        chk_blocks('abbabbXd', 'cabbabxd', [(7,7,1)])
-        chk_blocks('abbabbbb', 'cabbabbc', [])
-        chk_blocks('bbbbbbbb', 'cbbbbbbc', [])
+        self.assertDiffBlocks('abbabbXd', 'cabbabxd', [(7,7,1)])
+        self.assertDiffBlocks('abbabbbb', 'cabbabbc', [])
+        self.assertDiffBlocks('bbbbbbbb', 'cbbbbbbc', [])
 
     def test_matching_blocks_tuples(self):
-        def chk_blocks(a, b, expected_blocks):
-            # difflib always adds a signature of the total
-            # length, with no matching entries at the end
-            s = self._PatienceSequenceMatcher(None, a, b)
-            blocks = s.get_matching_blocks()
-            self.assertEquals((len(a), len(b), 0), blocks[-1])
-            self.assertEquals(expected_blocks, blocks[:-1])
-
         # Some basic matching tests
-        chk_blocks([], [], [])
-        chk_blocks([('a',), ('b',), ('c,')], [], [])
-        chk_blocks([], [('a',), ('b',), ('c,')], [])
-        chk_blocks([('a',), ('b',), ('c,')],
-                   [('a',), ('b',), ('c,')],
-                   [(0, 0, 3)])
-        chk_blocks([('a',), ('b',), ('c,')],
-                   [('a',), ('b',), ('d,')],
-                   [(0, 0, 2)])
-        chk_blocks([('d',), ('b',), ('c,')],
-                   [('a',), ('b',), ('c,')],
-                   [(1, 1, 2)])
-        chk_blocks([('d',), ('a',), ('b',), ('c,')],
-                   [('a',), ('b',), ('c,')],
-                   [(1, 0, 3)])
-        chk_blocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
-                   [('a', 'b'), ('c', 'X'), ('e', 'f')],
-                   [(0, 0, 1), (2, 2, 1)])
-        chk_blocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
-                   [('a', 'b'), ('c', 'dX'), ('e', 'f')],
-                   [(0, 0, 1), (2, 2, 1)])
+        self.assertDiffBlocks([], [], [])
+        self.assertDiffBlocks([('a',), ('b',), ('c,')], [], [])
+        self.assertDiffBlocks([], [('a',), ('b',), ('c,')], [])
+        self.assertDiffBlocks([('a',), ('b',), ('c,')],
+                              [('a',), ('b',), ('c,')],
+                              [(0, 0, 3)])
+        self.assertDiffBlocks([('a',), ('b',), ('c,')],
+                              [('a',), ('b',), ('d,')],
+                              [(0, 0, 2)])
+        self.assertDiffBlocks([('d',), ('b',), ('c,')],
+                              [('a',), ('b',), ('c,')],
+                              [(1, 1, 2)])
+        self.assertDiffBlocks([('d',), ('a',), ('b',), ('c,')],
+                              [('a',), ('b',), ('c,')],
+                              [(1, 0, 3)])
+        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
+                              [('a', 'b'), ('c', 'X'), ('e', 'f')],
+                              [(0, 0, 1), (2, 2, 1)])
+        self.assertDiffBlocks([('a', 'b'), ('c', 'd'), ('e', 'f')],
+                              [('a', 'b'), ('c', 'dX'), ('e', 'f')],
+                              [(0, 0, 1), (2, 2, 1)])
 
     def test_opcodes(self):
         def chk_ops(a, b, expected_codes):
@@ -948,25 +946,16 @@
     def test_multiple_ranges(self):
         # There was an earlier bug where we used a bad set of ranges,
         # this triggers that specific bug, to make sure it doesn't regress
-        def chk_blocks(a, b, expected_blocks):
-            # difflib always adds a signature of the total
-            # length, with no matching entries at the end
-            s = self._PatienceSequenceMatcher(None, a, b)
-            blocks = s.get_matching_blocks()
-            x = blocks.pop()
-            self.assertEquals(x, (len(a), len(b), 0))
-            self.assertEquals(expected_blocks, blocks)
-
-        chk_blocks('abcdefghijklmnop'
-                 , 'abcXghiYZQRSTUVWXYZijklmnop'
-                 , [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
-
-        chk_blocks('ABCd efghIjk  L'
-                 , 'AxyzBCn mo pqrstuvwI1 2  L'
-                 , [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
+        self.assertDiffBlocks('abcdefghijklmnop',
+                              'abcXghiYZQRSTUVWXYZijklmnop',
+                              [(0, 0, 3), (6, 4, 3), (9, 20, 7)])
+
+        self.assertDiffBlocks('ABCd efghIjk  L',
+                              'AxyzBCn mo pqrstuvwI1 2  L',
+                              [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
 
         # These are rot13 code snippets.
-        chk_blocks('''\
+        self.assertDiffBlocks('''\
     trg nqqrq jura lbh nqq n svyr va gur qverpgbel.
     """
     gnxrf_netf = ['svyr*']
@@ -1081,14 +1070,16 @@
 
     def test_unhashable(self):
         """We should get a proper exception here."""
-        # A sub-list is unhashable
+        # We need to be able to hash items in the sequence, lists are
+        # unhashable, and thus cannot be diffed
         e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
                                          None, [[]], [])
-
-    def test_no_length(self):
-        # Objects have no length
-        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
-                                         None, [object()], [])
+        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
+                                         None, ['valid', []], [])
+        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
+                                         None, ['valid'], [[]])
+        e = self.assertRaises(TypeError, self._PatienceSequenceMatcher,
+                                         None, ['valid'], ['valid', []])
 
 
 class TestPatienceDiffLibFiles(TestCaseInTempDir):



More information about the bazaar-commits mailing list