blob: 33617c9cbe19ec4bd3db9f68db3e0aa86d0731a7 [file] [log] [blame]
Bram Moolenaar42093c02016-07-30 16:16:54 +02001" Tests for diff mode
2
3func Test_diff_fold_sync()
4 enew!
5 let l = range(50)
6 call setline(1, l)
7 diffthis
8 let winone = win_getid()
9 new
10 let l[25] = 'diff'
11 call setline(1, l)
12 diffthis
13 let wintwo = win_getid()
14 " line 15 is inside the closed fold
15 call assert_equal(19, foldclosedend(10))
16 call win_gotoid(winone)
17 call assert_equal(19, foldclosedend(10))
18 " open the fold
19 normal zv
20 call assert_equal(-1, foldclosedend(10))
21 " fold in other window must have opened too
22 call win_gotoid(wintwo)
23 call assert_equal(-1, foldclosedend(10))
24
25 " cursor position is in sync
26 normal 23G
27 call win_gotoid(winone)
28 call assert_equal(23, getcurpos()[1])
29
30 windo diffoff
31 close!
32 set nomodified
33endfunc
34
35func Test_vert_split()
36 " Disable the title to avoid xterm keeping the wrong one.
37 set notitle noicon
38 new
39 let l = ['1 aa', '2 bb', '3 cc', '4 dd', '5 ee']
40 call setline(1, l)
41 w! Xtest
42 normal dd
43 $
44 put
45 normal kkrXoxxx
46 w! Xtest2
47 file Nop
48 normal ggoyyyjjjozzzz
49 set foldmethod=marker foldcolumn=4
50 call assert_equal(0, &diff)
51 call assert_equal('marker', &foldmethod)
52 call assert_equal(4, &foldcolumn)
53 call assert_equal(0, &scrollbind)
54 call assert_equal(0, &cursorbind)
55 call assert_equal(1, &wrap)
56
57 vert diffsplit Xtest
58 vert diffsplit Xtest2
59 call assert_equal(1, &diff)
60 call assert_equal('diff', &foldmethod)
61 call assert_equal(2, &foldcolumn)
62 call assert_equal(1, &scrollbind)
63 call assert_equal(1, &cursorbind)
64 call assert_equal(0, &wrap)
65
66 let diff_fdm = &fdm
67 let diff_fdc = &fdc
68 " repeat entering diff mode here to see if this saves the wrong settings
69 diffthis
70 " jump to second window for a moment to have filler line appear at start of
71 " first window
72 wincmd w
73 normal gg
74 wincmd p
75 normal gg
76 call assert_equal(2, winline())
77 normal j
78 call assert_equal(4, winline())
79 normal j
80 call assert_equal(5, winline())
81 normal j
82 call assert_equal(6, winline())
83 normal j
84 call assert_equal(8, winline())
85 normal j
86 call assert_equal(9, winline())
87
88 wincmd w
89 normal gg
90 call assert_equal(1, winline())
91 normal j
92 call assert_equal(2, winline())
93 normal j
94 call assert_equal(4, winline())
95 normal j
96 call assert_equal(5, winline())
97 normal j
98 call assert_equal(8, winline())
99
100 wincmd w
101 normal gg
102 call assert_equal(2, winline())
103 normal j
104 call assert_equal(3, winline())
105 normal j
106 call assert_equal(4, winline())
107 normal j
108 call assert_equal(5, winline())
109 normal j
110 call assert_equal(6, winline())
111 normal j
112 call assert_equal(7, winline())
113 normal j
114 call assert_equal(8, winline())
115
116 " Test diffoff
117 diffoff!
118 1wincmd 2
119 let &diff = 1
120 let &fdm = diff_fdm
121 let &fdc = diff_fdc
122 4wincmd w
123 diffoff!
124 1wincmd w
125 call assert_equal(0, &diff)
126 call assert_equal('marker', &foldmethod)
127 call assert_equal(4, &foldcolumn)
128 call assert_equal(0, &scrollbind)
129 call assert_equal(0, &cursorbind)
130 call assert_equal(1, &wrap)
131
132 wincmd w
133 call assert_equal(0, &diff)
134 call assert_equal('marker', &foldmethod)
135 call assert_equal(4, &foldcolumn)
136 call assert_equal(0, &scrollbind)
137 call assert_equal(0, &cursorbind)
138 call assert_equal(1, &wrap)
139
140 wincmd w
141 call assert_equal(0, &diff)
142 call assert_equal('marker', &foldmethod)
143 call assert_equal(4, &foldcolumn)
144 call assert_equal(0, &scrollbind)
145 call assert_equal(0, &cursorbind)
146 call assert_equal(1, &wrap)
147
Bram Moolenaar623cf882016-07-30 16:36:01 +0200148 call delete('Xtest')
149 call delete('Xtest2')
Bram Moolenaar42093c02016-07-30 16:16:54 +0200150 windo bw!
151endfunc
152
153func Test_filler_lines()
154 " Test that diffing shows correct filler lines
155 enew!
156 put =range(4,10)
157 1d _
158 vnew
159 put =range(1,10)
160 1d _
161 windo diffthis
162 wincmd h
163 call assert_equal(1, line('w0'))
164 unlet! diff_fdm diff_fdc
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200165 windo diffoff
166 bwipe!
167 enew!
168endfunc
Bram Moolenaar42093c02016-07-30 16:16:54 +0200169
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200170func Test_diffget_diffput()
171 enew!
172 let l = range(50)
173 call setline(1, l)
174 call assert_fails('diffget', 'E99:')
175 diffthis
176 call assert_fails('diffget', 'E100:')
177 new
178 let l[10] = 'one'
179 let l[20] = 'two'
180 let l[30] = 'three'
181 let l[40] = 'four'
182 call setline(1, l)
183 diffthis
184 call assert_equal('one', getline(11))
185 11diffget
186 call assert_equal('10', getline(11))
187 21diffput
188 wincmd w
189 call assert_equal('two', getline(21))
190 normal 31Gdo
191 call assert_equal('three', getline(31))
192 call assert_equal('40', getline(41))
193 normal 41Gdp
194 wincmd w
195 call assert_equal('40', getline(41))
196 new
197 diffthis
198 call assert_fails('diffget', 'E101:')
199
200 windo diffoff
201 bwipe!
202 bwipe!
203 enew!
Bram Moolenaar42093c02016-07-30 16:16:54 +0200204endfunc
Bram Moolenaare67d5462016-08-27 22:40:42 +0200205
206func Test_diffoff()
207 enew!
208 call setline(1, ['Two', 'Three'])
209 let normattr = screenattr(1, 1)
210 diffthis
211 botright vert new
212 call setline(1, ['One', '', 'Two', 'Three'])
213 diffthis
214 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100215 call assert_notequal(normattr, screenattr(1, 1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200216 diffoff!
217 redraw
218 call assert_equal(normattr, screenattr(1, 1))
219 bwipe!
220 bwipe!
221endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200222
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100223func Test_diffoff_hidden()
224 set diffopt=filler,foldcolumn:0
225 e! one
226 call setline(1, ['Two', 'Three'])
227 let normattr = screenattr(1, 1)
228 diffthis
229 botright vert new two
230 call setline(1, ['One', 'Four'])
231 diffthis
232 redraw
233 call assert_notequal(normattr, screenattr(1, 1))
234 set hidden
235 close
236 redraw
237 " diffing with hidden buffer two
238 call assert_notequal(normattr, screenattr(1, 1))
239 diffoff
240 redraw
241 call assert_equal(normattr, screenattr(1, 1))
242 diffthis
243 redraw
244 " still diffing with hidden buffer two
245 call assert_notequal(normattr, screenattr(1, 1))
246 diffoff!
247 redraw
248 call assert_equal(normattr, screenattr(1, 1))
249 diffthis
250 redraw
251 " no longer diffing with hidden buffer two
252 call assert_equal(normattr, screenattr(1, 1))
253
254 bwipe!
255 bwipe!
256 set hidden& diffopt&
257endfunc
258
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200259func Test_setting_cursor()
260 new Xtest1
261 put =range(1,90)
262 wq
263 new Xtest2
264 put =range(1,100)
265 wq
266
267 tabe Xtest2
268 $
269 diffsp Xtest1
270 tabclose
271
272 call delete('Xtest1')
273 call delete('Xtest2')
274endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100275
276func Test_diff_move_to()
277 new
278 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
279 diffthis
280 vnew
281 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
282 diffthis
283 norm ]c
284 call assert_equal(2, line('.'))
285 norm 3]c
286 call assert_equal(9, line('.'))
287 norm 10]c
288 call assert_equal(11, line('.'))
289 norm [c
290 call assert_equal(9, line('.'))
291 norm 2[c
292 call assert_equal(5, line('.'))
293 norm 10[c
294 call assert_equal(2, line('.'))
295 %bwipe!
296endfunc
297
298func Test_diffpatch()
299 " The patch program on MS-Windows may fail or hang.
300 if !executable('patch') || !has('unix')
301 return
302 endif
303 new
304 insert
305***************
306*** 1,3 ****
307 1
308! 2
309 3
310--- 1,4 ----
311 1
312! 2x
313 3
314+ 4
315.
316 saveas Xpatch
317 bwipe!
318 new
319 call assert_fails('diffpatch Xpatch', 'E816:')
320 call setline(1, ['1', '2', '3'])
321 diffpatch Xpatch
322 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
323 call delete('Xpatch')
324 bwipe!
325endfunc
326
327func Test_diff_too_many_buffers()
328 for i in range(1, 8)
329 exe "new Xtest" . i
330 diffthis
331 endfor
332 new Xtest9
333 call assert_fails('diffthis', 'E96:')
334 %bwipe!
335endfunc
336
337func Test_diff_nomodifiable()
338 new
339 call setline(1, [1, 2, 3, 4])
340 setl nomodifiable
341 diffthis
342 vnew
343 call setline(1, ['1x', 2, 3, 3, 4])
344 diffthis
345 call assert_fails('norm dp', 'E793:')
346 setl nomodifiable
347 call assert_fails('norm do', 'E21:')
348 %bwipe!
349endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100350
351func Test_diff_lastline()
352 enew!
353 only!
354 call setline(1, ['This is a ', 'line with five ', 'rows'])
355 diffthis
356 botright vert new
357 call setline(1, ['This is', 'a line with ', 'four rows'])
358 diffthis
359 1
360 call feedkeys("Je a\<CR>", 'tx')
361 call feedkeys("Je a\<CR>", 'tx')
362 let w1lines = winline()
363 wincmd w
364 $
365 let w2lines = winline()
366 call assert_equal(w2lines, w1lines)
367 bwipe!
368 bwipe!
369endfunc