~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Aaron Bentley
  • Date: 2006-05-23 01:18:33 UTC
  • mto: This revision was merged to the branch mainline in revision 1727.
  • Revision ID: aaron.bentley@utoronto.ca-20060523011833-43cf7eaa21074aaf
Clean up test_diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
from bzrlib.diff import internal_diff
4
4
from bzrlib.errors import BinaryFile
 
5
from bzrlib.patiencediff import (recurse_matches, SequenceMatcher, unique_lcs,
 
6
                                 unified_diff, unified_diff_files)
5
7
from bzrlib.tests import TestCase, TestCaseInTempDir
6
8
 
7
9
 
11
13
    output.seek(0, 0)
12
14
    return output.readlines()
13
15
 
 
16
 
14
17
class TestDiff(TestCase):
 
18
 
15
19
    def test_add_nl(self):
16
20
        """diff generates a valid diff for patches that add a newline"""
17
21
        lines = udiff_lines(['boo'], ['boo\n'])
57
61
        udiff_lines([1023 * 'a' + '\x00'], [], allow_binary=True)
58
62
        udiff_lines([], [1023 * 'a' + '\x00'], allow_binary=True)
59
63
 
 
64
 
60
65
class TestCDVDiffLib(TestCase):
61
66
 
62
67
    def test_unique_lcs(self):
63
 
        from bzrlib.patiencediff import unique_lcs
64
 
 
65
68
        self.assertEquals(unique_lcs('', ''), [])
66
69
        self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
67
70
        self.assertEquals(unique_lcs('a', 'b'), [])
73
76
        self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
74
77
 
75
78
    def test_recurse_matches(self):
76
 
        from bzrlib.patiencediff import recurse_matches
77
 
 
78
79
        def test_one(a, b, matches):
79
80
            test_matches = []
80
81
            recurse_matches(a, b, len(a), len(b), test_matches, 10)
96
97
        test_one('aBccDe', 'abccde', [(0,0), (5,5)])
97
98
 
98
99
    def test_matching_blocks(self):
99
 
        from bzrlib.patiencediff import SequenceMatcher
100
 
 
101
100
        def chk_blocks(a, b, matching):
102
101
            # difflib always adds a signature of the total
103
102
            # length, with no matching entries at the end
144
143
        chk_blocks('bbbbbbbb', 'cbbbbbbc', [(0,1,6)])
145
144
 
146
145
    def test_opcodes(self):
147
 
        from bzrlib.patiencediff import SequenceMatcher
148
 
 
149
146
        def chk_ops(a, b, codes):
150
147
            s = SequenceMatcher(None, a, b)
151
148
            self.assertEquals(codes, s.get_opcodes())
180
177
                , ('insert', 6,6,  6,11)
181
178
                , ('equal',  6,16, 11,21)
182
179
                ])
183
 
 
184
180
        chk_ops(
185
181
                [ 'hello there\n'
186
182
                , 'world\n'
191
187
                , ('delete', 1,2, 1,1)
192
188
                , ('equal',  2,3, 1,2)
193
189
                ])
194
 
 
195
190
        chk_ops('aBccDe', 'abccde', 
196
191
                [ ('equal',   0,1, 0,1)
197
192
                , ('replace', 1,2, 1,2)
199
194
                , ('replace', 4,5, 4,5)
200
195
                , ('equal',   5,6, 5,6)
201
196
                ])
202
 
 
203
197
        chk_ops('aBcdEcdFg', 'abcdecdfg', 
204
198
                [ ('equal',   0,1, 0,1)
205
199
                , ('replace', 1,2, 1,2)
213
207
    def test_multiple_ranges(self):
214
208
        # There was an earlier bug where we used a bad set of ranges,
215
209
        # this triggers that specific bug, to make sure it doesn't regress
216
 
        from bzrlib.patiencediff import SequenceMatcher
217
 
 
218
210
        def chk_blocks(a, b, matching):
219
211
            # difflib always adds a signature of the total
220
212
            # length, with no matching entries at the end
280
272
, [(0,0,1), (1, 4, 2), (9, 19, 1), (12, 23, 3)])
281
273
 
282
274
    def test_cdv_unified_diff(self):
283
 
        from bzrlib.patiencediff import SequenceMatcher, unified_diff
284
 
 
285
275
        txt_a = [ 'hello there\n'
286
276
                , 'world\n'
287
277
                , 'how are you today?\n']
288
278
        txt_b = [ 'hello there\n'
289
279
                , 'how are you today?\n']
290
 
 
291
280
        self.assertEquals([ '---  \n'
292
281
                          , '+++  \n'
293
282
                          , '@@ -1,3 +1,2 @@\n'
297
286
                          ]
298
287
                          , list(unified_diff(txt_a, txt_b
299
288
                                        , sequencematcher=SequenceMatcher)))
300
 
 
301
289
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
302
290
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
303
 
 
304
291
        # This is the result with LongestCommonSubstring matching
305
292
        self.assertEquals([ '---  \n'
306
293
                          , '+++  \n'
317
304
                          , ' e\n'
318
305
                          , ' f\n']
319
306
                          , list(unified_diff(txt_a, txt_b)))
320
 
 
321
307
        # And the cdv diff
322
308
        self.assertEquals([ '---  \n'
323
309
                          , '+++  \n'
334
320
                          , ' h\n'
335
321
                          , ' i\n'
336
322
                          ]
337
 
                          , list(unified_diff(txt_a, txt_b
338
 
                                        , sequencematcher=SequenceMatcher)))
 
323
                          , list(unified_diff(txt_a, txt_b,
 
324
                                 sequencematcher=SequenceMatcher)))
 
325
 
339
326
 
340
327
class TestCDVDiffLibFiles(TestCaseInTempDir):
341
328
 
342
329
    def test_cdv_unified_diff_files(self):
343
 
        from bzrlib.patiencediff import SequenceMatcher, unified_diff_files
344
 
 
345
330
        txt_a = [ 'hello there\n'
346
331
                , 'world\n'
347
332
                , 'how are you today?\n']
357
342
                          , '-world\n'
358
343
                          , ' how are you today?\n'
359
344
                          ]
360
 
                          , list(unified_diff_files('a1', 'b1'
361
 
                                        , sequencematcher=SequenceMatcher)))
 
345
                          , list(unified_diff_files('a1', 'b1',
 
346
                                 sequencematcher=SequenceMatcher)))
362
347
 
363
348
        txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
364
349
        txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
398
383
                          , ' h\n'
399
384
                          , ' i\n'
400
385
                          ]
401
 
                          , list(unified_diff_files('a2', 'b2'
402
 
                                        , sequencematcher=SequenceMatcher)))
 
386
                          , list(unified_diff_files('a2', 'b2',
 
387
                                 sequencematcher=SequenceMatcher)))