blob: c3c1eaf4a55a5a336d162ff4ab2753740d71a1e7 [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!
7 let l = range(50)
8 call setline(1, l)
9 diffthis
10 let winone = win_getid()
11 new
12 let l[25] = 'diff'
13 call setline(1, l)
14 diffthis
15 let wintwo = win_getid()
16 " line 15 is inside the closed fold
17 call assert_equal(19, foldclosedend(10))
18 call win_gotoid(winone)
19 call assert_equal(19, foldclosedend(10))
20 " open the fold
21 normal zv
22 call assert_equal(-1, foldclosedend(10))
23 " fold in other window must have opened too
24 call win_gotoid(wintwo)
25 call assert_equal(-1, foldclosedend(10))
26
27 " cursor position is in sync
28 normal 23G
29 call win_gotoid(winone)
30 call assert_equal(23, getcurpos()[1])
31
32 windo diffoff
33 close!
34 set nomodified
35endfunc
36
37func Test_vert_split()
Bram Moolenaare828b762018-09-10 17:51:58 +020038 set diffopt=filler
39 call Common_vert_split()
40 set diffopt&
41endfunc
42
43func Test_vert_split_internal()
44 set diffopt=internal,filler
45 call Common_vert_split()
46 set diffopt&
47endfunc
48
49func Common_vert_split()
Bram Moolenaar42093c02016-07-30 16:16:54 +020050 " Disable the title to avoid xterm keeping the wrong one.
51 set notitle noicon
52 new
53 let l = ['1 aa', '2 bb', '3 cc', '4 dd', '5 ee']
54 call setline(1, l)
55 w! Xtest
56 normal dd
57 $
58 put
59 normal kkrXoxxx
60 w! Xtest2
61 file Nop
62 normal ggoyyyjjjozzzz
63 set foldmethod=marker foldcolumn=4
64 call assert_equal(0, &diff)
65 call assert_equal('marker', &foldmethod)
66 call assert_equal(4, &foldcolumn)
67 call assert_equal(0, &scrollbind)
68 call assert_equal(0, &cursorbind)
69 call assert_equal(1, &wrap)
70
71 vert diffsplit Xtest
72 vert diffsplit Xtest2
73 call assert_equal(1, &diff)
74 call assert_equal('diff', &foldmethod)
75 call assert_equal(2, &foldcolumn)
76 call assert_equal(1, &scrollbind)
77 call assert_equal(1, &cursorbind)
78 call assert_equal(0, &wrap)
79
80 let diff_fdm = &fdm
81 let diff_fdc = &fdc
82 " repeat entering diff mode here to see if this saves the wrong settings
83 diffthis
84 " jump to second window for a moment to have filler line appear at start of
85 " first window
86 wincmd w
87 normal gg
88 wincmd p
89 normal gg
90 call assert_equal(2, winline())
91 normal j
92 call assert_equal(4, winline())
93 normal j
94 call assert_equal(5, winline())
95 normal j
96 call assert_equal(6, winline())
97 normal j
98 call assert_equal(8, winline())
99 normal j
100 call assert_equal(9, winline())
101
102 wincmd w
103 normal gg
104 call assert_equal(1, winline())
105 normal j
106 call assert_equal(2, winline())
107 normal j
108 call assert_equal(4, winline())
109 normal j
110 call assert_equal(5, winline())
111 normal j
112 call assert_equal(8, winline())
113
114 wincmd w
115 normal gg
116 call assert_equal(2, winline())
117 normal j
118 call assert_equal(3, winline())
119 normal j
120 call assert_equal(4, winline())
121 normal j
122 call assert_equal(5, winline())
123 normal j
124 call assert_equal(6, winline())
125 normal j
126 call assert_equal(7, winline())
127 normal j
128 call assert_equal(8, winline())
129
130 " Test diffoff
131 diffoff!
132 1wincmd 2
133 let &diff = 1
134 let &fdm = diff_fdm
135 let &fdc = diff_fdc
136 4wincmd w
137 diffoff!
138 1wincmd w
139 call assert_equal(0, &diff)
140 call assert_equal('marker', &foldmethod)
141 call assert_equal(4, &foldcolumn)
142 call assert_equal(0, &scrollbind)
143 call assert_equal(0, &cursorbind)
144 call assert_equal(1, &wrap)
145
146 wincmd w
147 call assert_equal(0, &diff)
148 call assert_equal('marker', &foldmethod)
149 call assert_equal(4, &foldcolumn)
150 call assert_equal(0, &scrollbind)
151 call assert_equal(0, &cursorbind)
152 call assert_equal(1, &wrap)
153
154 wincmd w
155 call assert_equal(0, &diff)
156 call assert_equal('marker', &foldmethod)
157 call assert_equal(4, &foldcolumn)
158 call assert_equal(0, &scrollbind)
159 call assert_equal(0, &cursorbind)
160 call assert_equal(1, &wrap)
161
Bram Moolenaar623cf882016-07-30 16:36:01 +0200162 call delete('Xtest')
163 call delete('Xtest2')
Bram Moolenaar42093c02016-07-30 16:16:54 +0200164 windo bw!
165endfunc
166
167func Test_filler_lines()
168 " Test that diffing shows correct filler lines
169 enew!
170 put =range(4,10)
171 1d _
172 vnew
173 put =range(1,10)
174 1d _
175 windo diffthis
176 wincmd h
177 call assert_equal(1, line('w0'))
178 unlet! diff_fdm diff_fdc
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200179 windo diffoff
180 bwipe!
181 enew!
182endfunc
Bram Moolenaar42093c02016-07-30 16:16:54 +0200183
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200184func Test_diffget_diffput()
185 enew!
186 let l = range(50)
187 call setline(1, l)
188 call assert_fails('diffget', 'E99:')
189 diffthis
190 call assert_fails('diffget', 'E100:')
191 new
192 let l[10] = 'one'
193 let l[20] = 'two'
194 let l[30] = 'three'
195 let l[40] = 'four'
196 call setline(1, l)
197 diffthis
198 call assert_equal('one', getline(11))
199 11diffget
200 call assert_equal('10', getline(11))
201 21diffput
202 wincmd w
203 call assert_equal('two', getline(21))
204 normal 31Gdo
205 call assert_equal('three', getline(31))
206 call assert_equal('40', getline(41))
207 normal 41Gdp
208 wincmd w
209 call assert_equal('40', getline(41))
210 new
211 diffthis
212 call assert_fails('diffget', 'E101:')
213
214 windo diffoff
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200215 %bwipe!
216endfunc
217
218func Test_dp_do_buffer()
219 e! one
220 let bn1=bufnr('%')
221 let l = range(60)
222 call setline(1, l)
223 diffthis
224
225 new two
226 let l[10] = 'one'
227 let l[20] = 'two'
228 let l[30] = 'three'
229 let l[40] = 'four'
230 let l[50] = 'five'
231 call setline(1, l)
232 diffthis
233
234 " dp and do with invalid buffer number.
235 11
236 call assert_fails('norm 99999dp', 'E102:')
237 call assert_fails('norm 99999do', 'E102:')
238 call assert_fails('diffput non_existing_buffer', 'E94:')
239 call assert_fails('diffget non_existing_buffer', 'E94:')
240
241 " dp and do with valid buffer number.
242 call assert_equal('one', getline('.'))
243 exe 'norm ' . bn1 . 'do'
244 call assert_equal('10', getline('.'))
245 21
246 call assert_equal('two', getline('.'))
247 diffget one
248 call assert_equal('20', getline('.'))
249
250 31
251 exe 'norm ' . bn1 . 'dp'
252 41
253 diffput one
254 wincmd w
255 31
256 call assert_equal('three', getline('.'))
257 41
258 call assert_equal('four', getline('.'))
259
260 " dp and do with buffer number which is not in diff mode.
261 new not_in_diff_mode
262 let bn3=bufnr('%')
263 wincmd w
264 51
265 call assert_fails('exe "norm" . bn3 . "dp"', 'E103:')
266 call assert_fails('exe "norm" . bn3 . "do"', 'E103:')
267 call assert_fails('diffput not_in_diff_mode', 'E94:')
268 call assert_fails('diffget not_in_diff_mode', 'E94:')
269
270 windo diffoff
271 %bwipe!
Bram Moolenaar42093c02016-07-30 16:16:54 +0200272endfunc
Bram Moolenaare67d5462016-08-27 22:40:42 +0200273
274func Test_diffoff()
275 enew!
276 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200277 redraw
Bram Moolenaare67d5462016-08-27 22:40:42 +0200278 let normattr = screenattr(1, 1)
279 diffthis
280 botright vert new
281 call setline(1, ['One', '', 'Two', 'Three'])
282 diffthis
283 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100284 call assert_notequal(normattr, screenattr(1, 1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200285 diffoff!
286 redraw
287 call assert_equal(normattr, screenattr(1, 1))
288 bwipe!
289 bwipe!
290endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200291
Bram Moolenaare828b762018-09-10 17:51:58 +0200292func Common_icase_test()
293 edit one
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200294 call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200295 redraw
296 let normattr = screenattr(1, 1)
297 diffthis
298
299 botright vert new two
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 diffthis
302
303 redraw
304 call assert_equal(normattr, screenattr(1, 1))
305 call assert_equal(normattr, screenattr(2, 1))
306 call assert_notequal(normattr, screenattr(3, 1))
307 call assert_equal(normattr, screenattr(4, 1))
308
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200309 let dtextattr = screenattr(5, 3)
310 call assert_notequal(dtextattr, screenattr(5, 1))
311 call assert_notequal(dtextattr, screenattr(5, 5))
312
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200313 diffoff!
314 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200315endfunc
316
317func Test_diffopt_icase()
318 set diffopt=icase,foldcolumn:0
319 call Common_icase_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200320 set diffopt&
321endfunc
322
Bram Moolenaare828b762018-09-10 17:51:58 +0200323func Test_diffopt_icase_internal()
324 set diffopt=icase,foldcolumn:0,internal
325 call Common_icase_test()
326 set diffopt&
327endfunc
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200328
Bram Moolenaare828b762018-09-10 17:51:58 +0200329func Common_iwhite_test()
330 edit one
331 " Difference in trailing spaces and amount of spaces should be ignored,
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200332 " but not other space differences.
Bram Moolenaare828b762018-09-10 17:51:58 +0200333 call setline(1, ["One \t", 'Two', 'Three', 'one two', 'one two', 'Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200334 redraw
335 let normattr = screenattr(1, 1)
336 diffthis
337
338 botright vert new two
Bram Moolenaare828b762018-09-10 17:51:58 +0200339 call setline(1, ["One\t ", "Two\t ", 'Three', 'one two', 'onetwo', ' Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200340 diffthis
341
342 redraw
343 call assert_equal(normattr, screenattr(1, 1))
344 call assert_equal(normattr, screenattr(2, 1))
345 call assert_equal(normattr, screenattr(3, 1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200346 call assert_equal(normattr, screenattr(4, 1))
347 call assert_notequal(normattr, screenattr(5, 1))
348 call assert_notequal(normattr, screenattr(6, 1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200349
350 diffoff!
351 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200352endfunc
353
354func Test_diffopt_iwhite()
355 set diffopt=iwhite,foldcolumn:0
356 call Common_iwhite_test()
357 set diffopt&
358endfunc
359
360func Test_diffopt_iwhite_internal()
361 set diffopt=internal,iwhite,foldcolumn:0
362 call Common_iwhite_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200363 set diffopt&
364endfunc
365
366func Test_diffopt_context()
367 enew!
368 call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
369 diffthis
370 new
371 call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
372 diffthis
373
374 set diffopt=context:2
375 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200376 set diffopt=internal,context:2
377 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
378
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200379 set diffopt=context:1
380 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200381 set diffopt=internal,context:1
382 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200383
384 diffoff!
385 %bwipe!
386 set diffopt&
387endfunc
388
389func Test_diffopt_horizontal()
Bram Moolenaare828b762018-09-10 17:51:58 +0200390 set diffopt=internal,horizontal
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200391 diffsplit
392
393 call assert_equal(&columns, winwidth(1))
394 call assert_equal(&columns, winwidth(2))
395 call assert_equal(&lines, winheight(1) + winheight(2) + 3)
396 call assert_inrange(0, 1, winheight(1) - winheight(2))
397
398 set diffopt&
399 diffoff!
400 %bwipe
401endfunc
402
403func Test_diffopt_vertical()
Bram Moolenaare828b762018-09-10 17:51:58 +0200404 set diffopt=internal,vertical
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200405 diffsplit
406
407 call assert_equal(&lines - 2, winheight(1))
408 call assert_equal(&lines - 2, winheight(2))
409 call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
410 call assert_inrange(0, 1, winwidth(1) - winwidth(2))
411
412 set diffopt&
413 diffoff!
414 %bwipe
415endfunc
416
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100417func Test_diffopt_hiddenoff()
Bram Moolenaare828b762018-09-10 17:51:58 +0200418 set diffopt=internal,filler,foldcolumn:0,hiddenoff
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100419 e! one
420 call setline(1, ['Two', 'Three'])
421 redraw
422 let normattr = screenattr(1, 1)
423 diffthis
424 botright vert new two
425 call setline(1, ['One', 'Four'])
426 diffthis
427 redraw
428 call assert_notequal(normattr, screenattr(1, 1))
429 set hidden
430 close
431 redraw
432 " should not diffing with hidden buffer two while 'hiddenoff' is enabled
433 call assert_equal(normattr, screenattr(1, 1))
434
435 bwipe!
436 bwipe!
437 set hidden& diffopt&
438endfunc
439
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100440func Test_diffoff_hidden()
Bram Moolenaare828b762018-09-10 17:51:58 +0200441 set diffopt=internal,filler,foldcolumn:0
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100442 e! one
443 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200444 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100445 let normattr = screenattr(1, 1)
446 diffthis
447 botright vert new two
448 call setline(1, ['One', 'Four'])
449 diffthis
450 redraw
451 call assert_notequal(normattr, screenattr(1, 1))
452 set hidden
453 close
454 redraw
455 " diffing with hidden buffer two
456 call assert_notequal(normattr, screenattr(1, 1))
457 diffoff
458 redraw
459 call assert_equal(normattr, screenattr(1, 1))
460 diffthis
461 redraw
462 " still diffing with hidden buffer two
463 call assert_notequal(normattr, screenattr(1, 1))
464 diffoff!
465 redraw
466 call assert_equal(normattr, screenattr(1, 1))
467 diffthis
468 redraw
469 " no longer diffing with hidden buffer two
470 call assert_equal(normattr, screenattr(1, 1))
471
472 bwipe!
473 bwipe!
474 set hidden& diffopt&
475endfunc
476
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200477func Test_setting_cursor()
478 new Xtest1
479 put =range(1,90)
480 wq
481 new Xtest2
482 put =range(1,100)
483 wq
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200484
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200485 tabe Xtest2
486 $
487 diffsp Xtest1
488 tabclose
489
490 call delete('Xtest1')
491 call delete('Xtest2')
492endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100493
494func Test_diff_move_to()
495 new
496 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
497 diffthis
498 vnew
499 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
500 diffthis
501 norm ]c
502 call assert_equal(2, line('.'))
503 norm 3]c
504 call assert_equal(9, line('.'))
505 norm 10]c
506 call assert_equal(11, line('.'))
507 norm [c
508 call assert_equal(9, line('.'))
509 norm 2[c
510 call assert_equal(5, line('.'))
511 norm 10[c
512 call assert_equal(2, line('.'))
513 %bwipe!
514endfunc
515
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200516func Test_diffexpr()
517 if !executable('diff')
518 return
519 endif
520
521 func DiffExpr()
522 silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>' . v:fname_out
523 endfunc
524 set diffexpr=DiffExpr()
525 set diffopt=foldcolumn:0
526
527 enew!
528 call setline(1, ['one', 'two', 'three'])
529 redraw
530 let normattr = screenattr(1, 1)
531 diffthis
532
533 botright vert new
534 call setline(1, ['one', 'two', 'three.'])
535 diffthis
536
537 redraw
538 call assert_equal(normattr, screenattr(1, 1))
539 call assert_equal(normattr, screenattr(2, 1))
540 call assert_notequal(normattr, screenattr(3, 1))
541
542 diffoff!
543 %bwipe!
544 set diffexpr& diffopt&
545endfunc
546
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100547func Test_diffpatch()
548 " The patch program on MS-Windows may fail or hang.
549 if !executable('patch') || !has('unix')
550 return
551 endif
552 new
553 insert
554***************
555*** 1,3 ****
556 1
557! 2
558 3
559--- 1,4 ----
560 1
561! 2x
562 3
563+ 4
564.
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200565 saveas! Xpatch
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100566 bwipe!
567 new
568 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100569
Bram Moolenaara95ab322017-03-11 19:21:53 +0100570 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100571 call setline(1, ['1', '2', '3'])
572 if name != 'Xpatch'
573 call rename('Xpatch', name)
574 endif
575 exe 'diffpatch ' . escape(name, '$')
576 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
577 if name != 'Xpatch'
578 call rename(name, 'Xpatch')
579 endif
580 bwipe!
581 endfor
582
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100583 call delete('Xpatch')
584 bwipe!
585endfunc
586
587func Test_diff_too_many_buffers()
588 for i in range(1, 8)
589 exe "new Xtest" . i
590 diffthis
591 endfor
592 new Xtest9
593 call assert_fails('diffthis', 'E96:')
594 %bwipe!
595endfunc
596
597func Test_diff_nomodifiable()
598 new
599 call setline(1, [1, 2, 3, 4])
600 setl nomodifiable
601 diffthis
602 vnew
603 call setline(1, ['1x', 2, 3, 3, 4])
604 diffthis
605 call assert_fails('norm dp', 'E793:')
606 setl nomodifiable
607 call assert_fails('norm do', 'E21:')
608 %bwipe!
609endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100610
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200611func Test_diff_hlID()
612 new
613 call setline(1, [1, 2, 3])
614 diffthis
615 vnew
616 call setline(1, ['1x', 2, 'x', 3])
617 diffthis
618 redraw
619
620 call assert_equal(synIDattr(diff_hlID(-1, 1), "name"), "")
621
622 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
623 call assert_equal(synIDattr(diff_hlID(1, 2), "name"), "DiffText")
624 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
625 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "DiffAdd")
626 call assert_equal(synIDattr(diff_hlID(4, 1), "name"), "")
627
628 wincmd w
629 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
630 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
631 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
632
633 %bwipe!
634endfunc
635
636func Test_diff_filler()
637 new
638 call setline(1, [1, 2, 3, 'x', 4])
639 diffthis
640 vnew
641 call setline(1, [1, 2, 'y', 'y', 3, 4])
642 diffthis
643 redraw
644
645 call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'diff_filler(v:val)'))
646 wincmd w
647 call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
648
649 %bwipe!
650endfunc
651
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100652func Test_diff_lastline()
653 enew!
654 only!
655 call setline(1, ['This is a ', 'line with five ', 'rows'])
656 diffthis
657 botright vert new
658 call setline(1, ['This is', 'a line with ', 'four rows'])
659 diffthis
660 1
661 call feedkeys("Je a\<CR>", 'tx')
662 call feedkeys("Je a\<CR>", 'tx')
663 let w1lines = winline()
664 wincmd w
665 $
666 let w2lines = winline()
667 call assert_equal(w2lines, w1lines)
668 bwipe!
669 bwipe!
670endfunc
Bram Moolenaare828b762018-09-10 17:51:58 +0200671
672func WriteDiffFiles(list1, list2)
673 call writefile(a:list1, 'Xfile1')
674 call writefile(a:list2, 'Xfile2')
675endfunc
676
677" Verify a screendump with both the external and external diff.
678func VerifyBoth(buf, dumpfile, extra)
679 call term_sendkeys(a:buf, ":diffupdate!\<cr>")
680 " trailing : for leaving the cursor on the command line
681 for cmd in [":set diffopt=filler" . a:extra . "\<cr>:", ":set diffopt+=internal\<cr>:"]
682 call term_sendkeys(a:buf, cmd)
683 if VerifyScreenDump(a:buf, a:dumpfile, {}, cmd =~ 'internal' ? 'internal' : 'external')
684 break " don't let the next iteration overwrite the "failed" file.
685 endif
686 endfor
687endfunc
688
689func Test_diff_screen()
690 if !CanRunVimInTerminal() || !has('menu')
691 return
692 endif
693 " clean up already existing swap files, just in case
694 call delete('.Xfile1.swp')
695 call delete('.Xfile2.swp')
696
697 " Test 1: Add a line in beginning of file 2
698 call WriteDiffFiles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
699 let buf = RunVimInTerminal('-d Xfile1 Xfile2', {})
700 " Set autoread mode, ,so that Vim won't complain once we re-write the test
701 " files
702 call term_sendkeys(buf, ":set autoread\<cr>\<c-w>w:set autoread\<cr>\<c-w>w")
703
704 call VerifyBoth(buf, 'Test_diff_01', '')
705
706 " Test 2: Add a line in beginning of file 1
707 call WriteDiffFiles([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
708 call VerifyBoth(buf, 'Test_diff_02', '')
709
710 " Test 3: Add a line at the end of file 2
711 call WriteDiffFiles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
712 call VerifyBoth(buf, 'Test_diff_03', '')
713
714 " Test 4: Add a line at the end of file 1
715 call WriteDiffFiles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
716 call VerifyBoth(buf, 'Test_diff_04', '')
717
718 " Test 5: Add a line in the middle of file 2, remove on at the end of file 1
719 call WriteDiffFiles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10])
720 call VerifyBoth(buf, 'Test_diff_05', '')
721
722 " Test 6: Add a line in the middle of file 1, remove on at the end of file 2
723 call WriteDiffFiles([1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
724 call VerifyBoth(buf, 'Test_diff_06', '')
725
726 " Test 7 - 9: Test normal/patience/histogram diff algorithm
727 call WriteDiffFiles(['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
728 \ ' int i;', ' for(i = 0; i < 10; i++)', ' {', ' printf("Your answer is: ");',
729 \ ' printf("%d\n", foo);', ' }', '}', '', 'int fact(int n)', '{', ' if(n > 1)', ' {',
730 \ ' return fact(n-1) * n;', ' }', ' return 1;', '}', '', 'int main(int argc, char **argv)',
731 \ '{', ' frobnitz(fact(10));', '}'],
732 \ ['#include <stdio.h>', '', 'int fib(int n)', '{', ' if(n > 2)', ' {',
733 \ ' return fib(n-1) + fib(n-2);', ' }', ' return 1;', '}', '', '// Frobs foo heartily',
734 \ 'int frobnitz(int foo)', '{', ' int i;', ' for(i = 0; i < 10; i++)', ' {',
735 \ ' printf("%d\n", foo);', ' }', '}', '',
736 \ 'int main(int argc, char **argv)', '{', ' frobnitz(fib(10));', '}'])
737 call term_sendkeys(buf, ":diffupdate!\<cr>")
738 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
739 call VerifyScreenDump(buf, 'Test_diff_07', {})
740
741 call term_sendkeys(buf, ":set diffopt+=algorithm:patience\<cr>")
742 call VerifyScreenDump(buf, 'Test_diff_08', {})
743
744 call term_sendkeys(buf, ":set diffopt+=algorithm:histogram\<cr>")
745 call VerifyScreenDump(buf, 'Test_diff_09', {})
746
747 " Test 10-11: normal/indent-heuristic
748 call term_sendkeys(buf, ":set diffopt&vim\<cr>")
749 call WriteDiffFiles(['', ' def finalize(values)', '', ' values.each do |v|', ' v.finalize', ' end'],
750 \ ['', ' def finalize(values)', '', ' values.each do |v|', ' v.prepare', ' end', '',
751 \ ' values.each do |v|', ' v.finalize', ' end'])
752 call term_sendkeys(buf, ":diffupdate!\<cr>")
753 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
754 call VerifyScreenDump(buf, 'Test_diff_10', {})
755
756 call term_sendkeys(buf, ":set diffopt+=indent-heuristic\<cr>")
757 call VerifyScreenDump(buf, 'Test_diff_11', {})
758
759 " Test 12: diff the same file
760 call WriteDiffFiles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
761 call VerifyBoth(buf, 'Test_diff_12', '')
762
763 " Test 13: diff an empty file
764 call WriteDiffFiles([], [])
765 call VerifyBoth(buf, 'Test_diff_13', '')
766
767 " Test 14: test diffopt+=icase
768 call WriteDiffFiles(['a', 'b', 'cd'], ['A', 'b', 'cDe'])
769 call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
770
771 " Test 15-16: test diffopt+=iwhite
772 call WriteDiffFiles(['int main()', '{', ' printf("Hello, World!");', ' return 0;', '}'],
773 \ ['int main()', '{', ' if (0)', ' {', ' printf("Hello, World!");', ' return 0;', ' }', '}'])
774 call term_sendkeys(buf, ":diffupdate!\<cr>")
775 call term_sendkeys(buf, ":set diffopt&vim diffopt+=filler diffopt+=iwhite\<cr>")
776 call VerifyScreenDump(buf, 'Test_diff_15', {})
777 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
778 call VerifyScreenDump(buf, 'Test_diff_16', {})
779
780 " clean up
781 call StopVimInTerminal(buf)
782 call delete('Xfile1')
783 call delete('Xfile2')
784endfunc
785