1 from __future__
import absolute_import
, division
, unicode_literals
6 from cola
.diffparse
import _parse_range_str
, DiffParser
8 from test
import helper
11 class ParseDiffTestCase(unittest
.TestCase
):
14 fixture_path
= helper
.fixture('diff.txt')
15 parser
= DiffParser('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')
26 ' from cola import gitcfg')
27 self
.assertEqual(hunks
[0].lines
[2], ' ')
28 self
.assertEqual(hunks
[0].lines
[3], ' ')
29 self
.assertEqual(hunks
[0].lines
[4], '+class DiffSource(object):')
32 " self._header_start_re = re.compile('^@@ -(\d+)"
33 " \+(\d+),(\d+) @@.*')")
35 self
.assertEqual(hunks
[1].first_line_idx
, 23)
36 self
.assertEqual(len(hunks
[1].lines
), 18)
39 '@@ -29,13 +40,11 @@ class DiffParser(object):')
42 ' self.diff_sel = []')
45 ' self.selected = []')
48 ' self.filename = filename')
51 '+ self.diff_source = diff_source or DiffSource()')
54 ' self.header = header')
56 self
.assertEqual(hunks
[2].first_line_idx
, 41)
57 self
.assertEqual(len(hunks
[2].lines
), 16)
60 '@@ -43,11 +52,10 @@ class DiffParser(object):')
63 ' """Writes a new diff corresponding to the user\'s'
66 def test_diff_at_start(self
):
67 fixture_path
= helper
.fixture('diff-start.txt')
68 parser
= DiffParser('foo bar/a', core
.read(fixture_path
))
71 self
.assertEqual(hunks
[0].lines
[0], '@@ -1 +1,4 @@')
72 self
.assertEqual(hunks
[-1].lines
[-1], '+c')
73 self
.assertEqual(hunks
[0].old_start
, 1)
74 self
.assertEqual(hunks
[0].old_count
, 1)
75 self
.assertEqual(hunks
[0].new_start
, 1)
76 self
.assertEqual(hunks
[0].new_count
, 4)
77 self
.assertEqual(parser
.generate_patch(1, 3),
84 self
.assertEqual(parser
.generate_patch(0, 4),
93 def test_diff_at_end(self
):
94 fixture_path
= helper
.fixture('diff-end.txt')
95 parser
= DiffParser('rijndael.js', core
.read(fixture_path
))
98 self
.assertEqual(hunks
[0].lines
[0], '@@ -1,39 +1 @@')
101 "+module.exports = require('./build/Release/rijndael');")
102 self
.assertEqual(hunks
[0].old_start
, 1)
103 self
.assertEqual(hunks
[0].old_count
, 39)
104 self
.assertEqual(hunks
[0].new_start
, 1)
105 self
.assertEqual(hunks
[0].new_count
, 1)
107 def test_diff_that_empties_file(self
):
108 fixture_path
= helper
.fixture('diff-empty.txt')
109 parser
= DiffParser('filename', core
.read(fixture_path
))
112 self
.assertEqual(hunks
[0].lines
[0], '@@ -1,2 +0,0 @@')
113 self
.assertEqual(hunks
[-1].lines
[-1], '-second')
114 self
.assertEqual(hunks
[0].old_start
, 1)
115 self
.assertEqual(hunks
[0].old_count
, 2)
116 self
.assertEqual(hunks
[0].new_start
, 0)
117 self
.assertEqual(hunks
[0].new_count
, 0)
118 self
.assertEqual(parser
.generate_patch(1, 1),
124 self
.assertEqual(parser
.generate_patch(0, 2),
132 class ParseRangeStrTestCase(unittest
.TestCase
):
133 def test_parse_range_str(self
):
134 start
, count
= _parse_range_str('1,2')
135 self
.assertEqual(start
, 1)
136 self
.assertEqual(count
, 2)
138 def test_parse_range_str_single_line(self
):
139 start
, count
= _parse_range_str('2')
140 self
.assertEqual(start
, 2)
141 self
.assertEqual(count
, 1)
143 def test_parse_range_str_empty(self
):
144 start
, count
= _parse_range_str('0,0')
145 self
.assertEqual(start
, 0)
146 self
.assertEqual(count
, 0)
149 if __name__
== '__main__':