blob: d60de5046c0aae9ece9a74dc71966f494a06ebd0 [file] [log] [blame]
Bram Moolenaar42093c02016-07-30 16:16:54 +02001" Tests for diff mode
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002
Bram Moolenaare828b762018-09-10 17:51:58 +02003source shared.vim
4source screendump.vim
Bram Moolenaar3c8ee622019-08-03 22:55:50 +02005source check.vim
Bram Moolenaar42093c02016-07-30 16:16:54 +02006
7func Test_diff_fold_sync()
8 enew!
Bram Moolenaare8fa05b2018-09-16 15:48:06 +02009 let g:update_count = 0
10 au DiffUpdated * let g:update_count += 1
11
Bram Moolenaar42093c02016-07-30 16:16:54 +020012 let l = range(50)
13 call setline(1, l)
14 diffthis
15 let winone = win_getid()
16 new
17 let l[25] = 'diff'
18 call setline(1, l)
19 diffthis
20 let wintwo = win_getid()
21 " line 15 is inside the closed fold
22 call assert_equal(19, foldclosedend(10))
23 call win_gotoid(winone)
24 call assert_equal(19, foldclosedend(10))
25 " open the fold
26 normal zv
27 call assert_equal(-1, foldclosedend(10))
28 " fold in other window must have opened too
29 call win_gotoid(wintwo)
30 call assert_equal(-1, foldclosedend(10))
31
32 " cursor position is in sync
33 normal 23G
34 call win_gotoid(winone)
35 call assert_equal(23, getcurpos()[1])
36
Bram Moolenaarf65cc662022-06-26 18:17:50 +010037 " depending on how redraw is done DiffUpdated may be triggered once or twice
38 call assert_inrange(1, 2, g:update_count)
Bram Moolenaare8fa05b2018-09-16 15:48:06 +020039 au! DiffUpdated
40
Bram Moolenaar42093c02016-07-30 16:16:54 +020041 windo diffoff
42 close!
43 set nomodified
44endfunc
45
46func Test_vert_split()
Bram Moolenaare828b762018-09-10 17:51:58 +020047 set diffopt=filler
48 call Common_vert_split()
49 set diffopt&
50endfunc
51
52func Test_vert_split_internal()
53 set diffopt=internal,filler
54 call Common_vert_split()
55 set diffopt&
56endfunc
57
58func Common_vert_split()
Bram Moolenaar42093c02016-07-30 16:16:54 +020059 " Disable the title to avoid xterm keeping the wrong one.
60 set notitle noicon
61 new
62 let l = ['1 aa', '2 bb', '3 cc', '4 dd', '5 ee']
63 call setline(1, l)
64 w! Xtest
65 normal dd
66 $
67 put
68 normal kkrXoxxx
69 w! Xtest2
70 file Nop
71 normal ggoyyyjjjozzzz
72 set foldmethod=marker foldcolumn=4
73 call assert_equal(0, &diff)
74 call assert_equal('marker', &foldmethod)
75 call assert_equal(4, &foldcolumn)
76 call assert_equal(0, &scrollbind)
77 call assert_equal(0, &cursorbind)
78 call assert_equal(1, &wrap)
79
80 vert diffsplit Xtest
81 vert diffsplit Xtest2
82 call assert_equal(1, &diff)
83 call assert_equal('diff', &foldmethod)
84 call assert_equal(2, &foldcolumn)
85 call assert_equal(1, &scrollbind)
86 call assert_equal(1, &cursorbind)
87 call assert_equal(0, &wrap)
88
89 let diff_fdm = &fdm
90 let diff_fdc = &fdc
91 " repeat entering diff mode here to see if this saves the wrong settings
92 diffthis
93 " jump to second window for a moment to have filler line appear at start of
94 " first window
95 wincmd w
96 normal gg
97 wincmd p
98 normal gg
99 call assert_equal(2, winline())
100 normal j
101 call assert_equal(4, winline())
102 normal j
103 call assert_equal(5, winline())
104 normal j
105 call assert_equal(6, winline())
106 normal j
107 call assert_equal(8, winline())
108 normal j
109 call assert_equal(9, winline())
110
111 wincmd w
112 normal gg
113 call assert_equal(1, winline())
114 normal j
115 call assert_equal(2, winline())
116 normal j
117 call assert_equal(4, winline())
118 normal j
119 call assert_equal(5, winline())
120 normal j
121 call assert_equal(8, winline())
122
123 wincmd w
124 normal gg
125 call assert_equal(2, winline())
126 normal j
127 call assert_equal(3, winline())
128 normal j
129 call assert_equal(4, winline())
130 normal j
131 call assert_equal(5, winline())
132 normal j
133 call assert_equal(6, winline())
134 normal j
135 call assert_equal(7, winline())
136 normal j
137 call assert_equal(8, winline())
138
139 " Test diffoff
140 diffoff!
zeertzjq5fd6ab82022-08-17 12:09:45 +0100141 1wincmd w
Bram Moolenaar42093c02016-07-30 16:16:54 +0200142 let &diff = 1
143 let &fdm = diff_fdm
144 let &fdc = diff_fdc
145 4wincmd w
146 diffoff!
147 1wincmd w
148 call assert_equal(0, &diff)
149 call assert_equal('marker', &foldmethod)
150 call assert_equal(4, &foldcolumn)
151 call assert_equal(0, &scrollbind)
152 call assert_equal(0, &cursorbind)
153 call assert_equal(1, &wrap)
154
155 wincmd w
156 call assert_equal(0, &diff)
157 call assert_equal('marker', &foldmethod)
158 call assert_equal(4, &foldcolumn)
159 call assert_equal(0, &scrollbind)
160 call assert_equal(0, &cursorbind)
161 call assert_equal(1, &wrap)
162
163 wincmd w
164 call assert_equal(0, &diff)
165 call assert_equal('marker', &foldmethod)
166 call assert_equal(4, &foldcolumn)
167 call assert_equal(0, &scrollbind)
168 call assert_equal(0, &cursorbind)
169 call assert_equal(1, &wrap)
170
Bram Moolenaar623cf882016-07-30 16:36:01 +0200171 call delete('Xtest')
172 call delete('Xtest2')
Bram Moolenaar42093c02016-07-30 16:16:54 +0200173 windo bw!
174endfunc
175
176func Test_filler_lines()
177 " Test that diffing shows correct filler lines
178 enew!
179 put =range(4,10)
180 1d _
181 vnew
182 put =range(1,10)
183 1d _
184 windo diffthis
185 wincmd h
186 call assert_equal(1, line('w0'))
187 unlet! diff_fdm diff_fdc
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200188 windo diffoff
189 bwipe!
190 enew!
191endfunc
Bram Moolenaar42093c02016-07-30 16:16:54 +0200192
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200193func Test_diffget_diffput()
194 enew!
195 let l = range(50)
196 call setline(1, l)
197 call assert_fails('diffget', 'E99:')
198 diffthis
199 call assert_fails('diffget', 'E100:')
200 new
201 let l[10] = 'one'
202 let l[20] = 'two'
203 let l[30] = 'three'
204 let l[40] = 'four'
205 call setline(1, l)
206 diffthis
207 call assert_equal('one', getline(11))
208 11diffget
209 call assert_equal('10', getline(11))
210 21diffput
211 wincmd w
212 call assert_equal('two', getline(21))
213 normal 31Gdo
214 call assert_equal('three', getline(31))
215 call assert_equal('40', getline(41))
216 normal 41Gdp
217 wincmd w
218 call assert_equal('40', getline(41))
219 new
220 diffthis
221 call assert_fails('diffget', 'E101:')
222
223 windo diffoff
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200224 %bwipe!
225endfunc
226
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +0200227" Test putting two changes from one buffer to another
228func Test_diffput_two()
229 new a
230 let win_a = win_getid()
231 call setline(1, range(1, 10))
232 diffthis
233 new b
234 let win_b = win_getid()
235 call setline(1, range(1, 10))
236 8del
237 5del
238 diffthis
239 call win_gotoid(win_a)
240 %diffput
241 call win_gotoid(win_b)
242 call assert_equal(map(range(1, 10), 'string(v:val)'), getline(1, '$'))
243 bwipe! a
244 bwipe! b
245endfunc
246
Yegappan Lakshmanan30443242021-06-10 21:52:15 +0200247" Test for :diffget/:diffput with a range that is inside a diff chunk
248func Test_diffget_diffput_range()
249 call setline(1, range(1, 10))
250 new
251 call setline(1, range(11, 20))
252 windo diffthis
253 3,5diffget
254 call assert_equal(['13', '14', '15'], getline(3, 5))
255 call setline(1, range(1, 10))
256 4,8diffput
257 wincmd p
258 call assert_equal(['13', '4', '5', '6', '7', '8', '19'], getline(3, 9))
259 %bw!
260endfunc
261
262" Test for :diffget/:diffput with an empty buffer and a non-empty buffer
263func Test_diffget_diffput_empty_buffer()
264 %d _
265 new
266 call setline(1, 'one')
267 windo diffthis
268 diffget
269 call assert_equal(['one'], getline(1, '$'))
270 %d _
271 diffput
272 wincmd p
273 call assert_equal([''], getline(1, '$'))
274 %bw!
275endfunc
276
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100277" :diffput and :diffget completes names of buffers which
Dominique Pelle923dce22021-11-21 11:36:04 +0000278" are in diff mode and which are different than current buffer.
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100279" No completion when the current window is not in diff mode.
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100280func Test_diffget_diffput_completion()
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100281 e Xdiff1 | diffthis
282 botright new Xdiff2
283 botright new Xdiff3 | split | diffthis
284 botright new Xdiff4 | diffthis
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100285
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100286 wincmd t
287 call assert_equal('Xdiff1', bufname('%'))
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100288 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100289 call assert_equal('"diffput Xdiff3 Xdiff4', @:)
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100290 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100291 call assert_equal('"diffget Xdiff3 Xdiff4', @:)
292 call assert_equal(['Xdiff3', 'Xdiff4'], getcompletion('', 'diff_buffer'))
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100293
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100294 " Xdiff2 is not in diff mode, so no completion for :diffput, :diffget
295 wincmd j
296 call assert_equal('Xdiff2', bufname('%'))
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100297 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
298 call assert_equal('"diffput ', @:)
299 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
300 call assert_equal('"diffget ', @:)
301 call assert_equal([], getcompletion('', 'diff_buffer'))
302
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100303 " Xdiff3 is split in 2 windows, only the top one is in diff mode.
304 " So completion of :diffput :diffget only happens in the top window.
305 wincmd j
306 call assert_equal('Xdiff3', bufname('%'))
307 call assert_equal(1, &diff)
308 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
309 call assert_equal('"diffput Xdiff1 Xdiff4', @:)
310 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
311 call assert_equal('"diffget Xdiff1 Xdiff4', @:)
312 call assert_equal(['Xdiff1', 'Xdiff4'], getcompletion('', 'diff_buffer'))
313
314 wincmd j
315 call assert_equal('Xdiff3', bufname('%'))
316 call assert_equal(0, &diff)
317 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
318 call assert_equal('"diffput ', @:)
319 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
320 call assert_equal('"diffget ', @:)
321 call assert_equal([], getcompletion('', 'diff_buffer'))
322
323 wincmd j
324 call assert_equal('Xdiff4', bufname('%'))
325 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
326 call assert_equal('"diffput Xdiff1 Xdiff3', @:)
327 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
328 call assert_equal('"diffget Xdiff1 Xdiff3', @:)
329 call assert_equal(['Xdiff1', 'Xdiff3'], getcompletion('', 'diff_buffer'))
330
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100331 %bwipe
332endfunc
333
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200334func Test_dp_do_buffer()
335 e! one
336 let bn1=bufnr('%')
337 let l = range(60)
338 call setline(1, l)
339 diffthis
340
341 new two
342 let l[10] = 'one'
343 let l[20] = 'two'
344 let l[30] = 'three'
345 let l[40] = 'four'
346 let l[50] = 'five'
347 call setline(1, l)
348 diffthis
349
350 " dp and do with invalid buffer number.
351 11
352 call assert_fails('norm 99999dp', 'E102:')
353 call assert_fails('norm 99999do', 'E102:')
354 call assert_fails('diffput non_existing_buffer', 'E94:')
355 call assert_fails('diffget non_existing_buffer', 'E94:')
356
357 " dp and do with valid buffer number.
358 call assert_equal('one', getline('.'))
359 exe 'norm ' . bn1 . 'do'
360 call assert_equal('10', getline('.'))
361 21
362 call assert_equal('two', getline('.'))
Bram Moolenaar94722c52023-01-28 19:19:03 +0000363 diffget one
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200364 call assert_equal('20', getline('.'))
365
366 31
367 exe 'norm ' . bn1 . 'dp'
368 41
369 diffput one
370 wincmd w
371 31
372 call assert_equal('three', getline('.'))
373 41
374 call assert_equal('four', getline('.'))
375
376 " dp and do with buffer number which is not in diff mode.
377 new not_in_diff_mode
378 let bn3=bufnr('%')
379 wincmd w
380 51
381 call assert_fails('exe "norm" . bn3 . "dp"', 'E103:')
382 call assert_fails('exe "norm" . bn3 . "do"', 'E103:')
383 call assert_fails('diffput not_in_diff_mode', 'E94:')
384 call assert_fails('diffget not_in_diff_mode', 'E94:')
385
386 windo diffoff
387 %bwipe!
Bram Moolenaar42093c02016-07-30 16:16:54 +0200388endfunc
Bram Moolenaare67d5462016-08-27 22:40:42 +0200389
Bram Moolenaardf77cef2018-10-07 17:46:42 +0200390func Test_do_lastline()
391 e! one
392 call setline(1, ['1','2','3','4','5','6'])
393 diffthis
394
395 new two
396 call setline(1, ['2','4','5'])
397 diffthis
398
399 1
400 norm dp]c
401 norm dp]c
402 wincmd w
403 call assert_equal(4, line('$'))
404 norm G
405 norm do
406 call assert_equal(3, line('$'))
407
408 windo diffoff
409 %bwipe!
410endfunc
411
Bram Moolenaare67d5462016-08-27 22:40:42 +0200412func Test_diffoff()
413 enew!
414 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200415 redraw
Bram Moolenaare67d5462016-08-27 22:40:42 +0200416 let normattr = screenattr(1, 1)
417 diffthis
418 botright vert new
419 call setline(1, ['One', '', 'Two', 'Three'])
420 diffthis
421 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +0200422 call assert_notequal(normattr, 1->screenattr(1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200423 diffoff!
424 redraw
425 call assert_equal(normattr, screenattr(1, 1))
426 bwipe!
427 bwipe!
428endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200429
Bram Moolenaare828b762018-09-10 17:51:58 +0200430func Common_icase_test()
431 edit one
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200432 call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200433 redraw
434 let normattr = screenattr(1, 1)
435 diffthis
436
437 botright vert new two
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200438 call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VE'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200439 diffthis
440
441 redraw
442 call assert_equal(normattr, screenattr(1, 1))
443 call assert_equal(normattr, screenattr(2, 1))
444 call assert_notequal(normattr, screenattr(3, 1))
445 call assert_equal(normattr, screenattr(4, 1))
446
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200447 let dtextattr = screenattr(5, 3)
448 call assert_notequal(dtextattr, screenattr(5, 1))
449 call assert_notequal(dtextattr, screenattr(5, 5))
450
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200451 diffoff!
452 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200453endfunc
454
455func Test_diffopt_icase()
456 set diffopt=icase,foldcolumn:0
457 call Common_icase_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200458 set diffopt&
459endfunc
460
Bram Moolenaare828b762018-09-10 17:51:58 +0200461func Test_diffopt_icase_internal()
462 set diffopt=icase,foldcolumn:0,internal
463 call Common_icase_test()
464 set diffopt&
465endfunc
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200466
Bram Moolenaare828b762018-09-10 17:51:58 +0200467func Common_iwhite_test()
468 edit one
469 " Difference in trailing spaces and amount of spaces should be ignored,
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200470 " but not other space differences.
Bram Moolenaare828b762018-09-10 17:51:58 +0200471 call setline(1, ["One \t", 'Two', 'Three', 'one two', 'one two', 'Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200472 redraw
473 let normattr = screenattr(1, 1)
474 diffthis
475
476 botright vert new two
Bram Moolenaare828b762018-09-10 17:51:58 +0200477 call setline(1, ["One\t ", "Two\t ", 'Three', 'one two', 'onetwo', ' Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200478 diffthis
479
480 redraw
481 call assert_equal(normattr, screenattr(1, 1))
482 call assert_equal(normattr, screenattr(2, 1))
483 call assert_equal(normattr, screenattr(3, 1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200484 call assert_equal(normattr, screenattr(4, 1))
485 call assert_notequal(normattr, screenattr(5, 1))
486 call assert_notequal(normattr, screenattr(6, 1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200487
488 diffoff!
489 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200490endfunc
491
492func Test_diffopt_iwhite()
493 set diffopt=iwhite,foldcolumn:0
494 call Common_iwhite_test()
495 set diffopt&
496endfunc
497
498func Test_diffopt_iwhite_internal()
499 set diffopt=internal,iwhite,foldcolumn:0
500 call Common_iwhite_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200501 set diffopt&
502endfunc
503
504func Test_diffopt_context()
505 enew!
506 call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
507 diffthis
508 new
509 call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
510 diffthis
511
512 set diffopt=context:2
513 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200514 set diffopt=internal,context:2
515 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
516
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200517 set diffopt=context:1
518 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200519 set diffopt=internal,context:1
520 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200521
522 diffoff!
523 %bwipe!
524 set diffopt&
525endfunc
526
527func Test_diffopt_horizontal()
Bram Moolenaare828b762018-09-10 17:51:58 +0200528 set diffopt=internal,horizontal
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200529 diffsplit
530
531 call assert_equal(&columns, winwidth(1))
532 call assert_equal(&columns, winwidth(2))
533 call assert_equal(&lines, winheight(1) + winheight(2) + 3)
534 call assert_inrange(0, 1, winheight(1) - winheight(2))
535
536 set diffopt&
537 diffoff!
538 %bwipe
539endfunc
540
541func Test_diffopt_vertical()
Bram Moolenaare828b762018-09-10 17:51:58 +0200542 set diffopt=internal,vertical
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200543 diffsplit
544
545 call assert_equal(&lines - 2, winheight(1))
546 call assert_equal(&lines - 2, winheight(2))
547 call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
548 call assert_inrange(0, 1, winwidth(1) - winwidth(2))
549
550 set diffopt&
551 diffoff!
552 %bwipe
553endfunc
554
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100555func Test_diffopt_hiddenoff()
Bram Moolenaare828b762018-09-10 17:51:58 +0200556 set diffopt=internal,filler,foldcolumn:0,hiddenoff
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100557 e! one
558 call setline(1, ['Two', 'Three'])
559 redraw
560 let normattr = screenattr(1, 1)
561 diffthis
562 botright vert new two
563 call setline(1, ['One', 'Four'])
564 diffthis
565 redraw
566 call assert_notequal(normattr, screenattr(1, 1))
567 set hidden
568 close
569 redraw
570 " should not diffing with hidden buffer two while 'hiddenoff' is enabled
571 call assert_equal(normattr, screenattr(1, 1))
572
573 bwipe!
574 bwipe!
575 set hidden& diffopt&
576endfunc
577
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100578func Test_diffoff_hidden()
Bram Moolenaare828b762018-09-10 17:51:58 +0200579 set diffopt=internal,filler,foldcolumn:0
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100580 e! one
581 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200582 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100583 let normattr = screenattr(1, 1)
584 diffthis
585 botright vert new two
586 call setline(1, ['One', 'Four'])
587 diffthis
588 redraw
589 call assert_notequal(normattr, screenattr(1, 1))
590 set hidden
591 close
592 redraw
593 " diffing with hidden buffer two
594 call assert_notequal(normattr, screenattr(1, 1))
595 diffoff
596 redraw
597 call assert_equal(normattr, screenattr(1, 1))
598 diffthis
599 redraw
600 " still diffing with hidden buffer two
601 call assert_notequal(normattr, screenattr(1, 1))
602 diffoff!
603 redraw
604 call assert_equal(normattr, screenattr(1, 1))
605 diffthis
606 redraw
607 " no longer diffing with hidden buffer two
608 call assert_equal(normattr, screenattr(1, 1))
609
610 bwipe!
611 bwipe!
612 set hidden& diffopt&
613endfunc
614
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200615func Test_setting_cursor()
616 new Xtest1
617 put =range(1,90)
618 wq
619 new Xtest2
620 put =range(1,100)
621 wq
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200622
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200623 tabe Xtest2
624 $
625 diffsp Xtest1
626 tabclose
627
628 call delete('Xtest1')
629 call delete('Xtest2')
630endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100631
632func Test_diff_move_to()
633 new
634 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
635 diffthis
636 vnew
637 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
638 diffthis
639 norm ]c
640 call assert_equal(2, line('.'))
641 norm 3]c
642 call assert_equal(9, line('.'))
643 norm 10]c
644 call assert_equal(11, line('.'))
645 norm [c
646 call assert_equal(9, line('.'))
647 norm 2[c
648 call assert_equal(5, line('.'))
649 norm 10[c
650 call assert_equal(2, line('.'))
651 %bwipe!
652endfunc
653
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200654func Test_diffexpr()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +0100655 CheckExecutable diff
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200656
657 func DiffExpr()
Bram Moolenaar485b6272021-05-18 19:19:03 +0200658 " Prepend some text to check diff type detection
Bram Moolenaar3b8defd2018-09-13 13:03:11 +0200659 call writefile(['warning', ' message'], v:fname_out)
660 silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>>' . v:fname_out
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200661 endfunc
662 set diffexpr=DiffExpr()
663 set diffopt=foldcolumn:0
664
665 enew!
666 call setline(1, ['one', 'two', 'three'])
667 redraw
668 let normattr = screenattr(1, 1)
669 diffthis
670
671 botright vert new
672 call setline(1, ['one', 'two', 'three.'])
673 diffthis
674
675 redraw
676 call assert_equal(normattr, screenattr(1, 1))
677 call assert_equal(normattr, screenattr(2, 1))
678 call assert_notequal(normattr, screenattr(3, 1))
Yegappan Lakshmanan30443242021-06-10 21:52:15 +0200679 diffoff!
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200680
Dominique Pelle923dce22021-11-21 11:36:04 +0000681 " Try using a non-existing function for 'diffexpr'.
Yegappan Lakshmanan30443242021-06-10 21:52:15 +0200682 set diffexpr=NewDiffFunc()
683 call assert_fails('windo diffthis', ['E117:', 'E97:'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200684 diffoff!
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000685
686 " Using a script-local function
687 func s:NewDiffExpr()
688 endfunc
689 set diffexpr=s:NewDiffExpr()
690 call assert_equal(expand('<SID>') .. 'NewDiffExpr()', &diffexpr)
691 set diffexpr=<SID>NewDiffExpr()
692 call assert_equal(expand('<SID>') .. 'NewDiffExpr()', &diffexpr)
693
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200694 %bwipe!
695 set diffexpr& diffopt&
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000696 delfunc DiffExpr
697 delfunc s:NewDiffExpr
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200698endfunc
699
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100700func Test_diffpatch()
701 " The patch program on MS-Windows may fail or hang.
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200702 CheckExecutable patch
703 CheckUnix
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100704 new
705 insert
706***************
707*** 1,3 ****
708 1
709! 2
710 3
711--- 1,4 ----
712 1
713! 2x
714 3
715+ 4
716.
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200717 saveas! Xpatch
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100718 bwipe!
719 new
720 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100721
Bram Moolenaara95ab322017-03-11 19:21:53 +0100722 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100723 call setline(1, ['1', '2', '3'])
724 if name != 'Xpatch'
725 call rename('Xpatch', name)
726 endif
727 exe 'diffpatch ' . escape(name, '$')
728 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
729 if name != 'Xpatch'
730 call rename(name, 'Xpatch')
731 endif
732 bwipe!
733 endfor
734
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100735 call delete('Xpatch')
736 bwipe!
737endfunc
738
739func Test_diff_too_many_buffers()
740 for i in range(1, 8)
741 exe "new Xtest" . i
742 diffthis
743 endfor
744 new Xtest9
745 call assert_fails('diffthis', 'E96:')
746 %bwipe!
747endfunc
748
749func Test_diff_nomodifiable()
750 new
751 call setline(1, [1, 2, 3, 4])
752 setl nomodifiable
753 diffthis
754 vnew
755 call setline(1, ['1x', 2, 3, 3, 4])
756 diffthis
757 call assert_fails('norm dp', 'E793:')
758 setl nomodifiable
759 call assert_fails('norm do', 'E21:')
760 %bwipe!
761endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100762
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200763func Test_diff_hlID()
764 new
765 call setline(1, [1, 2, 3])
766 diffthis
767 vnew
768 call setline(1, ['1x', 2, 'x', 3])
769 diffthis
770 redraw
771
Bram Moolenaara74e4942019-08-04 17:35:53 +0200772 call diff_hlID(-1, 1)->synIDattr("name")->assert_equal("")
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200773
Bram Moolenaara74e4942019-08-04 17:35:53 +0200774 call diff_hlID(1, 1)->synIDattr("name")->assert_equal("DiffChange")
775 call diff_hlID(1, 2)->synIDattr("name")->assert_equal("DiffText")
776 call diff_hlID(2, 1)->synIDattr("name")->assert_equal("")
777 call diff_hlID(3, 1)->synIDattr("name")->assert_equal("DiffAdd")
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200778 eval 4->diff_hlID(1)->synIDattr("name")->assert_equal("")
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200779
780 wincmd w
781 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
782 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
783 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
784
785 %bwipe!
786endfunc
787
788func Test_diff_filler()
789 new
790 call setline(1, [1, 2, 3, 'x', 4])
791 diffthis
792 vnew
793 call setline(1, [1, 2, 'y', 'y', 3, 4])
794 diffthis
795 redraw
796
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200797 call assert_equal([0, 0, 0, 0, 0, 0, 0, 1, 0], map(range(-1, 7), 'v:val->diff_filler()'))
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200798 wincmd w
799 call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
800
801 %bwipe!
802endfunc
803
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100804func Test_diff_lastline()
805 enew!
806 only!
807 call setline(1, ['This is a ', 'line with five ', 'rows'])
808 diffthis
809 botright vert new
810 call setline(1, ['This is', 'a line with ', 'four rows'])
811 diffthis
812 1
813 call feedkeys("Je a\<CR>", 'tx')
814 call feedkeys("Je a\<CR>", 'tx')
815 let w1lines = winline()
816 wincmd w
817 $
818 let w2lines = winline()
819 call assert_equal(w2lines, w1lines)
820 bwipe!
821 bwipe!
822endfunc
Bram Moolenaare828b762018-09-10 17:51:58 +0200823
Bram Moolenaar785fc652018-09-15 19:17:38 +0200824func WriteDiffFiles(buf, list1, list2)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100825 call writefile(a:list1, 'Xdifile1')
826 call writefile(a:list2, 'Xdifile2')
Bram Moolenaar785fc652018-09-15 19:17:38 +0200827 if a:buf
828 call term_sendkeys(a:buf, ":checktime\<CR>")
829 endif
Bram Moolenaare828b762018-09-10 17:51:58 +0200830endfunc
831
Bram Moolenaar785fc652018-09-15 19:17:38 +0200832" Verify a screendump with both the internal and external diff.
Bram Moolenaare828b762018-09-10 17:51:58 +0200833func VerifyBoth(buf, dumpfile, extra)
Bram Moolenaare828b762018-09-10 17:51:58 +0200834 " trailing : for leaving the cursor on the command line
Bram Moolenaar785fc652018-09-15 19:17:38 +0200835 for cmd in [":set diffopt=filler" . a:extra . "\<CR>:", ":set diffopt+=internal\<CR>:"]
Bram Moolenaare828b762018-09-10 17:51:58 +0200836 call term_sendkeys(a:buf, cmd)
837 if VerifyScreenDump(a:buf, a:dumpfile, {}, cmd =~ 'internal' ? 'internal' : 'external')
Bram Moolenaar485b6272021-05-18 19:19:03 +0200838 " don't let the next iteration overwrite the "failed" file.
839 return
Bram Moolenaare828b762018-09-10 17:51:58 +0200840 endif
841 endfor
Bram Moolenaar485b6272021-05-18 19:19:03 +0200842
843 " also test unified diff
844 call term_sendkeys(a:buf, ":call SetupUnified()\<CR>:")
glacambread5c1782021-05-24 14:20:53 +0200845 call term_sendkeys(a:buf, ":redraw!\<CR>:")
Bram Moolenaar485b6272021-05-18 19:19:03 +0200846 call VerifyScreenDump(a:buf, a:dumpfile, {}, 'unified')
847 call term_sendkeys(a:buf, ":call StopUnified()\<CR>:")
Bram Moolenaare828b762018-09-10 17:51:58 +0200848endfunc
849
Bram Moolenaar785fc652018-09-15 19:17:38 +0200850" Verify a screendump with the internal diff only.
851func VerifyInternal(buf, dumpfile, extra)
852 call term_sendkeys(a:buf, ":diffupdate!\<CR>")
853 " trailing : for leaving the cursor on the command line
854 call term_sendkeys(a:buf, ":set diffopt=internal,filler" . a:extra . "\<CR>:")
855 call VerifyScreenDump(a:buf, a:dumpfile, {})
856endfunc
857
Bram Moolenaare828b762018-09-10 17:51:58 +0200858func Test_diff_screen()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100859 let g:test_is_flaky = 1
Bram Moolenaar3c8ee622019-08-03 22:55:50 +0200860 CheckScreendump
861 CheckFeature menu
862
Bram Moolenaar485b6272021-05-18 19:19:03 +0200863 let lines =<< trim END
864 func UnifiedDiffExpr()
865 " Prepend some text to check diff type detection
866 call writefile(['warning', ' message'], v:fname_out)
glacambread5c1782021-05-24 14:20:53 +0200867 silent exe '!diff -U0 ' .. v:fname_in .. ' ' .. v:fname_new .. '>>' .. v:fname_out
Bram Moolenaar485b6272021-05-18 19:19:03 +0200868 endfunc
869 func SetupUnified()
870 set diffexpr=UnifiedDiffExpr()
glacambread5c1782021-05-24 14:20:53 +0200871 diffupdate
Bram Moolenaar485b6272021-05-18 19:19:03 +0200872 endfunc
873 func StopUnified()
874 set diffexpr=
875 endfunc
876 END
Bram Moolenaar59173412022-09-20 22:01:33 +0100877 call writefile(lines, 'XdiffSetup', 'D')
Bram Moolenaar485b6272021-05-18 19:19:03 +0200878
Bram Moolenaare828b762018-09-10 17:51:58 +0200879 " clean up already existing swap files, just in case
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100880 call delete('.Xdifile1.swp')
881 call delete('.Xdifile2.swp')
Bram Moolenaare828b762018-09-10 17:51:58 +0200882
883 " Test 1: Add a line in beginning of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200884 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 Moolenaar61abe7d2022-08-30 21:46:08 +0100885 let buf = RunVimInTerminal('-d -S XdiffSetup Xdifile1 Xdifile2', {})
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100886 " Set autoread mode, so that Vim won't complain once we re-write the test
Bram Moolenaare828b762018-09-10 17:51:58 +0200887 " files
Bram Moolenaar785fc652018-09-15 19:17:38 +0200888 call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
Bram Moolenaare828b762018-09-10 17:51:58 +0200889
890 call VerifyBoth(buf, 'Test_diff_01', '')
891
892 " Test 2: Add a line in beginning of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200893 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 +0200894 call VerifyBoth(buf, 'Test_diff_02', '')
895
896 " Test 3: Add a line at the end of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200897 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 +0200898 call VerifyBoth(buf, 'Test_diff_03', '')
899
900 " Test 4: Add a line at the end of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200901 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 +0200902 call VerifyBoth(buf, 'Test_diff_04', '')
903
904 " 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 +0200905 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 +0200906 call VerifyBoth(buf, 'Test_diff_05', '')
907
908 " 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 +0200909 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 +0200910 call VerifyBoth(buf, 'Test_diff_06', '')
911
Bram Moolenaarb9ddda62019-02-19 23:00:50 +0100912 " Variants on test 6 with different context settings
913 call term_sendkeys(buf, ":set diffopt+=context:2\<cr>")
914 call VerifyScreenDump(buf, 'Test_diff_06.2', {})
915 call term_sendkeys(buf, ":set diffopt-=context:2\<cr>")
916 call term_sendkeys(buf, ":set diffopt+=context:1\<cr>")
917 call VerifyScreenDump(buf, 'Test_diff_06.1', {})
918 call term_sendkeys(buf, ":set diffopt-=context:1\<cr>")
919 call term_sendkeys(buf, ":set diffopt+=context:0\<cr>")
920 call VerifyScreenDump(buf, 'Test_diff_06.0', {})
921 call term_sendkeys(buf, ":set diffopt-=context:0\<cr>")
922
Bram Moolenaare828b762018-09-10 17:51:58 +0200923 " Test 7 - 9: Test normal/patience/histogram diff algorithm
Bram Moolenaar785fc652018-09-15 19:17:38 +0200924 call WriteDiffFiles(buf, ['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
Bram Moolenaare828b762018-09-10 17:51:58 +0200925 \ ' int i;', ' for(i = 0; i < 10; i++)', ' {', ' printf("Your answer is: ");',
926 \ ' printf("%d\n", foo);', ' }', '}', '', 'int fact(int n)', '{', ' if(n > 1)', ' {',
927 \ ' return fact(n-1) * n;', ' }', ' return 1;', '}', '', 'int main(int argc, char **argv)',
928 \ '{', ' frobnitz(fact(10));', '}'],
929 \ ['#include <stdio.h>', '', 'int fib(int n)', '{', ' if(n > 2)', ' {',
930 \ ' return fib(n-1) + fib(n-2);', ' }', ' return 1;', '}', '', '// Frobs foo heartily',
931 \ 'int frobnitz(int foo)', '{', ' int i;', ' for(i = 0; i < 10; i++)', ' {',
932 \ ' printf("%d\n", foo);', ' }', '}', '',
933 \ 'int main(int argc, char **argv)', '{', ' frobnitz(fib(10));', '}'])
934 call term_sendkeys(buf, ":diffupdate!\<cr>")
935 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
936 call VerifyScreenDump(buf, 'Test_diff_07', {})
937
938 call term_sendkeys(buf, ":set diffopt+=algorithm:patience\<cr>")
939 call VerifyScreenDump(buf, 'Test_diff_08', {})
940
941 call term_sendkeys(buf, ":set diffopt+=algorithm:histogram\<cr>")
942 call VerifyScreenDump(buf, 'Test_diff_09', {})
943
944 " Test 10-11: normal/indent-heuristic
945 call term_sendkeys(buf, ":set diffopt&vim\<cr>")
Bram Moolenaar785fc652018-09-15 19:17:38 +0200946 call WriteDiffFiles(buf, ['', ' def finalize(values)', '', ' values.each do |v|', ' v.finalize', ' end'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200947 \ ['', ' def finalize(values)', '', ' values.each do |v|', ' v.prepare', ' end', '',
948 \ ' values.each do |v|', ' v.finalize', ' end'])
949 call term_sendkeys(buf, ":diffupdate!\<cr>")
950 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
951 call VerifyScreenDump(buf, 'Test_diff_10', {})
952
Bram Moolenaarb6fc7282018-12-04 22:24:16 +0100953 " Leave trailing : at commandline!
954 call term_sendkeys(buf, ":set diffopt+=indent-heuristic\<cr>:\<cr>")
955 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'one')
956 " shouldn't matter, if indent-algorithm comes before or after the algorithm
957 call term_sendkeys(buf, ":set diffopt&\<cr>")
958 call term_sendkeys(buf, ":set diffopt+=indent-heuristic,algorithm:patience\<cr>:\<cr>")
959 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'two')
960 call term_sendkeys(buf, ":set diffopt&\<cr>")
961 call term_sendkeys(buf, ":set diffopt+=algorithm:patience,indent-heuristic\<cr>:\<cr>")
962 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'three')
Bram Moolenaare828b762018-09-10 17:51:58 +0200963
964 " Test 12: diff the same file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200965 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 +0200966 call VerifyBoth(buf, 'Test_diff_12', '')
967
968 " Test 13: diff an empty file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200969 call WriteDiffFiles(buf, [], [])
Bram Moolenaare828b762018-09-10 17:51:58 +0200970 call VerifyBoth(buf, 'Test_diff_13', '')
971
972 " Test 14: test diffopt+=icase
Bram Moolenaar785fc652018-09-15 19:17:38 +0200973 call WriteDiffFiles(buf, ['a', 'b', 'cd'], ['A', 'b', 'cDe'])
Bram Moolenaare828b762018-09-10 17:51:58 +0200974 call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
975
976 " Test 15-16: test diffopt+=iwhite
Bram Moolenaar785fc652018-09-15 19:17:38 +0200977 call WriteDiffFiles(buf, ['int main()', '{', ' printf("Hello, World!");', ' return 0;', '}'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200978 \ ['int main()', '{', ' if (0)', ' {', ' printf("Hello, World!");', ' return 0;', ' }', '}'])
979 call term_sendkeys(buf, ":diffupdate!\<cr>")
980 call term_sendkeys(buf, ":set diffopt&vim diffopt+=filler diffopt+=iwhite\<cr>")
981 call VerifyScreenDump(buf, 'Test_diff_15', {})
982 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
983 call VerifyScreenDump(buf, 'Test_diff_16', {})
984
Bram Moolenaar785fc652018-09-15 19:17:38 +0200985 " Test 17: test diffopt+=iblank
986 call WriteDiffFiles(buf, ['a', ' ', 'cd', 'ef', 'xxx'], ['a', 'cd', '', 'ef', 'yyy'])
987 call VerifyInternal(buf, 'Test_diff_17', " diffopt+=iblank")
988
989 " Test 18: test diffopt+=iblank,iwhite / iwhiteall / iwhiteeol
990 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhite")
991 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteall")
992 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteeol")
993
994 " Test 19: test diffopt+=iwhiteeol
995 call WriteDiffFiles(buf, ['a ', 'x', 'cd', 'ef', 'xx xx', 'foo', 'bar'], ['a', 'x', 'c d', ' ef', 'xx xx', 'foo', '', 'bar'])
996 call VerifyInternal(buf, 'Test_diff_19', " diffopt+=iwhiteeol")
997
998 " Test 19: test diffopt+=iwhiteall
999 call VerifyInternal(buf, 'Test_diff_20', " diffopt+=iwhiteall")
1000
Bram Moolenaare828b762018-09-10 17:51:58 +02001001 " clean up
1002 call StopVimInTerminal(buf)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001003 call delete('Xdifile1')
1004 call delete('Xdifile2')
Bram Moolenaare828b762018-09-10 17:51:58 +02001005endfunc
1006
Bram Moolenaar04626c22021-09-01 16:02:07 +02001007func Test_diff_with_scroll_and_change()
1008 CheckScreendump
1009
1010 let lines =<< trim END
1011 call setline(1, range(1, 15))
1012 vnew
1013 call setline(1, range(9, 15))
1014 windo diffthis
1015 wincmd h
1016 exe "normal Gl5\<C-E>"
1017 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001018 call writefile(lines, 'Xtest_scroll_change', 'D')
Bram Moolenaar04626c22021-09-01 16:02:07 +02001019 let buf = RunVimInTerminal('-S Xtest_scroll_change', {})
1020
1021 call VerifyScreenDump(buf, 'Test_diff_scroll_change_01', {})
1022
1023 call term_sendkeys(buf, "ax\<Esc>")
1024 call VerifyScreenDump(buf, 'Test_diff_scroll_change_02', {})
1025
Bram Moolenaar841c2252021-10-22 20:56:55 +01001026 call term_sendkeys(buf, "\<C-W>lay\<Esc>")
1027 call VerifyScreenDump(buf, 'Test_diff_scroll_change_03', {})
1028
Bram Moolenaar04626c22021-09-01 16:02:07 +02001029 " clean up
1030 call StopVimInTerminal(buf)
Bram Moolenaar04626c22021-09-01 16:02:07 +02001031endfunc
1032
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001033func Test_diff_with_cursorline()
Bram Moolenaar3c8ee622019-08-03 22:55:50 +02001034 CheckScreendump
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001035
1036 call writefile([
1037 \ 'hi CursorLine ctermbg=red ctermfg=white',
1038 \ 'set cursorline',
1039 \ 'call setline(1, ["foo","foo","foo","bar"])',
1040 \ 'vnew',
1041 \ 'call setline(1, ["bee","foo","foo","baz"])',
1042 \ 'windo diffthis',
1043 \ '2wincmd w',
Bram Moolenaar59173412022-09-20 22:01:33 +01001044 \ ], 'Xtest_diff_cursorline', 'D')
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001045 let buf = RunVimInTerminal('-S Xtest_diff_cursorline', {})
1046
1047 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_01', {})
1048 call term_sendkeys(buf, "j")
1049 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_02', {})
1050 call term_sendkeys(buf, "j")
1051 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_03', {})
1052
1053 " clean up
1054 call StopVimInTerminal(buf)
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001055endfunc
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001056
Bram Moolenaar127969c2022-03-06 19:54:13 +00001057func Test_diff_with_cursorline_number()
1058 CheckScreendump
1059
1060 let lines =<< trim END
1061 hi CursorLine ctermbg=red ctermfg=white
1062 hi CursorLineNr ctermbg=white ctermfg=black cterm=underline
1063 set cursorline number
1064 call setline(1, ["baz", "foo", "foo", "bar"])
1065 2
1066 vnew
1067 call setline(1, ["foo", "foo", "bar"])
1068 windo diffthis
1069 1wincmd w
1070 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001071 call writefile(lines, 'Xtest_diff_cursorline_number', 'D')
Bram Moolenaar127969c2022-03-06 19:54:13 +00001072 let buf = RunVimInTerminal('-S Xtest_diff_cursorline_number', {})
1073
1074 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_01', {})
1075 call term_sendkeys(buf, ":set cursorlineopt=number\r")
1076 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_02', {})
1077
1078 " clean up
1079 call StopVimInTerminal(buf)
Bram Moolenaar127969c2022-03-06 19:54:13 +00001080endfunc
1081
zeertzjq4f33bc22021-08-05 17:57:02 +02001082func Test_diff_with_cursorline_breakindent()
1083 CheckScreendump
1084
1085 call writefile([
1086 \ 'hi CursorLine ctermbg=red ctermfg=white',
1087 \ 'set noequalalways wrap diffopt=followwrap cursorline breakindent',
1088 \ '50vnew',
1089 \ 'call setline(1, [" "," "," "," "])',
1090 \ 'exe "norm 20Afoo\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abar\<Esc>"',
1091 \ 'vnew',
1092 \ 'call setline(1, [" "," "," "," "])',
1093 \ 'exe "norm 20Abee\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abaz\<Esc>"',
1094 \ 'windo diffthis',
1095 \ '2wincmd w',
Bram Moolenaar59173412022-09-20 22:01:33 +01001096 \ ], 'Xtest_diff_cursorline_breakindent', 'D')
zeertzjq4f33bc22021-08-05 17:57:02 +02001097 let buf = RunVimInTerminal('-S Xtest_diff_cursorline_breakindent', {})
1098
1099 call term_sendkeys(buf, "gg0")
1100 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_01', {})
1101 call term_sendkeys(buf, "j")
1102 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_02', {})
1103 call term_sendkeys(buf, "j")
1104 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_03', {})
1105 call term_sendkeys(buf, "j")
1106 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_04', {})
1107
1108 " clean up
1109 call StopVimInTerminal(buf)
zeertzjq4f33bc22021-08-05 17:57:02 +02001110endfunc
1111
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001112func Test_diff_with_syntax()
1113 CheckScreendump
1114
1115 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001116 void doNothing() {
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001117 int x = 0;
1118 char *s = "hello";
1119 return 5;
1120 }
1121 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001122 call writefile(lines, 'Xprogram1.c', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001123 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001124 void doSomething() {
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001125 int x = 0;
1126 char *s = "there";
1127 return 5;
1128 }
1129 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001130 call writefile(lines, 'Xprogram2.c', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001131
1132 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001133 edit Xprogram1.c
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001134 diffsplit Xprogram2.c
1135 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001136 call writefile(lines, 'Xtest_diff_syntax', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001137 let buf = RunVimInTerminal('-S Xtest_diff_syntax', {})
1138
1139 call VerifyScreenDump(buf, 'Test_diff_syntax_1', {})
1140
1141 " clean up
1142 call StopVimInTerminal(buf)
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001143endfunc
1144
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001145func Test_diff_of_diff()
Bram Moolenaar3c8ee622019-08-03 22:55:50 +02001146 CheckScreendump
1147 CheckFeature rightleft
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001148
1149 call writefile([
1150 \ 'call setline(1, ["aa","bb","cc","@@ -3,2 +5,7 @@","dd","ee","ff"])',
1151 \ 'vnew',
1152 \ 'call setline(1, ["aa","bb","cc"])',
1153 \ 'windo diffthis',
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01001154 \ '1wincmd w',
1155 \ 'setlocal number',
Bram Moolenaar59173412022-09-20 22:01:33 +01001156 \ ], 'Xtest_diff_diff', 'D')
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001157 let buf = RunVimInTerminal('-S Xtest_diff_diff', {})
1158
1159 call VerifyScreenDump(buf, 'Test_diff_of_diff_01', {})
1160
Bram Moolenaare73f9112019-03-29 18:29:54 +01001161 call term_sendkeys(buf, ":set rightleft\<cr>")
1162 call VerifyScreenDump(buf, 'Test_diff_of_diff_02', {})
1163
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001164 " clean up
1165 call StopVimInTerminal(buf)
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001166endfunc
Bram Moolenaarc8234772019-11-10 21:00:27 +01001167
1168func CloseoffSetup()
1169 enew
1170 call setline(1, ['one', 'two', 'three'])
1171 diffthis
1172 new
1173 call setline(1, ['one', 'tow', 'three'])
1174 diffthis
1175 call assert_equal(1, &diff)
1176 only!
1177endfunc
1178
1179func Test_diff_closeoff()
1180 " "closeoff" included by default: last diff win gets 'diff' reset'
1181 call CloseoffSetup()
1182 call assert_equal(0, &diff)
1183 enew!
1184
1185 " "closeoff" excluded: last diff win keeps 'diff' set'
1186 set diffopt-=closeoff
1187 call CloseoffSetup()
1188 call assert_equal(1, &diff)
1189 diffoff!
1190 enew!
1191endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001192
Bram Moolenaar4223d432021-02-10 13:18:17 +01001193func Test_diff_followwrap()
1194 new
1195 set diffopt+=followwrap
1196 set wrap
1197 diffthis
1198 call assert_equal(1, &wrap)
1199 diffoff
1200 set nowrap
1201 diffthis
1202 call assert_equal(0, &wrap)
1203 diffoff
1204 set diffopt&
1205 bwipe!
1206endfunc
1207
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001208func Test_diff_maintains_change_mark()
Sean Dewarccc16442021-12-29 16:44:48 +00001209 func DiffMaintainsChangeMark()
1210 enew!
1211 call setline(1, ['a', 'b', 'c', 'd'])
1212 diffthis
1213 new
1214 call setline(1, ['a', 'b', 'c', 'e'])
1215 " Set '[ and '] marks
1216 2,3yank
1217 call assert_equal([2, 3], [line("'["), line("']")])
1218 " Verify they aren't affected by the implicit diff
1219 diffthis
1220 call assert_equal([2, 3], [line("'["), line("']")])
1221 " Verify they aren't affected by an explicit diff
1222 diffupdate
1223 call assert_equal([2, 3], [line("'["), line("']")])
1224 bwipe!
1225 bwipe!
1226 endfunc
1227
1228 set diffopt-=internal
1229 call DiffMaintainsChangeMark()
1230 set diffopt+=internal
1231 call DiffMaintainsChangeMark()
Bram Moolenaard9b74a22022-01-16 15:00:08 +00001232
Sean Dewarccc16442021-12-29 16:44:48 +00001233 set diffopt&
Bram Moolenaard9b74a22022-01-16 15:00:08 +00001234 delfunc DiffMaintainsChangeMark
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001235endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001236
1237" Test for 'patchexpr'
1238func Test_patchexpr()
1239 let g:patch_args = []
1240 func TPatch()
1241 call add(g:patch_args, readfile(v:fname_in))
1242 call add(g:patch_args, readfile(v:fname_diff))
1243 call writefile(['output file'], v:fname_out)
1244 endfunc
1245 set patchexpr=TPatch()
1246
Bram Moolenaar59173412022-09-20 22:01:33 +01001247 call writefile(['input file'], 'Xinput', 'D')
1248 call writefile(['diff file'], 'Xdiff', 'D')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001249 %bwipe!
1250 edit Xinput
1251 diffpatch Xdiff
1252 call assert_equal('output file', getline(1))
1253 call assert_equal('Xinput.new', bufname())
1254 call assert_equal(2, winnr('$'))
1255 call assert_true(&diff)
1256
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00001257 " Using a script-local function
1258 func s:NewPatchExpr()
1259 endfunc
1260 set patchexpr=s:NewPatchExpr()
1261 call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1262 set patchexpr=<SID>NewPatchExpr()
1263 call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1264
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001265 set patchexpr&
1266 delfunc TPatch
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00001267 delfunc s:NewPatchExpr
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001268 %bwipe!
1269endfunc
1270
Bram Moolenaar511feec2020-06-18 19:15:27 +02001271func Test_diff_rnu()
1272 CheckScreendump
1273
1274 let content =<< trim END
1275 call setline(1, ['a', 'a', 'a', 'y', 'b', 'b', 'b', 'b', 'b'])
1276 vnew
1277 call setline(1, ['a', 'a', 'a', 'x', 'x', 'x', 'b', 'b', 'b', 'b', 'b'])
1278 windo diffthis
1279 setlocal number rnu foldcolumn=0
1280 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001281 call writefile(content, 'Xtest_diff_rnu', 'D')
Bram Moolenaar511feec2020-06-18 19:15:27 +02001282 let buf = RunVimInTerminal('-S Xtest_diff_rnu', {})
1283
1284 call VerifyScreenDump(buf, 'Test_diff_rnu_01', {})
1285
1286 call term_sendkeys(buf, "j")
1287 call VerifyScreenDump(buf, 'Test_diff_rnu_02', {})
1288 call term_sendkeys(buf, "j")
1289 call VerifyScreenDump(buf, 'Test_diff_rnu_03', {})
1290
1291 " clean up
1292 call StopVimInTerminal(buf)
Bram Moolenaar511feec2020-06-18 19:15:27 +02001293endfunc
1294
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001295func Test_diff_multilineconceal()
1296 new
1297 diffthis
1298
1299 new
1300 call matchadd('Conceal', 'a\nb', 9, -1, {'conceal': 'Y'})
1301 set cole=2 cocu=n
1302 call setline(1, ["a", "b"])
1303 diffthis
1304 redraw
1305endfunc
1306
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001307func Test_diff_and_scroll()
1308 " this was causing an ml_get error
1309 set ls=2
Bram Moolenaar94722c52023-01-28 19:19:03 +00001310 for i in range(winheight(0) * 2)
1311 call setline(i, i < winheight(0) - 10 ? i : i + 10)
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001312 endfor
1313 vnew
Bram Moolenaar94722c52023-01-28 19:19:03 +00001314 for i in range(winheight(0)*2 + 10)
1315 call setline(i, i < winheight(0) - 10 ? 0 : i)
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001316 endfor
1317 diffthis
1318 wincmd p
1319 diffthis
1320 execute 'normal ' . winheight(0) . "\<C-d>"
1321
1322 bwipe!
1323 bwipe!
1324 set ls&
1325endfunc
1326
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001327func Test_diff_filler_cursorcolumn()
1328 CheckScreendump
1329
1330 let content =<< trim END
1331 call setline(1, ['aa', 'bb', 'cc'])
1332 vnew
1333 call setline(1, ['aa', 'cc'])
1334 windo diffthis
1335 wincmd p
1336 setlocal cursorcolumn foldcolumn=0
1337 norm! gg0
1338 redraw!
1339 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001340 call writefile(content, 'Xtest_diff_cuc', 'D')
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001341 let buf = RunVimInTerminal('-S Xtest_diff_cuc', {})
1342
1343 call VerifyScreenDump(buf, 'Test_diff_cuc_01', {})
1344
1345 call term_sendkeys(buf, "l")
1346 call term_sendkeys(buf, "\<C-l>")
1347 call VerifyScreenDump(buf, 'Test_diff_cuc_02', {})
1348 call term_sendkeys(buf, "0j")
1349 call term_sendkeys(buf, "\<C-l>")
1350 call VerifyScreenDump(buf, 'Test_diff_cuc_03', {})
1351 call term_sendkeys(buf, "l")
1352 call term_sendkeys(buf, "\<C-l>")
1353 call VerifyScreenDump(buf, 'Test_diff_cuc_04', {})
1354
1355 " clean up
1356 call StopVimInTerminal(buf)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001357endfunc
1358
Yegappan Lakshmanan30443242021-06-10 21:52:15 +02001359" Test for adding/removing lines inside diff chunks, between diff chunks
1360" and before diff chunks
1361func Test_diff_modify_chunks()
1362 enew!
1363 let w2_id = win_getid()
1364 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1365 new
1366 let w1_id = win_getid()
1367 call setline(1, ['a', '2', '3', 'd', 'e', 'f', '7', '8', 'i'])
1368 windo diffthis
1369
1370 " remove a line between two diff chunks and create a new diff chunk
1371 call win_gotoid(w2_id)
1372 5d
1373 call win_gotoid(w1_id)
1374 call diff_hlID(5, 1)->synIDattr('name')->assert_equal('DiffAdd')
1375
1376 " add a line between two diff chunks
1377 call win_gotoid(w2_id)
1378 normal! 4Goe
1379 call win_gotoid(w1_id)
1380 call diff_hlID(4, 1)->synIDattr('name')->assert_equal('')
1381 call diff_hlID(5, 1)->synIDattr('name')->assert_equal('')
1382
1383 " remove all the lines in a diff chunk.
1384 call win_gotoid(w2_id)
1385 7,8d
1386 call win_gotoid(w1_id)
1387 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1388 call assert_equal(['', 'DiffText', 'DiffText', '', '', '', 'DiffAdd',
1389 \ 'DiffAdd', ''], hl)
1390
1391 " remove lines from one diff chunk to just before the next diff chunk
1392 call win_gotoid(w2_id)
1393 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1394 2,6d
1395 call win_gotoid(w1_id)
1396 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1397 call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', 'DiffAdd',
1398 \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1399
1400 " remove lines just before the top of a diff chunk
1401 call win_gotoid(w2_id)
1402 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1403 5,6d
1404 call win_gotoid(w1_id)
1405 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1406 call assert_equal(['', 'DiffText', 'DiffText', '', 'DiffText', 'DiffText',
1407 \ 'DiffAdd', 'DiffAdd', ''], hl)
1408
1409 " remove line after the end of a diff chunk
1410 call win_gotoid(w2_id)
1411 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1412 4d
1413 call win_gotoid(w1_id)
1414 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1415 call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', '', '', 'DiffText',
1416 \ 'DiffText', ''], hl)
1417
1418 " remove lines starting from the end of one diff chunk and ending inside
1419 " another diff chunk
1420 call win_gotoid(w2_id)
1421 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1422 4,7d
1423 call win_gotoid(w1_id)
1424 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1425 call assert_equal(['', 'DiffText', 'DiffText', 'DiffText', 'DiffAdd',
1426 \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1427
1428 " removing the only remaining diff chunk should make the files equal
1429 call win_gotoid(w2_id)
1430 call setline(1, ['a', '2', '3', 'x', 'd', 'e', 'f', 'x', '7', '8', 'i'])
1431 8d
1432 let hl = range(1, 10)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1433 call assert_equal(['', '', '', 'DiffAdd', '', '', '', '', '', ''], hl)
1434 call win_gotoid(w2_id)
1435 4d
1436 call win_gotoid(w1_id)
1437 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1438 call assert_equal(['', '', '', '', '', '', '', '', ''], hl)
1439
1440 %bw!
1441endfunc
glacambread5c1782021-05-24 14:20:53 +02001442
Bram Moolenaar06f60952021-12-28 18:30:05 +00001443func Test_diff_binary()
1444 CheckScreendump
1445
1446 let content =<< trim END
1447 call setline(1, ['a', 'b', "c\n", 'd', 'e', 'f', 'g'])
1448 vnew
1449 call setline(1, ['A', 'b', 'c', 'd', 'E', 'f', 'g'])
1450 windo diffthis
1451 wincmd p
1452 norm! gg0
1453 redraw!
1454 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001455 call writefile(content, 'Xtest_diff_bin', 'D')
Bram Moolenaar06f60952021-12-28 18:30:05 +00001456 let buf = RunVimInTerminal('-S Xtest_diff_bin', {})
1457
1458 " Test using internal diff
1459 call VerifyScreenDump(buf, 'Test_diff_bin_01', {})
1460
1461 " Test using internal diff and case folding
1462 call term_sendkeys(buf, ":set diffopt+=icase\<cr>")
1463 call term_sendkeys(buf, "\<C-l>")
1464 call VerifyScreenDump(buf, 'Test_diff_bin_02', {})
1465 " Test using external diff
1466 call term_sendkeys(buf, ":set diffopt=filler\<cr>")
1467 call term_sendkeys(buf, "\<C-l>")
1468 call VerifyScreenDump(buf, 'Test_diff_bin_03', {})
1469 " Test using external diff and case folding
1470 call term_sendkeys(buf, ":set diffopt=filler,icase\<cr>")
1471 call term_sendkeys(buf, "\<C-l>")
1472 call VerifyScreenDump(buf, 'Test_diff_bin_04', {})
1473
1474 " clean up
1475 call StopVimInTerminal(buf)
Bram Moolenaar06f60952021-12-28 18:30:05 +00001476 set diffopt&vim
1477endfunc
1478
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001479" Test for using the 'zi' command to invert 'foldenable' in diff windows (test
1480" for the issue fixed by patch 6.2.317)
1481func Test_diff_foldinvert()
1482 %bw!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001483 edit Xdoffile1
1484 new Xdoffile2
1485 new Xdoffile3
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001486 windo diffthis
1487 " open a non-diff window
1488 botright new
1489 1wincmd w
1490 call assert_true(getwinvar(1, '&foldenable'))
1491 call assert_true(getwinvar(2, '&foldenable'))
1492 call assert_true(getwinvar(3, '&foldenable'))
1493 normal zi
1494 call assert_false(getwinvar(1, '&foldenable'))
1495 call assert_false(getwinvar(2, '&foldenable'))
1496 call assert_false(getwinvar(3, '&foldenable'))
1497 normal zi
1498 call assert_true(getwinvar(1, '&foldenable'))
1499 call assert_true(getwinvar(2, '&foldenable'))
1500 call assert_true(getwinvar(3, '&foldenable'))
1501
1502 " If the current window has 'noscrollbind', then 'zi' should not change
1503 " 'foldenable' in other windows.
1504 1wincmd w
1505 set noscrollbind
1506 normal zi
1507 call assert_false(getwinvar(1, '&foldenable'))
1508 call assert_true(getwinvar(2, '&foldenable'))
1509 call assert_true(getwinvar(3, '&foldenable'))
1510
1511 " 'zi' should not change the 'foldenable' for windows with 'noscrollbind'
1512 1wincmd w
1513 set scrollbind
1514 normal zi
1515 call setwinvar(2, '&scrollbind', v:false)
1516 normal zi
1517 call assert_false(getwinvar(1, '&foldenable'))
1518 call assert_true(getwinvar(2, '&foldenable'))
1519 call assert_false(getwinvar(3, '&foldenable'))
1520
1521 %bw!
1522 set scrollbind&
1523endfunc
1524
Bram Moolenaara315ce12022-06-24 12:38:57 +01001525" This was scrolling for 'cursorbind' but 'scrollbind' is more important
1526func Test_diff_scroll()
1527 CheckScreendump
1528
1529 let left =<< trim END
1530 line 1
1531 line 2
1532 line 3
1533 line 4
1534
1535 // Common block
1536 // one
1537 // containing
1538 // four lines
1539
1540 // Common block
1541 // two
1542 // containing
1543 // four lines
1544 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001545 call writefile(left, 'Xleft', 'D')
Bram Moolenaara315ce12022-06-24 12:38:57 +01001546 let right =<< trim END
1547 line 1
1548 line 2
1549 line 3
1550 line 4
1551
1552 Lorem
1553 ipsum
1554 dolor
1555 sit
1556 amet,
1557 consectetur
1558 adipiscing
1559 elit.
1560 Etiam
1561 luctus
1562 lectus
1563 sodales,
1564 dictum
1565
1566 // Common block
1567 // one
1568 // containing
1569 // four lines
1570
1571 Vestibulum
1572 tincidunt
1573 aliquet
1574 nulla.
1575
1576 // Common block
1577 // two
1578 // containing
1579 // four lines
1580 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001581 call writefile(right, 'Xright', 'D')
Bram Moolenaara315ce12022-06-24 12:38:57 +01001582 let buf = RunVimInTerminal('-d Xleft Xright', {'rows': 12})
1583 call term_sendkeys(buf, "\<C-W>\<C-W>jjjj")
1584 call VerifyScreenDump(buf, 'Test_diff_scroll_1', {})
1585 call term_sendkeys(buf, "j")
1586 call VerifyScreenDump(buf, 'Test_diff_scroll_2', {})
1587
1588 call StopVimInTerminal(buf)
Bram Moolenaara315ce12022-06-24 12:38:57 +01001589endfunc
1590
Bram Moolenaar38d867f2023-04-01 19:54:40 +01001591" This was scrolling too many lines.
1592func Test_diff_scroll_wrap_on()
1593 20new
1594 40vsplit
1595 call setline(1, map(range(1, 9), 'repeat(v:val, 200)'))
1596 setlocal number diff so=0
1597 redraw
1598 normal! jj
1599 call assert_equal(1, winsaveview().topline)
1600 normal! j
1601 call assert_equal(2, winsaveview().topline)
1602 bwipe!
1603 bwipe!
1604endfunc
1605
Bram Moolenaarcd38bb42022-06-26 14:04:07 +01001606" This was trying to update diffs for a buffer being closed
1607func Test_diff_only()
1608 silent! lfile
1609 set diff
1610 lopen
1611 norm o
1612 silent! norm o
1613
1614 set nodiff
1615 %bwipe!
1616endfunc
1617
Bram Moolenaarc101abf2022-06-26 16:53:34 +01001618" This was causing invalid diff block values
1619" FIXME: somehow this causes a valgrind error when run directly but not when
1620" run as a test.
1621func Test_diff_manipulations()
1622 set diff
1623 split 0
1624 sil! norm R doobdeuR doobdeuR doobdeu
1625
1626 set nodiff
1627 %bwipe!
1628endfunc
1629
Bram Moolenaar4e677b92022-07-28 18:44:27 +01001630" This was causing the line number in the diff block to go below one.
1631" FIXME: somehow this causes a valgrind error when run directly but not when
1632" run as a test.
1633func Test_diff_put_and_undo()
1634 set diff
1635 next 0
1636 split 00
1637 sil! norm o0gguudpo0ggJuudp
1638
1639 bwipe!
1640 bwipe!
1641 set nodiff
1642endfunc
1643
Bram Moolenaara315ce12022-06-24 12:38:57 +01001644
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001645" vim: shiftwidth=2 sts=2 expandtab