1 from __future__
import unicode_literals
5 from cola
.diffparse
import _parse_range_str
, DiffParser
7 from test
import helper
10 class ParseDiffTestCase(unittest
.TestCase
):
12 fixture_path
= helper
.fixture('diff.txt')
13 parser
= DiffParser('cola/diffparse.py', core
.read(fixture_path
))
16 self
.assertEqual(len(hunks
), 3)
18 self
.assertEqual(hunks
[0].first_line_idx
, 0)
19 self
.assertEqual(len(hunks
[0].lines
), 23)
20 self
.assertEqual(hunks
[0].lines
[0],
21 '@@ -6,10 +6,21 @@ from cola import gitcmds')
22 self
.assertEqual(hunks
[0].lines
[1],
23 ' from cola import gitcfg')
24 self
.assertEqual(hunks
[0].lines
[2],
26 self
.assertEqual(hunks
[0].lines
[3],
28 self
.assertEqual(hunks
[0].lines
[4],
29 '+class DiffSource(object):')
30 self
.assertEqual(hunks
[0].lines
[-1],
31 " self._header_start_re = re.compile('^@@ -(\d+)"
32 " \+(\d+),(\d+) @@.*')")
34 self
.assertEqual(hunks
[1].first_line_idx
, 23)
35 self
.assertEqual(len(hunks
[1].lines
), 18)
36 self
.assertEqual(hunks
[1].lines
[0],
37 '@@ -29,13 +40,11 @@ class DiffParser(object):')
38 self
.assertEqual(hunks
[1].lines
[1],
39 ' self.diff_sel = []')
40 self
.assertEqual(hunks
[1].lines
[2],
41 ' self.selected = []')
42 self
.assertEqual(hunks
[1].lines
[3],
43 ' self.filename = filename')
44 self
.assertEqual(hunks
[1].lines
[4],
45 '+ self.diff_source = diff_source or DiffSource()')
46 self
.assertEqual(hunks
[1].lines
[-1],
47 ' self.header = header')
49 self
.assertEqual(hunks
[2].first_line_idx
, 41)
50 self
.assertEqual(len(hunks
[2].lines
), 16)
51 self
.assertEqual(hunks
[2].lines
[0],
52 '@@ -43,11 +52,10 @@ class DiffParser(object):')
53 self
.assertEqual(hunks
[2].lines
[-1],
54 ' """Writes a new diff corresponding to the user\'s'
57 def test_diff_at_start(self
):
58 fixture_path
= helper
.fixture('diff-start.txt')
59 parser
= DiffParser('foo bar/a', core
.read(fixture_path
))
62 self
.assertEqual(hunks
[0].lines
[0], '@@ -1 +1,4 @@')
63 self
.assertEqual(hunks
[-1].lines
[-1], '+c')
64 self
.assertEqual(hunks
[0].old_start
, 1)
65 self
.assertEqual(hunks
[0].old_count
, 1)
66 self
.assertEqual(hunks
[0].new_start
, 1)
67 self
.assertEqual(hunks
[0].new_count
, 4)
68 self
.assertEqual(parser
.generate_patch(1, 3),
75 self
.assertEqual(parser
.generate_patch(0, 4),
84 def test_diff_at_end(self
):
85 fixture_path
= helper
.fixture('diff-end.txt')
86 parser
= DiffParser('rijndael.js', core
.read(fixture_path
))
89 self
.assertEqual(hunks
[0].lines
[0],
91 self
.assertEqual(hunks
[-1].lines
[-1],
92 "+module.exports = require('./build/Release/rijndael');")
93 self
.assertEqual(hunks
[0].old_start
, 1)
94 self
.assertEqual(hunks
[0].old_count
, 39)
95 self
.assertEqual(hunks
[0].new_start
, 1)
96 self
.assertEqual(hunks
[0].new_count
, 1)
98 def test_diff_that_empties_file(self
):
99 fixture_path
= helper
.fixture('diff-empty.txt')
100 parser
= DiffParser('filename', core
.read(fixture_path
))
103 self
.assertEqual(hunks
[0].lines
[0],
105 self
.assertEqual(hunks
[-1].lines
[-1],
107 self
.assertEqual(hunks
[0].old_start
, 1)
108 self
.assertEqual(hunks
[0].old_count
, 2)
109 self
.assertEqual(hunks
[0].new_start
, 0)
110 self
.assertEqual(hunks
[0].new_count
, 0)
111 self
.assertEqual(parser
.generate_patch(1, 1),
117 self
.assertEqual(parser
.generate_patch(0, 2),
125 class ParseRangeStrTestCase(unittest
.TestCase
):
126 def test_parse_range_str(self
):
127 start
, count
= _parse_range_str('1,2')
128 self
.assertEqual(start
, 1)
129 self
.assertEqual(count
, 2)
131 def test_parse_range_str_single_line(self
):
132 start
, count
= _parse_range_str('2')
133 self
.assertEqual(start
, 2)
134 self
.assertEqual(count
, 1)
136 def test_parse_range_str_empty(self
):
137 start
, count
= _parse_range_str('0,0')
138 self
.assertEqual(start
, 0)
139 self
.assertEqual(count
, 0)
142 if __name__
== '__main__':