Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_difflib.py

    r2 r391  
    55import sys
    66
     7
     8class TestWithAscii(unittest.TestCase):
     9    def test_one_insert(self):
     10        sm = difflib.SequenceMatcher(None, 'b' * 100, 'a' + 'b' * 100)
     11        self.assertAlmostEqual(sm.ratio(), 0.995, places=3)
     12        self.assertEqual(list(sm.get_opcodes()),
     13            [   ('insert', 0, 0, 0, 1),
     14                ('equal', 0, 100, 1, 101)])
     15        sm = difflib.SequenceMatcher(None, 'b' * 100, 'b' * 50 + 'a' + 'b' * 50)
     16        self.assertAlmostEqual(sm.ratio(), 0.995, places=3)
     17        self.assertEqual(list(sm.get_opcodes()),
     18            [   ('equal', 0, 50, 0, 50),
     19                ('insert', 50, 50, 50, 51),
     20                ('equal', 50, 100, 51, 101)])
     21
     22    def test_one_delete(self):
     23        sm = difflib.SequenceMatcher(None, 'a' * 40 + 'c' + 'b' * 40, 'a' * 40 + 'b' * 40)
     24        self.assertAlmostEqual(sm.ratio(), 0.994, places=3)
     25        self.assertEqual(list(sm.get_opcodes()),
     26            [   ('equal', 0, 40, 0, 40),
     27                ('delete', 40, 41, 40, 40),
     28                ('equal', 41, 81, 40, 80)])
     29
     30
     31class TestAutojunk(unittest.TestCase):
     32    """Tests for the autojunk parameter added in 2.7"""
     33    def test_one_insert_homogenous_sequence(self):
     34        # By default autojunk=True and the heuristic kicks in for a sequence
     35        # of length 200+
     36        seq1 = 'b' * 200
     37        seq2 = 'a' + 'b' * 200
     38
     39        sm = difflib.SequenceMatcher(None, seq1, seq2)
     40        self.assertAlmostEqual(sm.ratio(), 0, places=3)
     41
     42        # Now turn the heuristic off
     43        sm = difflib.SequenceMatcher(None, seq1, seq2, autojunk=False)
     44        self.assertAlmostEqual(sm.ratio(), 0.9975, places=3)
     45
     46
    747class TestSFbugs(unittest.TestCase):
    8 
    948    def test_ratio_for_null_seqn(self):
    1049        # Check clearing of SF bug 763023
     
    144183             ])
    145184        actual = full.replace('</body>','\n%s\n</body>' % tables)
    146         # temporarily uncomment next three lines to baseline this test
    147         #f = open('test_difflib_expect.html','w')
    148         #f.write(actual)
    149         #f.close()
    150         expect = open(findfile('test_difflib_expect.html')).read()
    151 
    152 
    153         self.assertEqual(actual,expect)
     185
     186        # temporarily uncomment next two lines to baseline this test
     187        #with open('test_difflib_expect.html','w') as fp:
     188        #    fp.write(actual)
     189
     190        with open(findfile('test_difflib_expect.html')) as fp:
     191            self.assertEqual(actual, fp.read())
    154192
    155193    def test_recursion_limit(self):
     
    161199
    162200
     201class TestOutputFormat(unittest.TestCase):
     202    def test_tab_delimiter(self):
     203        args = ['one', 'two', 'Original', 'Current',
     204            '2005-01-26 23:30:50', '2010-04-02 10:20:52']
     205        ud = difflib.unified_diff(*args, lineterm='')
     206        self.assertEqual(list(ud)[0:2], [
     207                           "--- Original\t2005-01-26 23:30:50",
     208                           "+++ Current\t2010-04-02 10:20:52"])
     209        cd = difflib.context_diff(*args, lineterm='')
     210        self.assertEqual(list(cd)[0:2], [
     211                           "*** Original\t2005-01-26 23:30:50",
     212                           "--- Current\t2010-04-02 10:20:52"])
     213
     214    def test_no_trailing_tab_on_empty_filedate(self):
     215        args = ['one', 'two', 'Original', 'Current']
     216        ud = difflib.unified_diff(*args, lineterm='')
     217        self.assertEqual(list(ud)[0:2], ["--- Original", "+++ Current"])
     218
     219        cd = difflib.context_diff(*args, lineterm='')
     220        self.assertEqual(list(cd)[0:2], ["*** Original", "--- Current"])
     221
     222    def test_range_format_unified(self):
     223        # Per the diff spec at http://www.unix.org/single_unix_specification/
     224        spec = '''\
     225           Each <range> field shall be of the form:
     226             %1d", <beginning line number>  if the range contains exactly one line,
     227           and:
     228            "%1d,%1d", <beginning line number>, <number of lines> otherwise.
     229           If a range is empty, its beginning line number shall be the number of
     230           the line just before the range, or 0 if the empty range starts the file.
     231        '''
     232        fmt = difflib._format_range_unified
     233        self.assertEqual(fmt(3,3), '3,0')
     234        self.assertEqual(fmt(3,4), '4')
     235        self.assertEqual(fmt(3,5), '4,2')
     236        self.assertEqual(fmt(3,6), '4,3')
     237        self.assertEqual(fmt(0,0), '0,0')
     238
     239    def test_range_format_context(self):
     240        # Per the diff spec at http://www.unix.org/single_unix_specification/
     241        spec = '''\
     242           The range of lines in file1 shall be written in the following format
     243           if the range contains two or more lines:
     244               "*** %d,%d ****\n", <beginning line number>, <ending line number>
     245           and the following format otherwise:
     246               "*** %d ****\n", <ending line number>
     247           The ending line number of an empty range shall be the number of the preceding line,
     248           or 0 if the range is at the start of the file.
     249
     250           Next, the range of lines in file2 shall be written in the following format
     251           if the range contains two or more lines:
     252               "--- %d,%d ----\n", <beginning line number>, <ending line number>
     253           and the following format otherwise:
     254               "--- %d ----\n", <ending line number>
     255        '''
     256        fmt = difflib._format_range_context
     257        self.assertEqual(fmt(3,3), '3')
     258        self.assertEqual(fmt(3,4), '4')
     259        self.assertEqual(fmt(3,5), '4,5')
     260        self.assertEqual(fmt(3,6), '4,6')
     261        self.assertEqual(fmt(0,0), '0')
     262
     263
    163264def test_main():
    164265    difflib.HtmlDiff._default_prefix = 0
    165266    Doctests = doctest.DocTestSuite(difflib)
    166     run_unittest(TestSFpatches, TestSFbugs, Doctests)
     267    run_unittest(
     268        TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
     269        TestOutputFormat, Doctests)
    167270
    168271if __name__ == '__main__':
Note: See TracChangeset for help on using the changeset viewer.