blob: d6c5c83fe566e5b17caa48efd47827fb32e3a323 [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 Moolenaar97ce4192017-12-01 20:35:58 +0100378func Test_diffopt_hiddenoff()
379 set diffopt=filler,foldcolumn:0,hiddenoff
380 e! one
381 call setline(1, ['Two', 'Three'])
382 redraw
383 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 " should not diffing with hidden buffer two while 'hiddenoff' is enabled
394 call assert_equal(normattr, screenattr(1, 1))
395
396 bwipe!
397 bwipe!
398 set hidden& diffopt&
399endfunc
400
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100401func Test_diffoff_hidden()
402 set diffopt=filler,foldcolumn:0
403 e! one
404 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200405 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100406 let normattr = screenattr(1, 1)
407 diffthis
408 botright vert new two
409 call setline(1, ['One', 'Four'])
410 diffthis
411 redraw
412 call assert_notequal(normattr, screenattr(1, 1))
413 set hidden
414 close
415 redraw
416 " diffing with hidden buffer two
417 call assert_notequal(normattr, screenattr(1, 1))
418 diffoff
419 redraw
420 call assert_equal(normattr, screenattr(1, 1))
421 diffthis
422 redraw
423 " still diffing with hidden buffer two
424 call assert_notequal(normattr, screenattr(1, 1))
425 diffoff!
426 redraw
427 call assert_equal(normattr, screenattr(1, 1))
428 diffthis
429 redraw
430 " no longer diffing with hidden buffer two
431 call assert_equal(normattr, screenattr(1, 1))
432
433 bwipe!
434 bwipe!
435 set hidden& diffopt&
436endfunc
437
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200438func Test_setting_cursor()
439 new Xtest1
440 put =range(1,90)
441 wq
442 new Xtest2
443 put =range(1,100)
444 wq
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200445
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200446 tabe Xtest2
447 $
448 diffsp Xtest1
449 tabclose
450
451 call delete('Xtest1')
452 call delete('Xtest2')
453endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100454
455func Test_diff_move_to()
456 new
457 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
458 diffthis
459 vnew
460 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
461 diffthis
462 norm ]c
463 call assert_equal(2, line('.'))
464 norm 3]c
465 call assert_equal(9, line('.'))
466 norm 10]c
467 call assert_equal(11, line('.'))
468 norm [c
469 call assert_equal(9, line('.'))
470 norm 2[c
471 call assert_equal(5, line('.'))
472 norm 10[c
473 call assert_equal(2, line('.'))
474 %bwipe!
475endfunc
476
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200477func Test_diffexpr()
478 if !executable('diff')
479 return
480 endif
481
482 func DiffExpr()
483 silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>' . v:fname_out
484 endfunc
485 set diffexpr=DiffExpr()
486 set diffopt=foldcolumn:0
487
488 enew!
489 call setline(1, ['one', 'two', 'three'])
490 redraw
491 let normattr = screenattr(1, 1)
492 diffthis
493
494 botright vert new
495 call setline(1, ['one', 'two', 'three.'])
496 diffthis
497
498 redraw
499 call assert_equal(normattr, screenattr(1, 1))
500 call assert_equal(normattr, screenattr(2, 1))
501 call assert_notequal(normattr, screenattr(3, 1))
502
503 diffoff!
504 %bwipe!
505 set diffexpr& diffopt&
506endfunc
507
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100508func Test_diffpatch()
509 " The patch program on MS-Windows may fail or hang.
510 if !executable('patch') || !has('unix')
511 return
512 endif
513 new
514 insert
515***************
516*** 1,3 ****
517 1
518! 2
519 3
520--- 1,4 ----
521 1
522! 2x
523 3
524+ 4
525.
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200526 saveas! Xpatch
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100527 bwipe!
528 new
529 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100530
Bram Moolenaara95ab322017-03-11 19:21:53 +0100531 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100532 call setline(1, ['1', '2', '3'])
533 if name != 'Xpatch'
534 call rename('Xpatch', name)
535 endif
536 exe 'diffpatch ' . escape(name, '$')
537 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
538 if name != 'Xpatch'
539 call rename(name, 'Xpatch')
540 endif
541 bwipe!
542 endfor
543
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100544 call delete('Xpatch')
545 bwipe!
546endfunc
547
548func Test_diff_too_many_buffers()
549 for i in range(1, 8)
550 exe "new Xtest" . i
551 diffthis
552 endfor
553 new Xtest9
554 call assert_fails('diffthis', 'E96:')
555 %bwipe!
556endfunc
557
558func Test_diff_nomodifiable()
559 new
560 call setline(1, [1, 2, 3, 4])
561 setl nomodifiable
562 diffthis
563 vnew
564 call setline(1, ['1x', 2, 3, 3, 4])
565 diffthis
566 call assert_fails('norm dp', 'E793:')
567 setl nomodifiable
568 call assert_fails('norm do', 'E21:')
569 %bwipe!
570endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100571
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200572func Test_diff_hlID()
573 new
574 call setline(1, [1, 2, 3])
575 diffthis
576 vnew
577 call setline(1, ['1x', 2, 'x', 3])
578 diffthis
579 redraw
580
581 call assert_equal(synIDattr(diff_hlID(-1, 1), "name"), "")
582
583 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
584 call assert_equal(synIDattr(diff_hlID(1, 2), "name"), "DiffText")
585 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
586 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "DiffAdd")
587 call assert_equal(synIDattr(diff_hlID(4, 1), "name"), "")
588
589 wincmd w
590 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
591 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
592 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
593
594 %bwipe!
595endfunc
596
597func Test_diff_filler()
598 new
599 call setline(1, [1, 2, 3, 'x', 4])
600 diffthis
601 vnew
602 call setline(1, [1, 2, 'y', 'y', 3, 4])
603 diffthis
604 redraw
605
606 call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'diff_filler(v:val)'))
607 wincmd w
608 call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
609
610 %bwipe!
611endfunc
612
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100613func Test_diff_lastline()
614 enew!
615 only!
616 call setline(1, ['This is a ', 'line with five ', 'rows'])
617 diffthis
618 botright vert new
619 call setline(1, ['This is', 'a line with ', 'four rows'])
620 diffthis
621 1
622 call feedkeys("Je a\<CR>", 'tx')
623 call feedkeys("Je a\<CR>", 'tx')
624 let w1lines = winline()
625 wincmd w
626 $
627 let w2lines = winline()
628 call assert_equal(w2lines, w1lines)
629 bwipe!
630 bwipe!
631endfunc