~bzr-pqm/bzr/bzr.dev

1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
1
from cStringIO import StringIO
2
3
from bzrlib.diff import internal_diff
4
from bzrlib.errors import BinaryFile
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
5
from bzrlib.tests import TestCase
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
6
7
1558.15.11 by Aaron Bentley
Apply merge review suggestions
8
def udiff_lines(old, new, allow_binary=False):
974.1.6 by Aaron Bentley
Added unit tests
9
    output = StringIO()
1558.15.11 by Aaron Bentley
Apply merge review suggestions
10
    internal_diff('old', old, 'new', new, output, allow_binary)
974.1.6 by Aaron Bentley
Added unit tests
11
    output.seek(0, 0)
12
    return output.readlines()
13
1102 by Martin Pool
- merge test refactoring from robertc
14
class TestDiff(TestCase):
15
    def test_add_nl(self):
16
        """diff generates a valid diff for patches that add a newline"""
974.1.6 by Aaron Bentley
Added unit tests
17
        lines = udiff_lines(['boo'], ['boo\n'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
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]
974.1.6 by Aaron Bentley
Added unit tests
21
1102 by Martin Pool
- merge test refactoring from robertc
22
    def test_add_nl_2(self):
23
        """diff generates a valid diff for patches that change last line and
24
        add a newline.
25
        """
974.1.6 by Aaron Bentley
Added unit tests
26
        lines = udiff_lines(['boo'], ['goo\n'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
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]
974.1.6 by Aaron Bentley
Added unit tests
30
1102 by Martin Pool
- merge test refactoring from robertc
31
    def test_remove_nl(self):
32
        """diff generates a valid diff for patches that change last line and
33
        add a newline.
34
        """
974.1.6 by Aaron Bentley
Added unit tests
35
        lines = udiff_lines(['boo\n'], ['boo'])
1185.16.145 by Martin Pool
Remove all assert statements from test cases.
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]
39
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)
53
1558.15.2 by Aaron Bentley
Implemented binary file handling for diff
54
    def test_binary_lines(self):
55
        self.assertRaises(BinaryFile, udiff_lines, [1023 * 'a' + '\x00'], [])
56
        self.assertRaises(BinaryFile, udiff_lines, [], [1023 * 'a' + '\x00'])
1558.15.11 by Aaron Bentley
Apply merge review suggestions
57
        udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
58
        udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)