blob: 1deb238735c9d4f836a3c9c0db383e031377fc45 [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
280func Test_diffoff()
281 enew!
282 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200283 redraw
Bram Moolenaare67d5462016-08-27 22:40:42 +0200284 let normattr = screenattr(1, 1)
285 diffthis
286 botright vert new
287 call setline(1, ['One', '', 'Two', 'Three'])
288 diffthis
289 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100290 call assert_notequal(normattr, screenattr(1, 1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200291 diffoff!
292 redraw
293 call assert_equal(normattr, screenattr(1, 1))
294 bwipe!
295 bwipe!
296endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200297
Bram Moolenaare828b762018-09-10 17:51:58 +0200298func Common_icase_test()
299 edit one
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200300 call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200301 redraw
302 let normattr = screenattr(1, 1)
303 diffthis
304
305 botright vert new two
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200306 call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VE'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200307 diffthis
308
309 redraw
310 call assert_equal(normattr, screenattr(1, 1))
311 call assert_equal(normattr, screenattr(2, 1))
312 call assert_notequal(normattr, screenattr(3, 1))
313 call assert_equal(normattr, screenattr(4, 1))
314
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200315 let dtextattr = screenattr(5, 3)
316 call assert_notequal(dtextattr, screenattr(5, 1))
317 call assert_notequal(dtextattr, screenattr(5, 5))
318
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200319 diffoff!
320 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200321endfunc
322
323func Test_diffopt_icase()
324 set diffopt=icase,foldcolumn:0
325 call Common_icase_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200326 set diffopt&
327endfunc
328
Bram Moolenaare828b762018-09-10 17:51:58 +0200329func Test_diffopt_icase_internal()
330 set diffopt=icase,foldcolumn:0,internal
331 call Common_icase_test()
332 set diffopt&
333endfunc
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200334
Bram Moolenaare828b762018-09-10 17:51:58 +0200335func Common_iwhite_test()
336 edit one
337 " Difference in trailing spaces and amount of spaces should be ignored,
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200338 " but not other space differences.
Bram Moolenaare828b762018-09-10 17:51:58 +0200339 call setline(1, ["One \t", 'Two', 'Three', 'one two', 'one two', 'Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200340 redraw
341 let normattr = screenattr(1, 1)
342 diffthis
343
344 botright vert new two
Bram Moolenaare828b762018-09-10 17:51:58 +0200345 call setline(1, ["One\t ", "Two\t ", 'Three', 'one two', 'onetwo', ' Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200346 diffthis
347
348 redraw
349 call assert_equal(normattr, screenattr(1, 1))
350 call assert_equal(normattr, screenattr(2, 1))
351 call assert_equal(normattr, screenattr(3, 1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200352 call assert_equal(normattr, screenattr(4, 1))
353 call assert_notequal(normattr, screenattr(5, 1))
354 call assert_notequal(normattr, screenattr(6, 1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200355
356 diffoff!
357 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200358endfunc
359
360func Test_diffopt_iwhite()
361 set diffopt=iwhite,foldcolumn:0
362 call Common_iwhite_test()
363 set diffopt&
364endfunc
365
366func Test_diffopt_iwhite_internal()
367 set diffopt=internal,iwhite,foldcolumn:0
368 call Common_iwhite_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200369 set diffopt&
370endfunc
371
372func Test_diffopt_context()
373 enew!
374 call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
375 diffthis
376 new
377 call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
378 diffthis
379
380 set diffopt=context:2
381 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200382 set diffopt=internal,context:2
383 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
384
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200385 set diffopt=context:1
386 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200387 set diffopt=internal,context:1
388 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200389
390 diffoff!
391 %bwipe!
392 set diffopt&
393endfunc
394
395func Test_diffopt_horizontal()
Bram Moolenaare828b762018-09-10 17:51:58 +0200396 set diffopt=internal,horizontal
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200397 diffsplit
398
399 call assert_equal(&columns, winwidth(1))
400 call assert_equal(&columns, winwidth(2))
401 call assert_equal(&lines, winheight(1) + winheight(2) + 3)
402 call assert_inrange(0, 1, winheight(1) - winheight(2))
403
404 set diffopt&
405 diffoff!
406 %bwipe
407endfunc
408
409func Test_diffopt_vertical()
Bram Moolenaare828b762018-09-10 17:51:58 +0200410 set diffopt=internal,vertical
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200411 diffsplit
412
413 call assert_equal(&lines - 2, winheight(1))
414 call assert_equal(&lines - 2, winheight(2))
415 call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
416 call assert_inrange(0, 1, winwidth(1) - winwidth(2))
417
418 set diffopt&
419 diffoff!
420 %bwipe
421endfunc
422
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100423func Test_diffopt_hiddenoff()
Bram Moolenaare828b762018-09-10 17:51:58 +0200424 set diffopt=internal,filler,foldcolumn:0,hiddenoff
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100425 e! one
426 call setline(1, ['Two', 'Three'])
427 redraw
428 let normattr = screenattr(1, 1)
429 diffthis
430 botright vert new two
431 call setline(1, ['One', 'Four'])
432 diffthis
433 redraw
434 call assert_notequal(normattr, screenattr(1, 1))
435 set hidden
436 close
437 redraw
438 " should not diffing with hidden buffer two while 'hiddenoff' is enabled
439 call assert_equal(normattr, screenattr(1, 1))
440
441 bwipe!
442 bwipe!
443 set hidden& diffopt&
444endfunc
445
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100446func Test_diffoff_hidden()
Bram Moolenaare828b762018-09-10 17:51:58 +0200447 set diffopt=internal,filler,foldcolumn:0
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100448 e! one
449 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200450 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100451 let normattr = screenattr(1, 1)
452 diffthis
453 botright vert new two
454 call setline(1, ['One', 'Four'])
455 diffthis
456 redraw
457 call assert_notequal(normattr, screenattr(1, 1))
458 set hidden
459 close
460 redraw
461 " diffing with hidden buffer two
462 call assert_notequal(normattr, screenattr(1, 1))
463 diffoff
464 redraw
465 call assert_equal(normattr, screenattr(1, 1))
466 diffthis
467 redraw
468 " still diffing with hidden buffer two
469 call assert_notequal(normattr, screenattr(1, 1))
470 diffoff!
471 redraw
472 call assert_equal(normattr, screenattr(1, 1))
473 diffthis
474 redraw
475 " no longer diffing with hidden buffer two
476 call assert_equal(normattr, screenattr(1, 1))
477
478 bwipe!
479 bwipe!
480 set hidden& diffopt&
481endfunc
482
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200483func Test_setting_cursor()
484 new Xtest1
485 put =range(1,90)
486 wq
487 new Xtest2
488 put =range(1,100)
489 wq
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200490
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200491 tabe Xtest2
492 $
493 diffsp Xtest1
494 tabclose
495
496 call delete('Xtest1')
497 call delete('Xtest2')
498endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100499
500func Test_diff_move_to()
501 new
502 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
503 diffthis
504 vnew
505 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
506 diffthis
507 norm ]c
508 call assert_equal(2, line('.'))
509 norm 3]c
510 call assert_equal(9, line('.'))
511 norm 10]c
512 call assert_equal(11, line('.'))
513 norm [c
514 call assert_equal(9, line('.'))
515 norm 2[c
516 call assert_equal(5, line('.'))
517 norm 10[c
518 call assert_equal(2, line('.'))
519 %bwipe!
520endfunc
521
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200522func Test_diffexpr()
523 if !executable('diff')
524 return
525 endif
526
527 func DiffExpr()
Bram Moolenaar3b8defd2018-09-13 13:03:11 +0200528 " Prepent some text to check diff type detection
529 call writefile(['warning', ' message'], v:fname_out)
530 silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>>' . v:fname_out
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200531 endfunc
532 set diffexpr=DiffExpr()
533 set diffopt=foldcolumn:0
534
535 enew!
536 call setline(1, ['one', 'two', 'three'])
537 redraw
538 let normattr = screenattr(1, 1)
539 diffthis
540
541 botright vert new
542 call setline(1, ['one', 'two', 'three.'])
543 diffthis
544
545 redraw
546 call assert_equal(normattr, screenattr(1, 1))
547 call assert_equal(normattr, screenattr(2, 1))
548 call assert_notequal(normattr, screenattr(3, 1))
549
550 diffoff!
551 %bwipe!
552 set diffexpr& diffopt&
553endfunc
554
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100555func Test_diffpatch()
556 " The patch program on MS-Windows may fail or hang.
557 if !executable('patch') || !has('unix')
558 return
559 endif
560 new
561 insert
562***************
563*** 1,3 ****
564 1
565! 2
566 3
567--- 1,4 ----
568 1
569! 2x
570 3
571+ 4
572.
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200573 saveas! Xpatch
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100574 bwipe!
575 new
576 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100577
Bram Moolenaara95ab322017-03-11 19:21:53 +0100578 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100579 call setline(1, ['1', '2', '3'])
580 if name != 'Xpatch'
581 call rename('Xpatch', name)
582 endif
583 exe 'diffpatch ' . escape(name, '$')
584 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
585 if name != 'Xpatch'
586 call rename(name, 'Xpatch')
587 endif
588 bwipe!
589 endfor
590
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100591 call delete('Xpatch')
592 bwipe!
593endfunc
594
595func Test_diff_too_many_buffers()
596 for i in range(1, 8)
597 exe "new Xtest" . i
598 diffthis
599 endfor
600 new Xtest9
601 call assert_fails('diffthis', 'E96:')
602 %bwipe!
603endfunc
604
605func Test_diff_nomodifiable()
606 new
607 call setline(1, [1, 2, 3, 4])
608 setl nomodifiable
609 diffthis
610 vnew
611 call setline(1, ['1x', 2, 3, 3, 4])
612 diffthis
613 call assert_fails('norm dp', 'E793:')
614 setl nomodifiable
615 call assert_fails('norm do', 'E21:')
616 %bwipe!
617endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100618
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200619func Test_diff_hlID()
620 new
621 call setline(1, [1, 2, 3])
622 diffthis
623 vnew
624 call setline(1, ['1x', 2, 'x', 3])
625 diffthis
626 redraw
627
628 call assert_equal(synIDattr(diff_hlID(-1, 1), "name"), "")
629
630 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
631 call assert_equal(synIDattr(diff_hlID(1, 2), "name"), "DiffText")
632 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
633 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "DiffAdd")
634 call assert_equal(synIDattr(diff_hlID(4, 1), "name"), "")
635
636 wincmd w
637 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
638 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
639 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
640
641 %bwipe!
642endfunc
643
644func Test_diff_filler()
645 new
646 call setline(1, [1, 2, 3, 'x', 4])
647 diffthis
648 vnew
649 call setline(1, [1, 2, 'y', 'y', 3, 4])
650 diffthis
651 redraw
652
653 call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'diff_filler(v:val)'))
654 wincmd w
655 call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
656
657 %bwipe!
658endfunc
659
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100660func Test_diff_lastline()
661 enew!
662 only!
663 call setline(1, ['This is a ', 'line with five ', 'rows'])
664 diffthis
665 botright vert new
666 call setline(1, ['This is', 'a line with ', 'four rows'])
667 diffthis
668 1
669 call feedkeys("Je a\<CR>", 'tx')
670 call feedkeys("Je a\<CR>", 'tx')
671 let w1lines = winline()
672 wincmd w
673 $
674 let w2lines = winline()
675 call assert_equal(w2lines, w1lines)
676 bwipe!
677 bwipe!
678endfunc
Bram Moolenaare828b762018-09-10 17:51:58 +0200679
Bram Moolenaar785fc652018-09-15 19:17:38 +0200680func WriteDiffFiles(buf, list1, list2)
Bram Moolenaare828b762018-09-10 17:51:58 +0200681 call writefile(a:list1, 'Xfile1')
682 call writefile(a:list2, 'Xfile2')
Bram Moolenaar785fc652018-09-15 19:17:38 +0200683 if a:buf
684 call term_sendkeys(a:buf, ":checktime\<CR>")
685 endif
Bram Moolenaare828b762018-09-10 17:51:58 +0200686endfunc
687
Bram Moolenaar785fc652018-09-15 19:17:38 +0200688" Verify a screendump with both the internal and external diff.
Bram Moolenaare828b762018-09-10 17:51:58 +0200689func VerifyBoth(buf, dumpfile, extra)
Bram Moolenaare828b762018-09-10 17:51:58 +0200690 " trailing : for leaving the cursor on the command line
Bram Moolenaar785fc652018-09-15 19:17:38 +0200691 for cmd in [":set diffopt=filler" . a:extra . "\<CR>:", ":set diffopt+=internal\<CR>:"]
Bram Moolenaare828b762018-09-10 17:51:58 +0200692 call term_sendkeys(a:buf, cmd)
693 if VerifyScreenDump(a:buf, a:dumpfile, {}, cmd =~ 'internal' ? 'internal' : 'external')
694 break " don't let the next iteration overwrite the "failed" file.
695 endif
696 endfor
697endfunc
698
Bram Moolenaar785fc652018-09-15 19:17:38 +0200699" Verify a screendump with the internal diff only.
700func VerifyInternal(buf, dumpfile, extra)
701 call term_sendkeys(a:buf, ":diffupdate!\<CR>")
702 " trailing : for leaving the cursor on the command line
703 call term_sendkeys(a:buf, ":set diffopt=internal,filler" . a:extra . "\<CR>:")
704 call VerifyScreenDump(a:buf, a:dumpfile, {})
705endfunc
706
Bram Moolenaare828b762018-09-10 17:51:58 +0200707func Test_diff_screen()
708 if !CanRunVimInTerminal() || !has('menu')
709 return
710 endif
711 " clean up already existing swap files, just in case
712 call delete('.Xfile1.swp')
713 call delete('.Xfile2.swp')
714
715 " Test 1: Add a line in beginning of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200716 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 +0200717 let buf = RunVimInTerminal('-d Xfile1 Xfile2', {})
718 " Set autoread mode, ,so that Vim won't complain once we re-write the test
719 " files
Bram Moolenaar785fc652018-09-15 19:17:38 +0200720 call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
Bram Moolenaare828b762018-09-10 17:51:58 +0200721
722 call VerifyBoth(buf, 'Test_diff_01', '')
723
724 " Test 2: Add a line in beginning of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200725 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 +0200726 call VerifyBoth(buf, 'Test_diff_02', '')
727
728 " Test 3: Add a line at the end of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200729 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 +0200730 call VerifyBoth(buf, 'Test_diff_03', '')
731
732 " Test 4: Add a line at the end of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200733 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 +0200734 call VerifyBoth(buf, 'Test_diff_04', '')
735
736 " 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 +0200737 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 +0200738 call VerifyBoth(buf, 'Test_diff_05', '')
739
740 " 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 +0200741 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 +0200742 call VerifyBoth(buf, 'Test_diff_06', '')
743
744 " Test 7 - 9: Test normal/patience/histogram diff algorithm
Bram Moolenaar785fc652018-09-15 19:17:38 +0200745 call WriteDiffFiles(buf, ['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
Bram Moolenaare828b762018-09-10 17:51:58 +0200746 \ ' int i;', ' for(i = 0; i < 10; i++)', ' {', ' printf("Your answer is: ");',
747 \ ' printf("%d\n", foo);', ' }', '}', '', 'int fact(int n)', '{', ' if(n > 1)', ' {',
748 \ ' return fact(n-1) * n;', ' }', ' return 1;', '}', '', 'int main(int argc, char **argv)',
749 \ '{', ' frobnitz(fact(10));', '}'],
750 \ ['#include <stdio.h>', '', 'int fib(int n)', '{', ' if(n > 2)', ' {',
751 \ ' return fib(n-1) + fib(n-2);', ' }', ' return 1;', '}', '', '// Frobs foo heartily',
752 \ 'int frobnitz(int foo)', '{', ' int i;', ' for(i = 0; i < 10; i++)', ' {',
753 \ ' printf("%d\n", foo);', ' }', '}', '',
754 \ 'int main(int argc, char **argv)', '{', ' frobnitz(fib(10));', '}'])
755 call term_sendkeys(buf, ":diffupdate!\<cr>")
756 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
757 call VerifyScreenDump(buf, 'Test_diff_07', {})
758
759 call term_sendkeys(buf, ":set diffopt+=algorithm:patience\<cr>")
760 call VerifyScreenDump(buf, 'Test_diff_08', {})
761
762 call term_sendkeys(buf, ":set diffopt+=algorithm:histogram\<cr>")
763 call VerifyScreenDump(buf, 'Test_diff_09', {})
764
765 " Test 10-11: normal/indent-heuristic
766 call term_sendkeys(buf, ":set diffopt&vim\<cr>")
Bram Moolenaar785fc652018-09-15 19:17:38 +0200767 call WriteDiffFiles(buf, ['', ' def finalize(values)', '', ' values.each do |v|', ' v.finalize', ' end'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200768 \ ['', ' def finalize(values)', '', ' values.each do |v|', ' v.prepare', ' end', '',
769 \ ' values.each do |v|', ' v.finalize', ' end'])
770 call term_sendkeys(buf, ":diffupdate!\<cr>")
771 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
772 call VerifyScreenDump(buf, 'Test_diff_10', {})
773
774 call term_sendkeys(buf, ":set diffopt+=indent-heuristic\<cr>")
775 call VerifyScreenDump(buf, 'Test_diff_11', {})
776
777 " Test 12: diff the same file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200778 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 +0200779 call VerifyBoth(buf, 'Test_diff_12', '')
780
781 " Test 13: diff an empty file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200782 call WriteDiffFiles(buf, [], [])
Bram Moolenaare828b762018-09-10 17:51:58 +0200783 call VerifyBoth(buf, 'Test_diff_13', '')
784
785 " Test 14: test diffopt+=icase
Bram Moolenaar785fc652018-09-15 19:17:38 +0200786 call WriteDiffFiles(buf, ['a', 'b', 'cd'], ['A', 'b', 'cDe'])
Bram Moolenaare828b762018-09-10 17:51:58 +0200787 call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
788
789 " Test 15-16: test diffopt+=iwhite
Bram Moolenaar785fc652018-09-15 19:17:38 +0200790 call WriteDiffFiles(buf, ['int main()', '{', ' printf("Hello, World!");', ' return 0;', '}'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200791 \ ['int main()', '{', ' if (0)', ' {', ' printf("Hello, World!");', ' return 0;', ' }', '}'])
792 call term_sendkeys(buf, ":diffupdate!\<cr>")
793 call term_sendkeys(buf, ":set diffopt&vim diffopt+=filler diffopt+=iwhite\<cr>")
794 call VerifyScreenDump(buf, 'Test_diff_15', {})
795 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
796 call VerifyScreenDump(buf, 'Test_diff_16', {})
797
Bram Moolenaar785fc652018-09-15 19:17:38 +0200798 " Test 17: test diffopt+=iblank
799 call WriteDiffFiles(buf, ['a', ' ', 'cd', 'ef', 'xxx'], ['a', 'cd', '', 'ef', 'yyy'])
800 call VerifyInternal(buf, 'Test_diff_17', " diffopt+=iblank")
801
802 " Test 18: test diffopt+=iblank,iwhite / iwhiteall / iwhiteeol
803 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhite")
804 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteall")
805 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteeol")
806
807 " Test 19: test diffopt+=iwhiteeol
808 call WriteDiffFiles(buf, ['a ', 'x', 'cd', 'ef', 'xx xx', 'foo', 'bar'], ['a', 'x', 'c d', ' ef', 'xx xx', 'foo', '', 'bar'])
809 call VerifyInternal(buf, 'Test_diff_19', " diffopt+=iwhiteeol")
810
811 " Test 19: test diffopt+=iwhiteall
812 call VerifyInternal(buf, 'Test_diff_20', " diffopt+=iwhiteall")
813
Bram Moolenaare828b762018-09-10 17:51:58 +0200814 " clean up
815 call StopVimInTerminal(buf)
816 call delete('Xfile1')
817 call delete('Xfile2')
818endfunc
819