5 from cola
import gitcmds
6 from cola
import diffparse
7 from cola
.diffparse
import DiffParser
12 class DiffParseModel(object):
14 self
.last_worktree_diff
= None
22 def apply_diff_to_worktree(self
, path
):
23 if os
.path
.exists(path
):
24 self
.last_worktree_diff
= core
.read(path
)
26 def apply_diff(self
, path
):
27 if os
.path
.exists(path
):
28 self
.last_diff
= core
.read(path
)
31 class DiffSource(object):
32 def __init__(self
, fwd
, reverse
):
33 self
.fwd
= core
.read(fwd
)
34 self
.reverse
= core
.read(reverse
)
36 def get(self
, head
, amending
, filename
, cached
, reverse
):
38 return self
.parse(self
.reverse
)
40 return self
.parse(self
.fwd
)
42 def parse(self
, diffoutput
):
43 return gitcmds
.extract_diff_header(
44 status
=0, deleted
=False,
45 with_diff_header
=True, suppress_header
=False,
46 diffoutput
=diffoutput
)
49 class DiffParseTestCase(unittest
.TestCase
):
51 self
.model
= DiffParseModel()
54 fwd
= helper
.fixture('diff.txt')
55 reverse
= helper
.fixture('diff-reverse.txt')
56 source
= DiffSource(fwd
, reverse
)
57 model
= DiffParseModel()
58 parser
= DiffParser(model
, filename
='',
59 cached
=False, reverse
=False,
62 self
.assertEqual(parser
.offsets(),
64 self
.assertEqual(parser
.spans(),
65 [[0, 916], [916, 1798], [1798, 2550]])
67 diffs
= parser
.diffs()
68 self
.assertEqual(len(diffs
), 3)
70 self
.assertEqual(len(diffs
[0]), 23)
71 self
.assertEqual(diffs
[0][0],
72 '@@ -6,10 +6,21 @@ from cola import gitcmds')
73 self
.assertEqual(diffs
[0][1],
74 ' from cola import gitcfg')
75 self
.assertEqual(diffs
[0][2],
77 self
.assertEqual(diffs
[0][3],
79 self
.assertEqual(diffs
[0][4],
80 '+class DiffSource(object):')
82 self
.assertEqual(len(diffs
[1]), 18)
83 self
.assertEqual(diffs
[1][0],
84 '@@ -29,13 +40,11 @@ class DiffParser(object):')
85 self
.assertEqual(diffs
[1][1],
86 ' self.diff_sel = []')
87 self
.assertEqual(diffs
[1][2],
88 ' self.selected = []')
89 self
.assertEqual(diffs
[1][3],
90 ' self.filename = filename')
91 self
.assertEqual(diffs
[1][4],
92 '+ self.diff_source = diff_source or DiffSource()')
94 self
.assertEqual(len(diffs
[2]), 18)
95 self
.assertEqual(diffs
[2][0],
96 '@@ -43,11 +52,10 @@ class DiffParser(object):')
98 def test_diff_at_start(self
):
99 fwd
= helper
.fixture('diff-start.txt')
100 reverse
= helper
.fixture('diff-start-reverse.txt')
102 source
= DiffSource(fwd
, reverse
)
103 model
= DiffParseModel()
104 parser
= DiffParser(model
, filename
='',
105 cached
=False, reverse
=False,
108 self
.assertEqual(parser
.diffs()[0][0], '@@ -1 +1,4 @@')
109 self
.assertEqual(parser
.offsets(), [30])
110 self
.assertEqual(parser
.spans(), [[0, 30]])
111 self
.assertEqual(parser
.diffs_for_range(0, 10),
112 ([u
'@@ -1 +1,4 @@\n bar\n+a\n+b\n+c\n\n'],
114 self
.assertEqual(parser
.ranges()[0].begin
, [1, 1])
115 self
.assertEqual(parser
.ranges()[0].end
, [1, 4])
116 self
.assertEqual(parser
.ranges()[0].make(), '@@ -1 +1,4 @@')
118 def test_diff_at_end(self
):
119 fwd
= helper
.fixture('diff-end.txt')
120 reverse
= helper
.fixture('diff-end-reverse.txt')
122 source
= DiffSource(fwd
, reverse
)
123 model
= DiffParseModel()
124 parser
= DiffParser(model
, filename
='',
125 cached
=False, reverse
=False,
128 self
.assertEqual(parser
.diffs()[0][0], '@@ -1,39 +1 @@')
129 self
.assertEqual(parser
.offsets(), [1114])
130 self
.assertEqual(parser
.spans(), [[0, 1114]])
131 self
.assertEqual(parser
.ranges()[0].begin
, [1, 39])
132 self
.assertEqual(parser
.ranges()[0].end
, [1, 1])
133 self
.assertEqual(parser
.ranges()[0].make(), '@@ -1,39 +1 @@')
135 def test_diff_that_empties_file(self
):
136 fwd
= helper
.fixture('diff-empty.txt')
137 reverse
= helper
.fixture('diff-empty-reverse.txt')
139 source
= DiffSource(fwd
, reverse
)
140 model
= DiffParseModel()
141 parser
= DiffParser(model
, filename
='',
142 cached
=False, reverse
=False,
145 self
.assertEqual(parser
.diffs()[0][0], '@@ -1,2 +0,0 @@')
146 self
.assertEqual(parser
.offsets(), [33])
147 self
.assertEqual(parser
.spans(), [[0, 33]])
148 self
.assertEqual(parser
.diffs_for_range(0, 1),
149 ([u
'@@ -1,2 +0,0 @@\n-first\n-second\n\n'],
152 self
.assertEqual(parser
.ranges()[0].begin
, [1, 2])
153 self
.assertEqual(parser
.ranges()[0].end
, [0, 0])
154 self
.assertEqual(parser
.ranges()[0].make(), '@@ -1,2 +0,0 @@')
157 class RangeTestCase(unittest
.TestCase
):
159 def test_empty_becomes_non_empty(self
):
160 r
= diffparse
.Range('1,2', '0,0')
161 self
.assertEqual(r
.begin
, [1,2])
162 self
.assertEqual(r
.end
, [0, 0])
163 self
.assertEqual(r
.make(), '@@ -1,2 +0,0 @@')
166 self
.assertEqual(r
.end
, [1, 1])
167 self
.assertEqual(r
.make(), '@@ -1,2 +1 @@')
169 def test_single_line(self
):
170 r
= diffparse
.Range('1', '1,2')
171 self
.assertEqual(r
.begin
, [1, 1])
172 self
.assertEqual(r
.end
, [1, 2])
173 self
.assertEqual(r
.make(), '@@ -1 +1,2 @@')
175 self
.assertEqual(r
.make(), '@@ -1 +1 @@')
178 if __name__
== '__main__':