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),
133 class DiffLinesTestCase(unittest
.TestCase
):
136 self
.parser
= diffparse
.DiffLines()
137 fixture_path
= helper
.fixture('diff.txt')
138 self
.text
= core
.read(fixture_path
)
140 def test_basic_diff_line_count(self
):
141 """Verify the basic line counts"""
142 lines
= self
.parser
.parse(self
.text
)
143 expect
= len(self
.text
.splitlines())
145 self
.assertEqual(expect
, actual
)
147 def test_diff_line_count_ranges(self
):
149 lines
= parser
.parse(self
.text
)
154 self
.assertEqual(lines
[line
][0], parser
.DASH
)
155 self
.assertEqual(lines
[line
][1], parser
.DASH
)
162 for i
in range(count
):
163 self
.assertEqual(lines
[line
+i
][0], current_old
+i
)
164 self
.assertEqual(lines
[line
+i
][1], current_new
+i
)
169 # 10 lines of new text
171 for i
in range(count
):
172 self
.assertEqual(lines
[line
+i
][0], parser
.EMPTY
)
173 self
.assertEqual(lines
[line
+i
][1], current_new
+i
)
178 # 3 more lines of context
180 for i
in range(count
):
181 self
.assertEqual(lines
[line
+i
][0], current_old
+i
)
182 self
.assertEqual(lines
[line
+i
][1], current_new
+i
)
189 for i
in range(count
):
190 self
.assertEqual(lines
[line
+i
][0], current_old
+i
)
191 self
.assertEqual(lines
[line
+i
][1], parser
.EMPTY
)
195 # 2 lines of addition
197 for i
in range(count
):
198 self
.assertEqual(lines
[line
+i
][0], parser
.EMPTY
)
199 self
.assertEqual(lines
[line
+i
][1], current_new
+i
)
203 # 3 more lines of context
205 for i
in range(count
):
206 self
.assertEqual(lines
[line
+i
][0], current_old
+i
)
207 self
.assertEqual(lines
[line
+i
][1], current_new
+i
)
214 for i
in range(count
):
215 self
.assertEqual(lines
[line
+i
][0], parser
.DASH
)
216 self
.assertEqual(lines
[line
+i
][1], parser
.DASH
)
219 # 3 more lines of context
223 for i
in range(count
):
224 self
.assertEqual(lines
[line
+i
][0], current_old
+i
)
225 self
.assertEqual(lines
[line
+i
][1], current_new
+i
)
231 self
.assertEqual(expect_max_old
, parser
.old
.max_value
)
234 self
.assertEqual(expect_max_new
, parser
.new
.max_value
)
236 self
.assertEqual(parser
.digits(), 2)
238 def test_diff_line_for_merge(self
):
239 """Verify the basic line counts"""
240 text
= """@@@ -1,23 -1,33 +1,75 @@@
246 lines
= parser
.parse(text
)
247 self
.assertEqual(len(lines
), 4)
248 self
.assertEqual(len(lines
[0]), 3)
249 self
.assertEqual(len(lines
[1]), 3)
250 self
.assertEqual(len(lines
[2]), 3)
251 self
.assertEqual(len(lines
[3]), 3)
253 self
.assertEqual(lines
[0][0], parser
.DASH
)
254 self
.assertEqual(lines
[0][1], parser
.DASH
)
255 self
.assertEqual(lines
[0][2], parser
.DASH
)
257 self
.assertEqual(lines
[1][0], parser
.EMPTY
)
258 self
.assertEqual(lines
[1][1], parser
.EMPTY
)
259 self
.assertEqual(lines
[1][2], 1)
261 self
.assertEqual(lines
[2][0], 1)
262 self
.assertEqual(lines
[2][1], parser
.EMPTY
)
263 self
.assertEqual(lines
[2][2], 2)
265 self
.assertEqual(lines
[3][0], 2)
266 self
.assertEqual(lines
[3][1], parser
.EMPTY
)
267 self
.assertEqual(lines
[3][2], 3)
270 class FormatDiffLinesTestCase(unittest
.TestCase
):
272 def test_format_basic(self
):
273 fmt
= diffparse
.FormatDigits()
277 actual
= fmt
.value(1, 99)
278 self
.assertEqual(expect
, actual
)
280 def test_format_reuse(self
):
281 fmt
= diffparse
.FormatDigits()
285 actual
= fmt
.value(1, 99)
286 self
.assertEqual(expect
, actual
)
290 actual
= fmt
.value(1, 99)
291 self
.assertEqual(expect
, actual
)
293 def test_format_special_values(self
):
294 fmt
= diffparse
.FormatDigits(dash
='-')
298 actual
= fmt
.value(fmt
.EMPTY
, 99)
299 self
.assertEqual(expect
, actual
)
302 actual
= fmt
.value(1, fmt
.EMPTY
)
303 self
.assertEqual(expect
, actual
)
306 actual
= fmt
.value(fmt
.EMPTY
, fmt
.EMPTY
)
307 self
.assertEqual(expect
, actual
)
310 actual
= fmt
.value(fmt
.DASH
, 1)
311 self
.assertEqual(expect
, actual
)
314 actual
= fmt
.value(99, fmt
.DASH
)
315 self
.assertEqual(expect
, actual
)
318 actual
= fmt
.value(fmt
.DASH
, fmt
.DASH
)
319 self
.assertEqual(expect
, actual
)
322 actual
= fmt
.value(fmt
.EMPTY
, fmt
.DASH
)
323 self
.assertEqual(expect
, actual
)
326 actual
= fmt
.value(fmt
.DASH
, fmt
.EMPTY
)
327 self
.assertEqual(expect
, actual
)
330 class ParseRangeStrTestCase(unittest
.TestCase
):
332 def test_parse_range_str(self
):
333 start
, count
= diffparse
.parse_range_str('1,2')
334 self
.assertEqual(start
, 1)
335 self
.assertEqual(count
, 2)
337 def test_parse_range_str_single_line(self
):
338 start
, count
= diffparse
.parse_range_str('2')
339 self
.assertEqual(start
, 2)
340 self
.assertEqual(count
, 1)
342 def test_parse_range_str_empty(self
):
343 start
, count
= diffparse
.parse_range_str('0,0')
344 self
.assertEqual(start
, 0)
345 self
.assertEqual(count
, 0)
348 if __name__
== '__main__':