~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
18
18
 
19
19
import os.path
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,
34
 
                            iter_patched_from_hunks,
 
32
                            hunk_from_header, 
 
33
                            iter_patched, 
35
34
                            parse_line,
36
35
                            parse_patch,
37
 
                            parse_patches,
38
 
                            NO_NL)
 
36
                            parse_patches)
39
37
 
40
38
 
41
39
class PatchesTester(TestCase):
42
40
 
43
41
    def datafile(self, filename):
44
 
        data_path = os.path.join(os.path.dirname(__file__),
 
42
        data_path = os.path.join(os.path.dirname(__file__), 
45
43
                                 "test_patches_data", filename)
46
44
        return file(data_path, "rb")
47
45
 
113
111
        self.lineThing(" hello\n", ContextLine)
114
112
        self.lineThing("+hello\n", InsertLine)
115
113
        self.lineThing("-hello\n", RemoveLine)
116
 
 
 
114
    
117
115
    def testMalformedLine(self):
118
116
        """Parse invalid valid hunk lines"""
119
117
        self.makeMalformedLine("hello\n")
120
 
 
121
 
    def testMalformedLineNO_NL(self):
122
 
        """Parse invalid '\ No newline at end of file' in hunk lines"""
123
 
        self.makeMalformedLine(NO_NL)
124
 
 
 
118
    
125
119
    def compare_parsed(self, patchtext):
126
120
        lines = patchtext.splitlines(True)
127
121
        patch = parse_patch(lines.__iter__())
179
173
            ('diff-4', 'orig-4', 'mod-4'),
180
174
            ('diff-5', 'orig-5', 'mod-5'),
181
175
            ('diff-6', 'orig-6', 'mod-6'),
182
 
            ('diff-7', 'orig-7', 'mod-7'),
183
176
        ]
184
177
        for diff, orig, mod in files:
185
178
            patch = self.datafile(diff)
194
187
                count += 1
195
188
            self.assertEqual(count, len(mod_lines))
196
189
 
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
 
 
220
190
    def testFirstLineRenumber(self):
221
191
        """Make sure we handle lines at the beginning of the hunk"""
222
192
        patch = parse_patch(self.datafile("insert_top.patch"))
257
227
        for patch in patches:
258
228
            patch_files.append((patch.oldname, patch.newname))
259
229
        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())