extras: pylint updates
[git-cola.git] / test / diffparse_test.py
blob50bcfb5c6adf637d0bfb07d5df38b2a343b017ec
1 from __future__ import absolute_import, division, unicode_literals
2 import unittest
4 from cola import core
5 from cola import diffparse
7 from . import helper
10 class ParseDiffTestCase(unittest.TestCase):
12 def test_diff(self):
13 fixture_path = helper.fixture('diff.txt')
14 parser = diffparse.DiffParser(
15 'cola/diffparse.py', core.read(fixture_path))
16 hunks = parser.hunks
18 self.assertEqual(len(hunks), 3)
19 self.assertEqual(hunks[0].first_line_idx, 0)
20 self.assertEqual(len(hunks[0].lines), 23)
21 self.assertEqual(
22 hunks[0].lines[0],
23 '@@ -6,10 +6,21 @@ from cola import gitcmds\n')
24 self.assertEqual(
25 hunks[0].lines[1],
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')
30 self.assertEqual(
31 hunks[0].lines[-1],
32 r" self._header_start_re = re.compile('^@@ -(\d+)"
33 r" \+(\d+),(\d+) @@.*')"
34 '\n')
36 self.assertEqual(hunks[1].first_line_idx, 23)
37 self.assertEqual(len(hunks[1].lines), 18)
38 self.assertEqual(
39 hunks[1].lines[0],
40 '@@ -29,13 +40,11 @@ class DiffParser(object):\n')
41 self.assertEqual(
42 hunks[1].lines[1],
43 ' self.diff_sel = []\n')
44 self.assertEqual(
45 hunks[1].lines[2],
46 ' self.selected = []\n')
47 self.assertEqual(
48 hunks[1].lines[3],
49 ' self.filename = filename\n')
50 self.assertEqual(
51 hunks[1].lines[4],
52 '+ self.diff_source = diff_source or DiffSource()\n')
53 self.assertEqual(
54 hunks[1].lines[-1],
55 ' self.header = header\n')
57 self.assertEqual(hunks[2].first_line_idx, 41)
58 self.assertEqual(len(hunks[2].lines), 16)
59 self.assertEqual(
60 hunks[2].lines[0],
61 '@@ -43,11 +52,10 @@ class DiffParser(object):\n')
62 self.assertEqual(
63 hunks[2].lines[-1],
64 ' """Writes a new diff corresponding to the user\'s'
65 ' selection."""\n')
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))
70 hunks = parser.hunks
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),
79 '--- a/foo bar/a\n'
80 '+++ b/foo bar/a\n'
81 '@@ -1 +1,3 @@\n'
82 ' bar\n'
83 '+a\n'
84 '+b\n')
85 self.assertEqual(parser.generate_patch(0, 4),
86 '--- a/foo bar/a\n'
87 '+++ b/foo bar/a\n'
88 '@@ -1 +1,4 @@\n'
89 ' bar\n'
90 '+a\n'
91 '+b\n'
92 '+c\n')
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))
97 hunks = parser.hunks
99 self.assertEqual(hunks[0].lines[0], '@@ -1,39 +1 @@\n')
100 self.assertEqual(
101 hunks[-1].lines[-1],
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))
111 hunks = parser.hunks
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),
120 '--- a/filename\n'
121 '+++ b/filename\n'
122 '@@ -1,2 +1 @@\n'
123 '-first\n'
124 ' second\n')
125 self.assertEqual(parser.generate_patch(0, 2),
126 '--- a/filename\n'
127 '+++ b/filename\n'
128 '@@ -1,2 +0,0 @@\n'
129 '-first\n'
130 '-second\n')
133 class DiffLinesTestCase(unittest.TestCase):
135 def setUp(self):
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())
144 actual = len(lines)
145 self.assertEqual(expect, actual)
147 def test_diff_line_count_ranges(self):
148 parser = self.parser
149 lines = parser.parse(self.text)
151 # Diff header
152 line = 0
153 count = 1
154 self.assertEqual(lines[line][0], parser.DASH)
155 self.assertEqual(lines[line][1], parser.DASH)
156 line += count
158 # 3 lines of context
159 count = 3
160 current_old = 6
161 current_new = 6
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)
165 line += count
166 current_old += count
167 current_new += count
169 # 10 lines of new text
170 count = 10
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)
175 line += count
176 current_new += count
178 # 3 more lines of context
179 count = 3
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)
183 line += count
184 current_new += count
185 current_old += count
187 # 1 line of removal
188 count = 1
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)
192 line += count
193 current_old += count
195 # 2 lines of addition
196 count = 2
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)
200 line += count
201 current_new += count
203 # 3 more lines of context
204 count = 3
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)
208 line += count
209 current_new += count
210 current_old += count
212 # 1 line of header
213 count = 1
214 for i in range(count):
215 self.assertEqual(lines[line+i][0], parser.DASH)
216 self.assertEqual(lines[line+i][1], parser.DASH)
217 line += count
219 # 3 more lines of context
220 current_old = 29
221 current_new = 40
222 count = 3
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)
226 line += count
227 current_new += count
228 current_old += count
230 expect_max_old = 54
231 self.assertEqual(expect_max_old, parser.old.max_value)
233 expect_max_new = 62
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 @@@
241 ++<<<<<<< upstream
245 parser = self.parser
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()
274 fmt.set_digits(2)
276 expect = '01 99'
277 actual = fmt.value(1, 99)
278 self.assertEqual(expect, actual)
280 def test_format_reuse(self):
281 fmt = diffparse.FormatDigits()
283 fmt.set_digits(3)
284 expect = '001 099'
285 actual = fmt.value(1, 99)
286 self.assertEqual(expect, actual)
288 fmt.set_digits(4)
289 expect = '0001 0099'
290 actual = fmt.value(1, 99)
291 self.assertEqual(expect, actual)
293 def test_format_special_values(self):
294 fmt = diffparse.FormatDigits(dash='-')
295 fmt.set_digits(3)
297 expect = ' 099'
298 actual = fmt.value(fmt.EMPTY, 99)
299 self.assertEqual(expect, actual)
301 expect = '001 '
302 actual = fmt.value(1, fmt.EMPTY)
303 self.assertEqual(expect, actual)
305 expect = ' '
306 actual = fmt.value(fmt.EMPTY, fmt.EMPTY)
307 self.assertEqual(expect, actual)
309 expect = '--- 001'
310 actual = fmt.value(fmt.DASH, 1)
311 self.assertEqual(expect, actual)
313 expect = '099 ---'
314 actual = fmt.value(99, fmt.DASH)
315 self.assertEqual(expect, actual)
317 expect = '--- ---'
318 actual = fmt.value(fmt.DASH, fmt.DASH)
319 self.assertEqual(expect, actual)
321 expect = ' ---'
322 actual = fmt.value(fmt.EMPTY, fmt.DASH)
323 self.assertEqual(expect, actual)
325 expect = '--- '
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__':
349 unittest.main()