1 from __future__
import absolute_import
, division
, unicode_literals
5 from cola
import diffparse
10 class ParseDiffTestCase(unittest
.TestCase
):
13 fixture_path
= helper
.fixture('diff.txt')
14 parser
= diffparse
.DiffParser('cola/diffparse.py', core
.read(fixture_path
))
17 self
.assertEqual(len(hunks
), 3)
18 self
.assertEqual(hunks
[0].first_line_idx
, 0)
19 self
.assertEqual(len(hunks
[0].lines
), 23)
21 hunks
[0].lines
[0], '@@ -6,10 +6,21 @@ from cola import gitcmds\n'
23 self
.assertEqual(hunks
[0].lines
[1], ' from cola import gitcfg\n')
24 self
.assertEqual(hunks
[0].lines
[2], ' \n')
25 self
.assertEqual(hunks
[0].lines
[3], ' \n')
26 self
.assertEqual(hunks
[0].lines
[4], '+class DiffSource(object):\n')
29 r
" self._header_start_re = re.compile('^@@ -(\d+)"
30 r
" \+(\d+),(\d+) @@.*')"
34 self
.assertEqual(hunks
[1].first_line_idx
, 23)
35 self
.assertEqual(len(hunks
[1].lines
), 18)
37 hunks
[1].lines
[0], '@@ -29,13 +40,11 @@ class DiffParser(object):\n'
39 self
.assertEqual(hunks
[1].lines
[1], ' self.diff_sel = []\n')
40 self
.assertEqual(hunks
[1].lines
[2], ' self.selected = []\n')
41 self
.assertEqual(hunks
[1].lines
[3], ' self.filename = filename\n')
44 '+ self.diff_source = diff_source or DiffSource()\n',
46 self
.assertEqual(hunks
[1].lines
[-1], ' self.header = header\n')
48 self
.assertEqual(hunks
[2].first_line_idx
, 41)
49 self
.assertEqual(len(hunks
[2].lines
), 16)
51 hunks
[2].lines
[0], '@@ -43,11 +52,10 @@ class DiffParser(object):\n'
55 ' """Writes a new diff corresponding to the user\'s'
59 def test_diff_at_start(self
):
60 fixture_path
= helper
.fixture('diff-start.txt')
61 parser
= diffparse
.DiffParser('foo bar/a', core
.read(fixture_path
))
64 self
.assertEqual(hunks
[0].lines
[0], '@@ -1 +1,4 @@\n')
65 self
.assertEqual(hunks
[-1].lines
[-1], '+c\n')
66 self
.assertEqual(hunks
[0].old_start
, 1)
67 self
.assertEqual(hunks
[0].old_count
, 1)
68 self
.assertEqual(hunks
[0].new_start
, 1)
69 self
.assertEqual(hunks
[0].new_count
, 4)
71 parser
.generate_patch(1, 3),
80 parser
.generate_patch(0, 4),
90 def test_diff_at_end(self
):
91 fixture_path
= helper
.fixture('diff-end.txt')
92 parser
= diffparse
.DiffParser('rijndael.js', core
.read(fixture_path
))
95 self
.assertEqual(hunks
[0].lines
[0], '@@ -1,39 +1 @@\n')
98 "+module.exports = require('./build/Release/rijndael');\n",
100 self
.assertEqual(hunks
[0].old_start
, 1)
101 self
.assertEqual(hunks
[0].old_count
, 39)
102 self
.assertEqual(hunks
[0].new_start
, 1)
103 self
.assertEqual(hunks
[0].new_count
, 1)
105 def test_diff_that_empties_file(self
):
106 fixture_path
= helper
.fixture('diff-empty.txt')
107 parser
= diffparse
.DiffParser('filename', core
.read(fixture_path
))
110 self
.assertEqual(hunks
[0].lines
[0], '@@ -1,2 +0,0 @@\n')
111 self
.assertEqual(hunks
[-1].lines
[-1], '-second\n')
112 self
.assertEqual(hunks
[0].old_start
, 1)
113 self
.assertEqual(hunks
[0].old_count
, 2)
114 self
.assertEqual(hunks
[0].new_start
, 0)
115 self
.assertEqual(hunks
[0].new_count
, 0)
117 parser
.generate_patch(1, 1),
125 parser
.generate_patch(0, 2),
133 def test_diff_file_removal(self
):
135 deleted file mode 100755
139 parser
= diffparse
.DiffParser('deleted.txt', diff_text
)
140 self
.assertEqual(1, len(parser
.hunks
))
142 # Selecting the first two lines generate no diff
144 actual
= parser
.generate_patch(0, 1)
145 self
.assertEqual(expect
, actual
)
147 # Selecting the last line should generate a line removal
154 actual
= parser
.generate_patch(1, 2)
155 self
.assertEqual(expect
, actual
)
157 # All three lines should map to the same hunk diff
158 actual
= parser
.generate_hunk_patch(0)
159 self
.assertEqual(expect
, actual
)
161 actual
= parser
.generate_hunk_patch(1)
162 self
.assertEqual(expect
, actual
)
164 actual
= parser
.generate_hunk_patch(2)
165 self
.assertEqual(expect
, actual
)
168 class DiffLinesTestCase(unittest
.TestCase
):
170 self
.parser
= diffparse
.DiffLines()
171 fixture_path
= helper
.fixture('diff.txt')
172 self
.text
= core
.read(fixture_path
)
174 def test_basic_diff_line_count(self
):
175 """Verify the basic line counts"""
176 lines
= self
.parser
.parse(self
.text
)
177 expect
= len(self
.text
.splitlines())
179 self
.assertEqual(expect
, actual
)
181 def test_diff_line_count_ranges(self
):
183 lines
= parser
.parse(self
.text
)
188 self
.assertEqual(lines
[line
][0], parser
.DASH
)
189 self
.assertEqual(lines
[line
][1], parser
.DASH
)
196 for i
in range(count
):
197 self
.assertEqual(lines
[line
+ i
][0], current_old
+ i
)
198 self
.assertEqual(lines
[line
+ i
][1], current_new
+ i
)
203 # 10 lines of new text
205 for i
in range(count
):
206 self
.assertEqual(lines
[line
+ i
][0], parser
.EMPTY
)
207 self
.assertEqual(lines
[line
+ i
][1], current_new
+ i
)
212 # 3 more lines of context
214 for i
in range(count
):
215 self
.assertEqual(lines
[line
+ i
][0], current_old
+ i
)
216 self
.assertEqual(lines
[line
+ i
][1], current_new
+ i
)
223 for i
in range(count
):
224 self
.assertEqual(lines
[line
+ i
][0], current_old
+ i
)
225 self
.assertEqual(lines
[line
+ i
][1], parser
.EMPTY
)
229 # 2 lines of addition
231 for i
in range(count
):
232 self
.assertEqual(lines
[line
+ i
][0], parser
.EMPTY
)
233 self
.assertEqual(lines
[line
+ i
][1], current_new
+ i
)
237 # 3 more lines of context
239 for i
in range(count
):
240 self
.assertEqual(lines
[line
+ i
][0], current_old
+ i
)
241 self
.assertEqual(lines
[line
+ i
][1], current_new
+ i
)
248 for i
in range(count
):
249 self
.assertEqual(lines
[line
+ i
][0], parser
.DASH
)
250 self
.assertEqual(lines
[line
+ i
][1], parser
.DASH
)
253 # 3 more lines of context
257 for i
in range(count
):
258 self
.assertEqual(lines
[line
+ i
][0], current_old
+ i
)
259 self
.assertEqual(lines
[line
+ i
][1], current_new
+ i
)
265 self
.assertEqual(expect_max_old
, parser
.old
.max_value
)
268 self
.assertEqual(expect_max_new
, parser
.new
.max_value
)
270 self
.assertEqual(parser
.digits(), 2)
272 def test_diff_line_for_merge(self
):
273 """Verify the basic line counts"""
274 text
= """@@@ -1,23 -1,33 +1,75 @@@
280 lines
= parser
.parse(text
)
281 self
.assertEqual(len(lines
), 4)
282 self
.assertEqual(len(lines
[0]), 3)
283 self
.assertEqual(len(lines
[1]), 3)
284 self
.assertEqual(len(lines
[2]), 3)
285 self
.assertEqual(len(lines
[3]), 3)
287 self
.assertEqual(lines
[0][0], parser
.DASH
)
288 self
.assertEqual(lines
[0][1], parser
.DASH
)
289 self
.assertEqual(lines
[0][2], parser
.DASH
)
291 self
.assertEqual(lines
[1][0], parser
.EMPTY
)
292 self
.assertEqual(lines
[1][1], parser
.EMPTY
)
293 self
.assertEqual(lines
[1][2], 1)
295 self
.assertEqual(lines
[2][0], 1)
296 self
.assertEqual(lines
[2][1], parser
.EMPTY
)
297 self
.assertEqual(lines
[2][2], 2)
299 self
.assertEqual(lines
[3][0], 2)
300 self
.assertEqual(lines
[3][1], parser
.EMPTY
)
301 self
.assertEqual(lines
[3][2], 3)
304 class FormatDiffLinesTestCase(unittest
.TestCase
):
305 def test_format_basic(self
):
306 fmt
= diffparse
.FormatDigits()
310 actual
= fmt
.value(1, 99)
311 self
.assertEqual(expect
, actual
)
313 def test_format_reuse(self
):
314 fmt
= diffparse
.FormatDigits()
318 actual
= fmt
.value(1, 99)
319 self
.assertEqual(expect
, actual
)
323 actual
= fmt
.value(1, 99)
324 self
.assertEqual(expect
, actual
)
326 def test_format_special_values(self
):
327 fmt
= diffparse
.FormatDigits(dash
='-')
331 actual
= fmt
.value(fmt
.EMPTY
, 99)
332 self
.assertEqual(expect
, actual
)
335 actual
= fmt
.value(1, fmt
.EMPTY
)
336 self
.assertEqual(expect
, actual
)
339 actual
= fmt
.value(fmt
.EMPTY
, fmt
.EMPTY
)
340 self
.assertEqual(expect
, actual
)
343 actual
= fmt
.value(fmt
.DASH
, 1)
344 self
.assertEqual(expect
, actual
)
347 actual
= fmt
.value(99, fmt
.DASH
)
348 self
.assertEqual(expect
, actual
)
351 actual
= fmt
.value(fmt
.DASH
, fmt
.DASH
)
352 self
.assertEqual(expect
, actual
)
355 actual
= fmt
.value(fmt
.EMPTY
, fmt
.DASH
)
356 self
.assertEqual(expect
, actual
)
359 actual
= fmt
.value(fmt
.DASH
, fmt
.EMPTY
)
360 self
.assertEqual(expect
, actual
)
363 class ParseRangeStrTestCase(unittest
.TestCase
):
364 def test_parse_range_str(self
):
365 start
, count
= diffparse
.parse_range_str('1,2')
366 self
.assertEqual(start
, 1)
367 self
.assertEqual(count
, 2)
369 def test_parse_range_str_single_line(self
):
370 start
, count
= diffparse
.parse_range_str('2')
371 self
.assertEqual(start
, 2)
372 self
.assertEqual(count
, 1)
374 def test_parse_range_str_empty(self
):
375 start
, count
= diffparse
.parse_range_str('0,0')
376 self
.assertEqual(start
, 0)
377 self
.assertEqual(count
, 0)