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