~bzr-pqm/bzr/bzr.dev

1092.1.2 by Robert Collins
switch testsweet.TestBase to TestCase which is convention for xUnit
1
from bzrlib.selftest 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
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)
23
1092.1.2 by Robert Collins
switch testsweet.TestBase to TestCase which is convention for xUnit
24
class AddNL(TestCase):
974.1.6 by Aaron Bentley
Added unit tests
25
    """
26
    diff generates a valid diff for patches that add a newline
27
    """
28
    def runTest(self):
29
        lines = udiff_lines(['boo'], ['boo\n'])
30
        check_patch(lines)
31
        assert lines[4] == '\\ No newline at end of file\n', \
32
            "expected no-nl, got %r" % lines[4]
33
34
1092.1.2 by Robert Collins
switch testsweet.TestBase to TestCase which is convention for xUnit
35
class AddNL2(TestCase):
974.1.6 by Aaron Bentley
Added unit tests
36
    """
37
    diff generates a valid diff for patches that change last line and add a
38
    newline
39
    """
40
    def runTest(self):
41
        lines = udiff_lines(['boo'], ['goo\n'])
42
        check_patch(lines)
43
        assert lines[4] == '\\ No newline at end of file\n', \
44
            "expected no-nl, got %r" % lines[4]
45
1092.1.2 by Robert Collins
switch testsweet.TestBase to TestCase which is convention for xUnit
46
class RemoveNL(TestCase):
974.1.6 by Aaron Bentley
Added unit tests
47
    """
48
    diff generates a valid diff for patches that change last line and add a
49
    newline
50
    """
51
    def runTest(self):
52
        lines = udiff_lines(['boo\n'], ['boo'])
53
        check_patch(lines)
54
        assert lines[5] == '\\ No newline at end of file\n', \
55
            "expected no-nl, got %r" % lines[5]
56
57
TEST_CLASSES = [
58
    AddNL, 
59
    AddNL2, 
60
    RemoveNL,
61
]