~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/mdiff.py

  • Committer: Martin Pool
  • Date: 2005-08-12 15:41:44 UTC
  • Revision ID: mbp@sourcefrog.net-20050812154144-bc98570a78b8f633
- merge in deferred revfile work

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
# TODO: maybe work on files not strings?
27
27
 
28
 
# FIXME: doesn't work properly on files without trailing newlines
29
28
 
30
 
import unittest
31
29
import difflib, sys, struct
32
30
from cStringIO import StringIO
33
31
 
34
32
def linesplit(a):
35
 
    """Split into two lists: content and line positions.
36
 
 
37
 
    This returns (al, ap).
38
 
 
39
 
    al[i] is the string content of line i of the file, including its
40
 
    newline (if any).
41
 
 
42
 
    ap[i] is the byte position in the file where that line starts.
43
 
 
44
 
    ap[-1] is the byte position of the end of the file (i.e. the
45
 
    length of the file.)
46
 
 
47
 
    This transformation allows us to do a line-based diff and then map
48
 
    back to byte positions.
49
 
    """
50
 
 
 
33
    """Split into two lists: content and line positions"""
51
34
    al, ap = [], []
52
35
    last = 0
53
36
 
58
41
        last = n
59
42
        n = a.find("\n", n) + 1
60
43
 
61
 
    if last < len(a):
62
 
        al.append(a[last:])
63
 
        ap.append(last)
64
 
 
65
44
    # position at the end
66
45
    ap.append(len(a))
67
46
 
138
117
 
139
118
 
140
119
 
141
 
class TestDiffPatch(unittest.TestCase):
142
 
    def doDiffPatch(self, old, new):
143
 
        diff = bdiff(old, new)
144
 
        result = bpatch(old, diff)
145
 
        self.assertEquals(new, result)
146
 
 
147
 
 
148
 
    def testSimpleDiff(self):
149
 
        """Simply add a line at the end"""
150
 
        self.doDiffPatch('a\nb\n', 'a\nb\nc\n')
151
 
        
152
 
 
153
 
        
154
 
    def testTrailingLine(self):
155
 
        """Test diff that adds an unterminated line.
156
 
 
157
 
        (Old versions didn't do this properly.)"""
158
 
        self.doDiffPatch('a\nb\nc\n',
159
 
                         'a\nb\nc\nd')
160
 
 
161
 
 
162
 
if __name__ == '__main__':
163
 
    unittest.main()