1" Tests for diff mode
2set belloff=all
3
4func Test_diff_fold_sync()
5  enew!
6  let l = range(50)
7  call setline(1, l)
8  diffthis
9  let winone = win_getid()
10  new
11  let l[25] = 'diff'
12  call setline(1, l)
13  diffthis
14  let wintwo = win_getid()
15  " line 15 is inside the closed fold
16  call assert_equal(19, foldclosedend(10))
17  call win_gotoid(winone)
18  call assert_equal(19, foldclosedend(10))
19  " open the fold
20  normal zv
21  call assert_equal(-1, foldclosedend(10))
22  " fold in other window must have opened too
23  call win_gotoid(wintwo)
24  call assert_equal(-1, foldclosedend(10))
25
26  " cursor position is in sync
27  normal 23G
28  call win_gotoid(winone)
29  call assert_equal(23, getcurpos()[1])
30
31  windo diffoff
32  close!
33  set nomodified
34endfunc
35
36func Test_vert_split()
37  " Disable the title to avoid xterm keeping the wrong one.
38  set notitle noicon
39  new
40  let l = ['1 aa', '2 bb', '3 cc', '4 dd', '5 ee']
41  call setline(1, l)
42  w! Xtest
43  normal dd
44  $
45  put
46  normal kkrXoxxx
47  w! Xtest2
48  file Nop
49  normal ggoyyyjjjozzzz
50  set foldmethod=marker foldcolumn=4
51  call assert_equal(0, &diff)
52  call assert_equal('marker', &foldmethod)
53  call assert_equal(4, &foldcolumn)
54  call assert_equal(0, &scrollbind)
55  call assert_equal(0, &cursorbind)
56  call assert_equal(1, &wrap)
57
58  vert diffsplit Xtest
59  vert diffsplit Xtest2
60  call assert_equal(1, &diff)
61  call assert_equal('diff', &foldmethod)
62  call assert_equal(2, &foldcolumn)
63  call assert_equal(1, &scrollbind)
64  call assert_equal(1, &cursorbind)
65  call assert_equal(0, &wrap)
66
67  let diff_fdm = &fdm
68  let diff_fdc = &fdc
69  " repeat entering diff mode here to see if this saves the wrong settings
70  diffthis
71  " jump to second window for a moment to have filler line appear at start of
72  " first window
73  wincmd w
74  normal gg
75  wincmd p
76  normal gg
77  call assert_equal(2, winline())
78  normal j
79  call assert_equal(4, winline())
80  normal j
81  call assert_equal(5, winline())
82  normal j
83  call assert_equal(6, winline())
84  normal j
85  call assert_equal(8, winline())
86  normal j
87  call assert_equal(9, winline())
88
89  wincmd w
90  normal gg
91  call assert_equal(1, winline())
92  normal j
93  call assert_equal(2, winline())
94  normal j
95  call assert_equal(4, winline())
96  normal j
97  call assert_equal(5, winline())
98  normal j
99  call assert_equal(8, winline())
100
101  wincmd w
102  normal gg
103  call assert_equal(2, winline())
104  normal j
105  call assert_equal(3, winline())
106  normal j
107  call assert_equal(4, winline())
108  normal j
109  call assert_equal(5, winline())
110  normal j
111  call assert_equal(6, winline())
112  normal j
113  call assert_equal(7, winline())
114  normal j
115  call assert_equal(8, winline())
116
117  " Test diffoff
118  diffoff!
119  1wincmd 2
120  let &diff = 1
121  let &fdm = diff_fdm
122  let &fdc = diff_fdc
123  4wincmd w
124  diffoff!
125  1wincmd w
126  call assert_equal(0, &diff)
127  call assert_equal('marker', &foldmethod)
128  call assert_equal(4, &foldcolumn)
129  call assert_equal(0, &scrollbind)
130  call assert_equal(0, &cursorbind)
131  call assert_equal(1, &wrap)
132
133  wincmd w
134  call assert_equal(0, &diff)
135  call assert_equal('marker', &foldmethod)
136  call assert_equal(4, &foldcolumn)
137  call assert_equal(0, &scrollbind)
138  call assert_equal(0, &cursorbind)
139  call assert_equal(1, &wrap)
140
141  wincmd w
142  call assert_equal(0, &diff)
143  call assert_equal('marker', &foldmethod)
144  call assert_equal(4, &foldcolumn)
145  call assert_equal(0, &scrollbind)
146  call assert_equal(0, &cursorbind)
147  call assert_equal(1, &wrap)
148
149  call delete('Xtest')
150  call delete('Xtest2')
151  windo bw!
152endfunc
153
154func Test_filler_lines()
155  " Test that diffing shows correct filler lines
156  enew!
157  put =range(4,10)
158  1d _
159  vnew
160  put =range(1,10)
161  1d _
162  windo diffthis
163  wincmd h
164  call assert_equal(1, line('w0'))
165  unlet! diff_fdm diff_fdc
166  windo diffoff
167  bwipe!
168  enew!
169endfunc
170
171func Test_diffget_diffput()
172  enew!
173  let l = range(50)
174  call setline(1, l)
175  call assert_fails('diffget', 'E99:')
176  diffthis
177  call assert_fails('diffget', 'E100:')
178  new
179  let l[10] = 'one'
180  let l[20] = 'two'
181  let l[30] = 'three'
182  let l[40] = 'four'
183  call setline(1, l)
184  diffthis
185  call assert_equal('one', getline(11))
186  11diffget
187  call assert_equal('10', getline(11))
188  21diffput
189  wincmd w
190  call assert_equal('two', getline(21))
191  normal 31Gdo
192  call assert_equal('three', getline(31))
193  call assert_equal('40', getline(41))
194  normal 41Gdp
195  wincmd w
196  call assert_equal('40', getline(41))
197  new
198  diffthis
199  call assert_fails('diffget', 'E101:')
200
201  windo diffoff
202  %bwipe!
203endfunc
204
205func Test_dp_do_buffer()
206  e! one
207  let bn1=bufnr('%')
208  let l = range(60)
209  call setline(1, l)
210  diffthis
211
212  new two
213  let l[10] = 'one'
214  let l[20] = 'two'
215  let l[30] = 'three'
216  let l[40] = 'four'
217  let l[50] = 'five'
218  call setline(1, l)
219  diffthis
220
221  " dp and do with invalid buffer number.
222  11
223  call assert_fails('norm 99999dp', 'E102:')
224  call assert_fails('norm 99999do', 'E102:')
225  call assert_fails('diffput non_existing_buffer', 'E94:')
226  call assert_fails('diffget non_existing_buffer', 'E94:')
227
228  " dp and do with valid buffer number.
229  call assert_equal('one', getline('.'))
230  exe 'norm ' . bn1 . 'do'
231  call assert_equal('10', getline('.'))
232  21
233  call assert_equal('two', getline('.'))
234  diffget one
235  call assert_equal('20', getline('.'))
236
237  31
238  exe 'norm ' . bn1 . 'dp'
239  41
240  diffput one
241  wincmd w
242  31
243  call assert_equal('three', getline('.'))
244  41
245  call assert_equal('four', getline('.'))
246
247  " dp and do with buffer number which is not in diff mode.
248  new not_in_diff_mode
249  let bn3=bufnr('%')
250  wincmd w
251  51
252  call assert_fails('exe "norm" . bn3 . "dp"', 'E103:')
253  call assert_fails('exe "norm" . bn3 . "do"', 'E103:')
254  call assert_fails('diffput not_in_diff_mode', 'E94:')
255  call assert_fails('diffget not_in_diff_mode', 'E94:')
256
257  windo diffoff
258  %bwipe!
259endfunc
260
261func Test_diffoff()
262  enew!
263  call setline(1, ['Two', 'Three'])
264  redraw
265  let normattr = screenattr(1, 1)
266  diffthis
267  botright vert new
268  call setline(1, ['One', '', 'Two', 'Three'])
269  diffthis
270  redraw
271  call assert_notequal(normattr, screenattr(1, 1))
272  diffoff!
273  redraw
274  call assert_equal(normattr, screenattr(1, 1))
275  bwipe!
276  bwipe!
277endfunc
278
279func Test_diffopt_icase()
280  set diffopt=icase,foldcolumn:0
281
282  e one
283  call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve'])
284  redraw
285  let normattr = screenattr(1, 1)
286  diffthis
287
288  botright vert new two
289  call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VE'])
290  diffthis
291
292  redraw
293  call assert_equal(normattr, screenattr(1, 1))
294  call assert_equal(normattr, screenattr(2, 1))
295  call assert_notequal(normattr, screenattr(3, 1))
296  call assert_equal(normattr, screenattr(4, 1))
297
298  let dtextattr = screenattr(5, 3)
299  call assert_notequal(dtextattr, screenattr(5, 1))
300  call assert_notequal(dtextattr, screenattr(5, 5))
301
302  diffoff!
303  %bwipe!
304  set diffopt&
305endfunc
306
307func Test_diffopt_iwhite()
308  set diffopt=iwhite,foldcolumn:0
309
310  e one
311  " Difference in trailing spaces should be ignored,
312  " but not other space differences.
313  call setline(1, ["One \t", 'Two', 'Three', 'Four'])
314  redraw
315  let normattr = screenattr(1, 1)
316  diffthis
317
318  botright vert new two
319  call setline(1, ["One\t ", "Two\t ", 'Three', ' Four'])
320  diffthis
321
322  redraw
323  call assert_equal(normattr, screenattr(1, 1))
324  call assert_equal(normattr, screenattr(2, 1))
325  call assert_equal(normattr, screenattr(3, 1))
326  call assert_notequal(normattr, screenattr(4, 1))
327
328  diffoff!
329  %bwipe!
330  set diffopt&
331endfunc
332
333func Test_diffopt_context()
334  enew!
335  call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
336  diffthis
337  new
338  call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
339  diffthis
340
341  set diffopt=context:2
342  call assert_equal('+--  2 lines: 1', foldtextresult(1))
343  set diffopt=context:1
344  call assert_equal('+--  3 lines: 1', foldtextresult(1))
345
346  diffoff!
347  %bwipe!
348  set diffopt&
349endfunc
350
351func Test_diffopt_horizontal()
352  set diffopt=horizontal
353  diffsplit
354
355  call assert_equal(&columns, winwidth(1))
356  call assert_equal(&columns, winwidth(2))
357  call assert_equal(&lines, winheight(1) + winheight(2) + 3)
358  call assert_inrange(0, 1, winheight(1) - winheight(2))
359
360  set diffopt&
361  diffoff!
362  %bwipe
363endfunc
364
365func Test_diffopt_vertical()
366  set diffopt=vertical
367  diffsplit
368
369  call assert_equal(&lines - 2, winheight(1))
370  call assert_equal(&lines - 2, winheight(2))
371  call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
372  call assert_inrange(0, 1, winwidth(1) - winwidth(2))
373
374  set diffopt&
375  diffoff!
376  %bwipe
377endfunc
378
379func Test_diffoff_hidden()
380  set diffopt=filler,foldcolumn:0
381  e! one
382  call setline(1, ['Two', 'Three'])
383  redraw
384  let normattr = screenattr(1, 1)
385  diffthis
386  botright vert new two
387  call setline(1, ['One', 'Four'])
388  diffthis
389  redraw
390  call assert_notequal(normattr, screenattr(1, 1))
391  set hidden
392  close
393  redraw
394  " diffing with hidden buffer two
395  call assert_notequal(normattr, screenattr(1, 1))
396  diffoff
397  redraw
398  call assert_equal(normattr, screenattr(1, 1))
399  diffthis
400  redraw
401  " still diffing with hidden buffer two
402  call assert_notequal(normattr, screenattr(1, 1))
403  diffoff!
404  redraw
405  call assert_equal(normattr, screenattr(1, 1))
406  diffthis
407  redraw
408  " no longer diffing with hidden buffer two
409  call assert_equal(normattr, screenattr(1, 1))
410
411  bwipe!
412  bwipe!
413  set hidden& diffopt&
414endfunc
415
416func Test_setting_cursor()
417  new Xtest1
418  put =range(1,90)
419  wq
420  new Xtest2
421  put =range(1,100)
422  wq
423
424  tabe Xtest2
425  $
426  diffsp Xtest1
427  tabclose
428
429  call delete('Xtest1')
430  call delete('Xtest2')
431endfunc
432
433func Test_diff_move_to()
434  new
435  call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
436  diffthis
437  vnew
438  call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
439  diffthis
440  norm ]c
441  call assert_equal(2, line('.'))
442  norm 3]c
443  call assert_equal(9, line('.'))
444  norm 10]c
445  call assert_equal(11, line('.'))
446  norm [c
447  call assert_equal(9, line('.'))
448  norm 2[c
449  call assert_equal(5, line('.'))
450  norm 10[c
451  call assert_equal(2, line('.'))
452  %bwipe!
453endfunc
454
455func Test_diffexpr()
456  if !executable('diff')
457    return
458  endif
459
460  func DiffExpr()
461    silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>' . v:fname_out
462  endfunc
463  set diffexpr=DiffExpr()
464  set diffopt=foldcolumn:0
465
466  enew!
467  call setline(1, ['one', 'two', 'three'])
468  redraw
469  let normattr = screenattr(1, 1)
470  diffthis
471
472  botright vert new
473  call setline(1, ['one', 'two', 'three.'])
474  diffthis
475
476  redraw
477  call assert_equal(normattr, screenattr(1, 1))
478  call assert_equal(normattr, screenattr(2, 1))
479  call assert_notequal(normattr, screenattr(3, 1))
480
481  diffoff!
482  %bwipe!
483  set diffexpr& diffopt&
484endfunc
485
486func Test_diffpatch()
487  " The patch program on MS-Windows may fail or hang.
488  if !executable('patch') || !has('unix')
489    return
490  endif
491  new
492  insert
493***************
494*** 1,3 ****
495  1
496! 2
497  3
498--- 1,4 ----
499  1
500! 2x
501  3
502+ 4
503.
504  saveas! Xpatch
505  bwipe!
506  new
507  call assert_fails('diffpatch Xpatch', 'E816:')
508
509  for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
510    call setline(1, ['1', '2', '3'])
511    if name != 'Xpatch'
512      call rename('Xpatch', name)
513    endif
514    exe 'diffpatch ' . escape(name, '$')
515    call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
516    if name != 'Xpatch'
517      call rename(name, 'Xpatch')
518    endif
519    bwipe!
520  endfor
521
522  call delete('Xpatch')
523  bwipe!
524endfunc
525
526func Test_diff_too_many_buffers()
527  for i in range(1, 8)
528    exe "new Xtest" . i
529    diffthis
530  endfor
531  new Xtest9
532  call assert_fails('diffthis', 'E96:')
533  %bwipe!
534endfunc
535
536func Test_diff_nomodifiable()
537  new
538  call setline(1, [1, 2, 3, 4])
539  setl nomodifiable
540  diffthis
541  vnew
542  call setline(1, ['1x', 2, 3, 3, 4])
543  diffthis
544  call assert_fails('norm dp', 'E793:')
545  setl nomodifiable
546  call assert_fails('norm do', 'E21:')
547  %bwipe!
548endfunc
549
550func Test_diff_hlID()
551  new
552  call setline(1, [1, 2, 3])
553  diffthis
554  vnew
555  call setline(1, ['1x', 2, 'x', 3])
556  diffthis
557  redraw
558
559  call assert_equal(synIDattr(diff_hlID(-1, 1), "name"), "")
560
561  call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
562  call assert_equal(synIDattr(diff_hlID(1, 2), "name"), "DiffText")
563  call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
564  call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "DiffAdd")
565  call assert_equal(synIDattr(diff_hlID(4, 1), "name"), "")
566
567  wincmd w
568  call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
569  call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
570  call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
571
572  %bwipe!
573endfunc
574
575func Test_diff_filler()
576  new
577  call setline(1, [1, 2, 3, 'x', 4])
578  diffthis
579  vnew
580  call setline(1, [1, 2, 'y', 'y', 3, 4])
581  diffthis
582  redraw
583
584  call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'diff_filler(v:val)'))
585  wincmd w
586  call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
587
588  %bwipe!
589endfunc
590
591func Test_diff_lastline()
592  enew!
593  only!
594  call setline(1, ['This is a ', 'line with five ', 'rows'])
595  diffthis
596  botright vert new
597  call setline(1, ['This is', 'a line with ', 'four rows'])
598  diffthis
599  1
600  call feedkeys("Je a\<CR>", 'tx')
601  call feedkeys("Je a\<CR>", 'tx')
602  let w1lines = winline()
603  wincmd w
604  $
605  let w2lines = winline()
606  call assert_equal(w2lines, w1lines)
607  bwipe!
608  bwipe!
609endfunc
610