blob: e1da19fefe3a22fa7bc1274910ef05162b146c8c [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
224func Test_dp_do_buffer()
225 e! one
226 let bn1=bufnr('%')
227 let l = range(60)
228 call setline(1, l)
229 diffthis
230
231 new two
232 let l[10] = 'one'
233 let l[20] = 'two'
234 let l[30] = 'three'
235 let l[40] = 'four'
236 let l[50] = 'five'
237 call setline(1, l)
238 diffthis
239
240 " dp and do with invalid buffer number.
241 11
242 call assert_fails('norm 99999dp', 'E102:')
243 call assert_fails('norm 99999do', 'E102:')
244 call assert_fails('diffput non_existing_buffer', 'E94:')
245 call assert_fails('diffget non_existing_buffer', 'E94:')
246
247 " dp and do with valid buffer number.
248 call assert_equal('one', getline('.'))
249 exe 'norm ' . bn1 . 'do'
250 call assert_equal('10', getline('.'))
251 21
252 call assert_equal('two', getline('.'))
253 diffget one
254 call assert_equal('20', getline('.'))
255
256 31
257 exe 'norm ' . bn1 . 'dp'
258 41
259 diffput one
260 wincmd w
261 31
262 call assert_equal('three', getline('.'))
263 41
264 call assert_equal('four', getline('.'))
265
266 " dp and do with buffer number which is not in diff mode.
267 new not_in_diff_mode
268 let bn3=bufnr('%')
269 wincmd w
270 51
271 call assert_fails('exe "norm" . bn3 . "dp"', 'E103:')
272 call assert_fails('exe "norm" . bn3 . "do"', 'E103:')
273 call assert_fails('diffput not_in_diff_mode', 'E94:')
274 call assert_fails('diffget not_in_diff_mode', 'E94:')
275
276 windo diffoff
277 %bwipe!
Bram Moolenaar42093c02016-07-30 16:16:54 +0200278endfunc
Bram Moolenaare67d5462016-08-27 22:40:42 +0200279
Bram Moolenaardf77cef2018-10-07 17:46:42 +0200280func Test_do_lastline()
281 e! one
282 call setline(1, ['1','2','3','4','5','6'])
283 diffthis
284
285 new two
286 call setline(1, ['2','4','5'])
287 diffthis
288
289 1
290 norm dp]c
291 norm dp]c
292 wincmd w
293 call assert_equal(4, line('$'))
294 norm G
295 norm do
296 call assert_equal(3, line('$'))
297
298 windo diffoff
299 %bwipe!
300endfunc
301
Bram Moolenaare67d5462016-08-27 22:40:42 +0200302func Test_diffoff()
303 enew!
304 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200305 redraw
Bram Moolenaare67d5462016-08-27 22:40:42 +0200306 let normattr = screenattr(1, 1)
307 diffthis
308 botright vert new
309 call setline(1, ['One', '', 'Two', 'Three'])
310 diffthis
311 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100312 call assert_notequal(normattr, screenattr(1, 1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200313 diffoff!
314 redraw
315 call assert_equal(normattr, screenattr(1, 1))
316 bwipe!
317 bwipe!
318endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200319
Bram Moolenaare828b762018-09-10 17:51:58 +0200320func Common_icase_test()
321 edit one
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200322 call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200323 redraw
324 let normattr = screenattr(1, 1)
325 diffthis
326
327 botright vert new two
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200328 call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VE'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200329 diffthis
330
331 redraw
332 call assert_equal(normattr, screenattr(1, 1))
333 call assert_equal(normattr, screenattr(2, 1))
334 call assert_notequal(normattr, screenattr(3, 1))
335 call assert_equal(normattr, screenattr(4, 1))
336
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200337 let dtextattr = screenattr(5, 3)
338 call assert_notequal(dtextattr, screenattr(5, 1))
339 call assert_notequal(dtextattr, screenattr(5, 5))
340
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200341 diffoff!
342 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200343endfunc
344
345func Test_diffopt_icase()
346 set diffopt=icase,foldcolumn:0
347 call Common_icase_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200348 set diffopt&
349endfunc
350
Bram Moolenaare828b762018-09-10 17:51:58 +0200351func Test_diffopt_icase_internal()
352 set diffopt=icase,foldcolumn:0,internal
353 call Common_icase_test()
354 set diffopt&
355endfunc
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200356
Bram Moolenaare828b762018-09-10 17:51:58 +0200357func Common_iwhite_test()
358 edit one
359 " Difference in trailing spaces and amount of spaces should be ignored,
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200360 " but not other space differences.
Bram Moolenaare828b762018-09-10 17:51:58 +0200361 call setline(1, ["One \t", 'Two', 'Three', 'one two', 'one two', 'Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200362 redraw
363 let normattr = screenattr(1, 1)
364 diffthis
365
366 botright vert new two
Bram Moolenaare828b762018-09-10 17:51:58 +0200367 call setline(1, ["One\t ", "Two\t ", 'Three', 'one two', 'onetwo', ' Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200368 diffthis
369
370 redraw
371 call assert_equal(normattr, screenattr(1, 1))
372 call assert_equal(normattr, screenattr(2, 1))
373 call assert_equal(normattr, screenattr(3, 1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200374 call assert_equal(normattr, screenattr(4, 1))
375 call assert_notequal(normattr, screenattr(5, 1))
376 call assert_notequal(normattr, screenattr(6, 1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200377
378 diffoff!
379 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200380endfunc
381
382func Test_diffopt_iwhite()
383 set diffopt=iwhite,foldcolumn:0
384 call Common_iwhite_test()
385 set diffopt&
386endfunc
387
388func Test_diffopt_iwhite_internal()
389 set diffopt=internal,iwhite,foldcolumn:0
390 call Common_iwhite_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200391 set diffopt&
392endfunc
393
394func Test_diffopt_context()
395 enew!
396 call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
397 diffthis
398 new
399 call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
400 diffthis
401
402 set diffopt=context:2
403 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200404 set diffopt=internal,context:2
405 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
406
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200407 set diffopt=context:1
408 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200409 set diffopt=internal,context:1
410 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200411
412 diffoff!
413 %bwipe!
414 set diffopt&
415endfunc
416
417func Test_diffopt_horizontal()
Bram Moolenaare828b762018-09-10 17:51:58 +0200418 set diffopt=internal,horizontal
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200419 diffsplit
420
421 call assert_equal(&columns, winwidth(1))
422 call assert_equal(&columns, winwidth(2))
423 call assert_equal(&lines, winheight(1) + winheight(2) + 3)
424 call assert_inrange(0, 1, winheight(1) - winheight(2))
425
426 set diffopt&
427 diffoff!
428 %bwipe
429endfunc
430
431func Test_diffopt_vertical()
Bram Moolenaare828b762018-09-10 17:51:58 +0200432 set diffopt=internal,vertical
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200433 diffsplit
434
435 call assert_equal(&lines - 2, winheight(1))
436 call assert_equal(&lines - 2, winheight(2))
437 call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
438 call assert_inrange(0, 1, winwidth(1) - winwidth(2))
439
440 set diffopt&
441 diffoff!
442 %bwipe
443endfunc
444
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100445func Test_diffopt_hiddenoff()
Bram Moolenaare828b762018-09-10 17:51:58 +0200446 set diffopt=internal,filler,foldcolumn:0,hiddenoff
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100447 e! one
448 call setline(1, ['Two', 'Three'])
449 redraw
450 let normattr = screenattr(1, 1)
451 diffthis
452 botright vert new two
453 call setline(1, ['One', 'Four'])
454 diffthis
455 redraw
456 call assert_notequal(normattr, screenattr(1, 1))
457 set hidden
458 close
459 redraw
460 " should not diffing with hidden buffer two while 'hiddenoff' is enabled
461 call assert_equal(normattr, screenattr(1, 1))
462
463 bwipe!
464 bwipe!
465 set hidden& diffopt&
466endfunc
467
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100468func Test_diffoff_hidden()
Bram Moolenaare828b762018-09-10 17:51:58 +0200469 set diffopt=internal,filler,foldcolumn:0
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100470 e! one
471 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200472 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100473 let normattr = screenattr(1, 1)
474 diffthis
475 botright vert new two
476 call setline(1, ['One', 'Four'])
477 diffthis
478 redraw
479 call assert_notequal(normattr, screenattr(1, 1))
480 set hidden
481 close
482 redraw
483 " diffing with hidden buffer two
484 call assert_notequal(normattr, screenattr(1, 1))
485 diffoff
486 redraw
487 call assert_equal(normattr, screenattr(1, 1))
488 diffthis
489 redraw
490 " still diffing with hidden buffer two
491 call assert_notequal(normattr, screenattr(1, 1))
492 diffoff!
493 redraw
494 call assert_equal(normattr, screenattr(1, 1))
495 diffthis
496 redraw
497 " no longer diffing with hidden buffer two
498 call assert_equal(normattr, screenattr(1, 1))
499
500 bwipe!
501 bwipe!
502 set hidden& diffopt&
503endfunc
504
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200505func Test_setting_cursor()
506 new Xtest1
507 put =range(1,90)
508 wq
509 new Xtest2
510 put =range(1,100)
511 wq
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200512
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200513 tabe Xtest2
514 $
515 diffsp Xtest1
516 tabclose
517
518 call delete('Xtest1')
519 call delete('Xtest2')
520endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100521
522func Test_diff_move_to()
523 new
524 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
525 diffthis
526 vnew
527 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
528 diffthis
529 norm ]c
530 call assert_equal(2, line('.'))
531 norm 3]c
532 call assert_equal(9, line('.'))
533 norm 10]c
534 call assert_equal(11, line('.'))
535 norm [c
536 call assert_equal(9, line('.'))
537 norm 2[c
538 call assert_equal(5, line('.'))
539 norm 10[c
540 call assert_equal(2, line('.'))
541 %bwipe!
542endfunc
543
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200544func Test_diffexpr()
545 if !executable('diff')
546 return
547 endif
548
549 func DiffExpr()
Bram Moolenaar3b8defd2018-09-13 13:03:11 +0200550 " Prepent some text to check diff type detection
551 call writefile(['warning', ' message'], v:fname_out)
552 silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>>' . v:fname_out
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200553 endfunc
554 set diffexpr=DiffExpr()
555 set diffopt=foldcolumn:0
556
557 enew!
558 call setline(1, ['one', 'two', 'three'])
559 redraw
560 let normattr = screenattr(1, 1)
561 diffthis
562
563 botright vert new
564 call setline(1, ['one', 'two', 'three.'])
565 diffthis
566
567 redraw
568 call assert_equal(normattr, screenattr(1, 1))
569 call assert_equal(normattr, screenattr(2, 1))
570 call assert_notequal(normattr, screenattr(3, 1))
571
572 diffoff!
573 %bwipe!
574 set diffexpr& diffopt&
575endfunc
576
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100577func Test_diffpatch()
578 " The patch program on MS-Windows may fail or hang.
579 if !executable('patch') || !has('unix')
580 return
581 endif
582 new
583 insert
584***************
585*** 1,3 ****
586 1
587! 2
588 3
589--- 1,4 ----
590 1
591! 2x
592 3
593+ 4
594.
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200595 saveas! Xpatch
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100596 bwipe!
597 new
598 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100599
Bram Moolenaara95ab322017-03-11 19:21:53 +0100600 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100601 call setline(1, ['1', '2', '3'])
602 if name != 'Xpatch'
603 call rename('Xpatch', name)
604 endif
605 exe 'diffpatch ' . escape(name, '$')
606 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
607 if name != 'Xpatch'
608 call rename(name, 'Xpatch')
609 endif
610 bwipe!
611 endfor
612
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100613 call delete('Xpatch')
614 bwipe!
615endfunc
616
617func Test_diff_too_many_buffers()
618 for i in range(1, 8)
619 exe "new Xtest" . i
620 diffthis
621 endfor
622 new Xtest9
623 call assert_fails('diffthis', 'E96:')
624 %bwipe!
625endfunc
626
627func Test_diff_nomodifiable()
628 new
629 call setline(1, [1, 2, 3, 4])
630 setl nomodifiable
631 diffthis
632 vnew
633 call setline(1, ['1x', 2, 3, 3, 4])
634 diffthis
635 call assert_fails('norm dp', 'E793:')
636 setl nomodifiable
637 call assert_fails('norm do', 'E21:')
638 %bwipe!
639endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100640
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200641func Test_diff_hlID()
642 new
643 call setline(1, [1, 2, 3])
644 diffthis
645 vnew
646 call setline(1, ['1x', 2, 'x', 3])
647 diffthis
648 redraw
649
650 call assert_equal(synIDattr(diff_hlID(-1, 1), "name"), "")
651
652 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
653 call assert_equal(synIDattr(diff_hlID(1, 2), "name"), "DiffText")
654 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
655 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "DiffAdd")
656 call assert_equal(synIDattr(diff_hlID(4, 1), "name"), "")
657
658 wincmd w
659 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
660 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
661 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
662
663 %bwipe!
664endfunc
665
666func Test_diff_filler()
667 new
668 call setline(1, [1, 2, 3, 'x', 4])
669 diffthis
670 vnew
671 call setline(1, [1, 2, 'y', 'y', 3, 4])
672 diffthis
673 redraw
674
675 call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'diff_filler(v:val)'))
676 wincmd w
677 call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
678
679 %bwipe!
680endfunc
681
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100682func Test_diff_lastline()
683 enew!
684 only!
685 call setline(1, ['This is a ', 'line with five ', 'rows'])
686 diffthis
687 botright vert new
688 call setline(1, ['This is', 'a line with ', 'four rows'])
689 diffthis
690 1
691 call feedkeys("Je a\<CR>", 'tx')
692 call feedkeys("Je a\<CR>", 'tx')
693 let w1lines = winline()
694 wincmd w
695 $
696 let w2lines = winline()
697 call assert_equal(w2lines, w1lines)
698 bwipe!
699 bwipe!
700endfunc
Bram Moolenaare828b762018-09-10 17:51:58 +0200701
Bram Moolenaar785fc652018-09-15 19:17:38 +0200702func WriteDiffFiles(buf, list1, list2)
Bram Moolenaare828b762018-09-10 17:51:58 +0200703 call writefile(a:list1, 'Xfile1')
704 call writefile(a:list2, 'Xfile2')
Bram Moolenaar785fc652018-09-15 19:17:38 +0200705 if a:buf
706 call term_sendkeys(a:buf, ":checktime\<CR>")
707 endif
Bram Moolenaare828b762018-09-10 17:51:58 +0200708endfunc
709
Bram Moolenaar785fc652018-09-15 19:17:38 +0200710" Verify a screendump with both the internal and external diff.
Bram Moolenaare828b762018-09-10 17:51:58 +0200711func VerifyBoth(buf, dumpfile, extra)
Bram Moolenaare828b762018-09-10 17:51:58 +0200712 " trailing : for leaving the cursor on the command line
Bram Moolenaar785fc652018-09-15 19:17:38 +0200713 for cmd in [":set diffopt=filler" . a:extra . "\<CR>:", ":set diffopt+=internal\<CR>:"]
Bram Moolenaare828b762018-09-10 17:51:58 +0200714 call term_sendkeys(a:buf, cmd)
715 if VerifyScreenDump(a:buf, a:dumpfile, {}, cmd =~ 'internal' ? 'internal' : 'external')
716 break " don't let the next iteration overwrite the "failed" file.
717 endif
718 endfor
719endfunc
720
Bram Moolenaar785fc652018-09-15 19:17:38 +0200721" Verify a screendump with the internal diff only.
722func VerifyInternal(buf, dumpfile, extra)
723 call term_sendkeys(a:buf, ":diffupdate!\<CR>")
724 " trailing : for leaving the cursor on the command line
725 call term_sendkeys(a:buf, ":set diffopt=internal,filler" . a:extra . "\<CR>:")
726 call VerifyScreenDump(a:buf, a:dumpfile, {})
727endfunc
728
Bram Moolenaare828b762018-09-10 17:51:58 +0200729func Test_diff_screen()
730 if !CanRunVimInTerminal() || !has('menu')
731 return
732 endif
733 " clean up already existing swap files, just in case
734 call delete('.Xfile1.swp')
735 call delete('.Xfile2.swp')
736
737 " Test 1: Add a line in beginning of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200738 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 +0200739 let buf = RunVimInTerminal('-d Xfile1 Xfile2', {})
740 " Set autoread mode, ,so that Vim won't complain once we re-write the test
741 " files
Bram Moolenaar785fc652018-09-15 19:17:38 +0200742 call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
Bram Moolenaare828b762018-09-10 17:51:58 +0200743
744 call VerifyBoth(buf, 'Test_diff_01', '')
745
746 " Test 2: Add a line in beginning of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200747 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 +0200748 call VerifyBoth(buf, 'Test_diff_02', '')
749
750 " Test 3: Add a line at the end of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200751 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 +0200752 call VerifyBoth(buf, 'Test_diff_03', '')
753
754 " Test 4: Add a line at the end of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200755 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 +0200756 call VerifyBoth(buf, 'Test_diff_04', '')
757
758 " 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 +0200759 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 +0200760 call VerifyBoth(buf, 'Test_diff_05', '')
761
762 " 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 +0200763 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 +0200764 call VerifyBoth(buf, 'Test_diff_06', '')
765
766 " Test 7 - 9: Test normal/patience/histogram diff algorithm
Bram Moolenaar785fc652018-09-15 19:17:38 +0200767 call WriteDiffFiles(buf, ['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
Bram Moolenaare828b762018-09-10 17:51:58 +0200768 \ ' int i;', ' for(i = 0; i < 10; i++)', ' {', ' printf("Your answer is: ");',
769 \ ' printf("%d\n", foo);', ' }', '}', '', 'int fact(int n)', '{', ' if(n > 1)', ' {',
770 \ ' return fact(n-1) * n;', ' }', ' return 1;', '}', '', 'int main(int argc, char **argv)',
771 \ '{', ' frobnitz(fact(10));', '}'],
772 \ ['#include <stdio.h>', '', 'int fib(int n)', '{', ' if(n > 2)', ' {',
773 \ ' return fib(n-1) + fib(n-2);', ' }', ' return 1;', '}', '', '// Frobs foo heartily',
774 \ 'int frobnitz(int foo)', '{', ' int i;', ' for(i = 0; i < 10; i++)', ' {',
775 \ ' printf("%d\n", foo);', ' }', '}', '',
776 \ 'int main(int argc, char **argv)', '{', ' frobnitz(fib(10));', '}'])
777 call term_sendkeys(buf, ":diffupdate!\<cr>")
778 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
779 call VerifyScreenDump(buf, 'Test_diff_07', {})
780
781 call term_sendkeys(buf, ":set diffopt+=algorithm:patience\<cr>")
782 call VerifyScreenDump(buf, 'Test_diff_08', {})
783
784 call term_sendkeys(buf, ":set diffopt+=algorithm:histogram\<cr>")
785 call VerifyScreenDump(buf, 'Test_diff_09', {})
786
787 " Test 10-11: normal/indent-heuristic
788 call term_sendkeys(buf, ":set diffopt&vim\<cr>")
Bram Moolenaar785fc652018-09-15 19:17:38 +0200789 call WriteDiffFiles(buf, ['', ' def finalize(values)', '', ' values.each do |v|', ' v.finalize', ' end'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200790 \ ['', ' def finalize(values)', '', ' values.each do |v|', ' v.prepare', ' end', '',
791 \ ' values.each do |v|', ' v.finalize', ' end'])
792 call term_sendkeys(buf, ":diffupdate!\<cr>")
793 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
794 call VerifyScreenDump(buf, 'Test_diff_10', {})
795
796 call term_sendkeys(buf, ":set diffopt+=indent-heuristic\<cr>")
797 call VerifyScreenDump(buf, 'Test_diff_11', {})
798
799 " Test 12: diff the same file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200800 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 +0200801 call VerifyBoth(buf, 'Test_diff_12', '')
802
803 " Test 13: diff an empty file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200804 call WriteDiffFiles(buf, [], [])
Bram Moolenaare828b762018-09-10 17:51:58 +0200805 call VerifyBoth(buf, 'Test_diff_13', '')
806
807 " Test 14: test diffopt+=icase
Bram Moolenaar785fc652018-09-15 19:17:38 +0200808 call WriteDiffFiles(buf, ['a', 'b', 'cd'], ['A', 'b', 'cDe'])
Bram Moolenaare828b762018-09-10 17:51:58 +0200809 call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
810
811 " Test 15-16: test diffopt+=iwhite
Bram Moolenaar785fc652018-09-15 19:17:38 +0200812 call WriteDiffFiles(buf, ['int main()', '{', ' printf("Hello, World!");', ' return 0;', '}'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200813 \ ['int main()', '{', ' if (0)', ' {', ' printf("Hello, World!");', ' return 0;', ' }', '}'])
814 call term_sendkeys(buf, ":diffupdate!\<cr>")
815 call term_sendkeys(buf, ":set diffopt&vim diffopt+=filler diffopt+=iwhite\<cr>")
816 call VerifyScreenDump(buf, 'Test_diff_15', {})
817 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
818 call VerifyScreenDump(buf, 'Test_diff_16', {})
819
Bram Moolenaar785fc652018-09-15 19:17:38 +0200820 " Test 17: test diffopt+=iblank
821 call WriteDiffFiles(buf, ['a', ' ', 'cd', 'ef', 'xxx'], ['a', 'cd', '', 'ef', 'yyy'])
822 call VerifyInternal(buf, 'Test_diff_17', " diffopt+=iblank")
823
824 " Test 18: test diffopt+=iblank,iwhite / iwhiteall / iwhiteeol
825 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhite")
826 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteall")
827 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteeol")
828
829 " Test 19: test diffopt+=iwhiteeol
830 call WriteDiffFiles(buf, ['a ', 'x', 'cd', 'ef', 'xx xx', 'foo', 'bar'], ['a', 'x', 'c d', ' ef', 'xx xx', 'foo', '', 'bar'])
831 call VerifyInternal(buf, 'Test_diff_19', " diffopt+=iwhiteeol")
832
833 " Test 19: test diffopt+=iwhiteall
834 call VerifyInternal(buf, 'Test_diff_20', " diffopt+=iwhiteall")
835
Bram Moolenaare828b762018-09-10 17:51:58 +0200836 " clean up
837 call StopVimInTerminal(buf)
838 call delete('Xfile1')
839 call delete('Xfile2')
840endfunc
841
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +0200842func Test_diff_with_cursorline()
843 if !CanRunVimInTerminal()
844 return
845 endif
846
847 call writefile([
848 \ 'hi CursorLine ctermbg=red ctermfg=white',
849 \ 'set cursorline',
850 \ 'call setline(1, ["foo","foo","foo","bar"])',
851 \ 'vnew',
852 \ 'call setline(1, ["bee","foo","foo","baz"])',
853 \ 'windo diffthis',
854 \ '2wincmd w',
855 \ ], 'Xtest_diff_cursorline')
856 let buf = RunVimInTerminal('-S Xtest_diff_cursorline', {})
857
858 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_01', {})
859 call term_sendkeys(buf, "j")
860 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_02', {})
861 call term_sendkeys(buf, "j")
862 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_03', {})
863
864 " clean up
865 call StopVimInTerminal(buf)
866 call delete('Xtest_diff_cursorline')
867endfunc