~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: Martin Pool
  • Date: 2009-03-24 05:21:02 UTC
  • mfrom: (4192 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4202.
  • Revision ID: mbp@sourcefrog.net-20090324052102-8kk087b32tep3d9h
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from bzrlib.tests import TestCase
22
22
 
23
23
from bzrlib.iterablefile import IterableFile
24
 
from bzrlib.patches import (MalformedLine, 
25
 
                            MalformedHunkHeader, 
26
 
                            MalformedPatchHeader, 
27
 
                            ContextLine, 
 
24
from bzrlib.patches import (MalformedLine,
 
25
                            MalformedHunkHeader,
 
26
                            MalformedPatchHeader,
 
27
                            ContextLine,
28
28
                            InsertLine,
29
 
                            RemoveLine, 
30
 
                            difference_index, 
 
29
                            RemoveLine,
 
30
                            difference_index,
31
31
                            get_patch_names,
32
 
                            hunk_from_header, 
33
 
                            iter_patched, 
 
32
                            hunk_from_header,
 
33
                            iter_patched,
 
34
                            iter_patched_from_hunks,
34
35
                            parse_line,
35
36
                            parse_patch,
36
 
                            parse_patches)
 
37
                            parse_patches,
 
38
                            NO_NL)
37
39
 
38
40
 
39
41
class PatchesTester(TestCase):
40
42
 
41
43
    def datafile(self, filename):
42
 
        data_path = os.path.join(os.path.dirname(__file__), 
 
44
        data_path = os.path.join(os.path.dirname(__file__),
43
45
                                 "test_patches_data", filename)
44
46
        return file(data_path, "rb")
45
47
 
111
113
        self.lineThing(" hello\n", ContextLine)
112
114
        self.lineThing("+hello\n", InsertLine)
113
115
        self.lineThing("-hello\n", RemoveLine)
114
 
    
 
116
 
115
117
    def testMalformedLine(self):
116
118
        """Parse invalid valid hunk lines"""
117
119
        self.makeMalformedLine("hello\n")
118
 
    
 
120
 
 
121
    def testMalformedLineNO_NL(self):
 
122
        """Parse invalid '\ No newline at end of file' in hunk lines"""
 
123
        self.makeMalformedLine(NO_NL)
 
124
 
119
125
    def compare_parsed(self, patchtext):
120
126
        lines = patchtext.splitlines(True)
121
127
        patch = parse_patch(lines.__iter__())
173
179
            ('diff-4', 'orig-4', 'mod-4'),
174
180
            ('diff-5', 'orig-5', 'mod-5'),
175
181
            ('diff-6', 'orig-6', 'mod-6'),
 
182
            ('diff-7', 'orig-7', 'mod-7'),
176
183
        ]
177
184
        for diff, orig, mod in files:
178
185
            patch = self.datafile(diff)
187
194
                count += 1
188
195
            self.assertEqual(count, len(mod_lines))
189
196
 
 
197
    def test_iter_patched_from_hunks(self):
 
198
        """Test a few patch files, and make sure they work."""
 
199
        files = [
 
200
            ('diff-2', 'orig-2', 'mod-2'),
 
201
            ('diff-3', 'orig-3', 'mod-3'),
 
202
            ('diff-4', 'orig-4', 'mod-4'),
 
203
            ('diff-5', 'orig-5', 'mod-5'),
 
204
            ('diff-6', 'orig-6', 'mod-6'),
 
205
            ('diff-7', 'orig-7', 'mod-7'),
 
206
        ]
 
207
        for diff, orig, mod in files:
 
208
            parsed = parse_patch(self.datafile(diff))
 
209
            orig_lines = list(self.datafile(orig))
 
210
            mod_lines = list(self.datafile(mod))
 
211
            iter_patched = iter_patched_from_hunks(orig_lines, parsed.hunks)
 
212
            patched_file = IterableFile(iter_patched)
 
213
            lines = []
 
214
            count = 0
 
215
            for patch_line in patched_file:
 
216
                self.assertEqual(patch_line, mod_lines[count])
 
217
                count += 1
 
218
            self.assertEqual(count, len(mod_lines))
 
219
 
190
220
    def testFirstLineRenumber(self):
191
221
        """Make sure we handle lines at the beginning of the hunk"""
192
222
        patch = parse_patch(self.datafile("insert_top.patch"))
227
257
        for patch in patches:
228
258
            patch_files.append((patch.oldname, patch.newname))
229
259
        self.assertEqual(patch_files, filenames)
 
260
 
 
261
    def testStatsValues(self):
 
262
        """Test the added, removed and hunks values for stats_values."""
 
263
        patch = parse_patch(self.datafile("diff"))
 
264
        self.assertEqual((299, 407, 48), patch.stats_values())