68
class TestDiffOptions(tests.TestCase):
70
def test_unified_added(self):
71
"""Check for default style '-u' only if no other style specified
74
# Verify that style defaults to unified, id est '-u' appended
75
# to option list, in the absence of an alternative style.
76
self.assertEqual(['-a', '-u'], diff.default_style_unified(['-a']))
79
class TestDiffOptionsScenarios(tests.TestCase):
81
scenarios = [(s, dict(style=s)) for s in diff.style_option_list]
82
style = None # Set by load_tests_apply_scenarios from scenarios
84
def test_unified_not_added(self):
85
# Verify that for all valid style options, '-u' is not
86
# appended to option list.
87
ret_opts = diff.default_style_unified(diff_opts=["%s" % (self.style,)])
88
self.assertEqual(["%s" % (self.style,)], ret_opts)
91
66
class TestDiff(tests.TestCase):
93
68
def test_add_nl(self):
94
69
"""diff generates a valid diff for patches that add a newline"""
95
70
lines = udiff_lines(['boo'], ['boo\n'])
96
71
self.check_patch(lines)
97
self.assertEqual(lines[4], '\\ No newline at end of file\n')
72
self.assertEquals(lines[4], '\\ No newline at end of file\n')
98
73
## "expected no-nl, got %r" % lines[4]
100
75
def test_add_nl_2(self):
113
88
lines = udiff_lines(['boo\n'], ['boo'])
114
89
self.check_patch(lines)
115
self.assertEqual(lines[5], '\\ No newline at end of file\n')
90
self.assertEquals(lines[5], '\\ No newline at end of file\n')
116
91
## "expected no-nl, got %r" % lines[5]
118
93
def check_patch(self, lines):
119
self.assertTrue(len(lines) > 1)
94
self.assert_(len(lines) > 1)
120
95
## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
121
self.assertTrue(lines[0].startswith ('---'))
96
self.assert_(lines[0].startswith ('---'))
122
97
## 'No orig line for patch:\n%s' % "".join(lines)
123
self.assertTrue(lines[1].startswith ('+++'))
98
self.assert_(lines[1].startswith ('+++'))
124
99
## 'No mod line for patch:\n%s' % "".join(lines)
125
self.assertTrue(len(lines) > 2)
100
self.assert_(len(lines) > 2)
126
101
## "No hunks for patch:\n%s" % "".join(lines)
127
self.assertTrue(lines[2].startswith('@@'))
102
self.assert_(lines[2].startswith('@@'))
128
103
## "No hunk header for patch:\n%s" % "".join(lines)
129
self.assertTrue('@@' in lines[2][2:])
104
self.assert_('@@' in lines[2][2:])
130
105
## "Unterminated hunk header for patch:\n%s" % "".join(lines)
132
107
def test_binary_lines(self):
236
211
self.assertIsInstance(output.getvalue(), str,
237
212
'internal_diff should return bytestrings')
239
def test_internal_diff_default_context(self):
241
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
242
'same_text\n','same_text\n','old_text\n'],
243
'new', ['same_text\n','same_text\n','same_text\n',
244
'same_text\n','same_text\n','new_text\n'], output)
245
lines = output.getvalue().splitlines(True)
246
self.check_patch(lines)
247
self.assertEqual(['--- old\n',
259
def test_internal_diff_no_context(self):
261
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
262
'same_text\n','same_text\n','old_text\n'],
263
'new', ['same_text\n','same_text\n','same_text\n',
264
'same_text\n','same_text\n','new_text\n'], output,
266
lines = output.getvalue().splitlines(True)
267
self.check_patch(lines)
268
self.assertEqual(['--- old\n',
277
def test_internal_diff_more_context(self):
279
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
280
'same_text\n','same_text\n','old_text\n'],
281
'new', ['same_text\n','same_text\n','same_text\n',
282
'same_text\n','same_text\n','new_text\n'], output,
284
lines = output.getvalue().splitlines(True)
285
self.check_patch(lines)
286
self.assertEqual(['--- old\n',
303
215
class TestDiffFiles(tests.TestCaseInTempDir):
308
220
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
310
222
cmd = ['diff', '-u', '--binary', 'old', 'new']
311
with open('old', 'wb') as f: f.write('\x00foobar\n')
312
with open('new', 'wb') as f: f.write('foo\x00bar\n')
223
open('old', 'wb').write('\x00foobar\n')
224
open('new', 'wb').write('foo\x00bar\n')
313
225
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
314
226
stdin=subprocess.PIPE)
315
227
out, err = pipe.communicate()
864
776
b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
865
777
sm = self._PatienceSequenceMatcher(None, a, b)
866
778
mb = sm.get_matching_blocks()
867
self.assertEqual(35, len(mb))
779
self.assertEquals(35, len(mb))
869
781
def test_unique_lcs(self):
870
782
unique_lcs = self._unique_lcs
871
self.assertEqual(unique_lcs('', ''), [])
872
self.assertEqual(unique_lcs('', 'a'), [])
873
self.assertEqual(unique_lcs('a', ''), [])
874
self.assertEqual(unique_lcs('a', 'a'), [(0,0)])
875
self.assertEqual(unique_lcs('a', 'b'), [])
876
self.assertEqual(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
877
self.assertEqual(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
878
self.assertEqual(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
879
self.assertEqual(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
783
self.assertEquals(unique_lcs('', ''), [])
784
self.assertEquals(unique_lcs('', 'a'), [])
785
self.assertEquals(unique_lcs('a', ''), [])
786
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
787
self.assertEquals(unique_lcs('a', 'b'), [])
788
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
789
self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
790
self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
791
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
881
self.assertEqual(unique_lcs('acbac', 'abc'), [(2,1)])
793
self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
883
795
def test_recurse_matches(self):
884
796
def test_one(a, b, matches):
885
797
test_matches = []
886
798
self._recurse_matches(
887
799
a, b, 0, 0, len(a), len(b), test_matches, 10)
888
self.assertEqual(test_matches, matches)
800
self.assertEquals(test_matches, matches)
890
802
test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
891
803
[(0, 0), (2, 2), (4, 4)])
1495
1408
def test_encodable_filename(self):
1496
1409
# Just checks file path for external diff tool.
1497
1410
# We cannot change CPython's internal encoding used by os.exec*.
1498
1412
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1499
1413
None, None, None)
1500
1414
for _, scenario in EncodingAdapter.encoding_scenarios:
1501
1415
encoding = scenario['encoding']
1502
dirname = scenario['info']['directory']
1416
dirname = scenario['info']['directory']
1503
1417
filename = scenario['info']['filename']
1505
1419
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1506
1420
relpath = dirname + u'/' + filename
1507
1421
fullpath = diffobj._safe_filename('safe', relpath)
1508
self.assertEqual(fullpath,
1509
fullpath.encode(encoding).decode(encoding))
1510
self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
1424
fullpath.encode(encoding).decode(encoding)
1426
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1512
1428
def test_unencodable_filename(self):
1513
1430
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1514
1431
None, None, None)
1515
1432
for _, scenario in EncodingAdapter.encoding_scenarios:
1516
1433
encoding = scenario['encoding']
1517
dirname = scenario['info']['directory']
1434
dirname = scenario['info']['directory']
1518
1435
filename = scenario['info']['filename']
1520
1437
if encoding == 'iso-8859-1':