PyXR

c:\python24\lib \ test \ test_difflib.py



0001 import difflib
0002 from test.test_support import run_unittest, findfile
0003 import unittest
0004 import doctest
0005 
0006 class TestSFbugs(unittest.TestCase):
0007 
0008     def test_ratio_for_null_seqn(self):
0009         # Check clearing of SF bug 763023
0010         s = difflib.SequenceMatcher(None, [], [])
0011         self.assertEqual(s.ratio(), 1)
0012         self.assertEqual(s.quick_ratio(), 1)
0013         self.assertEqual(s.real_quick_ratio(), 1)
0014 
0015     def test_comparing_empty_lists(self):
0016         # Check fix for bug #979794
0017         group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes()
0018         self.assertRaises(StopIteration, group_gen.next)
0019         diff_gen = difflib.unified_diff([], [])
0020         self.assertRaises(StopIteration, diff_gen.next)
0021 
0022 patch914575_from1 = """
0023    1. Beautiful is beTTer than ugly.
0024    2. Explicit is better than implicit.
0025    3. Simple is better than complex.
0026    4. Complex is better than complicated.
0027 """
0028 
0029 patch914575_to1 = """
0030    1. Beautiful is better than ugly.
0031    3.   Simple is better than complex.
0032    4. Complicated is better than complex.
0033    5. Flat is better than nested.
0034 """
0035 
0036 patch914575_from2 = """
0037 \t\tLine 1: preceeded by from:[tt] to:[ssss]
0038   \t\tLine 2: preceeded by from:[sstt] to:[sssst]
0039   \t \tLine 3: preceeded by from:[sstst] to:[ssssss]
0040 Line 4:  \thas from:[sst] to:[sss] after :
0041 Line 5: has from:[t] to:[ss] at end\t
0042 """
0043 
0044 patch914575_to2 = """
0045     Line 1: preceeded by from:[tt] to:[ssss]
0046     \tLine 2: preceeded by from:[sstt] to:[sssst]
0047       Line 3: preceeded by from:[sstst] to:[ssssss]
0048 Line 4:   has from:[sst] to:[sss] after :
0049 Line 5: has from:[t] to:[ss] at end
0050 """
0051 
0052 patch914575_from3 = """line 0
0053 1234567890123456789012345689012345
0054 line 1
0055 line 2
0056 line 3
0057 line 4   changed
0058 line 5   changed
0059 line 6   changed
0060 line 7
0061 line 8  subtracted
0062 line 9
0063 1234567890123456789012345689012345
0064 short line
0065 just fits in!!
0066 just fits in two lines yup!!
0067 the end"""
0068 
0069 patch914575_to3 = """line 0
0070 1234567890123456789012345689012345
0071 line 1
0072 line 2    added
0073 line 3
0074 line 4   chanGEd
0075 line 5a  chanGed
0076 line 6a  changEd
0077 line 7
0078 line 8
0079 line 9
0080 1234567890
0081 another long line that needs to be wrapped
0082 just fitS in!!
0083 just fits in two lineS yup!!
0084 the end"""
0085 
0086 class TestSFpatches(unittest.TestCase):
0087 
0088     def test_html_diff(self):
0089         # Check SF patch 914575 for generating HTML differences
0090         f1a = ((patch914575_from1 + '123\n'*10)*3)
0091         t1a = (patch914575_to1 + '123\n'*10)*3
0092         f1b = '456\n'*10 + f1a
0093         t1b = '456\n'*10 + t1a
0094         f1a = f1a.splitlines()
0095         t1a = t1a.splitlines()
0096         f1b = f1b.splitlines()
0097         t1b = t1b.splitlines()
0098         f2 = patch914575_from2.splitlines()
0099         t2 = patch914575_to2.splitlines()
0100         f3 = patch914575_from3
0101         t3 = patch914575_to3
0102         i = difflib.HtmlDiff()
0103         j = difflib.HtmlDiff(tabsize=2)
0104         k = difflib.HtmlDiff(wrapcolumn=14)
0105 
0106         full = i.make_file(f1a,t1a,'from','to',context=False,numlines=5)
0107         tables = '\n'.join(
0108             [
0109              '<h2>Context (first diff within numlines=5(default))</h2>',
0110              i.make_table(f1a,t1a,'from','to',context=True),
0111              '<h2>Context (first diff after numlines=5(default))</h2>',
0112              i.make_table(f1b,t1b,'from','to',context=True),
0113              '<h2>Context (numlines=6)</h2>',
0114              i.make_table(f1a,t1a,'from','to',context=True,numlines=6),
0115              '<h2>Context (numlines=0)</h2>',
0116              i.make_table(f1a,t1a,'from','to',context=True,numlines=0),
0117              '<h2>Same Context</h2>',
0118              i.make_table(f1a,f1a,'from','to',context=True),
0119              '<h2>Same Full</h2>',
0120              i.make_table(f1a,f1a,'from','to',context=False),
0121              '<h2>Empty Context</h2>',
0122              i.make_table([],[],'from','to',context=True),
0123              '<h2>Empty Full</h2>',
0124              i.make_table([],[],'from','to',context=False),
0125              '<h2>tabsize=2</h2>',
0126              j.make_table(f2,t2),
0127              '<h2>tabsize=default</h2>',
0128              i.make_table(f2,t2),
0129              '<h2>Context (wrapcolumn=14,numlines=0)</h2>',
0130              k.make_table(f3.splitlines(),t3.splitlines(),context=True,numlines=0),
0131              '<h2>wrapcolumn=14,splitlines()</h2>',
0132              k.make_table(f3.splitlines(),t3.splitlines()),
0133              '<h2>wrapcolumn=14,splitlines(True)</h2>',
0134              k.make_table(f3.splitlines(True),t3.splitlines(True)),
0135              ])
0136         actual = full.replace('</body>','\n%s\n</body>' % tables)
0137         # temporarily uncomment next three lines to baseline this test
0138         #f = open('test_difflib_expect.html','w')
0139         #f.write(actual)
0140         #f.close()
0141         expect = open(findfile('test_difflib_expect.html')).read()
0142 
0143 
0144         self.assertEqual(actual,expect)
0145 
0146 Doctests = doctest.DocTestSuite(difflib)
0147 
0148 run_unittest(TestSFpatches, TestSFbugs, Doctests)
0149 

Generated by PyXR 0.9.4
SourceForge.net Logo