1 """Tests for the diffparse module"""
2 # pylint: disable=redefined-outer-name
3 from __future__
import absolute_import
, division
, print_function
, unicode_literals
8 from cola
import diffparse
13 class DiffLinesTestData(object):
14 """Test data used by DiffLines tests"""
17 self
.parser
= diffparse
.DiffLines()
18 fixture_path
= helper
.fixture('diff.txt')
19 self
.text
= core
.read(fixture_path
)
24 """Return test data for diffparse.DiffLines tests"""
25 return DiffLinesTestData()
29 fixture_path
= helper
.fixture('diff.txt')
30 parser
= diffparse
.DiffParser('cola/diffparse.py', core
.read(fixture_path
))
33 assert len(hunks
) == 3
34 assert hunks
[0].first_line_idx
== 0
35 assert len(hunks
[0].lines
) == 23
36 assert hunks
[0].lines
[0] == '@@ -6,10 +6,21 @@ from cola import gitcmds\n'
37 assert hunks
[0].lines
[1] == ' from cola import gitcfg\n'
38 assert hunks
[0].lines
[2] == ' \n'
39 assert hunks
[0].lines
[3] == ' \n'
40 assert hunks
[0].lines
[4] == '+class DiffSource(object):\n'
41 assert hunks
[0].lines
[-1] == (
42 r
" self._header_start_re = re.compile('^@@ -(\d+)"
43 r
" \+(\d+),(\d+) @@.*')"
46 assert hunks
[1].first_line_idx
== 23
47 assert len(hunks
[1].lines
) == 18
48 assert hunks
[1].lines
[0] == '@@ -29,13 +40,11 @@ class DiffParser(object):\n'
49 assert hunks
[1].lines
[1] == ' self.diff_sel = []\n'
50 assert hunks
[1].lines
[2] == ' self.selected = []\n'
51 assert hunks
[1].lines
[3] == ' self.filename = filename\n'
52 assert hunks
[1].lines
[4] == (
53 '+ self.diff_source = diff_source or DiffSource()\n'
55 assert hunks
[1].lines
[-1] == ' self.header = header\n'
57 assert hunks
[2].first_line_idx
== 41
58 assert len(hunks
[2].lines
) == 16
59 assert hunks
[2].lines
[0] == '@@ -43,11 +52,10 @@ class DiffParser(object):\n'
60 assert hunks
[2].lines
[-1] == (
61 ' """Writes a new diff corresponding to the user\'s'
66 def test_diff_at_start():
67 fixture_path
= helper
.fixture('diff-start.txt')
68 parser
= diffparse
.DiffParser('foo bar/a', core
.read(fixture_path
))
71 assert hunks
[0].lines
[0] == '@@ -1 +1,4 @@\n'
72 assert hunks
[-1].lines
[-1] == '+c\n'
73 assert hunks
[0].old_start
== 1
74 assert hunks
[0].old_count
== 1
75 assert hunks
[0].new_start
== 1
76 assert hunks
[0].new_count
== 4
77 assert parser
.generate_patch(1, 3) == (
85 assert parser
.generate_patch(0, 4) == (
96 def test_diff_at_end():
97 fixture_path
= helper
.fixture('diff-end.txt')
98 parser
= diffparse
.DiffParser('rijndael.js', core
.read(fixture_path
))
101 assert hunks
[0].lines
[0] == '@@ -1,39 +1 @@\n'
102 assert hunks
[-1].lines
[-1] == (
103 "+module.exports = require('./build/Release/rijndael');\n"
105 assert hunks
[0].old_start
== 1
106 assert hunks
[0].old_count
== 39
107 assert hunks
[0].new_start
== 1
108 assert hunks
[0].new_count
== 1
111 def test_diff_that_empties_file():
112 fixture_path
= helper
.fixture('diff-empty.txt')
113 parser
= diffparse
.DiffParser('filename', core
.read(fixture_path
))
116 assert hunks
[0].lines
[0] == '@@ -1,2 +0,0 @@\n'
117 assert hunks
[-1].lines
[-1] == '-second\n'
118 assert hunks
[0].old_start
== 1
119 assert hunks
[0].old_count
== 2
120 assert hunks
[0].new_start
== 0
121 assert hunks
[0].new_count
== 0
122 assert parser
.generate_patch(1, 1) == (
129 assert parser
.generate_patch(0, 2) == (
138 def test_diff_file_removal():
140 deleted file mode 100755
144 parser
= diffparse
.DiffParser('deleted.txt', diff_text
)
147 actual
= len(parser
.hunks
)
148 assert expect
== actual
150 # Selecting the first two lines generate no diff
152 actual
= parser
.generate_patch(0, 1)
153 assert expect
== actual
155 # Selecting the last line should generate a line removal
162 actual
= parser
.generate_patch(1, 2)
163 assert expect
== actual
165 # All three lines should map to the same hunk diff
166 actual
= parser
.generate_hunk_patch(0)
167 assert expect
== actual
169 actual
= parser
.generate_hunk_patch(1)
170 assert expect
== actual
172 actual
= parser
.generate_hunk_patch(2)
173 assert expect
== actual
176 def test_basic_diff_line_count(difflines_data
):
177 """Verify the basic line counts"""
178 lines
= difflines_data
.parser
.parse(difflines_data
.text
)
179 expect
= len(difflines_data
.text
.splitlines())
181 assert expect
== actual
184 def test_diff_line_count_ranges(difflines_data
):
185 parser
= difflines_data
.parser
186 lines
= parser
.parse(difflines_data
.text
)
191 assert lines
[line
][0] == parser
.DASH
192 assert lines
[line
][1] == parser
.DASH
199 for i
in range(count
):
200 assert lines
[line
+ i
][0] == current_old
+ i
201 assert lines
[line
+ i
][1] == current_new
+ i
206 # 10 lines of new text
208 for i
in range(count
):
209 assert lines
[line
+ i
][0] == parser
.EMPTY
210 assert lines
[line
+ i
][1] == current_new
+ i
215 # 3 more lines of context
217 for i
in range(count
):
218 assert lines
[line
+ i
][0] == current_old
+ i
219 assert lines
[line
+ i
][1] == current_new
+ i
226 for i
in range(count
):
227 assert lines
[line
+ i
][0] == current_old
+ i
228 assert lines
[line
+ i
][1] == parser
.EMPTY
232 # 2 lines of addition
234 for i
in range(count
):
235 assert lines
[line
+ i
][0] == parser
.EMPTY
236 assert lines
[line
+ i
][1] == current_new
+ i
240 # 3 more lines of context
242 for i
in range(count
):
243 assert lines
[line
+ i
][0] == current_old
+ i
244 assert lines
[line
+ i
][1] == current_new
+ i
251 for i
in range(count
):
252 assert lines
[line
+ i
][0] == parser
.DASH
253 assert lines
[line
+ i
][1] == parser
.DASH
256 # 3 more lines of context
260 for i
in range(count
):
261 assert lines
[line
+ i
][0] == current_old
+ i
262 assert lines
[line
+ i
][1] == current_new
+ i
268 assert expect_max_old
== parser
.old
.max_value
271 assert expect_max_new
== parser
.new
.max_value
273 assert parser
.digits() == 2
276 def test_diff_line_for_merge(difflines_data
):
277 """Verify the basic line counts"""
278 text
= """@@@ -1,23 -1,33 +1,75 @@@
283 parser
= difflines_data
.parser
284 lines
= parser
.parse(text
)
285 assert len(lines
) == 4
286 assert len(lines
[0]) == 3
287 assert len(lines
[1]) == 3
288 assert len(lines
[2]) == 3
289 assert len(lines
[3]) == 3
291 assert lines
[0][0] == parser
.DASH
292 assert lines
[0][1] == parser
.DASH
293 assert lines
[0][2] == parser
.DASH
295 assert lines
[1][0] == parser
.EMPTY
296 assert lines
[1][1] == parser
.EMPTY
297 assert lines
[1][2] == 1
299 assert lines
[2][0] == 1
300 assert lines
[2][1] == parser
.EMPTY
301 assert lines
[2][2] == 2
303 assert lines
[3][0] == 2
304 assert lines
[3][1] == parser
.EMPTY
305 assert lines
[3][2] == 3
308 def test_diff_line_digits(difflines_data
):
309 parser
= difflines_data
.parser
311 text
= """@@ -1,99 +1,99 @@"""
313 assert parser
.digits() == 2
315 text
= """@@ -2,99 +2,99 @@"""
317 assert parser
.digits() == 3
320 def test_format_basic():
321 fmt
= diffparse
.FormatDigits()
325 actual
= fmt
.value(1, 99)
326 assert expect
== actual
329 def test_format_reuse():
330 fmt
= diffparse
.FormatDigits()
334 actual
= fmt
.value(1, 99)
335 assert expect
== actual
339 actual
= fmt
.value(1, 99)
340 assert expect
== actual
343 def test_format_special_values():
344 fmt
= diffparse
.FormatDigits(dash
='-')
348 actual
= fmt
.value(fmt
.EMPTY
, 99)
349 assert expect
== actual
352 actual
= fmt
.value(1, fmt
.EMPTY
)
353 assert expect
== actual
356 actual
= fmt
.value(fmt
.EMPTY
, fmt
.EMPTY
)
357 assert expect
== actual
360 actual
= fmt
.value(fmt
.DASH
, 1)
361 assert expect
== actual
364 actual
= fmt
.value(99, fmt
.DASH
)
365 assert expect
== actual
368 actual
= fmt
.value(fmt
.DASH
, fmt
.DASH
)
369 assert expect
== actual
372 actual
= fmt
.value(fmt
.EMPTY
, fmt
.DASH
)
373 assert expect
== actual
376 actual
= fmt
.value(fmt
.DASH
, fmt
.EMPTY
)
377 assert expect
== actual
380 def test_parse_range_str():
381 start
, count
= diffparse
.parse_range_str('1,2')
386 def test_parse_range_str_single_line():
387 start
, count
= diffparse
.parse_range_str('2')
392 def test_parse_range_str_empty():
393 start
, count
= diffparse
.parse_range_str('0,0')