blob: 5a3a6beee473b8ae6e6a544f6579f36fe9bd3b68 [file] [log] [blame]
Bram Moolenaar42093c02016-07-30 16:16:54 +02001" Tests for diff mode
Bram Moolenaare828b762018-09-10 17:51:58 +02002source shared.vim
3source screendump.vim
Bram Moolenaar42093c02016-07-30 16:16:54 +02004
5func Test_diff_fold_sync()
6 enew!
Bram Moolenaare8fa05b2018-09-16 15:48:06 +02007 let g:update_count = 0
8 au DiffUpdated * let g:update_count += 1
9
Bram Moolenaar42093c02016-07-30 16:16:54 +020010 let l = range(50)
11 call setline(1, l)
12 diffthis
13 let winone = win_getid()
14 new
15 let l[25] = 'diff'
16 call setline(1, l)
17 diffthis
18 let wintwo = win_getid()
19 " line 15 is inside the closed fold
20 call assert_equal(19, foldclosedend(10))
21 call win_gotoid(winone)
22 call assert_equal(19, foldclosedend(10))
23 " open the fold
24 normal zv
25 call assert_equal(-1, foldclosedend(10))
26 " fold in other window must have opened too
27 call win_gotoid(wintwo)
28 call assert_equal(-1, foldclosedend(10))
29
30 " cursor position is in sync
31 normal 23G
32 call win_gotoid(winone)
33 call assert_equal(23, getcurpos()[1])
34
Bram Moolenaare8fa05b2018-09-16 15:48:06 +020035 call assert_equal(1, g:update_count)
36 au! DiffUpdated
37
Bram Moolenaar42093c02016-07-30 16:16:54 +020038 windo diffoff
39 close!
40 set nomodified
41endfunc
42
43func Test_vert_split()
Bram Moolenaare828b762018-09-10 17:51:58 +020044 set diffopt=filler
45 call Common_vert_split()
46 set diffopt&
47endfunc
48
49func Test_vert_split_internal()
50 set diffopt=internal,filler
51 call Common_vert_split()
52 set diffopt&
53endfunc
54
55func Common_vert_split()
Bram Moolenaar42093c02016-07-30 16:16:54 +020056 " Disable the title to avoid xterm keeping the wrong one.
57 set notitle noicon
58 new
59 let l = ['1 aa', '2 bb', '3 cc', '4 dd', '5 ee']
60 call setline(1, l)
61 w! Xtest
62 normal dd
63 $
64 put
65 normal kkrXoxxx
66 w! Xtest2
67 file Nop
68 normal ggoyyyjjjozzzz
69 set foldmethod=marker foldcolumn=4
70 call assert_equal(0, &diff)
71 call assert_equal('marker', &foldmethod)
72 call assert_equal(4, &foldcolumn)
73 call assert_equal(0, &scrollbind)
74 call assert_equal(0, &cursorbind)
75 call assert_equal(1, &wrap)
76
77 vert diffsplit Xtest
78 vert diffsplit Xtest2
79 call assert_equal(1, &diff)
80 call assert_equal('diff', &foldmethod)
81 call assert_equal(2, &foldcolumn)
82 call assert_equal(1, &scrollbind)
83 call assert_equal(1, &cursorbind)
84 call assert_equal(0, &wrap)
85
86 let diff_fdm = &fdm
87 let diff_fdc = &fdc
88 " repeat entering diff mode here to see if this saves the wrong settings
89 diffthis
90 " jump to second window for a moment to have filler line appear at start of
91 " first window
92 wincmd w
93 normal gg
94 wincmd p
95 normal gg
96 call assert_equal(2, winline())
97 normal j
98 call assert_equal(4, winline())
99 normal j
100 call assert_equal(5, winline())
101 normal j
102 call assert_equal(6, winline())
103 normal j
104 call assert_equal(8, winline())
105 normal j
106 call assert_equal(9, winline())
107
108 wincmd w
109 normal gg
110 call assert_equal(1, winline())
111 normal j
112 call assert_equal(2, winline())
113 normal j
114 call assert_equal(4, winline())
115 normal j
116 call assert_equal(5, winline())
117 normal j
118 call assert_equal(8, winline())
119
120 wincmd w
121 normal gg
122 call assert_equal(2, winline())
123 normal j
124 call assert_equal(3, winline())
125 normal j
126 call assert_equal(4, winline())
127 normal j
128 call assert_equal(5, winline())
129 normal j
130 call assert_equal(6, winline())
131 normal j
132 call assert_equal(7, winline())
133 normal j
134 call assert_equal(8, winline())
135
136 " Test diffoff
137 diffoff!
138 1wincmd 2
139 let &diff = 1
140 let &fdm = diff_fdm
141 let &fdc = diff_fdc
142 4wincmd w
143 diffoff!
144 1wincmd w
145 call assert_equal(0, &diff)
146 call assert_equal('marker', &foldmethod)
147 call assert_equal(4, &foldcolumn)
148 call assert_equal(0, &scrollbind)
149 call assert_equal(0, &cursorbind)
150 call assert_equal(1, &wrap)
151
152 wincmd w
153 call assert_equal(0, &diff)
154 call assert_equal('marker', &foldmethod)
155 call assert_equal(4, &foldcolumn)
156 call assert_equal(0, &scrollbind)
157 call assert_equal(0, &cursorbind)
158 call assert_equal(1, &wrap)
159
160 wincmd w
161 call assert_equal(0, &diff)
162 call assert_equal('marker', &foldmethod)
163 call assert_equal(4, &foldcolumn)
164 call assert_equal(0, &scrollbind)
165 call assert_equal(0, &cursorbind)
166 call assert_equal(1, &wrap)
167
Bram Moolenaar623cf882016-07-30 16:36:01 +0200168 call delete('Xtest')
169 call delete('Xtest2')
Bram Moolenaar42093c02016-07-30 16:16:54 +0200170 windo bw!
171endfunc
172
173func Test_filler_lines()
174 " Test that diffing shows correct filler lines
175 enew!
176 put =range(4,10)
177 1d _
178 vnew
179 put =range(1,10)
180 1d _
181 windo diffthis
182 wincmd h
183 call assert_equal(1, line('w0'))
184 unlet! diff_fdm diff_fdc
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200185 windo diffoff
186 bwipe!
187 enew!
188endfunc
Bram Moolenaar42093c02016-07-30 16:16:54 +0200189
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200190func Test_diffget_diffput()
191 enew!
192 let l = range(50)
193 call setline(1, l)
194 call assert_fails('diffget', 'E99:')
195 diffthis
196 call assert_fails('diffget', 'E100:')
197 new
198 let l[10] = 'one'
199 let l[20] = 'two'
200 let l[30] = 'three'
201 let l[40] = 'four'
202 call setline(1, l)
203 diffthis
204 call assert_equal('one', getline(11))
205 11diffget
206 call assert_equal('10', getline(11))
207 21diffput
208 wincmd w
209 call assert_equal('two', getline(21))
210 normal 31Gdo
211 call assert_equal('three', getline(31))
212 call assert_equal('40', getline(41))
213 normal 41Gdp
214 wincmd w
215 call assert_equal('40', getline(41))
216 new
217 diffthis
218 call assert_fails('diffget', 'E101:')
219
220 windo diffoff
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200221 %bwipe!
222endfunc
223
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +0200224" Test putting two changes from one buffer to another
225func Test_diffput_two()
226 new a
227 let win_a = win_getid()
228 call setline(1, range(1, 10))
229 diffthis
230 new b
231 let win_b = win_getid()
232 call setline(1, range(1, 10))
233 8del
234 5del
235 diffthis
236 call win_gotoid(win_a)
237 %diffput
238 call win_gotoid(win_b)
239 call assert_equal(map(range(1, 10), 'string(v:val)'), getline(1, '$'))
240 bwipe! a
241 bwipe! b
242endfunc
243
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200244func Test_dp_do_buffer()
245 e! one
246 let bn1=bufnr('%')
247 let l = range(60)
248 call setline(1, l)
249 diffthis
250
251 new two
252 let l[10] = 'one'
253 let l[20] = 'two'
254 let l[30] = 'three'
255 let l[40] = 'four'
256 let l[50] = 'five'
257 call setline(1, l)
258 diffthis
259
260 " dp and do with invalid buffer number.
261 11
262 call assert_fails('norm 99999dp', 'E102:')
263 call assert_fails('norm 99999do', 'E102:')
264 call assert_fails('diffput non_existing_buffer', 'E94:')
265 call assert_fails('diffget non_existing_buffer', 'E94:')
266
267 " dp and do with valid buffer number.
268 call assert_equal('one', getline('.'))
269 exe 'norm ' . bn1 . 'do'
270 call assert_equal('10', getline('.'))
271 21
272 call assert_equal('two', getline('.'))
273 diffget one
274 call assert_equal('20', getline('.'))
275
276 31
277 exe 'norm ' . bn1 . 'dp'
278 41
279 diffput one
280 wincmd w
281 31
282 call assert_equal('three', getline('.'))
283 41
284 call assert_equal('four', getline('.'))
285
286 " dp and do with buffer number which is not in diff mode.
287 new not_in_diff_mode
288 let bn3=bufnr('%')
289 wincmd w
290 51
291 call assert_fails('exe "norm" . bn3 . "dp"', 'E103:')
292 call assert_fails('exe "norm" . bn3 . "do"', 'E103:')
293 call assert_fails('diffput not_in_diff_mode', 'E94:')
294 call assert_fails('diffget not_in_diff_mode', 'E94:')
295
296 windo diffoff
297 %bwipe!
Bram Moolenaar42093c02016-07-30 16:16:54 +0200298endfunc
Bram Moolenaare67d5462016-08-27 22:40:42 +0200299
Bram Moolenaardf77cef2018-10-07 17:46:42 +0200300func Test_do_lastline()
301 e! one
302 call setline(1, ['1','2','3','4','5','6'])
303 diffthis
304
305 new two
306 call setline(1, ['2','4','5'])
307 diffthis
308
309 1
310 norm dp]c
311 norm dp]c
312 wincmd w
313 call assert_equal(4, line('$'))
314 norm G
315 norm do
316 call assert_equal(3, line('$'))
317
318 windo diffoff
319 %bwipe!
320endfunc
321
Bram Moolenaare67d5462016-08-27 22:40:42 +0200322func Test_diffoff()
323 enew!
324 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200325 redraw
Bram Moolenaare67d5462016-08-27 22:40:42 +0200326 let normattr = screenattr(1, 1)
327 diffthis
328 botright vert new
329 call setline(1, ['One', '', 'Two', 'Three'])
330 diffthis
331 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100332 call assert_notequal(normattr, screenattr(1, 1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200333 diffoff!
334 redraw
335 call assert_equal(normattr, screenattr(1, 1))
336 bwipe!
337 bwipe!
338endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200339
Bram Moolenaare828b762018-09-10 17:51:58 +0200340func Common_icase_test()
341 edit one
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200342 call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200343 redraw
344 let normattr = screenattr(1, 1)
345 diffthis
346
347 botright vert new two
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200348 call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VE'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200349 diffthis
350
351 redraw
352 call assert_equal(normattr, screenattr(1, 1))
353 call assert_equal(normattr, screenattr(2, 1))
354 call assert_notequal(normattr, screenattr(3, 1))
355 call assert_equal(normattr, screenattr(4, 1))
356
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200357 let dtextattr = screenattr(5, 3)
358 call assert_notequal(dtextattr, screenattr(5, 1))
359 call assert_notequal(dtextattr, screenattr(5, 5))
360
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200361 diffoff!
362 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200363endfunc
364
365func Test_diffopt_icase()
366 set diffopt=icase,foldcolumn:0
367 call Common_icase_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200368 set diffopt&
369endfunc
370
Bram Moolenaare828b762018-09-10 17:51:58 +0200371func Test_diffopt_icase_internal()
372 set diffopt=icase,foldcolumn:0,internal
373 call Common_icase_test()
374 set diffopt&
375endfunc
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200376
Bram Moolenaare828b762018-09-10 17:51:58 +0200377func Common_iwhite_test()
378 edit one
379 " Difference in trailing spaces and amount of spaces should be ignored,
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200380 " but not other space differences.
Bram Moolenaare828b762018-09-10 17:51:58 +0200381 call setline(1, ["One \t", 'Two', 'Three', 'one two', 'one two', 'Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200382 redraw
383 let normattr = screenattr(1, 1)
384 diffthis
385
386 botright vert new two
Bram Moolenaare828b762018-09-10 17:51:58 +0200387 call setline(1, ["One\t ", "Two\t ", 'Three', 'one two', 'onetwo', ' Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200388 diffthis
389
390 redraw
391 call assert_equal(normattr, screenattr(1, 1))
392 call assert_equal(normattr, screenattr(2, 1))
393 call assert_equal(normattr, screenattr(3, 1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200394 call assert_equal(normattr, screenattr(4, 1))
395 call assert_notequal(normattr, screenattr(5, 1))
396 call assert_notequal(normattr, screenattr(6, 1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200397
398 diffoff!
399 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200400endfunc
401
402func Test_diffopt_iwhite()
403 set diffopt=iwhite,foldcolumn:0
404 call Common_iwhite_test()
405 set diffopt&
406endfunc
407
408func Test_diffopt_iwhite_internal()
409 set diffopt=internal,iwhite,foldcolumn:0
410 call Common_iwhite_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200411 set diffopt&
412endfunc
413
414func Test_diffopt_context()
415 enew!
416 call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
417 diffthis
418 new
419 call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
420 diffthis
421
422 set diffopt=context:2
423 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200424 set diffopt=internal,context:2
425 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
426
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200427 set diffopt=context:1
428 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200429 set diffopt=internal,context:1
430 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200431
432 diffoff!
433 %bwipe!
434 set diffopt&
435endfunc
436
437func Test_diffopt_horizontal()
Bram Moolenaare828b762018-09-10 17:51:58 +0200438 set diffopt=internal,horizontal
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200439 diffsplit
440
441 call assert_equal(&columns, winwidth(1))
442 call assert_equal(&columns, winwidth(2))
443 call assert_equal(&lines, winheight(1) + winheight(2) + 3)
444 call assert_inrange(0, 1, winheight(1) - winheight(2))
445
446 set diffopt&
447 diffoff!
448 %bwipe
449endfunc
450
451func Test_diffopt_vertical()
Bram Moolenaare828b762018-09-10 17:51:58 +0200452 set diffopt=internal,vertical
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200453 diffsplit
454
455 call assert_equal(&lines - 2, winheight(1))
456 call assert_equal(&lines - 2, winheight(2))
457 call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
458 call assert_inrange(0, 1, winwidth(1) - winwidth(2))
459
460 set diffopt&
461 diffoff!
462 %bwipe
463endfunc
464
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100465func Test_diffopt_hiddenoff()
Bram Moolenaare828b762018-09-10 17:51:58 +0200466 set diffopt=internal,filler,foldcolumn:0,hiddenoff
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100467 e! one
468 call setline(1, ['Two', 'Three'])
469 redraw
470 let normattr = screenattr(1, 1)
471 diffthis
472 botright vert new two
473 call setline(1, ['One', 'Four'])
474 diffthis
475 redraw
476 call assert_notequal(normattr, screenattr(1, 1))
477 set hidden
478 close
479 redraw
480 " should not diffing with hidden buffer two while 'hiddenoff' is enabled
481 call assert_equal(normattr, screenattr(1, 1))
482
483 bwipe!
484 bwipe!
485 set hidden& diffopt&
486endfunc
487
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100488func Test_diffoff_hidden()
Bram Moolenaare828b762018-09-10 17:51:58 +0200489 set diffopt=internal,filler,foldcolumn:0
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100490 e! one
491 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200492 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100493 let normattr = screenattr(1, 1)
494 diffthis
495 botright vert new two
496 call setline(1, ['One', 'Four'])
497 diffthis
498 redraw
499 call assert_notequal(normattr, screenattr(1, 1))
500 set hidden
501 close
502 redraw
503 " diffing with hidden buffer two
504 call assert_notequal(normattr, screenattr(1, 1))
505 diffoff
506 redraw
507 call assert_equal(normattr, screenattr(1, 1))
508 diffthis
509 redraw
510 " still diffing with hidden buffer two
511 call assert_notequal(normattr, screenattr(1, 1))
512 diffoff!
513 redraw
514 call assert_equal(normattr, screenattr(1, 1))
515 diffthis
516 redraw
517 " no longer diffing with hidden buffer two
518 call assert_equal(normattr, screenattr(1, 1))
519
520 bwipe!
521 bwipe!
522 set hidden& diffopt&
523endfunc
524
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200525func Test_setting_cursor()
526 new Xtest1
527 put =range(1,90)
528 wq
529 new Xtest2
530 put =range(1,100)
531 wq
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200532
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200533 tabe Xtest2
534 $
535 diffsp Xtest1
536 tabclose
537
538 call delete('Xtest1')
539 call delete('Xtest2')
540endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100541
542func Test_diff_move_to()
543 new
544 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
545 diffthis
546 vnew
547 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
548 diffthis
549 norm ]c
550 call assert_equal(2, line('.'))
551 norm 3]c
552 call assert_equal(9, line('.'))
553 norm 10]c
554 call assert_equal(11, line('.'))
555 norm [c
556 call assert_equal(9, line('.'))
557 norm 2[c
558 call assert_equal(5, line('.'))
559 norm 10[c
560 call assert_equal(2, line('.'))
561 %bwipe!
562endfunc
563
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200564func Test_diffexpr()
565 if !executable('diff')
566 return
567 endif
568
569 func DiffExpr()
Bram Moolenaar3b8defd2018-09-13 13:03:11 +0200570 " Prepent some text to check diff type detection
571 call writefile(['warning', ' message'], v:fname_out)
572 silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>>' . v:fname_out
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200573 endfunc
574 set diffexpr=DiffExpr()
575 set diffopt=foldcolumn:0
576
577 enew!
578 call setline(1, ['one', 'two', 'three'])
579 redraw
580 let normattr = screenattr(1, 1)
581 diffthis
582
583 botright vert new
584 call setline(1, ['one', 'two', 'three.'])
585 diffthis
586
587 redraw
588 call assert_equal(normattr, screenattr(1, 1))
589 call assert_equal(normattr, screenattr(2, 1))
590 call assert_notequal(normattr, screenattr(3, 1))
591
592 diffoff!
593 %bwipe!
594 set diffexpr& diffopt&
595endfunc
596
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100597func Test_diffpatch()
598 " The patch program on MS-Windows may fail or hang.
599 if !executable('patch') || !has('unix')
600 return
601 endif
602 new
603 insert
604***************
605*** 1,3 ****
606 1
607! 2
608 3
609--- 1,4 ----
610 1
611! 2x
612 3
613+ 4
614.
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200615 saveas! Xpatch
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100616 bwipe!
617 new
618 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100619
Bram Moolenaara95ab322017-03-11 19:21:53 +0100620 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100621 call setline(1, ['1', '2', '3'])
622 if name != 'Xpatch'
623 call rename('Xpatch', name)
624 endif
625 exe 'diffpatch ' . escape(name, '$')
626 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
627 if name != 'Xpatch'
628 call rename(name, 'Xpatch')
629 endif
630 bwipe!
631 endfor
632
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100633 call delete('Xpatch')
634 bwipe!
635endfunc
636
637func Test_diff_too_many_buffers()
638 for i in range(1, 8)
639 exe "new Xtest" . i
640 diffthis
641 endfor
642 new Xtest9
643 call assert_fails('diffthis', 'E96:')
644 %bwipe!
645endfunc
646
647func Test_diff_nomodifiable()
648 new
649 call setline(1, [1, 2, 3, 4])
650 setl nomodifiable
651 diffthis
652 vnew
653 call setline(1, ['1x', 2, 3, 3, 4])
654 diffthis
655 call assert_fails('norm dp', 'E793:')
656 setl nomodifiable
657 call assert_fails('norm do', 'E21:')
658 %bwipe!
659endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100660
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200661func Test_diff_hlID()
662 new
663 call setline(1, [1, 2, 3])
664 diffthis
665 vnew
666 call setline(1, ['1x', 2, 'x', 3])
667 diffthis
668 redraw
669
670 call assert_equal(synIDattr(diff_hlID(-1, 1), "name"), "")
671
672 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
673 call assert_equal(synIDattr(diff_hlID(1, 2), "name"), "DiffText")
674 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
675 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "DiffAdd")
676 call assert_equal(synIDattr(diff_hlID(4, 1), "name"), "")
677
678 wincmd w
679 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
680 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
681 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
682
683 %bwipe!
684endfunc
685
686func Test_diff_filler()
687 new
688 call setline(1, [1, 2, 3, 'x', 4])
689 diffthis
690 vnew
691 call setline(1, [1, 2, 'y', 'y', 3, 4])
692 diffthis
693 redraw
694
695 call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'diff_filler(v:val)'))
696 wincmd w
697 call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
698
699 %bwipe!
700endfunc
701
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100702func Test_diff_lastline()
703 enew!
704 only!
705 call setline(1, ['This is a ', 'line with five ', 'rows'])
706 diffthis
707 botright vert new
708 call setline(1, ['This is', 'a line with ', 'four rows'])
709 diffthis
710 1
711 call feedkeys("Je a\<CR>", 'tx')
712 call feedkeys("Je a\<CR>", 'tx')
713 let w1lines = winline()
714 wincmd w
715 $
716 let w2lines = winline()
717 call assert_equal(w2lines, w1lines)
718 bwipe!
719 bwipe!
720endfunc
Bram Moolenaare828b762018-09-10 17:51:58 +0200721
Bram Moolenaar785fc652018-09-15 19:17:38 +0200722func WriteDiffFiles(buf, list1, list2)
Bram Moolenaare828b762018-09-10 17:51:58 +0200723 call writefile(a:list1, 'Xfile1')
724 call writefile(a:list2, 'Xfile2')
Bram Moolenaar785fc652018-09-15 19:17:38 +0200725 if a:buf
726 call term_sendkeys(a:buf, ":checktime\<CR>")
727 endif
Bram Moolenaare828b762018-09-10 17:51:58 +0200728endfunc
729
Bram Moolenaar785fc652018-09-15 19:17:38 +0200730" Verify a screendump with both the internal and external diff.
Bram Moolenaare828b762018-09-10 17:51:58 +0200731func VerifyBoth(buf, dumpfile, extra)
Bram Moolenaare828b762018-09-10 17:51:58 +0200732 " trailing : for leaving the cursor on the command line
Bram Moolenaar785fc652018-09-15 19:17:38 +0200733 for cmd in [":set diffopt=filler" . a:extra . "\<CR>:", ":set diffopt+=internal\<CR>:"]
Bram Moolenaare828b762018-09-10 17:51:58 +0200734 call term_sendkeys(a:buf, cmd)
735 if VerifyScreenDump(a:buf, a:dumpfile, {}, cmd =~ 'internal' ? 'internal' : 'external')
736 break " don't let the next iteration overwrite the "failed" file.
737 endif
738 endfor
739endfunc
740
Bram Moolenaar785fc652018-09-15 19:17:38 +0200741" Verify a screendump with the internal diff only.
742func VerifyInternal(buf, dumpfile, extra)
743 call term_sendkeys(a:buf, ":diffupdate!\<CR>")
744 " trailing : for leaving the cursor on the command line
745 call term_sendkeys(a:buf, ":set diffopt=internal,filler" . a:extra . "\<CR>:")
746 call VerifyScreenDump(a:buf, a:dumpfile, {})
747endfunc
748
Bram Moolenaare828b762018-09-10 17:51:58 +0200749func Test_diff_screen()
750 if !CanRunVimInTerminal() || !has('menu')
751 return
752 endif
753 " clean up already existing swap files, just in case
754 call delete('.Xfile1.swp')
755 call delete('.Xfile2.swp')
756
757 " Test 1: Add a line in beginning of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200758 call WriteDiffFiles(0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Bram Moolenaare828b762018-09-10 17:51:58 +0200759 let buf = RunVimInTerminal('-d Xfile1 Xfile2', {})
760 " Set autoread mode, ,so that Vim won't complain once we re-write the test
761 " files
Bram Moolenaar785fc652018-09-15 19:17:38 +0200762 call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
Bram Moolenaare828b762018-09-10 17:51:58 +0200763
764 call VerifyBoth(buf, 'Test_diff_01', '')
765
766 " Test 2: Add a line in beginning of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200767 call WriteDiffFiles(buf, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Bram Moolenaare828b762018-09-10 17:51:58 +0200768 call VerifyBoth(buf, 'Test_diff_02', '')
769
770 " Test 3: Add a line at the end of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200771 call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Bram Moolenaare828b762018-09-10 17:51:58 +0200772 call VerifyBoth(buf, 'Test_diff_03', '')
773
774 " Test 4: Add a line at the end of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200775 call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Bram Moolenaare828b762018-09-10 17:51:58 +0200776 call VerifyBoth(buf, 'Test_diff_04', '')
777
778 " Test 5: Add a line in the middle of file 2, remove on at the end of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200779 call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10])
Bram Moolenaare828b762018-09-10 17:51:58 +0200780 call VerifyBoth(buf, 'Test_diff_05', '')
781
782 " Test 6: Add a line in the middle of file 1, remove on at the end of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200783 call WriteDiffFiles(buf, [1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
Bram Moolenaare828b762018-09-10 17:51:58 +0200784 call VerifyBoth(buf, 'Test_diff_06', '')
785
Bram Moolenaarb9ddda62019-02-19 23:00:50 +0100786 " Variants on test 6 with different context settings
787 call term_sendkeys(buf, ":set diffopt+=context:2\<cr>")
788 call VerifyScreenDump(buf, 'Test_diff_06.2', {})
789 call term_sendkeys(buf, ":set diffopt-=context:2\<cr>")
790 call term_sendkeys(buf, ":set diffopt+=context:1\<cr>")
791 call VerifyScreenDump(buf, 'Test_diff_06.1', {})
792 call term_sendkeys(buf, ":set diffopt-=context:1\<cr>")
793 call term_sendkeys(buf, ":set diffopt+=context:0\<cr>")
794 call VerifyScreenDump(buf, 'Test_diff_06.0', {})
795 call term_sendkeys(buf, ":set diffopt-=context:0\<cr>")
796
Bram Moolenaare828b762018-09-10 17:51:58 +0200797 " Test 7 - 9: Test normal/patience/histogram diff algorithm
Bram Moolenaar785fc652018-09-15 19:17:38 +0200798 call WriteDiffFiles(buf, ['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
Bram Moolenaare828b762018-09-10 17:51:58 +0200799 \ ' int i;', ' for(i = 0; i < 10; i++)', ' {', ' printf("Your answer is: ");',
800 \ ' printf("%d\n", foo);', ' }', '}', '', 'int fact(int n)', '{', ' if(n > 1)', ' {',
801 \ ' return fact(n-1) * n;', ' }', ' return 1;', '}', '', 'int main(int argc, char **argv)',
802 \ '{', ' frobnitz(fact(10));', '}'],
803 \ ['#include <stdio.h>', '', 'int fib(int n)', '{', ' if(n > 2)', ' {',
804 \ ' return fib(n-1) + fib(n-2);', ' }', ' return 1;', '}', '', '// Frobs foo heartily',
805 \ 'int frobnitz(int foo)', '{', ' int i;', ' for(i = 0; i < 10; i++)', ' {',
806 \ ' printf("%d\n", foo);', ' }', '}', '',
807 \ 'int main(int argc, char **argv)', '{', ' frobnitz(fib(10));', '}'])
808 call term_sendkeys(buf, ":diffupdate!\<cr>")
809 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
810 call VerifyScreenDump(buf, 'Test_diff_07', {})
811
812 call term_sendkeys(buf, ":set diffopt+=algorithm:patience\<cr>")
813 call VerifyScreenDump(buf, 'Test_diff_08', {})
814
815 call term_sendkeys(buf, ":set diffopt+=algorithm:histogram\<cr>")
816 call VerifyScreenDump(buf, 'Test_diff_09', {})
817
818 " Test 10-11: normal/indent-heuristic
819 call term_sendkeys(buf, ":set diffopt&vim\<cr>")
Bram Moolenaar785fc652018-09-15 19:17:38 +0200820 call WriteDiffFiles(buf, ['', ' def finalize(values)', '', ' values.each do |v|', ' v.finalize', ' end'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200821 \ ['', ' def finalize(values)', '', ' values.each do |v|', ' v.prepare', ' end', '',
822 \ ' values.each do |v|', ' v.finalize', ' end'])
823 call term_sendkeys(buf, ":diffupdate!\<cr>")
824 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
825 call VerifyScreenDump(buf, 'Test_diff_10', {})
826
Bram Moolenaarb6fc7282018-12-04 22:24:16 +0100827 " Leave trailing : at commandline!
828 call term_sendkeys(buf, ":set diffopt+=indent-heuristic\<cr>:\<cr>")
829 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'one')
830 " shouldn't matter, if indent-algorithm comes before or after the algorithm
831 call term_sendkeys(buf, ":set diffopt&\<cr>")
832 call term_sendkeys(buf, ":set diffopt+=indent-heuristic,algorithm:patience\<cr>:\<cr>")
833 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'two')
834 call term_sendkeys(buf, ":set diffopt&\<cr>")
835 call term_sendkeys(buf, ":set diffopt+=algorithm:patience,indent-heuristic\<cr>:\<cr>")
836 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'three')
Bram Moolenaare828b762018-09-10 17:51:58 +0200837
838 " Test 12: diff the same file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200839 call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Bram Moolenaare828b762018-09-10 17:51:58 +0200840 call VerifyBoth(buf, 'Test_diff_12', '')
841
842 " Test 13: diff an empty file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200843 call WriteDiffFiles(buf, [], [])
Bram Moolenaare828b762018-09-10 17:51:58 +0200844 call VerifyBoth(buf, 'Test_diff_13', '')
845
846 " Test 14: test diffopt+=icase
Bram Moolenaar785fc652018-09-15 19:17:38 +0200847 call WriteDiffFiles(buf, ['a', 'b', 'cd'], ['A', 'b', 'cDe'])
Bram Moolenaare828b762018-09-10 17:51:58 +0200848 call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
849
850 " Test 15-16: test diffopt+=iwhite
Bram Moolenaar785fc652018-09-15 19:17:38 +0200851 call WriteDiffFiles(buf, ['int main()', '{', ' printf("Hello, World!");', ' return 0;', '}'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200852 \ ['int main()', '{', ' if (0)', ' {', ' printf("Hello, World!");', ' return 0;', ' }', '}'])
853 call term_sendkeys(buf, ":diffupdate!\<cr>")
854 call term_sendkeys(buf, ":set diffopt&vim diffopt+=filler diffopt+=iwhite\<cr>")
855 call VerifyScreenDump(buf, 'Test_diff_15', {})
856 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
857 call VerifyScreenDump(buf, 'Test_diff_16', {})
858
Bram Moolenaar785fc652018-09-15 19:17:38 +0200859 " Test 17: test diffopt+=iblank
860 call WriteDiffFiles(buf, ['a', ' ', 'cd', 'ef', 'xxx'], ['a', 'cd', '', 'ef', 'yyy'])
861 call VerifyInternal(buf, 'Test_diff_17', " diffopt+=iblank")
862
863 " Test 18: test diffopt+=iblank,iwhite / iwhiteall / iwhiteeol
864 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhite")
865 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteall")
866 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteeol")
867
868 " Test 19: test diffopt+=iwhiteeol
869 call WriteDiffFiles(buf, ['a ', 'x', 'cd', 'ef', 'xx xx', 'foo', 'bar'], ['a', 'x', 'c d', ' ef', 'xx xx', 'foo', '', 'bar'])
870 call VerifyInternal(buf, 'Test_diff_19', " diffopt+=iwhiteeol")
871
872 " Test 19: test diffopt+=iwhiteall
873 call VerifyInternal(buf, 'Test_diff_20', " diffopt+=iwhiteall")
874
Bram Moolenaare828b762018-09-10 17:51:58 +0200875 " clean up
876 call StopVimInTerminal(buf)
877 call delete('Xfile1')
878 call delete('Xfile2')
879endfunc
880
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200881func Test_diff_with_cursorline()
882 if !CanRunVimInTerminal()
883 return
884 endif
885
886 call writefile([
887 \ 'hi CursorLine ctermbg=red ctermfg=white',
888 \ 'set cursorline',
889 \ 'call setline(1, ["foo","foo","foo","bar"])',
890 \ 'vnew',
891 \ 'call setline(1, ["bee","foo","foo","baz"])',
892 \ 'windo diffthis',
893 \ '2wincmd w',
894 \ ], 'Xtest_diff_cursorline')
895 let buf = RunVimInTerminal('-S Xtest_diff_cursorline', {})
896
897 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_01', {})
898 call term_sendkeys(buf, "j")
899 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_02', {})
900 call term_sendkeys(buf, "j")
901 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_03', {})
902
903 " clean up
904 call StopVimInTerminal(buf)
905 call delete('Xtest_diff_cursorline')
906endfunc
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +0100907
908func Test_diff_of_diff()
909 if !CanRunVimInTerminal()
910 return
911 endif
912
913 call writefile([
914 \ 'call setline(1, ["aa","bb","cc","@@ -3,2 +5,7 @@","dd","ee","ff"])',
915 \ 'vnew',
916 \ 'call setline(1, ["aa","bb","cc"])',
917 \ 'windo diffthis',
918 \ ], 'Xtest_diff_diff')
919 let buf = RunVimInTerminal('-S Xtest_diff_diff', {})
920
921 call VerifyScreenDump(buf, 'Test_diff_of_diff_01', {})
922
923 " clean up
924 call StopVimInTerminal(buf)
925 call delete('Xtest_diff_diff')
926endfunc