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(
15 'cola/diffparse.py', core
.read(fixture_path
))
18 self
.assertEqual(len(hunks
), 3)
19 self
.assertEqual(hunks
[0].first_line_idx
, 0)
20 self
.assertEqual(len(hunks
[0].lines
), 23)
23 '@@ -6,10 +6,21 @@ from cola import gitcmds\n')
26 ' from cola import gitcfg\n')
27 self
.assertEqual(hunks
[0].lines
[2], ' \n')
28 self
.assertEqual(hunks
[0].lines
[3], ' \n')
29 self
.assertEqual(hunks
[0].lines
[4], '+class DiffSource(object):\n')
32 r
" self._header_start_re = re.compile('^@@ -(\d+)"
33 r
" \+(\d+),(\d+) @@.*')"
36 self
.assertEqual(hunks
[1].first_line_idx
, 23)
37 self
.assertEqual(len(hunks
[1].lines
), 18)
40 '@@ -29,13 +40,11 @@ class DiffParser(object):\n')
43 ' self.diff_sel = []\n')
46 ' self.selected = []\n')
49 ' self.filename = filename\n')
52 '+ self.diff_source = diff_source or DiffSource()\n')
55 ' self.header = header\n')
57 self
.assertEqual(hunks
[2].first_line_idx
, 41)
58 self
.assertEqual(len(hunks
[2].lines
), 16)
61 '@@ -43,11 +52,10 @@ class DiffParser(object):\n')
64 ' """Writes a new diff corresponding to the user\'s'
67 def test_diff_at_start(self
):
68 fixture_path
= helper
.fixture('diff-start.txt')
69 parser
= diffparse
.DiffParser('foo bar/a', core
.read(fixture_path
))
72 self
.assertEqual(hunks
[0].lines
[0], '@@ -1 +1,4 @@\n')
73 self
.assertEqual(hunks
[-1].lines
[-1], '+c\n')
74 self
.assertEqual(hunks
[0].old_start
, 1)
75 self
.assertEqual(hunks
[0].old_count
, 1)
76 self
.assertEqual(hunks
[0].new_start
, 1)
77 self
.assertEqual(hunks
[0].new_count
, 4)
78 self
.assertEqual(parser
.generate_patch(1, 3),
85 self
.assertEqual(parser
.generate_patch(0, 4),
94 def test_diff_at_end(self
):
95 fixture_path
= helper
.fixture('diff-end.txt')
96 parser
= diffparse
.DiffParser('rijndael.js', core
.read(fixture_path
))
99 self
.assertEqual(hunks
[0].lines
[0], '@@ -1,39 +1 @@\n')
102 "+module.exports = require('./build/Release/rijndael');\n")
103 self
.assertEqual(hunks
[0].old_start
, 1)
104 self
.assertEqual(hunks
[0].old_count
, 39)
105 self
.assertEqual(hunks
[0].new_start
, 1)
106 self
.assertEqual(hunks
[0].new_count
, 1)
108 def test_diff_that_empties_file(self
):
109 fixture_path
= helper
.fixture('diff-empty.txt')
110 parser
= diffparse
.DiffParser('filename', core
.read(fixture_path
))
113 self
.assertEqual(hunks
[0].lines
[0], '@@ -1,2 +0,0 @@\n')
114 self
.assertEqual(hunks
[-1].lines
[-1], '-second\n')
115 self
.assertEqual(hunks
[0].old_start
, 1)
116 self
.assertEqual(hunks
[0].old_count
, 2)
117 self
.assertEqual(hunks
[0].new_start
, 0)
118 self
.assertEqual(hunks
[0].new_count
, 0)
119 self
.assertEqual(parser
.generate_patch(1, 1),
125 self
.assertEqual(parser
.generate_patch(0, 2),
132 def test_diff_file_removal(self
):
134 deleted file mode 100755
138 parser
= diffparse
.DiffParser('deleted.txt', diff_text
)
139 self
.assertEqual(1, len(parser
.hunks
))
141 # Selecting the first two lines generate no diff
143 actual
= parser
.generate_patch(0, 1)
144 self
.assertEqual(expect
, actual
)
146 # Selecting the last line should generate a line removal
153 actual
= parser
.generate_patch(1, 2)
154 self
.assertEqual(expect
, actual
)
156 # All three lines should map to the same hunk diff
157 actual
= parser
.generate_hunk_patch(0)
158 self
.assertEqual(expect
, actual
)
160 actual
= parser
.generate_hunk_patch(1)
161 self
.assertEqual(expect
, actual
)
163 actual
= parser
.generate_hunk_patch(2)
164 self
.assertEqual(expect
, actual
)
167 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
):
306 def test_format_basic(self
):
307 fmt
= diffparse
.FormatDigits()
311 actual
= fmt
.value(1, 99)
312 self
.assertEqual(expect
, actual
)
314 def test_format_reuse(self
):
315 fmt
= diffparse
.FormatDigits()
319 actual
= fmt
.value(1, 99)
320 self
.assertEqual(expect
, actual
)
324 actual
= fmt
.value(1, 99)
325 self
.assertEqual(expect
, actual
)
327 def test_format_special_values(self
):
328 fmt
= diffparse
.FormatDigits(dash
='-')
332 actual
= fmt
.value(fmt
.EMPTY
, 99)
333 self
.assertEqual(expect
, actual
)
336 actual
= fmt
.value(1, fmt
.EMPTY
)
337 self
.assertEqual(expect
, actual
)
340 actual
= fmt
.value(fmt
.EMPTY
, fmt
.EMPTY
)
341 self
.assertEqual(expect
, actual
)
344 actual
= fmt
.value(fmt
.DASH
, 1)
345 self
.assertEqual(expect
, actual
)
348 actual
= fmt
.value(99, fmt
.DASH
)
349 self
.assertEqual(expect
, actual
)
352 actual
= fmt
.value(fmt
.DASH
, fmt
.DASH
)
353 self
.assertEqual(expect
, actual
)
356 actual
= fmt
.value(fmt
.EMPTY
, fmt
.DASH
)
357 self
.assertEqual(expect
, actual
)
360 actual
= fmt
.value(fmt
.DASH
, fmt
.EMPTY
)
361 self
.assertEqual(expect
, actual
)
364 class ParseRangeStrTestCase(unittest
.TestCase
):
366 def test_parse_range_str(self
):
367 start
, count
= diffparse
.parse_range_str('1,2')
368 self
.assertEqual(start
, 1)
369 self
.assertEqual(count
, 2)
371 def test_parse_range_str_single_line(self
):
372 start
, count
= diffparse
.parse_range_str('2')
373 self
.assertEqual(start
, 2)
374 self
.assertEqual(count
, 1)
376 def test_parse_range_str_empty(self
):
377 start
, count
= diffparse
.parse_range_str('0,0')
378 self
.assertEqual(start
, 0)
379 self
.assertEqual(count
, 0)
382 if __name__
== '__main__':