blob: 3273dcbd5fdc59795f5fbfee9a2f3fbb03babc88 [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
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200201 %bwipe!
202endfunc
203
204func Test_dp_do_buffer()
205 e! one
206 let bn1=bufnr('%')
207 let l = range(60)
208 call setline(1, l)
209 diffthis
210
211 new two
212 let l[10] = 'one'
213 let l[20] = 'two'
214 let l[30] = 'three'
215 let l[40] = 'four'
216 let l[50] = 'five'
217 call setline(1, l)
218 diffthis
219
220 " dp and do with invalid buffer number.
221 11
222 call assert_fails('norm 99999dp', 'E102:')
223 call assert_fails('norm 99999do', 'E102:')
224 call assert_fails('diffput non_existing_buffer', 'E94:')
225 call assert_fails('diffget non_existing_buffer', 'E94:')
226
227 " dp and do with valid buffer number.
228 call assert_equal('one', getline('.'))
229 exe 'norm ' . bn1 . 'do'
230 call assert_equal('10', getline('.'))
231 21
232 call assert_equal('two', getline('.'))
233 diffget one
234 call assert_equal('20', getline('.'))
235
236 31
237 exe 'norm ' . bn1 . 'dp'
238 41
239 diffput one
240 wincmd w
241 31
242 call assert_equal('three', getline('.'))
243 41
244 call assert_equal('four', getline('.'))
245
246 " dp and do with buffer number which is not in diff mode.
247 new not_in_diff_mode
248 let bn3=bufnr('%')
249 wincmd w
250 51
251 call assert_fails('exe "norm" . bn3 . "dp"', 'E103:')
252 call assert_fails('exe "norm" . bn3 . "do"', 'E103:')
253 call assert_fails('diffput not_in_diff_mode', 'E94:')
254 call assert_fails('diffget not_in_diff_mode', 'E94:')
255
256 windo diffoff
257 %bwipe!
Bram Moolenaar42093c02016-07-30 16:16:54 +0200258endfunc
Bram Moolenaare67d5462016-08-27 22:40:42 +0200259
260func Test_diffoff()
261 enew!
262 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200263 redraw
Bram Moolenaare67d5462016-08-27 22:40:42 +0200264 let normattr = screenattr(1, 1)
265 diffthis
266 botright vert new
267 call setline(1, ['One', '', 'Two', 'Three'])
268 diffthis
269 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100270 call assert_notequal(normattr, screenattr(1, 1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200271 diffoff!
272 redraw
273 call assert_equal(normattr, screenattr(1, 1))
274 bwipe!
275 bwipe!
276endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200277
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200278func Test_diffopt_icase()
279 set diffopt=icase,foldcolumn:0
280
281 e one
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200282 call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200283 redraw
284 let normattr = screenattr(1, 1)
285 diffthis
286
287 botright vert new two
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200288 call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VE'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200289 diffthis
290
291 redraw
292 call assert_equal(normattr, screenattr(1, 1))
293 call assert_equal(normattr, screenattr(2, 1))
294 call assert_notequal(normattr, screenattr(3, 1))
295 call assert_equal(normattr, screenattr(4, 1))
296
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200297 let dtextattr = screenattr(5, 3)
298 call assert_notequal(dtextattr, screenattr(5, 1))
299 call assert_notequal(dtextattr, screenattr(5, 5))
300
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200301 diffoff!
302 %bwipe!
303 set diffopt&
304endfunc
305
306func Test_diffopt_iwhite()
307 set diffopt=iwhite,foldcolumn:0
308
309 e one
310 " Difference in trailing spaces should be ignored,
311 " but not other space differences.
312 call setline(1, ["One \t", 'Two', 'Three', 'Four'])
313 redraw
314 let normattr = screenattr(1, 1)
315 diffthis
316
317 botright vert new two
318 call setline(1, ["One\t ", "Two\t ", 'Three', ' Four'])
319 diffthis
320
321 redraw
322 call assert_equal(normattr, screenattr(1, 1))
323 call assert_equal(normattr, screenattr(2, 1))
324 call assert_equal(normattr, screenattr(3, 1))
325 call assert_notequal(normattr, screenattr(4, 1))
326
327 diffoff!
328 %bwipe!
329 set diffopt&
330endfunc
331
332func Test_diffopt_context()
333 enew!
334 call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
335 diffthis
336 new
337 call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
338 diffthis
339
340 set diffopt=context:2
341 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
342 set diffopt=context:1
343 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
344
345 diffoff!
346 %bwipe!
347 set diffopt&
348endfunc
349
350func Test_diffopt_horizontal()
351 set diffopt=horizontal
352 diffsplit
353
354 call assert_equal(&columns, winwidth(1))
355 call assert_equal(&columns, winwidth(2))
356 call assert_equal(&lines, winheight(1) + winheight(2) + 3)
357 call assert_inrange(0, 1, winheight(1) - winheight(2))
358
359 set diffopt&
360 diffoff!
361 %bwipe
362endfunc
363
364func Test_diffopt_vertical()
365 set diffopt=vertical
366 diffsplit
367
368 call assert_equal(&lines - 2, winheight(1))
369 call assert_equal(&lines - 2, winheight(2))
370 call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
371 call assert_inrange(0, 1, winwidth(1) - winwidth(2))
372
373 set diffopt&
374 diffoff!
375 %bwipe
376endfunc
377
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100378func Test_diffoff_hidden()
379 set diffopt=filler,foldcolumn:0
380 e! one
381 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200382 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100383 let normattr = screenattr(1, 1)
384 diffthis
385 botright vert new two
386 call setline(1, ['One', 'Four'])
387 diffthis
388 redraw
389 call assert_notequal(normattr, screenattr(1, 1))
390 set hidden
391 close
392 redraw
393 " diffing with hidden buffer two
394 call assert_notequal(normattr, screenattr(1, 1))
395 diffoff
396 redraw
397 call assert_equal(normattr, screenattr(1, 1))
398 diffthis
399 redraw
400 " still diffing with hidden buffer two
401 call assert_notequal(normattr, screenattr(1, 1))
402 diffoff!
403 redraw
404 call assert_equal(normattr, screenattr(1, 1))
405 diffthis
406 redraw
407 " no longer diffing with hidden buffer two
408 call assert_equal(normattr, screenattr(1, 1))
409
410 bwipe!
411 bwipe!
412 set hidden& diffopt&
413endfunc
414
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200415func Test_setting_cursor()
416 new Xtest1
417 put =range(1,90)
418 wq
419 new Xtest2
420 put =range(1,100)
421 wq
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200422
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200423 tabe Xtest2
424 $
425 diffsp Xtest1
426 tabclose
427
428 call delete('Xtest1')
429 call delete('Xtest2')
430endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100431
432func Test_diff_move_to()
433 new
434 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
435 diffthis
436 vnew
437 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
438 diffthis
439 norm ]c
440 call assert_equal(2, line('.'))
441 norm 3]c
442 call assert_equal(9, line('.'))
443 norm 10]c
444 call assert_equal(11, line('.'))
445 norm [c
446 call assert_equal(9, line('.'))
447 norm 2[c
448 call assert_equal(5, line('.'))
449 norm 10[c
450 call assert_equal(2, line('.'))
451 %bwipe!
452endfunc
453
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200454func Test_diffexpr()
455 if !executable('diff')
456 return
457 endif
458
459 func DiffExpr()
460 silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>' . v:fname_out
461 endfunc
462 set diffexpr=DiffExpr()
463 set diffopt=foldcolumn:0
464
465 enew!
466 call setline(1, ['one', 'two', 'three'])
467 redraw
468 let normattr = screenattr(1, 1)
469 diffthis
470
471 botright vert new
472 call setline(1, ['one', 'two', 'three.'])
473 diffthis
474
475 redraw
476 call assert_equal(normattr, screenattr(1, 1))
477 call assert_equal(normattr, screenattr(2, 1))
478 call assert_notequal(normattr, screenattr(3, 1))
479
480 diffoff!
481 %bwipe!
482 set diffexpr& diffopt&
483endfunc
484
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100485func Test_diffpatch()
486 " The patch program on MS-Windows may fail or hang.
487 if !executable('patch') || !has('unix')
488 return
489 endif
490 new
491 insert
492***************
493*** 1,3 ****
494 1
495! 2
496 3
497--- 1,4 ----
498 1
499! 2x
500 3
501+ 4
502.
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200503 saveas! Xpatch
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100504 bwipe!
505 new
506 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100507
Bram Moolenaara95ab322017-03-11 19:21:53 +0100508 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100509 call setline(1, ['1', '2', '3'])
510 if name != 'Xpatch'
511 call rename('Xpatch', name)
512 endif
513 exe 'diffpatch ' . escape(name, '$')
514 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
515 if name != 'Xpatch'
516 call rename(name, 'Xpatch')
517 endif
518 bwipe!
519 endfor
520
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100521 call delete('Xpatch')
522 bwipe!
523endfunc
524
525func Test_diff_too_many_buffers()
526 for i in range(1, 8)
527 exe "new Xtest" . i
528 diffthis
529 endfor
530 new Xtest9
531 call assert_fails('diffthis', 'E96:')
532 %bwipe!
533endfunc
534
535func Test_diff_nomodifiable()
536 new
537 call setline(1, [1, 2, 3, 4])
538 setl nomodifiable
539 diffthis
540 vnew
541 call setline(1, ['1x', 2, 3, 3, 4])
542 diffthis
543 call assert_fails('norm dp', 'E793:')
544 setl nomodifiable
545 call assert_fails('norm do', 'E21:')
546 %bwipe!
547endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100548
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200549func Test_diff_hlID()
550 new
551 call setline(1, [1, 2, 3])
552 diffthis
553 vnew
554 call setline(1, ['1x', 2, 'x', 3])
555 diffthis
556 redraw
557
558 call assert_equal(synIDattr(diff_hlID(-1, 1), "name"), "")
559
560 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
561 call assert_equal(synIDattr(diff_hlID(1, 2), "name"), "DiffText")
562 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
563 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "DiffAdd")
564 call assert_equal(synIDattr(diff_hlID(4, 1), "name"), "")
565
566 wincmd w
567 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
568 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
569 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
570
571 %bwipe!
572endfunc
573
574func Test_diff_filler()
575 new
576 call setline(1, [1, 2, 3, 'x', 4])
577 diffthis
578 vnew
579 call setline(1, [1, 2, 'y', 'y', 3, 4])
580 diffthis
581 redraw
582
583 call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'diff_filler(v:val)'))
584 wincmd w
585 call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
586
587 %bwipe!
588endfunc
589
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100590func Test_diff_lastline()
591 enew!
592 only!
593 call setline(1, ['This is a ', 'line with five ', 'rows'])
594 diffthis
595 botright vert new
596 call setline(1, ['This is', 'a line with ', 'four rows'])
597 diffthis
598 1
599 call feedkeys("Je a\<CR>", 'tx')
600 call feedkeys("Je a\<CR>", 'tx')
601 let w1lines = winline()
602 wincmd w
603 $
604 let w2lines = winline()
605 call assert_equal(w2lines, w1lines)
606 bwipe!
607 bwipe!
608endfunc