blob: 088ec5ccf60d8a6c5537c22e49f49988766a3d72 [file] [log] [blame]
Bram Moolenaar42093c02016-07-30 16:16:54 +02001" Tests for diff mode
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01002set belloff=all
Bram Moolenaar42093c02016-07-30 16:16:54 +02003
4func Test_diff_fold_sync()
5 enew!
6 let l = range(50)
7 call setline(1, l)
8 diffthis
9 let winone = win_getid()
10 new
11 let l[25] = 'diff'
12 call setline(1, l)
13 diffthis
14 let wintwo = win_getid()
15 " line 15 is inside the closed fold
16 call assert_equal(19, foldclosedend(10))
17 call win_gotoid(winone)
18 call assert_equal(19, foldclosedend(10))
19 " open the fold
20 normal zv
21 call assert_equal(-1, foldclosedend(10))
22 " fold in other window must have opened too
23 call win_gotoid(wintwo)
24 call assert_equal(-1, foldclosedend(10))
25
26 " cursor position is in sync
27 normal 23G
28 call win_gotoid(winone)
29 call assert_equal(23, getcurpos()[1])
30
31 windo diffoff
32 close!
33 set nomodified
34endfunc
35
36func Test_vert_split()
37 " Disable the title to avoid xterm keeping the wrong one.
38 set notitle noicon
39 new
40 let l = ['1 aa', '2 bb', '3 cc', '4 dd', '5 ee']
41 call setline(1, l)
42 w! Xtest
43 normal dd
44 $
45 put
46 normal kkrXoxxx
47 w! Xtest2
48 file Nop
49 normal ggoyyyjjjozzzz
50 set foldmethod=marker foldcolumn=4
51 call assert_equal(0, &diff)
52 call assert_equal('marker', &foldmethod)
53 call assert_equal(4, &foldcolumn)
54 call assert_equal(0, &scrollbind)
55 call assert_equal(0, &cursorbind)
56 call assert_equal(1, &wrap)
57
58 vert diffsplit Xtest
59 vert diffsplit Xtest2
60 call assert_equal(1, &diff)
61 call assert_equal('diff', &foldmethod)
62 call assert_equal(2, &foldcolumn)
63 call assert_equal(1, &scrollbind)
64 call assert_equal(1, &cursorbind)
65 call assert_equal(0, &wrap)
66
67 let diff_fdm = &fdm
68 let diff_fdc = &fdc
69 " repeat entering diff mode here to see if this saves the wrong settings
70 diffthis
71 " jump to second window for a moment to have filler line appear at start of
72 " first window
73 wincmd w
74 normal gg
75 wincmd p
76 normal gg
77 call assert_equal(2, winline())
78 normal j
79 call assert_equal(4, winline())
80 normal j
81 call assert_equal(5, winline())
82 normal j
83 call assert_equal(6, winline())
84 normal j
85 call assert_equal(8, winline())
86 normal j
87 call assert_equal(9, winline())
88
89 wincmd w
90 normal gg
91 call assert_equal(1, winline())
92 normal j
93 call assert_equal(2, winline())
94 normal j
95 call assert_equal(4, winline())
96 normal j
97 call assert_equal(5, winline())
98 normal j
99 call assert_equal(8, winline())
100
101 wincmd w
102 normal gg
103 call assert_equal(2, winline())
104 normal j
105 call assert_equal(3, winline())
106 normal j
107 call assert_equal(4, winline())
108 normal j
109 call assert_equal(5, winline())
110 normal j
111 call assert_equal(6, winline())
112 normal j
113 call assert_equal(7, winline())
114 normal j
115 call assert_equal(8, winline())
116
117 " Test diffoff
118 diffoff!
119 1wincmd 2
120 let &diff = 1
121 let &fdm = diff_fdm
122 let &fdc = diff_fdc
123 4wincmd w
124 diffoff!
125 1wincmd w
126 call assert_equal(0, &diff)
127 call assert_equal('marker', &foldmethod)
128 call assert_equal(4, &foldcolumn)
129 call assert_equal(0, &scrollbind)
130 call assert_equal(0, &cursorbind)
131 call assert_equal(1, &wrap)
132
133 wincmd w
134 call assert_equal(0, &diff)
135 call assert_equal('marker', &foldmethod)
136 call assert_equal(4, &foldcolumn)
137 call assert_equal(0, &scrollbind)
138 call assert_equal(0, &cursorbind)
139 call assert_equal(1, &wrap)
140
141 wincmd w
142 call assert_equal(0, &diff)
143 call assert_equal('marker', &foldmethod)
144 call assert_equal(4, &foldcolumn)
145 call assert_equal(0, &scrollbind)
146 call assert_equal(0, &cursorbind)
147 call assert_equal(1, &wrap)
148
Bram Moolenaar623cf882016-07-30 16:36:01 +0200149 call delete('Xtest')
150 call delete('Xtest2')
Bram Moolenaar42093c02016-07-30 16:16:54 +0200151 windo bw!
152endfunc
153
154func Test_filler_lines()
155 " Test that diffing shows correct filler lines
156 enew!
157 put =range(4,10)
158 1d _
159 vnew
160 put =range(1,10)
161 1d _
162 windo diffthis
163 wincmd h
164 call assert_equal(1, line('w0'))
165 unlet! diff_fdm diff_fdc
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200166 windo diffoff
167 bwipe!
168 enew!
169endfunc
Bram Moolenaar42093c02016-07-30 16:16:54 +0200170
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200171func Test_diffget_diffput()
172 enew!
173 let l = range(50)
174 call setline(1, l)
175 call assert_fails('diffget', 'E99:')
176 diffthis
177 call assert_fails('diffget', 'E100:')
178 new
179 let l[10] = 'one'
180 let l[20] = 'two'
181 let l[30] = 'three'
182 let l[40] = 'four'
183 call setline(1, l)
184 diffthis
185 call assert_equal('one', getline(11))
186 11diffget
187 call assert_equal('10', getline(11))
188 21diffput
189 wincmd w
190 call assert_equal('two', getline(21))
191 normal 31Gdo
192 call assert_equal('three', getline(31))
193 call assert_equal('40', getline(41))
194 normal 41Gdp
195 wincmd w
196 call assert_equal('40', getline(41))
197 new
198 diffthis
199 call assert_fails('diffget', 'E101:')
200
201 windo diffoff
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200202 %bwipe!
203endfunc
204
205func Test_dp_do_buffer()
206 e! one
207 let bn1=bufnr('%')
208 let l = range(60)
209 call setline(1, l)
210 diffthis
211
212 new two
213 let l[10] = 'one'
214 let l[20] = 'two'
215 let l[30] = 'three'
216 let l[40] = 'four'
217 let l[50] = 'five'
218 call setline(1, l)
219 diffthis
220
221 " dp and do with invalid buffer number.
222 11
223 call assert_fails('norm 99999dp', 'E102:')
224 call assert_fails('norm 99999do', 'E102:')
225 call assert_fails('diffput non_existing_buffer', 'E94:')
226 call assert_fails('diffget non_existing_buffer', 'E94:')
227
228 " dp and do with valid buffer number.
229 call assert_equal('one', getline('.'))
230 exe 'norm ' . bn1 . 'do'
231 call assert_equal('10', getline('.'))
232 21
233 call assert_equal('two', getline('.'))
234 diffget one
235 call assert_equal('20', getline('.'))
236
237 31
238 exe 'norm ' . bn1 . 'dp'
239 41
240 diffput one
241 wincmd w
242 31
243 call assert_equal('three', getline('.'))
244 41
245 call assert_equal('four', getline('.'))
246
247 " dp and do with buffer number which is not in diff mode.
248 new not_in_diff_mode
249 let bn3=bufnr('%')
250 wincmd w
251 51
252 call assert_fails('exe "norm" . bn3 . "dp"', 'E103:')
253 call assert_fails('exe "norm" . bn3 . "do"', 'E103:')
254 call assert_fails('diffput not_in_diff_mode', 'E94:')
255 call assert_fails('diffget not_in_diff_mode', 'E94:')
256
257 windo diffoff
258 %bwipe!
Bram Moolenaar42093c02016-07-30 16:16:54 +0200259endfunc
Bram Moolenaare67d5462016-08-27 22:40:42 +0200260
261func Test_diffoff()
262 enew!
263 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200264 redraw
Bram Moolenaare67d5462016-08-27 22:40:42 +0200265 let normattr = screenattr(1, 1)
266 diffthis
267 botright vert new
268 call setline(1, ['One', '', 'Two', 'Three'])
269 diffthis
270 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100271 call assert_notequal(normattr, screenattr(1, 1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200272 diffoff!
273 redraw
274 call assert_equal(normattr, screenattr(1, 1))
275 bwipe!
276 bwipe!
277endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200278
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200279func Test_diffopt_icase()
280 set diffopt=icase,foldcolumn:0
281
282 e one
283 call setline(1, ['One', 'Two', 'Three', 'Four'])
284 redraw
285 let normattr = screenattr(1, 1)
286 diffthis
287
288 botright vert new two
289 call setline(1, ['one', 'TWO', 'Three ', 'Four'])
290 diffthis
291
292 redraw
293 call assert_equal(normattr, screenattr(1, 1))
294 call assert_equal(normattr, screenattr(2, 1))
295 call assert_notequal(normattr, screenattr(3, 1))
296 call assert_equal(normattr, screenattr(4, 1))
297
298 diffoff!
299 %bwipe!
300 set diffopt&
301endfunc
302
303func Test_diffopt_iwhite()
304 set diffopt=iwhite,foldcolumn:0
305
306 e one
307 " Difference in trailing spaces should be ignored,
308 " but not other space differences.
309 call setline(1, ["One \t", 'Two', 'Three', 'Four'])
310 redraw
311 let normattr = screenattr(1, 1)
312 diffthis
313
314 botright vert new two
315 call setline(1, ["One\t ", "Two\t ", 'Three', ' Four'])
316 diffthis
317
318 redraw
319 call assert_equal(normattr, screenattr(1, 1))
320 call assert_equal(normattr, screenattr(2, 1))
321 call assert_equal(normattr, screenattr(3, 1))
322 call assert_notequal(normattr, screenattr(4, 1))
323
324 diffoff!
325 %bwipe!
326 set diffopt&
327endfunc
328
329func Test_diffopt_context()
330 enew!
331 call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
332 diffthis
333 new
334 call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
335 diffthis
336
337 set diffopt=context:2
338 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
339 set diffopt=context:1
340 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
341
342 diffoff!
343 %bwipe!
344 set diffopt&
345endfunc
346
347func Test_diffopt_horizontal()
348 set diffopt=horizontal
349 diffsplit
350
351 call assert_equal(&columns, winwidth(1))
352 call assert_equal(&columns, winwidth(2))
353 call assert_equal(&lines, winheight(1) + winheight(2) + 3)
354 call assert_inrange(0, 1, winheight(1) - winheight(2))
355
356 set diffopt&
357 diffoff!
358 %bwipe
359endfunc
360
361func Test_diffopt_vertical()
362 set diffopt=vertical
363 diffsplit
364
365 call assert_equal(&lines - 2, winheight(1))
366 call assert_equal(&lines - 2, winheight(2))
367 call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
368 call assert_inrange(0, 1, winwidth(1) - winwidth(2))
369
370 set diffopt&
371 diffoff!
372 %bwipe
373endfunc
374
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100375func Test_diffoff_hidden()
376 set diffopt=filler,foldcolumn:0
377 e! one
378 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200379 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100380 let normattr = screenattr(1, 1)
381 diffthis
382 botright vert new two
383 call setline(1, ['One', 'Four'])
384 diffthis
385 redraw
386 call assert_notequal(normattr, screenattr(1, 1))
387 set hidden
388 close
389 redraw
390 " diffing with hidden buffer two
391 call assert_notequal(normattr, screenattr(1, 1))
392 diffoff
393 redraw
394 call assert_equal(normattr, screenattr(1, 1))
395 diffthis
396 redraw
397 " still diffing with hidden buffer two
398 call assert_notequal(normattr, screenattr(1, 1))
399 diffoff!
400 redraw
401 call assert_equal(normattr, screenattr(1, 1))
402 diffthis
403 redraw
404 " no longer diffing with hidden buffer two
405 call assert_equal(normattr, screenattr(1, 1))
406
407 bwipe!
408 bwipe!
409 set hidden& diffopt&
410endfunc
411
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200412func Test_setting_cursor()
413 new Xtest1
414 put =range(1,90)
415 wq
416 new Xtest2
417 put =range(1,100)
418 wq
419
420 tabe Xtest2
421 $
422 diffsp Xtest1
423 tabclose
424
425 call delete('Xtest1')
426 call delete('Xtest2')
427endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100428
429func Test_diff_move_to()
430 new
431 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
432 diffthis
433 vnew
434 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
435 diffthis
436 norm ]c
437 call assert_equal(2, line('.'))
438 norm 3]c
439 call assert_equal(9, line('.'))
440 norm 10]c
441 call assert_equal(11, line('.'))
442 norm [c
443 call assert_equal(9, line('.'))
444 norm 2[c
445 call assert_equal(5, line('.'))
446 norm 10[c
447 call assert_equal(2, line('.'))
448 %bwipe!
449endfunc
450
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200451func Test_diffexpr()
452 if !executable('diff')
453 return
454 endif
455
456 func DiffExpr()
457 silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>' . v:fname_out
458 endfunc
459 set diffexpr=DiffExpr()
460 set diffopt=foldcolumn:0
461
462 enew!
463 call setline(1, ['one', 'two', 'three'])
464 redraw
465 let normattr = screenattr(1, 1)
466 diffthis
467
468 botright vert new
469 call setline(1, ['one', 'two', 'three.'])
470 diffthis
471
472 redraw
473 call assert_equal(normattr, screenattr(1, 1))
474 call assert_equal(normattr, screenattr(2, 1))
475 call assert_notequal(normattr, screenattr(3, 1))
476
477 diffoff!
478 %bwipe!
479 set diffexpr& diffopt&
480endfunc
481
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100482func Test_diffpatch()
483 " The patch program on MS-Windows may fail or hang.
484 if !executable('patch') || !has('unix')
485 return
486 endif
487 new
488 insert
489***************
490*** 1,3 ****
491 1
492! 2
493 3
494--- 1,4 ----
495 1
496! 2x
497 3
498+ 4
499.
500 saveas Xpatch
501 bwipe!
502 new
503 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100504
Bram Moolenaara95ab322017-03-11 19:21:53 +0100505 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100506 call setline(1, ['1', '2', '3'])
507 if name != 'Xpatch'
508 call rename('Xpatch', name)
509 endif
510 exe 'diffpatch ' . escape(name, '$')
511 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
512 if name != 'Xpatch'
513 call rename(name, 'Xpatch')
514 endif
515 bwipe!
516 endfor
517
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100518 call delete('Xpatch')
519 bwipe!
520endfunc
521
522func Test_diff_too_many_buffers()
523 for i in range(1, 8)
524 exe "new Xtest" . i
525 diffthis
526 endfor
527 new Xtest9
528 call assert_fails('diffthis', 'E96:')
529 %bwipe!
530endfunc
531
532func Test_diff_nomodifiable()
533 new
534 call setline(1, [1, 2, 3, 4])
535 setl nomodifiable
536 diffthis
537 vnew
538 call setline(1, ['1x', 2, 3, 3, 4])
539 diffthis
540 call assert_fails('norm dp', 'E793:')
541 setl nomodifiable
542 call assert_fails('norm do', 'E21:')
543 %bwipe!
544endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100545
546func Test_diff_lastline()
547 enew!
548 only!
549 call setline(1, ['This is a ', 'line with five ', 'rows'])
550 diffthis
551 botright vert new
552 call setline(1, ['This is', 'a line with ', 'four rows'])
553 diffthis
554 1
555 call feedkeys("Je a\<CR>", 'tx')
556 call feedkeys("Je a\<CR>", 'tx')
557 let w1lines = winline()
558 wincmd w
559 $
560 let w2lines = winline()
561 call assert_equal(w2lines, w1lines)
562 bwipe!
563 bwipe!
564endfunc