2002-07-23 16:04:11 -03:00
|
|
|
import difflib
|
2008-05-20 18:35:26 -03:00
|
|
|
from test.support import run_unittest, findfile
|
2003-07-01 11:59:46 -03:00
|
|
|
import unittest
|
2003-07-16 01:34:56 -03:00
|
|
|
import doctest
|
2006-01-31 14:34:13 -04:00
|
|
|
import sys
|
2003-07-01 11:59:46 -03:00
|
|
|
|
|
|
|
class TestSFbugs(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_ratio_for_null_seqn(self):
|
|
|
|
# Check clearing of SF bug 763023
|
|
|
|
s = difflib.SequenceMatcher(None, [], [])
|
|
|
|
self.assertEqual(s.ratio(), 1)
|
|
|
|
self.assertEqual(s.quick_ratio(), 1)
|
|
|
|
self.assertEqual(s.real_quick_ratio(), 1)
|
|
|
|
|
2004-07-10 20:54:07 -03:00
|
|
|
def test_comparing_empty_lists(self):
|
|
|
|
# Check fix for bug #979794
|
|
|
|
group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes()
|
2007-04-21 12:47:16 -03:00
|
|
|
self.assertRaises(StopIteration, next, group_gen)
|
2004-07-10 20:54:07 -03:00
|
|
|
diff_gen = difflib.unified_diff([], [])
|
2007-04-21 12:47:16 -03:00
|
|
|
self.assertRaises(StopIteration, next, diff_gen)
|
2004-07-10 20:54:07 -03:00
|
|
|
|
2009-11-23 15:06:11 -04:00
|
|
|
def test_added_tab_hint(self):
|
|
|
|
# Check fix for bug #1488943
|
|
|
|
diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))
|
|
|
|
self.assertEqual("- \tI am a buggy", diff[0])
|
|
|
|
self.assertEqual("? --\n", diff[1])
|
|
|
|
self.assertEqual("+ \t\tI am a bug", diff[2])
|
|
|
|
self.assertEqual("? +\n", diff[3])
|
|
|
|
|
2004-08-29 13:34:40 -03:00
|
|
|
patch914575_from1 = """
|
|
|
|
1. Beautiful is beTTer than ugly.
|
|
|
|
2. Explicit is better than implicit.
|
|
|
|
3. Simple is better than complex.
|
|
|
|
4. Complex is better than complicated.
|
|
|
|
"""
|
|
|
|
|
|
|
|
patch914575_to1 = """
|
|
|
|
1. Beautiful is better than ugly.
|
|
|
|
3. Simple is better than complex.
|
|
|
|
4. Complicated is better than complex.
|
|
|
|
5. Flat is better than nested.
|
|
|
|
"""
|
|
|
|
|
|
|
|
patch914575_from2 = """
|
|
|
|
\t\tLine 1: preceeded by from:[tt] to:[ssss]
|
|
|
|
\t\tLine 2: preceeded by from:[sstt] to:[sssst]
|
|
|
|
\t \tLine 3: preceeded by from:[sstst] to:[ssssss]
|
|
|
|
Line 4: \thas from:[sst] to:[sss] after :
|
|
|
|
Line 5: has from:[t] to:[ss] at end\t
|
|
|
|
"""
|
|
|
|
|
|
|
|
patch914575_to2 = """
|
|
|
|
Line 1: preceeded by from:[tt] to:[ssss]
|
|
|
|
\tLine 2: preceeded by from:[sstt] to:[sssst]
|
|
|
|
Line 3: preceeded by from:[sstst] to:[ssssss]
|
|
|
|
Line 4: has from:[sst] to:[sss] after :
|
2004-08-29 19:38:38 -03:00
|
|
|
Line 5: has from:[t] to:[ss] at end
|
2004-08-29 13:34:40 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
patch914575_from3 = """line 0
|
|
|
|
1234567890123456789012345689012345
|
|
|
|
line 1
|
|
|
|
line 2
|
|
|
|
line 3
|
2004-08-29 19:38:38 -03:00
|
|
|
line 4 changed
|
|
|
|
line 5 changed
|
|
|
|
line 6 changed
|
2004-08-29 13:34:40 -03:00
|
|
|
line 7
|
|
|
|
line 8 subtracted
|
|
|
|
line 9
|
|
|
|
1234567890123456789012345689012345
|
|
|
|
short line
|
|
|
|
just fits in!!
|
|
|
|
just fits in two lines yup!!
|
|
|
|
the end"""
|
|
|
|
|
|
|
|
patch914575_to3 = """line 0
|
|
|
|
1234567890123456789012345689012345
|
|
|
|
line 1
|
|
|
|
line 2 added
|
|
|
|
line 3
|
2004-08-29 19:38:38 -03:00
|
|
|
line 4 chanGEd
|
|
|
|
line 5a chanGed
|
|
|
|
line 6a changEd
|
2004-08-29 13:34:40 -03:00
|
|
|
line 7
|
|
|
|
line 8
|
|
|
|
line 9
|
|
|
|
1234567890
|
|
|
|
another long line that needs to be wrapped
|
|
|
|
just fitS in!!
|
|
|
|
just fits in two lineS yup!!
|
|
|
|
the end"""
|
|
|
|
|
|
|
|
class TestSFpatches(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_html_diff(self):
|
|
|
|
# Check SF patch 914575 for generating HTML differences
|
|
|
|
f1a = ((patch914575_from1 + '123\n'*10)*3)
|
|
|
|
t1a = (patch914575_to1 + '123\n'*10)*3
|
|
|
|
f1b = '456\n'*10 + f1a
|
|
|
|
t1b = '456\n'*10 + t1a
|
|
|
|
f1a = f1a.splitlines()
|
|
|
|
t1a = t1a.splitlines()
|
|
|
|
f1b = f1b.splitlines()
|
|
|
|
t1b = t1b.splitlines()
|
|
|
|
f2 = patch914575_from2.splitlines()
|
|
|
|
t2 = patch914575_to2.splitlines()
|
|
|
|
f3 = patch914575_from3
|
|
|
|
t3 = patch914575_to3
|
|
|
|
i = difflib.HtmlDiff()
|
|
|
|
j = difflib.HtmlDiff(tabsize=2)
|
|
|
|
k = difflib.HtmlDiff(wrapcolumn=14)
|
2004-08-29 19:38:38 -03:00
|
|
|
|
2004-08-29 13:34:40 -03:00
|
|
|
full = i.make_file(f1a,t1a,'from','to',context=False,numlines=5)
|
|
|
|
tables = '\n'.join(
|
|
|
|
[
|
2004-08-29 19:38:38 -03:00
|
|
|
'<h2>Context (first diff within numlines=5(default))</h2>',
|
2004-08-29 13:34:40 -03:00
|
|
|
i.make_table(f1a,t1a,'from','to',context=True),
|
2004-08-29 19:38:38 -03:00
|
|
|
'<h2>Context (first diff after numlines=5(default))</h2>',
|
2004-08-29 13:34:40 -03:00
|
|
|
i.make_table(f1b,t1b,'from','to',context=True),
|
2004-08-29 19:38:38 -03:00
|
|
|
'<h2>Context (numlines=6)</h2>',
|
2004-08-29 13:34:40 -03:00
|
|
|
i.make_table(f1a,t1a,'from','to',context=True,numlines=6),
|
2004-08-29 19:38:38 -03:00
|
|
|
'<h2>Context (numlines=0)</h2>',
|
2004-08-29 13:34:40 -03:00
|
|
|
i.make_table(f1a,t1a,'from','to',context=True,numlines=0),
|
2004-08-29 19:38:38 -03:00
|
|
|
'<h2>Same Context</h2>',
|
2004-08-29 13:34:40 -03:00
|
|
|
i.make_table(f1a,f1a,'from','to',context=True),
|
2004-08-29 19:38:38 -03:00
|
|
|
'<h2>Same Full</h2>',
|
2004-08-29 13:34:40 -03:00
|
|
|
i.make_table(f1a,f1a,'from','to',context=False),
|
|
|
|
'<h2>Empty Context</h2>',
|
|
|
|
i.make_table([],[],'from','to',context=True),
|
|
|
|
'<h2>Empty Full</h2>',
|
|
|
|
i.make_table([],[],'from','to',context=False),
|
|
|
|
'<h2>tabsize=2</h2>',
|
|
|
|
j.make_table(f2,t2),
|
|
|
|
'<h2>tabsize=default</h2>',
|
|
|
|
i.make_table(f2,t2),
|
|
|
|
'<h2>Context (wrapcolumn=14,numlines=0)</h2>',
|
|
|
|
k.make_table(f3.splitlines(),t3.splitlines(),context=True,numlines=0),
|
|
|
|
'<h2>wrapcolumn=14,splitlines()</h2>',
|
|
|
|
k.make_table(f3.splitlines(),t3.splitlines()),
|
|
|
|
'<h2>wrapcolumn=14,splitlines(True)</h2>',
|
|
|
|
k.make_table(f3.splitlines(True),t3.splitlines(True)),
|
|
|
|
])
|
|
|
|
actual = full.replace('</body>','\n%s\n</body>' % tables)
|
2004-08-29 19:38:38 -03:00
|
|
|
|
2009-05-28 03:09:08 -03:00
|
|
|
# temporarily uncomment next two lines to baseline this test
|
|
|
|
#with open('test_difflib_expect.html','w') as fp:
|
|
|
|
# fp.write(actual)
|
2004-08-29 19:38:38 -03:00
|
|
|
|
2009-05-28 03:09:08 -03:00
|
|
|
with open(findfile('test_difflib_expect.html')) as fp:
|
|
|
|
self.assertEqual(actual, fp.read())
|
2004-08-29 13:34:40 -03:00
|
|
|
|
2006-01-31 14:34:13 -04:00
|
|
|
def test_recursion_limit(self):
|
|
|
|
# Check if the problem described in patch #1413711 exists.
|
|
|
|
limit = sys.getrecursionlimit()
|
|
|
|
old = [(i%2 and "K:%d" or "V:A:%d") % i for i in range(limit*2)]
|
|
|
|
new = [(i%2 and "K:%d" or "V:B:%d") % i for i in range(limit*2)]
|
|
|
|
difflib.SequenceMatcher(None, old, new).get_opcodes()
|
|
|
|
|
|
|
|
|
2011-04-12 19:14:12 -03:00
|
|
|
class TestOutputFormat(unittest.TestCase):
|
|
|
|
def test_tab_delimiter(self):
|
|
|
|
args = ['one', 'two', 'Original', 'Current',
|
|
|
|
'2005-01-26 23:30:50', '2010-04-02 10:20:52']
|
|
|
|
ud = difflib.unified_diff(*args, lineterm='')
|
|
|
|
self.assertEqual(list(ud)[0:2], [
|
|
|
|
"--- Original\t2005-01-26 23:30:50",
|
|
|
|
"+++ Current\t2010-04-02 10:20:52"])
|
|
|
|
cd = difflib.context_diff(*args, lineterm='')
|
|
|
|
self.assertEqual(list(cd)[0:2], [
|
|
|
|
"*** Original\t2005-01-26 23:30:50",
|
|
|
|
"--- Current\t2010-04-02 10:20:52"])
|
|
|
|
|
|
|
|
def test_no_trailing_tab_on_empty_filedate(self):
|
|
|
|
args = ['one', 'two', 'Original', 'Current']
|
|
|
|
ud = difflib.unified_diff(*args, lineterm='')
|
|
|
|
self.assertEqual(list(ud)[0:2], ["--- Original", "+++ Current"])
|
|
|
|
|
|
|
|
cd = difflib.context_diff(*args, lineterm='')
|
|
|
|
self.assertEqual(list(cd)[0:2], ["*** Original", "--- Current"])
|
|
|
|
|
|
|
|
def test_range_format_unified(self):
|
|
|
|
# Per the diff spec at http://www.unix.org/single_unix_specification/
|
|
|
|
spec = '''\
|
|
|
|
Each <range> field shall be of the form:
|
|
|
|
%1d", <beginning line number> if the range contains exactly one line,
|
|
|
|
and:
|
|
|
|
"%1d,%1d", <beginning line number>, <number of lines> otherwise.
|
|
|
|
If a range is empty, its beginning line number shall be the number of
|
|
|
|
the line just before the range, or 0 if the empty range starts the file.
|
|
|
|
'''
|
|
|
|
fmt = difflib._format_range_unified
|
|
|
|
self.assertEqual(fmt(3,3), '3,0')
|
|
|
|
self.assertEqual(fmt(3,4), '4')
|
|
|
|
self.assertEqual(fmt(3,5), '4,2')
|
|
|
|
self.assertEqual(fmt(3,6), '4,3')
|
|
|
|
self.assertEqual(fmt(0,0), '0,0')
|
|
|
|
|
|
|
|
def test_range_format_context(self):
|
|
|
|
# Per the diff spec at http://www.unix.org/single_unix_specification/
|
|
|
|
spec = '''\
|
|
|
|
The range of lines in file1 shall be written in the following format
|
|
|
|
if the range contains two or more lines:
|
|
|
|
"*** %d,%d ****\n", <beginning line number>, <ending line number>
|
|
|
|
and the following format otherwise:
|
|
|
|
"*** %d ****\n", <ending line number>
|
|
|
|
The ending line number of an empty range shall be the number of the preceding line,
|
|
|
|
or 0 if the range is at the start of the file.
|
|
|
|
|
|
|
|
Next, the range of lines in file2 shall be written in the following format
|
|
|
|
if the range contains two or more lines:
|
|
|
|
"--- %d,%d ----\n", <beginning line number>, <ending line number>
|
|
|
|
and the following format otherwise:
|
|
|
|
"--- %d ----\n", <ending line number>
|
|
|
|
'''
|
|
|
|
fmt = difflib._format_range_context
|
|
|
|
self.assertEqual(fmt(3,3), '3')
|
|
|
|
self.assertEqual(fmt(3,4), '4')
|
|
|
|
self.assertEqual(fmt(3,5), '4,5')
|
|
|
|
self.assertEqual(fmt(3,6), '4,6')
|
|
|
|
self.assertEqual(fmt(0,0), '0')
|
|
|
|
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
def test_main():
|
|
|
|
difflib.HtmlDiff._default_prefix = 0
|
|
|
|
Doctests = doctest.DocTestSuite(difflib)
|
2011-04-12 19:14:12 -03:00
|
|
|
run_unittest(TestSFpatches, TestSFbugs, Doctests, TestOutputFormat)
|
2003-07-16 01:34:56 -03:00
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|