1" Tests for diff mode
2source shared.vim
3source screendump.vim
4
5func Test_diff_fold_sync()
6  enew!
7  let g:update_count = 0
8  au DiffUpdated * let g:update_count += 1
9
10  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
35  call assert_equal(1, g:update_count)
36  au! DiffUpdated
37
38  windo diffoff
39  close!
40  set nomodified
41endfunc
42
43func Test_vert_split()
44  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()
56  " 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
168  call delete('Xtest')
169  call delete('Xtest2')
170  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
185  windo diffoff
186  bwipe!
187  enew!
188endfunc
189
190func 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
221  %bwipe!
222endfunc
223
224" 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
244func 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!
298endfunc
299
300func 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
322func Test_diffoff()
323  enew!
324  call setline(1, ['Two', 'Three'])
325  redraw
326  let normattr = screenattr(1, 1)
327  diffthis
328  botright vert new
329  call setline(1, ['One', '', 'Two', 'Three'])
330  diffthis
331  redraw
332  call assert_notequal(normattr, screenattr(1, 1))
333  diffoff!
334  redraw
335  call assert_equal(normattr, screenattr(1, 1))
336  bwipe!
337  bwipe!
338endfunc
339
340func Common_icase_test()
341  edit one
342  call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve'])
343  redraw
344  let normattr = screenattr(1, 1)
345  diffthis
346
347  botright vert new two
348  call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VE'])
349  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
357  let dtextattr = screenattr(5, 3)
358  call assert_notequal(dtextattr, screenattr(5, 1))
359  call assert_notequal(dtextattr, screenattr(5, 5))
360
361  diffoff!
362  %bwipe!
363endfunc
364
365func Test_diffopt_icase()
366  set diffopt=icase,foldcolumn:0
367  call Common_icase_test()
368  set diffopt&
369endfunc
370
371func Test_diffopt_icase_internal()
372  set diffopt=icase,foldcolumn:0,internal
373  call Common_icase_test()
374  set diffopt&
375endfunc
376
377func Common_iwhite_test()
378  edit one
379  " Difference in trailing spaces and amount of spaces should be ignored,
380  " but not other space differences.
381  call setline(1, ["One \t", 'Two', 'Three', 'one two', 'one two', 'Four'])
382  redraw
383  let normattr = screenattr(1, 1)
384  diffthis
385
386  botright vert new two
387  call setline(1, ["One\t ", "Two\t ", 'Three', 'one   two', 'onetwo', ' Four'])
388  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))
394  call assert_equal(normattr, screenattr(4, 1))
395  call assert_notequal(normattr, screenattr(5, 1))
396  call assert_notequal(normattr, screenattr(6, 1))
397
398  diffoff!
399  %bwipe!
400endfunc
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()
411  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))
424  set diffopt=internal,context:2
425  call assert_equal('+--  2 lines: 1', foldtextresult(1))
426
427  set diffopt=context:1
428  call assert_equal('+--  3 lines: 1', foldtextresult(1))
429  set diffopt=internal,context:1
430  call assert_equal('+--  3 lines: 1', foldtextresult(1))
431
432  diffoff!
433  %bwipe!
434  set diffopt&
435endfunc
436
437func Test_diffopt_horizontal()
438  set diffopt=internal,horizontal
439  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()
452  set diffopt=internal,vertical
453  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
465func Test_diffopt_hiddenoff()
466  set diffopt=internal,filler,foldcolumn:0,hiddenoff
467  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
488func Test_diffoff_hidden()
489  set diffopt=internal,filler,foldcolumn:0
490  e! one
491  call setline(1, ['Two', 'Three'])
492  redraw
493  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
525func Test_setting_cursor()
526  new Xtest1
527  put =range(1,90)
528  wq
529  new Xtest2
530  put =range(1,100)
531  wq
532
533  tabe Xtest2
534  $
535  diffsp Xtest1
536  tabclose
537
538  call delete('Xtest1')
539  call delete('Xtest2')
540endfunc
541
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
564func Test_diffexpr()
565  if !executable('diff')
566    return
567  endif
568
569  func DiffExpr()
570    " 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
573  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
597func 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.
615  saveas! Xpatch
616  bwipe!
617  new
618  call assert_fails('diffpatch Xpatch', 'E816:')
619
620  for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
621    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
633  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
660
661func 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
702func 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
721
722func WriteDiffFiles(buf, list1, list2)
723  call writefile(a:list1, 'Xfile1')
724  call writefile(a:list2, 'Xfile2')
725  if a:buf
726    call term_sendkeys(a:buf, ":checktime\<CR>")
727  endif
728endfunc
729
730" Verify a screendump with both the internal and external diff.
731func VerifyBoth(buf, dumpfile, extra)
732  " trailing : for leaving the cursor on the command line
733  for cmd in [":set diffopt=filler" . a:extra . "\<CR>:", ":set diffopt+=internal\<CR>:"]
734    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
741" 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
749func Test_diff_screen()
750  if !CanRunVimInTerminal() || !has('menu')
751    throw 'Skipped: cannot make screendumps and/or menu feature missing'
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
758  call WriteDiffFiles(0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
759  let buf = RunVimInTerminal('-d Xfile1 Xfile2', {})
760  " Set autoread mode, so that Vim won't complain once we re-write the test
761  " files
762  call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
763
764  call VerifyBoth(buf, 'Test_diff_01', '')
765
766  " Test 2: Add a line in beginning of file 1
767  call WriteDiffFiles(buf, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
768  call VerifyBoth(buf, 'Test_diff_02', '')
769
770  " Test 3: Add a line at the end of file 2
771  call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
772  call VerifyBoth(buf, 'Test_diff_03', '')
773
774  " Test 4: Add a line at the end of file 1
775  call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
776  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
779  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])
780  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
783  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])
784  call VerifyBoth(buf, 'Test_diff_06', '')
785
786  " 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
797  " Test 7 - 9: Test normal/patience/histogram diff algorithm
798  call WriteDiffFiles(buf, ['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
799      \ '    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>")
820  call WriteDiffFiles(buf, ['', '  def finalize(values)', '', '    values.each do |v|', '      v.finalize', '    end'],
821      \ ['', '  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
827  " 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')
837
838  " Test 12: diff the same file
839  call WriteDiffFiles(buf, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
840  call VerifyBoth(buf, 'Test_diff_12', '')
841
842  " Test 13: diff an empty file
843  call WriteDiffFiles(buf, [], [])
844  call VerifyBoth(buf, 'Test_diff_13', '')
845
846  " Test 14: test diffopt+=icase
847  call WriteDiffFiles(buf, ['a', 'b', 'cd'], ['A', 'b', 'cDe'])
848  call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
849
850  " Test 15-16: test diffopt+=iwhite
851  call WriteDiffFiles(buf, ['int main()', '{', '   printf("Hello, World!");', '   return 0;', '}'],
852      \ ['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
859  " 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
875  " clean up
876  call StopVimInTerminal(buf)
877  call delete('Xfile1')
878  call delete('Xfile2')
879endfunc
880
881func Test_diff_with_cursorline()
882  if !CanRunVimInTerminal()
883    throw 'Skipped: cannot run Vim in a terminal window'
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
907
908func Test_diff_of_diff()
909  if !CanRunVimInTerminal()
910    throw 'Skipped: cannot run Vim in a terminal window'
911  endif
912  if !has("rightleft")
913    throw 'Skipped: rightleft not supported'
914  endif
915
916  call writefile([
917	\ 'call setline(1, ["aa","bb","cc","@@ -3,2 +5,7 @@","dd","ee","ff"])',
918	\ 'vnew',
919	\ 'call setline(1, ["aa","bb","cc"])',
920	\ 'windo diffthis',
921	\ '1wincmd w',
922	\ 'setlocal number',
923	\ ], 'Xtest_diff_diff')
924  let buf = RunVimInTerminal('-S Xtest_diff_diff', {})
925
926  call VerifyScreenDump(buf, 'Test_diff_of_diff_01', {})
927
928  call term_sendkeys(buf, ":set rightleft\<cr>")
929  call VerifyScreenDump(buf, 'Test_diff_of_diff_02', {})
930
931  " clean up
932  call StopVimInTerminal(buf)
933  call delete('Xtest_diff_diff')
934endfunc
935