~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_patches.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-10-25 08:29:08 UTC
  • mfrom: (2940.1.2 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20071025082908-abn3kunrb2ivdvth
renaming of experimental pack formats to include knitpack in their name (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2008 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2004 - 2006 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
19
20
import os.path
20
21
 
21
 
from bzrlib.tests import TestCase
22
 
 
23
22
from bzrlib.iterablefile import IterableFile
24
23
from bzrlib.patches import (MalformedLine, 
25
24
                            MalformedHunkHeader, 
36
35
                            parse_patches)
37
36
 
38
37
 
39
 
class PatchesTester(TestCase):
40
 
 
 
38
class PatchesTester(unittest.TestCase):
41
39
    def datafile(self, filename):
42
40
        data_path = os.path.join(os.path.dirname(__file__), 
43
41
                                 "test_patches_data", filename)
47
45
        """Parse a valid patch header"""
48
46
        lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
49
47
        (orig, mod) = get_patch_names(lines.__iter__())
50
 
        self.assertEqual(orig, "orig/commands.py")
51
 
        self.assertEqual(mod, "mod/dommands.py")
 
48
        assert(orig == "orig/commands.py")
 
49
        assert(mod == "mod/dommands.py")
52
50
 
53
51
    def testInvalidPatchHeader(self):
54
52
        """Parse an invalid patch header"""
60
58
        """Parse a valid hunk header"""
61
59
        header = "@@ -34,11 +50,6 @@\n"
62
60
        hunk = hunk_from_header(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)
 
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)
68
66
 
69
67
    def testValidHunkHeader2(self):
70
68
        """Parse a tricky, valid hunk header"""
71
69
        header = "@@ -1 +0,0 @@\n"
72
70
        hunk = hunk_from_header(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)
 
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)
78
76
 
79
77
    def testPDiff(self):
80
78
        """Parse a hunk header produced by diff -p"""
100
98
 
101
99
    def lineThing(self,text, type):
102
100
        line = parse_line(text)
103
 
        self.assertIsInstance(line, type)
104
 
        self.assertEqual(str(line), text)
 
101
        assert(isinstance(line, type))
 
102
        assert(str(line)==text)
105
103
 
106
104
    def makeMalformedLine(self, text):
107
105
        self.assertRaises(MalformedLine, parse_line, text)
153
151
            if mod_pos is None:
154
152
                removals.append(orig[i])
155
153
                continue
156
 
            self.assertEqual(mod[mod_pos], orig[i])
 
154
            assert(mod[mod_pos]==orig[i])
157
155
        rem_iter = removals.__iter__()
158
156
        for hunk in patch.hunks:
159
157
            for line in hunk.lines:
162
160
                    if line.contents != next:
163
161
                        sys.stdout.write(" orig:%spatch:%s" % (next,
164
162
                                         line.contents))
165
 
                    self.assertEqual(line.contents, next)
 
163
                    assert(line.contents == next)
166
164
        self.assertRaises(StopIteration, rem_iter.next)
167
165
 
168
166
    def testPatching(self):
190
188
    def testFirstLineRenumber(self):
191
189
        """Make sure we handle lines at the beginning of the hunk"""
192
190
        patch = parse_patch(self.datafile("insert_top.patch"))
193
 
        self.assertEqual(patch.pos_in_mod(0), 1)
 
191
        assert (patch.pos_in_mod(0)==1)
194
192
 
195
193
    def testParsePatches(self):
196
194
        """Make sure file names can be extracted from tricky unified diffs"""
226
224
        patch_files = []
227
225
        for patch in patches:
228
226
            patch_files.append((patch.oldname, patch.newname))
229
 
        self.assertEqual(patch_files, filenames)
 
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()