~lifeless/bzr/index.range_map

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: Robert Collins
  • Date: 2008-06-19 01:17:19 UTC
  • mfrom: (3218.1.277 +trunk)
  • Revision ID: robertc@robertcollins.net-20080619011719-1c4g4uxzzhdls2wf
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2008 Aaron Bentley, Canonical Ltd
2
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
 
19
 
import unittest
20
19
import os.path
21
20
 
 
21
from bzrlib.tests import TestCase
 
22
 
22
23
from bzrlib.iterablefile import IterableFile
23
24
from bzrlib.patches import (MalformedLine, 
24
25
                            MalformedHunkHeader, 
35
36
                            parse_patches)
36
37
 
37
38
 
38
 
class PatchesTester(unittest.TestCase):
 
39
class PatchesTester(TestCase):
 
40
 
39
41
    def datafile(self, filename):
40
42
        data_path = os.path.join(os.path.dirname(__file__), 
41
43
                                 "test_patches_data", filename)
45
47
        """Parse a valid patch header"""
46
48
        lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
47
49
        (orig, mod) = get_patch_names(lines.__iter__())
48
 
        assert(orig == "orig/commands.py")
49
 
        assert(mod == "mod/dommands.py")
 
50
        self.assertEqual(orig, "orig/commands.py")
 
51
        self.assertEqual(mod, "mod/dommands.py")
50
52
 
51
53
    def testInvalidPatchHeader(self):
52
54
        """Parse an invalid patch header"""
58
60
        """Parse a valid hunk header"""
59
61
        header = "@@ -34,11 +50,6 @@\n"
60
62
        hunk = hunk_from_header(header);
61
 
        assert (hunk.orig_pos == 34)
62
 
        assert (hunk.orig_range == 11)
63
 
        assert (hunk.mod_pos == 50)
64
 
        assert (hunk.mod_range == 6)
65
 
        assert (str(hunk) == header)
 
63
        self.assertEqual(hunk.orig_pos, 34)
 
64
        self.assertEqual(hunk.orig_range, 11)
 
65
        self.assertEqual(hunk.mod_pos, 50)
 
66
        self.assertEqual(hunk.mod_range, 6)
 
67
        self.assertEqual(str(hunk), header)
66
68
 
67
69
    def testValidHunkHeader2(self):
68
70
        """Parse a tricky, valid hunk header"""
69
71
        header = "@@ -1 +0,0 @@\n"
70
72
        hunk = hunk_from_header(header);
71
 
        assert (hunk.orig_pos == 1)
72
 
        assert (hunk.orig_range == 1)
73
 
        assert (hunk.mod_pos == 0)
74
 
        assert (hunk.mod_range == 0)
75
 
        assert (str(hunk) == header)
 
73
        self.assertEqual(hunk.orig_pos, 1)
 
74
        self.assertEqual(hunk.orig_range, 1)
 
75
        self.assertEqual(hunk.mod_pos, 0)
 
76
        self.assertEqual(hunk.mod_range, 0)
 
77
        self.assertEqual(str(hunk), header)
76
78
 
77
79
    def testPDiff(self):
78
80
        """Parse a hunk header produced by diff -p"""
98
100
 
99
101
    def lineThing(self,text, type):
100
102
        line = parse_line(text)
101
 
        assert(isinstance(line, type))
102
 
        assert(str(line)==text)
 
103
        self.assertIsInstance(line, type)
 
104
        self.assertEqual(str(line), text)
103
105
 
104
106
    def makeMalformedLine(self, text):
105
107
        self.assertRaises(MalformedLine, parse_line, text)
151
153
            if mod_pos is None:
152
154
                removals.append(orig[i])
153
155
                continue
154
 
            assert(mod[mod_pos]==orig[i])
 
156
            self.assertEqual(mod[mod_pos], orig[i])
155
157
        rem_iter = removals.__iter__()
156
158
        for hunk in patch.hunks:
157
159
            for line in hunk.lines:
160
162
                    if line.contents != next:
161
163
                        sys.stdout.write(" orig:%spatch:%s" % (next,
162
164
                                         line.contents))
163
 
                    assert(line.contents == next)
 
165
                    self.assertEqual(line.contents, next)
164
166
        self.assertRaises(StopIteration, rem_iter.next)
165
167
 
166
168
    def testPatching(self):
188
190
    def testFirstLineRenumber(self):
189
191
        """Make sure we handle lines at the beginning of the hunk"""
190
192
        patch = parse_patch(self.datafile("insert_top.patch"))
191
 
        assert (patch.pos_in_mod(0)==1)
 
193
        self.assertEqual(patch.pos_in_mod(0), 1)
192
194
 
193
195
    def testParsePatches(self):
194
196
        """Make sure file names can be extracted from tricky unified diffs"""
224
226
        patch_files = []
225
227
        for patch in patches:
226
228
            patch_files.append((patch.oldname, patch.newname))
227
 
        assert (patch_files == filenames)
228
 
            
229
 
def test():
230
 
    patchesTestSuite = unittest.makeSuite(PatchesTester,'test')
231
 
    runner = unittest.TextTestRunner(verbosity=0)
232
 
    return runner.run(patchesTestSuite)
233
 
 
234
 
 
235
 
if __name__ == "__main__":
236
 
    test()
 
229
        self.assertEqual(patch_files, filenames)