~bzr-pqm/bzr/bzr.dev

1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
1
from bzrlib.tests import TestCase
974.1.6 by Aaron Bentley
Added unit tests
2
from bzrlib.diff import internal_diff
3
from cStringIO import StringIO
4
def udiff_lines(old, new):
5
    output = StringIO()
6
    internal_diff('old', old, 'new', new, output)
7
    output.seek(0, 0)
8
    return output.readlines()
9
1102 by Martin Pool
- merge test refactoring from robertc
10
class TestDiff(TestCase):
11
    def test_add_nl(self):
12
        """diff generates a valid diff for patches that add a newline"""
974.1.6 by Aaron Bentley
Added unit tests
13
        lines = udiff_lines(['boo'], ['boo\n'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
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]
974.1.6 by Aaron Bentley
Added unit tests
17
1102 by Martin Pool
- merge test refactoring from robertc
18
    def test_add_nl_2(self):
19
        """diff generates a valid diff for patches that change last line and
20
        add a newline.
21
        """
974.1.6 by Aaron Bentley
Added unit tests
22
        lines = udiff_lines(['boo'], ['goo\n'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
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]
974.1.6 by Aaron Bentley
Added unit tests
26
1102 by Martin Pool
- merge test refactoring from robertc
27
    def test_remove_nl(self):
28
        """diff generates a valid diff for patches that change last line and
29
        add a newline.
30
        """
974.1.6 by Aaron Bentley
Added unit tests
31
        lines = udiff_lines(['boo\n'], ['boo'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
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]
35
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)
49