1
from bzrlib.selftest import TestCase
2
from bzrlib.diff import internal_diff
1
3
from cStringIO import StringIO
3
from bzrlib.diff import internal_diff
4
from bzrlib.errors import BinaryFile
5
from bzrlib.tests import TestCase
8
def udiff_lines(old, new, allow_binary=False):
4
def udiff_lines(old, new):
10
internal_diff('old', old, 'new', new, output, allow_binary)
6
internal_diff('old', old, 'new', new, output)
12
8
return output.readlines()
10
def check_patch(lines):
11
assert len(lines) > 1, \
12
"Not enough lines for a file header for patch:\n%s" % "".join(lines)
13
assert lines[0].startswith ('---'), \
14
'No orig line for patch:\n%s' % "".join(lines)
15
assert lines[1].startswith ('+++'), \
16
'No mod line for patch:\n%s' % "".join(lines)
17
assert len(lines) > 2, \
18
"No hunks for patch:\n%s" % "".join(lines)
19
assert lines[2].startswith('@@'),\
20
"No hunk header for patch:\n%s" % "".join(lines)
21
assert '@@' in lines[2][2:], \
22
"Unterminated hunk header for patch:\n%s" % "".join(lines)
14
24
class TestDiff(TestCase):
15
25
def test_add_nl(self):
16
26
"""diff generates a valid diff for patches that add a newline"""
17
27
lines = udiff_lines(['boo'], ['boo\n'])
18
self.check_patch(lines)
19
self.assertEquals(lines[4], '\\ No newline at end of file\n')
20
## "expected no-nl, got %r" % lines[4]
29
assert lines[4] == '\\ No newline at end of file\n', \
30
"expected no-nl, got %r" % lines[4]
22
32
def test_add_nl_2(self):
23
33
"""diff generates a valid diff for patches that change last line and
26
36
lines = udiff_lines(['boo'], ['goo\n'])
27
self.check_patch(lines)
28
self.assertEquals(lines[4], '\\ No newline at end of file\n')
29
## "expected no-nl, got %r" % lines[4]
38
assert lines[4] == '\\ No newline at end of file\n', \
39
"expected no-nl, got %r" % lines[4]
31
41
def test_remove_nl(self):
32
42
"""diff generates a valid diff for patches that change last line and
35
45
lines = udiff_lines(['boo\n'], ['boo'])
36
self.check_patch(lines)
37
self.assertEquals(lines[5], '\\ No newline at end of file\n')
38
## "expected no-nl, got %r" % lines[5]
40
def check_patch(self, lines):
41
self.assert_(len(lines) > 1)
42
## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
43
self.assert_(lines[0].startswith ('---'))
44
## 'No orig line for patch:\n%s' % "".join(lines)
45
self.assert_(lines[1].startswith ('+++'))
46
## 'No mod line for patch:\n%s' % "".join(lines)
47
self.assert_(len(lines) > 2)
48
## "No hunks for patch:\n%s" % "".join(lines)
49
self.assert_(lines[2].startswith('@@'))
50
## "No hunk header for patch:\n%s" % "".join(lines)
51
self.assert_('@@' in lines[2][2:])
52
## "Unterminated hunk header for patch:\n%s" % "".join(lines)
54
def test_binary_lines(self):
55
self.assertRaises(BinaryFile, udiff_lines, [1023 * 'a' + '\x00'], [])
56
self.assertRaises(BinaryFile, udiff_lines, [], [1023 * 'a' + '\x00'])
57
udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
58
udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
47
assert lines[5] == '\\ No newline at end of file\n', \
48
"expected no-nl, got %r" % lines[5]