','\n%s\n' % tables)
# temporarily uncomment next two lines to baseline this test
#with open('test_difflib_expect.html','w') as fp:
# fp.write(actual)
with open(findfile('test_difflib_expect.html'), encoding="utf-8") as fp:
self.assertEqual(actual, fp.read())
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()
def test_make_file_default_charset(self):
html_diff = difflib.HtmlDiff()
output = html_diff.make_file(patch914575_from1.splitlines(),
patch914575_to1.splitlines())
self.assertIn('content="text/html; charset=utf-8"', output)
def test_make_file_iso88591_charset(self):
html_diff = difflib.HtmlDiff()
output = html_diff.make_file(patch914575_from1.splitlines(),
patch914575_to1.splitlines(),
charset='iso-8859-1')
self.assertIn('content="text/html; charset=iso-8859-1"', output)
def test_make_file_usascii_charset_with_nonascii_input(self):
html_diff = difflib.HtmlDiff()
output = html_diff.make_file(patch914575_nonascii_from1.splitlines(),
patch914575_nonascii_to1.splitlines(),
charset='us-ascii')
self.assertIn('content="text/html; charset=us-ascii"', output)
self.assertIn('ımplıcıt', output)
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 field shall be of the form:
%1d", if the range contains exactly one line,
and:
"%1d,%1d", , 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", ,
and the following format otherwise:
"*** %d ****\n",
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", ,
and the following format otherwise:
"--- %d ----\n",
'''
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')
class TestBytes(unittest.TestCase):
# don't really care about the content of the output, just the fact
# that it's bytes and we don't crash
def check(self, diff):
diff = list(diff) # trigger exceptions first
for line in diff:
self.assertIsInstance(
line, bytes,
"all lines of diff should be bytes, but got: %r" % line)
def test_byte_content(self):
# if we receive byte strings, we return byte strings
a = [b'hello', b'andr\xe9'] # iso-8859-1 bytes
b = [b'hello', b'andr\xc3\xa9'] # utf-8 bytes
unified = difflib.unified_diff
context = difflib.context_diff
check = self.check
check(difflib.diff_bytes(unified, a, a))
check(difflib.diff_bytes(unified, a, b))
# now with filenames (content and filenames are all bytes!)
check(difflib.diff_bytes(unified, a, a, b'a', b'a'))
check(difflib.diff_bytes(unified, a, b, b'a', b'b'))
# and with filenames and dates
check(difflib.diff_bytes(unified, a, a, b'a', b'a', b'2005', b'2013'))
check(difflib.diff_bytes(unified, a, b, b'a', b'b', b'2005', b'2013'))
# same all over again, with context diff
check(difflib.diff_bytes(context, a, a))
check(difflib.diff_bytes(context, a, b))
check(difflib.diff_bytes(context, a, a, b'a', b'a'))
check(difflib.diff_bytes(context, a, b, b'a', b'b'))
check(difflib.diff_bytes(context, a, a, b'a', b'a', b'2005', b'2013'))
check(difflib.diff_bytes(context, a, b, b'a', b'b', b'2005', b'2013'))
def test_byte_filenames(self):
# somebody renamed a file from ISO-8859-2 to UTF-8
fna = b'\xb3odz.txt' # "łodz.txt"
fnb = b'\xc5\x82odz.txt'
# they transcoded the content at the same time
a = [b'\xa3odz is a city in Poland.']
b = [b'\xc5\x81odz is a city in Poland.']
check = self.check
unified = difflib.unified_diff
context = difflib.context_diff
check(difflib.diff_bytes(unified, a, b, fna, fnb))
check(difflib.diff_bytes(context, a, b, fna, fnb))
def assertDiff(expect, actual):
# do not compare expect and equal as lists, because unittest
# uses difflib to report difference between lists
actual = list(actual)
self.assertEqual(len(expect), len(actual))
for e, a in zip(expect, actual):
self.assertEqual(e, a)
expect = [
b'--- \xb3odz.txt',
b'+++ \xc5\x82odz.txt',
b'@@ -1 +1 @@',
b'-\xa3odz is a city in Poland.',
b'+\xc5\x81odz is a city in Poland.',
]
actual = difflib.diff_bytes(unified, a, b, fna, fnb, lineterm=b'')
assertDiff(expect, actual)
# with dates (plain ASCII)
datea = b'2005-03-18'
dateb = b'2005-03-19'
check(difflib.diff_bytes(unified, a, b, fna, fnb, datea, dateb))
check(difflib.diff_bytes(context, a, b, fna, fnb, datea, dateb))
expect = [
# note the mixed encodings here: this is deeply wrong by every
# tenet of Unicode, but it doesn't crash, it's parseable by
# patch, and it's how UNIX(tm) diff behaves
b'--- \xb3odz.txt\t2005-03-18',
b'+++ \xc5\x82odz.txt\t2005-03-19',
b'@@ -1 +1 @@',
b'-\xa3odz is a city in Poland.',
b'+\xc5\x81odz is a city in Poland.',
]
actual = difflib.diff_bytes(unified, a, b, fna, fnb, datea, dateb,
lineterm=b'')
assertDiff(expect, actual)
def test_mixed_types_content(self):
# type of input content must be consistent: all str or all bytes
a = [b'hello']
b = ['hello']
unified = difflib.unified_diff
context = difflib.context_diff
expect = "lines to compare must be str, not bytes (b'hello')"
self._assert_type_error(expect, unified, a, b)
self._assert_type_error(expect, unified, b, a)
self._assert_type_error(expect, context, a, b)
self._assert_type_error(expect, context, b, a)
expect = "all arguments must be bytes, not str ('hello')"
self._assert_type_error(expect, difflib.diff_bytes, unified, a, b)
self._assert_type_error(expect, difflib.diff_bytes, unified, b, a)
self._assert_type_error(expect, difflib.diff_bytes, context, a, b)
self._assert_type_error(expect, difflib.diff_bytes, context, b, a)
def test_mixed_types_filenames(self):
# cannot pass filenames as bytes if content is str (this may not be
# the right behaviour, but at least the test demonstrates how
# things work)
a = ['hello\n']
b = ['ohell\n']
fna = b'ol\xe9.txt' # filename transcoded from ISO-8859-1
fnb = b'ol\xc3a9.txt' # to UTF-8
self._assert_type_error(
"all arguments must be str, not: b'ol\\xe9.txt'",
difflib.unified_diff, a, b, fna, fnb)
def test_mixed_types_dates(self):
# type of dates must be consistent with type of contents
a = [b'foo\n']
b = [b'bar\n']
datea = '1 fév'
dateb = '3 fév'
self._assert_type_error(
"all arguments must be bytes, not str ('1 fév')",
difflib.diff_bytes, difflib.unified_diff,
a, b, b'a', b'b', datea, dateb)
# if input is str, non-ASCII dates are fine
a = ['foo\n']
b = ['bar\n']
list(difflib.unified_diff(a, b, 'a', 'b', datea, dateb))
def _assert_type_error(self, msg, generator, *args):
with self.assertRaises(TypeError) as ctx:
list(generator(*args))
self.assertEqual(msg, str(ctx.exception))
class TestJunkAPIs(unittest.TestCase):
def test_is_line_junk_true(self):
for line in ['#', ' ', ' #', '# ', ' # ', '']:
self.assertTrue(difflib.IS_LINE_JUNK(line), repr(line))
def test_is_line_junk_false(self):
for line in ['##', ' ##', '## ', 'abc ', 'abc #', 'Mr. Moose is up!']:
self.assertFalse(difflib.IS_LINE_JUNK(line), repr(line))
def test_is_line_junk_REDOS(self):
evil_input = ('\t' * 1000000) + '##'
self.assertFalse(difflib.IS_LINE_JUNK(evil_input))
def test_is_character_junk_true(self):
for char in [' ', '\t']:
self.assertTrue(difflib.IS_CHARACTER_JUNK(char), repr(char))
def test_is_character_junk_false(self):
for char in ['a', '#', '\n', '\f', '\r', '\v']:
self.assertFalse(difflib.IS_CHARACTER_JUNK(char), repr(char))
class TestFindLongest(unittest.TestCase):
def longer_match_exists(self, a, b, n):
return any(b_part in a for b_part in
[b[i:i + n + 1] for i in range(0, len(b) - n - 1)])
def test_default_args(self):
a = 'foo bar'
b = 'foo baz bar'
sm = difflib.SequenceMatcher(a=a, b=b)
match = sm.find_longest_match()
self.assertEqual(match.a, 0)
self.assertEqual(match.b, 0)
self.assertEqual(match.size, 6)
self.assertEqual(a[match.a: match.a + match.size],
b[match.b: match.b + match.size])
self.assertFalse(self.longer_match_exists(a, b, match.size))
match = sm.find_longest_match(alo=2, blo=4)
self.assertEqual(match.a, 3)
self.assertEqual(match.b, 7)
self.assertEqual(match.size, 4)
self.assertEqual(a[match.a: match.a + match.size],
b[match.b: match.b + match.size])
self.assertFalse(self.longer_match_exists(a[2:], b[4:], match.size))
match = sm.find_longest_match(bhi=5, blo=1)
self.assertEqual(match.a, 1)
self.assertEqual(match.b, 1)
self.assertEqual(match.size, 4)
self.assertEqual(a[match.a: match.a + match.size],
b[match.b: match.b + match.size])
self.assertFalse(self.longer_match_exists(a, b[1:5], match.size))
def test_longest_match_with_popular_chars(self):
a = 'dabcd'
b = 'd'*100 + 'abc' + 'd'*100 # length over 200 so popular used
sm = difflib.SequenceMatcher(a=a, b=b)
match = sm.find_longest_match(0, len(a), 0, len(b))
self.assertEqual(match.a, 0)
self.assertEqual(match.b, 99)
self.assertEqual(match.size, 5)
self.assertEqual(a[match.a: match.a + match.size],
b[match.b: match.b + match.size])
self.assertFalse(self.longer_match_exists(a, b, match.size))
def setUpModule():
difflib.HtmlDiff._default_prefix = 0
def load_tests(loader, tests, pattern):
tests.addTest(doctest.DocTestSuite(difflib))
return tests
if __name__ == '__main__':
unittest.main()