blob: b8d0057a135f925434ca039897a20bfad5de6bbf [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
zeertzjq9e7f1fc2024-03-16 09:40:22 +01006source view_util.vim
Bram Moolenaar42093c02016-07-30 16:16:54 +02007
8func Test_diff_fold_sync()
9 enew!
Bram Moolenaare8fa05b2018-09-16 15:48:06 +020010 let g:update_count = 0
11 au DiffUpdated * let g:update_count += 1
12
Bram Moolenaar42093c02016-07-30 16:16:54 +020013 let l = range(50)
14 call setline(1, l)
15 diffthis
16 let winone = win_getid()
17 new
18 let l[25] = 'diff'
19 call setline(1, l)
20 diffthis
21 let wintwo = win_getid()
22 " line 15 is inside the closed fold
23 call assert_equal(19, foldclosedend(10))
24 call win_gotoid(winone)
25 call assert_equal(19, foldclosedend(10))
26 " open the fold
27 normal zv
28 call assert_equal(-1, foldclosedend(10))
29 " fold in other window must have opened too
30 call win_gotoid(wintwo)
31 call assert_equal(-1, foldclosedend(10))
32
33 " cursor position is in sync
34 normal 23G
35 call win_gotoid(winone)
36 call assert_equal(23, getcurpos()[1])
37
Bram Moolenaarf65cc662022-06-26 18:17:50 +010038 " depending on how redraw is done DiffUpdated may be triggered once or twice
39 call assert_inrange(1, 2, g:update_count)
Bram Moolenaare8fa05b2018-09-16 15:48:06 +020040 au! DiffUpdated
41
Bram Moolenaar42093c02016-07-30 16:16:54 +020042 windo diffoff
43 close!
44 set nomodified
45endfunc
46
47func Test_vert_split()
Bram Moolenaare828b762018-09-10 17:51:58 +020048 set diffopt=filler
49 call Common_vert_split()
50 set diffopt&
51endfunc
52
53func Test_vert_split_internal()
54 set diffopt=internal,filler
55 call Common_vert_split()
56 set diffopt&
57endfunc
58
59func Common_vert_split()
Bram Moolenaar42093c02016-07-30 16:16:54 +020060 " Disable the title to avoid xterm keeping the wrong one.
61 set notitle noicon
62 new
63 let l = ['1 aa', '2 bb', '3 cc', '4 dd', '5 ee']
64 call setline(1, l)
65 w! Xtest
66 normal dd
67 $
68 put
69 normal kkrXoxxx
70 w! Xtest2
71 file Nop
72 normal ggoyyyjjjozzzz
73 set foldmethod=marker foldcolumn=4
74 call assert_equal(0, &diff)
75 call assert_equal('marker', &foldmethod)
76 call assert_equal(4, &foldcolumn)
77 call assert_equal(0, &scrollbind)
78 call assert_equal(0, &cursorbind)
79 call assert_equal(1, &wrap)
80
81 vert diffsplit Xtest
82 vert diffsplit Xtest2
83 call assert_equal(1, &diff)
84 call assert_equal('diff', &foldmethod)
85 call assert_equal(2, &foldcolumn)
86 call assert_equal(1, &scrollbind)
87 call assert_equal(1, &cursorbind)
88 call assert_equal(0, &wrap)
89
90 let diff_fdm = &fdm
91 let diff_fdc = &fdc
92 " repeat entering diff mode here to see if this saves the wrong settings
93 diffthis
94 " jump to second window for a moment to have filler line appear at start of
95 " first window
96 wincmd w
97 normal gg
98 wincmd p
99 normal gg
100 call assert_equal(2, winline())
101 normal j
102 call assert_equal(4, winline())
103 normal j
104 call assert_equal(5, winline())
105 normal j
106 call assert_equal(6, winline())
107 normal j
108 call assert_equal(8, winline())
109 normal j
110 call assert_equal(9, winline())
111
112 wincmd w
113 normal gg
114 call assert_equal(1, winline())
115 normal j
116 call assert_equal(2, winline())
117 normal j
118 call assert_equal(4, winline())
119 normal j
120 call assert_equal(5, winline())
121 normal j
122 call assert_equal(8, winline())
123
124 wincmd w
125 normal gg
126 call assert_equal(2, winline())
127 normal j
128 call assert_equal(3, winline())
129 normal j
130 call assert_equal(4, winline())
131 normal j
132 call assert_equal(5, winline())
133 normal j
134 call assert_equal(6, winline())
135 normal j
136 call assert_equal(7, winline())
137 normal j
138 call assert_equal(8, winline())
139
140 " Test diffoff
141 diffoff!
zeertzjq5fd6ab82022-08-17 12:09:45 +0100142 1wincmd w
Bram Moolenaar42093c02016-07-30 16:16:54 +0200143 let &diff = 1
144 let &fdm = diff_fdm
145 let &fdc = diff_fdc
146 4wincmd w
147 diffoff!
148 1wincmd w
149 call assert_equal(0, &diff)
150 call assert_equal('marker', &foldmethod)
151 call assert_equal(4, &foldcolumn)
152 call assert_equal(0, &scrollbind)
153 call assert_equal(0, &cursorbind)
154 call assert_equal(1, &wrap)
155
156 wincmd w
157 call assert_equal(0, &diff)
158 call assert_equal('marker', &foldmethod)
159 call assert_equal(4, &foldcolumn)
160 call assert_equal(0, &scrollbind)
161 call assert_equal(0, &cursorbind)
162 call assert_equal(1, &wrap)
163
164 wincmd w
165 call assert_equal(0, &diff)
166 call assert_equal('marker', &foldmethod)
167 call assert_equal(4, &foldcolumn)
168 call assert_equal(0, &scrollbind)
169 call assert_equal(0, &cursorbind)
170 call assert_equal(1, &wrap)
171
Bram Moolenaar623cf882016-07-30 16:36:01 +0200172 call delete('Xtest')
173 call delete('Xtest2')
Bram Moolenaar42093c02016-07-30 16:16:54 +0200174 windo bw!
175endfunc
176
177func Test_filler_lines()
178 " Test that diffing shows correct filler lines
179 enew!
180 put =range(4,10)
181 1d _
182 vnew
183 put =range(1,10)
184 1d _
185 windo diffthis
186 wincmd h
187 call assert_equal(1, line('w0'))
188 unlet! diff_fdm diff_fdc
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200189 windo diffoff
190 bwipe!
191 enew!
192endfunc
Bram Moolenaar42093c02016-07-30 16:16:54 +0200193
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200194func Test_diffget_diffput()
195 enew!
196 let l = range(50)
197 call setline(1, l)
198 call assert_fails('diffget', 'E99:')
199 diffthis
200 call assert_fails('diffget', 'E100:')
201 new
202 let l[10] = 'one'
203 let l[20] = 'two'
204 let l[30] = 'three'
205 let l[40] = 'four'
206 call setline(1, l)
207 diffthis
208 call assert_equal('one', getline(11))
209 11diffget
210 call assert_equal('10', getline(11))
211 21diffput
212 wincmd w
213 call assert_equal('two', getline(21))
214 normal 31Gdo
215 call assert_equal('three', getline(31))
216 call assert_equal('40', getline(41))
217 normal 41Gdp
218 wincmd w
219 call assert_equal('40', getline(41))
220 new
221 diffthis
222 call assert_fails('diffget', 'E101:')
223
224 windo diffoff
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200225 %bwipe!
226endfunc
227
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +0200228" Test putting two changes from one buffer to another
229func Test_diffput_two()
230 new a
231 let win_a = win_getid()
232 call setline(1, range(1, 10))
233 diffthis
234 new b
235 let win_b = win_getid()
236 call setline(1, range(1, 10))
237 8del
238 5del
239 diffthis
240 call win_gotoid(win_a)
241 %diffput
242 call win_gotoid(win_b)
243 call assert_equal(map(range(1, 10), 'string(v:val)'), getline(1, '$'))
244 bwipe! a
245 bwipe! b
246endfunc
247
Yegappan Lakshmanan30443242021-06-10 21:52:15 +0200248" Test for :diffget/:diffput with a range that is inside a diff chunk
249func Test_diffget_diffput_range()
250 call setline(1, range(1, 10))
251 new
252 call setline(1, range(11, 20))
253 windo diffthis
254 3,5diffget
255 call assert_equal(['13', '14', '15'], getline(3, 5))
256 call setline(1, range(1, 10))
257 4,8diffput
258 wincmd p
259 call assert_equal(['13', '4', '5', '6', '7', '8', '19'], getline(3, 9))
260 %bw!
261endfunc
262
263" Test for :diffget/:diffput with an empty buffer and a non-empty buffer
264func Test_diffget_diffput_empty_buffer()
265 %d _
266 new
267 call setline(1, 'one')
268 windo diffthis
269 diffget
270 call assert_equal(['one'], getline(1, '$'))
271 %d _
272 diffput
273 wincmd p
274 call assert_equal([''], getline(1, '$'))
275 %bw!
276endfunc
277
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100278" :diffput and :diffget completes names of buffers which
Dominique Pelle923dce22021-11-21 11:36:04 +0000279" are in diff mode and which are different than current buffer.
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100280" No completion when the current window is not in diff mode.
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100281func Test_diffget_diffput_completion()
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100282 e Xdiff1 | diffthis
283 botright new Xdiff2
284 botright new Xdiff3 | split | diffthis
285 botright new Xdiff4 | diffthis
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100286
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100287 wincmd t
288 call assert_equal('Xdiff1', bufname('%'))
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100289 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100290 call assert_equal('"diffput Xdiff3 Xdiff4', @:)
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100291 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100292 call assert_equal('"diffget Xdiff3 Xdiff4', @:)
293 call assert_equal(['Xdiff3', 'Xdiff4'], getcompletion('', 'diff_buffer'))
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100294
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100295 " Xdiff2 is not in diff mode, so no completion for :diffput, :diffget
296 wincmd j
297 call assert_equal('Xdiff2', bufname('%'))
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100298 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
299 call assert_equal('"diffput ', @:)
300 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
301 call assert_equal('"diffget ', @:)
302 call assert_equal([], getcompletion('', 'diff_buffer'))
303
Bram Moolenaarefcc3292019-12-30 21:59:03 +0100304 " Xdiff3 is split in 2 windows, only the top one is in diff mode.
305 " So completion of :diffput :diffget only happens in the top window.
306 wincmd j
307 call assert_equal('Xdiff3', bufname('%'))
308 call assert_equal(1, &diff)
309 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
310 call assert_equal('"diffput Xdiff1 Xdiff4', @:)
311 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
312 call assert_equal('"diffget Xdiff1 Xdiff4', @:)
313 call assert_equal(['Xdiff1', 'Xdiff4'], getcompletion('', 'diff_buffer'))
314
315 wincmd j
316 call assert_equal('Xdiff3', bufname('%'))
317 call assert_equal(0, &diff)
318 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
319 call assert_equal('"diffput ', @:)
320 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
321 call assert_equal('"diffget ', @:)
322 call assert_equal([], getcompletion('', 'diff_buffer'))
323
324 wincmd j
325 call assert_equal('Xdiff4', bufname('%'))
326 call feedkeys(":diffput \<C-A>\<C-B>\"\<CR>", 'tx')
327 call assert_equal('"diffput Xdiff1 Xdiff3', @:)
328 call feedkeys(":diffget \<C-A>\<C-B>\"\<CR>", 'tx')
329 call assert_equal('"diffget Xdiff1 Xdiff3', @:)
330 call assert_equal(['Xdiff1', 'Xdiff3'], getcompletion('', 'diff_buffer'))
331
Bram Moolenaarae7dba82019-12-29 13:56:33 +0100332 %bwipe
333endfunc
334
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200335func Test_dp_do_buffer()
336 e! one
337 let bn1=bufnr('%')
338 let l = range(60)
339 call setline(1, l)
340 diffthis
341
342 new two
343 let l[10] = 'one'
344 let l[20] = 'two'
345 let l[30] = 'three'
346 let l[40] = 'four'
347 let l[50] = 'five'
348 call setline(1, l)
349 diffthis
350
351 " dp and do with invalid buffer number.
352 11
353 call assert_fails('norm 99999dp', 'E102:')
354 call assert_fails('norm 99999do', 'E102:')
355 call assert_fails('diffput non_existing_buffer', 'E94:')
356 call assert_fails('diffget non_existing_buffer', 'E94:')
357
358 " dp and do with valid buffer number.
359 call assert_equal('one', getline('.'))
360 exe 'norm ' . bn1 . 'do'
361 call assert_equal('10', getline('.'))
362 21
363 call assert_equal('two', getline('.'))
Bram Moolenaar94722c52023-01-28 19:19:03 +0000364 diffget one
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200365 call assert_equal('20', getline('.'))
366
367 31
368 exe 'norm ' . bn1 . 'dp'
369 41
370 diffput one
371 wincmd w
372 31
373 call assert_equal('three', getline('.'))
374 41
375 call assert_equal('four', getline('.'))
376
377 " dp and do with buffer number which is not in diff mode.
378 new not_in_diff_mode
379 let bn3=bufnr('%')
380 wincmd w
381 51
382 call assert_fails('exe "norm" . bn3 . "dp"', 'E103:')
383 call assert_fails('exe "norm" . bn3 . "do"', 'E103:')
384 call assert_fails('diffput not_in_diff_mode', 'E94:')
385 call assert_fails('diffget not_in_diff_mode', 'E94:')
386
387 windo diffoff
388 %bwipe!
Bram Moolenaar42093c02016-07-30 16:16:54 +0200389endfunc
Bram Moolenaare67d5462016-08-27 22:40:42 +0200390
Bram Moolenaardf77cef2018-10-07 17:46:42 +0200391func Test_do_lastline()
392 e! one
393 call setline(1, ['1','2','3','4','5','6'])
394 diffthis
395
396 new two
397 call setline(1, ['2','4','5'])
398 diffthis
399
400 1
401 norm dp]c
402 norm dp]c
403 wincmd w
404 call assert_equal(4, line('$'))
405 norm G
406 norm do
407 call assert_equal(3, line('$'))
408
409 windo diffoff
410 %bwipe!
411endfunc
412
Bram Moolenaare67d5462016-08-27 22:40:42 +0200413func Test_diffoff()
414 enew!
415 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200416 redraw
Bram Moolenaare67d5462016-08-27 22:40:42 +0200417 let normattr = screenattr(1, 1)
418 diffthis
419 botright vert new
420 call setline(1, ['One', '', 'Two', 'Three'])
421 diffthis
422 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +0200423 call assert_notequal(normattr, 1->screenattr(1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200424 diffoff!
425 redraw
426 call assert_equal(normattr, screenattr(1, 1))
427 bwipe!
428 bwipe!
429endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200430
Bram Moolenaare828b762018-09-10 17:51:58 +0200431func Common_icase_test()
432 edit one
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200433 call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200434 redraw
435 let normattr = screenattr(1, 1)
436 diffthis
437
438 botright vert new two
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200439 call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VE'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200440 diffthis
441
442 redraw
443 call assert_equal(normattr, screenattr(1, 1))
444 call assert_equal(normattr, screenattr(2, 1))
445 call assert_notequal(normattr, screenattr(3, 1))
446 call assert_equal(normattr, screenattr(4, 1))
447
Bram Moolenaarda22b8c2017-09-02 18:01:50 +0200448 let dtextattr = screenattr(5, 3)
449 call assert_notequal(dtextattr, screenattr(5, 1))
450 call assert_notequal(dtextattr, screenattr(5, 5))
451
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200452 diffoff!
453 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200454endfunc
455
456func Test_diffopt_icase()
457 set diffopt=icase,foldcolumn:0
458 call Common_icase_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200459 set diffopt&
460endfunc
461
Bram Moolenaare828b762018-09-10 17:51:58 +0200462func Test_diffopt_icase_internal()
463 set diffopt=icase,foldcolumn:0,internal
464 call Common_icase_test()
465 set diffopt&
466endfunc
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200467
Bram Moolenaare828b762018-09-10 17:51:58 +0200468func Common_iwhite_test()
469 edit one
470 " Difference in trailing spaces and amount of spaces should be ignored,
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200471 " but not other space differences.
Bram Moolenaare828b762018-09-10 17:51:58 +0200472 call setline(1, ["One \t", 'Two', 'Three', 'one two', 'one two', 'Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200473 redraw
474 let normattr = screenattr(1, 1)
475 diffthis
476
477 botright vert new two
Bram Moolenaare828b762018-09-10 17:51:58 +0200478 call setline(1, ["One\t ", "Two\t ", 'Three', 'one two', 'onetwo', ' Four'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200479 diffthis
480
481 redraw
482 call assert_equal(normattr, screenattr(1, 1))
483 call assert_equal(normattr, screenattr(2, 1))
484 call assert_equal(normattr, screenattr(3, 1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200485 call assert_equal(normattr, screenattr(4, 1))
486 call assert_notequal(normattr, screenattr(5, 1))
487 call assert_notequal(normattr, screenattr(6, 1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200488
489 diffoff!
490 %bwipe!
Bram Moolenaare828b762018-09-10 17:51:58 +0200491endfunc
492
493func Test_diffopt_iwhite()
494 set diffopt=iwhite,foldcolumn:0
495 call Common_iwhite_test()
496 set diffopt&
497endfunc
498
499func Test_diffopt_iwhite_internal()
500 set diffopt=internal,iwhite,foldcolumn:0
501 call Common_iwhite_test()
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200502 set diffopt&
503endfunc
504
505func Test_diffopt_context()
506 enew!
507 call setline(1, ['1', '2', '3', '4', '5', '6', '7'])
508 diffthis
509 new
510 call setline(1, ['1', '2', '3', '4', '5x', '6', '7'])
511 diffthis
512
513 set diffopt=context:2
514 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200515 set diffopt=internal,context:2
516 call assert_equal('+-- 2 lines: 1', foldtextresult(1))
517
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200518 set diffopt=context:1
519 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaare828b762018-09-10 17:51:58 +0200520 set diffopt=internal,context:1
521 call assert_equal('+-- 3 lines: 1', foldtextresult(1))
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200522
523 diffoff!
524 %bwipe!
525 set diffopt&
526endfunc
527
528func Test_diffopt_horizontal()
Bram Moolenaare828b762018-09-10 17:51:58 +0200529 set diffopt=internal,horizontal
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200530 diffsplit
531
532 call assert_equal(&columns, winwidth(1))
533 call assert_equal(&columns, winwidth(2))
534 call assert_equal(&lines, winheight(1) + winheight(2) + 3)
535 call assert_inrange(0, 1, winheight(1) - winheight(2))
536
537 set diffopt&
538 diffoff!
539 %bwipe
540endfunc
541
542func Test_diffopt_vertical()
Bram Moolenaare828b762018-09-10 17:51:58 +0200543 set diffopt=internal,vertical
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200544 diffsplit
545
546 call assert_equal(&lines - 2, winheight(1))
547 call assert_equal(&lines - 2, winheight(2))
548 call assert_equal(&columns, winwidth(1) + winwidth(2) + 1)
549 call assert_inrange(0, 1, winwidth(1) - winwidth(2))
550
551 set diffopt&
552 diffoff!
553 %bwipe
554endfunc
555
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100556func Test_diffopt_hiddenoff()
Bram Moolenaare828b762018-09-10 17:51:58 +0200557 set diffopt=internal,filler,foldcolumn:0,hiddenoff
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100558 e! one
559 call setline(1, ['Two', 'Three'])
560 redraw
561 let normattr = screenattr(1, 1)
562 diffthis
563 botright vert new two
564 call setline(1, ['One', 'Four'])
565 diffthis
566 redraw
567 call assert_notequal(normattr, screenattr(1, 1))
568 set hidden
569 close
570 redraw
571 " should not diffing with hidden buffer two while 'hiddenoff' is enabled
572 call assert_equal(normattr, screenattr(1, 1))
573
574 bwipe!
575 bwipe!
576 set hidden& diffopt&
577endfunc
578
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100579func Test_diffoff_hidden()
Bram Moolenaare828b762018-09-10 17:51:58 +0200580 set diffopt=internal,filler,foldcolumn:0
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100581 e! one
582 call setline(1, ['Two', 'Three'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200583 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100584 let normattr = screenattr(1, 1)
585 diffthis
586 botright vert new two
587 call setline(1, ['One', 'Four'])
588 diffthis
589 redraw
590 call assert_notequal(normattr, screenattr(1, 1))
591 set hidden
592 close
593 redraw
594 " diffing with hidden buffer two
595 call assert_notequal(normattr, screenattr(1, 1))
596 diffoff
597 redraw
598 call assert_equal(normattr, screenattr(1, 1))
599 diffthis
600 redraw
601 " still diffing with hidden buffer two
602 call assert_notequal(normattr, screenattr(1, 1))
603 diffoff!
604 redraw
605 call assert_equal(normattr, screenattr(1, 1))
606 diffthis
607 redraw
608 " no longer diffing with hidden buffer two
609 call assert_equal(normattr, screenattr(1, 1))
610
611 bwipe!
612 bwipe!
613 set hidden& diffopt&
614endfunc
615
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200616func Test_setting_cursor()
617 new Xtest1
618 put =range(1,90)
619 wq
620 new Xtest2
621 put =range(1,100)
622 wq
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200623
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200624 tabe Xtest2
625 $
626 diffsp Xtest1
627 tabclose
628
629 call delete('Xtest1')
630 call delete('Xtest2')
631endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100632
633func Test_diff_move_to()
634 new
635 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
636 diffthis
637 vnew
638 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
639 diffthis
640 norm ]c
641 call assert_equal(2, line('.'))
642 norm 3]c
643 call assert_equal(9, line('.'))
644 norm 10]c
645 call assert_equal(11, line('.'))
646 norm [c
647 call assert_equal(9, line('.'))
648 norm 2[c
649 call assert_equal(5, line('.'))
650 norm 10[c
651 call assert_equal(2, line('.'))
652 %bwipe!
653endfunc
654
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200655func Test_diffexpr()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +0100656 CheckExecutable diff
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200657
658 func DiffExpr()
Bram Moolenaar485b6272021-05-18 19:19:03 +0200659 " Prepend some text to check diff type detection
Bram Moolenaar3b8defd2018-09-13 13:03:11 +0200660 call writefile(['warning', ' message'], v:fname_out)
661 silent exe '!diff ' . v:fname_in . ' ' . v:fname_new . '>>' . v:fname_out
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200662 endfunc
663 set diffexpr=DiffExpr()
664 set diffopt=foldcolumn:0
665
666 enew!
667 call setline(1, ['one', 'two', 'three'])
668 redraw
669 let normattr = screenattr(1, 1)
670 diffthis
671
672 botright vert new
673 call setline(1, ['one', 'two', 'three.'])
674 diffthis
675
676 redraw
677 call assert_equal(normattr, screenattr(1, 1))
678 call assert_equal(normattr, screenattr(2, 1))
679 call assert_notequal(normattr, screenattr(3, 1))
Yegappan Lakshmanan30443242021-06-10 21:52:15 +0200680 diffoff!
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200681
Dominique Pelle923dce22021-11-21 11:36:04 +0000682 " Try using a non-existing function for 'diffexpr'.
Yegappan Lakshmanan30443242021-06-10 21:52:15 +0200683 set diffexpr=NewDiffFunc()
684 call assert_fails('windo diffthis', ['E117:', 'E97:'])
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200685 diffoff!
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000686
687 " Using a script-local function
688 func s:NewDiffExpr()
689 endfunc
690 set diffexpr=s:NewDiffExpr()
691 call assert_equal(expand('<SID>') .. 'NewDiffExpr()', &diffexpr)
692 set diffexpr=<SID>NewDiffExpr()
693 call assert_equal(expand('<SID>') .. 'NewDiffExpr()', &diffexpr)
694
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200695 %bwipe!
696 set diffexpr& diffopt&
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000697 delfunc DiffExpr
698 delfunc s:NewDiffExpr
Bram Moolenaar79a213d2017-05-16 13:15:18 +0200699endfunc
700
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100701func Test_diffpatch()
702 " The patch program on MS-Windows may fail or hang.
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200703 CheckExecutable patch
704 CheckUnix
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100705 new
706 insert
707***************
708*** 1,3 ****
709 1
710! 2
711 3
712--- 1,4 ----
713 1
714! 2x
715 3
716+ 4
717.
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200718 saveas! Xpatch
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100719 bwipe!
720 new
721 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100722
Bram Moolenaara95ab322017-03-11 19:21:53 +0100723 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100724 call setline(1, ['1', '2', '3'])
725 if name != 'Xpatch'
726 call rename('Xpatch', name)
727 endif
728 exe 'diffpatch ' . escape(name, '$')
729 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
730 if name != 'Xpatch'
731 call rename(name, 'Xpatch')
732 endif
733 bwipe!
734 endfor
735
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100736 call delete('Xpatch')
737 bwipe!
738endfunc
739
Bram Moolenaar23a971d2023-04-04 22:04:53 +0100740" FIXME: test fails, the Xresult file can't be read
741func No_Test_diffpatch_restricted()
742 let lines =<< trim END
743 call assert_fails('diffpatch NoSuchDiff', 'E145:')
744
745 call writefile(v:errors, 'Xresult')
746 qa!
747 END
748 call writefile(lines, 'Xrestricted', 'D')
749 if RunVim([], [], '-Z --clean -S Xrestricted')
750 call assert_equal([], readfile('Xresult'))
751 endif
752 call delete('Xresult')
753endfunc
754
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100755func Test_diff_too_many_buffers()
756 for i in range(1, 8)
757 exe "new Xtest" . i
758 diffthis
759 endfor
760 new Xtest9
761 call assert_fails('diffthis', 'E96:')
762 %bwipe!
763endfunc
764
765func Test_diff_nomodifiable()
766 new
767 call setline(1, [1, 2, 3, 4])
768 setl nomodifiable
769 diffthis
770 vnew
771 call setline(1, ['1x', 2, 3, 3, 4])
772 diffthis
773 call assert_fails('norm dp', 'E793:')
774 setl nomodifiable
775 call assert_fails('norm do', 'E21:')
776 %bwipe!
777endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100778
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200779func Test_diff_hlID()
780 new
781 call setline(1, [1, 2, 3])
782 diffthis
783 vnew
784 call setline(1, ['1x', 2, 'x', 3])
785 diffthis
786 redraw
787
Bram Moolenaara74e4942019-08-04 17:35:53 +0200788 call diff_hlID(-1, 1)->synIDattr("name")->assert_equal("")
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200789
Bram Moolenaara74e4942019-08-04 17:35:53 +0200790 call diff_hlID(1, 1)->synIDattr("name")->assert_equal("DiffChange")
791 call diff_hlID(1, 2)->synIDattr("name")->assert_equal("DiffText")
792 call diff_hlID(2, 1)->synIDattr("name")->assert_equal("")
793 call diff_hlID(3, 1)->synIDattr("name")->assert_equal("DiffAdd")
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200794 eval 4->diff_hlID(1)->synIDattr("name")->assert_equal("")
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200795
796 wincmd w
797 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
798 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
799 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
800
801 %bwipe!
802endfunc
803
804func Test_diff_filler()
805 new
806 call setline(1, [1, 2, 3, 'x', 4])
807 diffthis
808 vnew
809 call setline(1, [1, 2, 'y', 'y', 3, 4])
810 diffthis
811 redraw
812
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200813 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 +0200814 wincmd w
815 call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
816
817 %bwipe!
818endfunc
819
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100820func Test_diff_lastline()
821 enew!
822 only!
823 call setline(1, ['This is a ', 'line with five ', 'rows'])
824 diffthis
825 botright vert new
826 call setline(1, ['This is', 'a line with ', 'four rows'])
827 diffthis
828 1
829 call feedkeys("Je a\<CR>", 'tx')
830 call feedkeys("Je a\<CR>", 'tx')
831 let w1lines = winline()
832 wincmd w
833 $
834 let w2lines = winline()
835 call assert_equal(w2lines, w1lines)
836 bwipe!
837 bwipe!
838endfunc
Bram Moolenaare828b762018-09-10 17:51:58 +0200839
Bram Moolenaar785fc652018-09-15 19:17:38 +0200840func WriteDiffFiles(buf, list1, list2)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100841 call writefile(a:list1, 'Xdifile1')
842 call writefile(a:list2, 'Xdifile2')
Bram Moolenaar785fc652018-09-15 19:17:38 +0200843 if a:buf
844 call term_sendkeys(a:buf, ":checktime\<CR>")
845 endif
Bram Moolenaare828b762018-09-10 17:51:58 +0200846endfunc
847
Bram Moolenaar785fc652018-09-15 19:17:38 +0200848" Verify a screendump with both the internal and external diff.
Bram Moolenaare828b762018-09-10 17:51:58 +0200849func VerifyBoth(buf, dumpfile, extra)
Bram Moolenaare828b762018-09-10 17:51:58 +0200850 " trailing : for leaving the cursor on the command line
Bram Moolenaar785fc652018-09-15 19:17:38 +0200851 for cmd in [":set diffopt=filler" . a:extra . "\<CR>:", ":set diffopt+=internal\<CR>:"]
Bram Moolenaare828b762018-09-10 17:51:58 +0200852 call term_sendkeys(a:buf, cmd)
853 if VerifyScreenDump(a:buf, a:dumpfile, {}, cmd =~ 'internal' ? 'internal' : 'external')
Bram Moolenaar485b6272021-05-18 19:19:03 +0200854 " don't let the next iteration overwrite the "failed" file.
855 return
Bram Moolenaare828b762018-09-10 17:51:58 +0200856 endif
857 endfor
Bram Moolenaar485b6272021-05-18 19:19:03 +0200858
859 " also test unified diff
860 call term_sendkeys(a:buf, ":call SetupUnified()\<CR>:")
glacambread5c1782021-05-24 14:20:53 +0200861 call term_sendkeys(a:buf, ":redraw!\<CR>:")
Bram Moolenaar485b6272021-05-18 19:19:03 +0200862 call VerifyScreenDump(a:buf, a:dumpfile, {}, 'unified')
863 call term_sendkeys(a:buf, ":call StopUnified()\<CR>:")
Bram Moolenaare828b762018-09-10 17:51:58 +0200864endfunc
865
Bram Moolenaar785fc652018-09-15 19:17:38 +0200866" Verify a screendump with the internal diff only.
867func VerifyInternal(buf, dumpfile, extra)
868 call term_sendkeys(a:buf, ":diffupdate!\<CR>")
869 " trailing : for leaving the cursor on the command line
870 call term_sendkeys(a:buf, ":set diffopt=internal,filler" . a:extra . "\<CR>:")
871 call VerifyScreenDump(a:buf, a:dumpfile, {})
872endfunc
873
Bram Moolenaare828b762018-09-10 17:51:58 +0200874func Test_diff_screen()
Yee Cheng Chin49f2ba62024-02-14 20:34:58 +0100875 if has('osxdarwin') && system('diff --version') =~ '^Apple diff'
876 throw 'Skipped: unified diff does not work properly on this macOS version'
rhysde93d5ca2024-02-01 21:22:14 +0100877 endif
878
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100879 let g:test_is_flaky = 1
Bram Moolenaar3c8ee622019-08-03 22:55:50 +0200880 CheckScreendump
881 CheckFeature menu
882
Bram Moolenaar485b6272021-05-18 19:19:03 +0200883 let lines =<< trim END
884 func UnifiedDiffExpr()
885 " Prepend some text to check diff type detection
886 call writefile(['warning', ' message'], v:fname_out)
glacambread5c1782021-05-24 14:20:53 +0200887 silent exe '!diff -U0 ' .. v:fname_in .. ' ' .. v:fname_new .. '>>' .. v:fname_out
Bram Moolenaar485b6272021-05-18 19:19:03 +0200888 endfunc
889 func SetupUnified()
890 set diffexpr=UnifiedDiffExpr()
glacambread5c1782021-05-24 14:20:53 +0200891 diffupdate
Bram Moolenaar485b6272021-05-18 19:19:03 +0200892 endfunc
893 func StopUnified()
894 set diffexpr=
895 endfunc
896 END
Bram Moolenaar59173412022-09-20 22:01:33 +0100897 call writefile(lines, 'XdiffSetup', 'D')
Bram Moolenaar485b6272021-05-18 19:19:03 +0200898
Bram Moolenaare828b762018-09-10 17:51:58 +0200899 " clean up already existing swap files, just in case
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100900 call delete('.Xdifile1.swp')
901 call delete('.Xdifile2.swp')
Bram Moolenaare828b762018-09-10 17:51:58 +0200902
903 " Test 1: Add a line in beginning of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200904 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 +0100905 let buf = RunVimInTerminal('-d -S XdiffSetup Xdifile1 Xdifile2', {})
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100906 " Set autoread mode, so that Vim won't complain once we re-write the test
Bram Moolenaare828b762018-09-10 17:51:58 +0200907 " files
Bram Moolenaar785fc652018-09-15 19:17:38 +0200908 call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
Bram Moolenaare828b762018-09-10 17:51:58 +0200909
910 call VerifyBoth(buf, 'Test_diff_01', '')
911
912 " Test 2: Add a line in beginning of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200913 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 +0200914 call VerifyBoth(buf, 'Test_diff_02', '')
915
916 " Test 3: Add a line at the end of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200917 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 +0200918 call VerifyBoth(buf, 'Test_diff_03', '')
919
920 " Test 4: Add a line at the end of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200921 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 +0200922 call VerifyBoth(buf, 'Test_diff_04', '')
923
924 " 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 +0200925 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 +0200926 call VerifyBoth(buf, 'Test_diff_05', '')
927
928 " 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 +0200929 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 +0200930 call VerifyBoth(buf, 'Test_diff_06', '')
931
Bram Moolenaarb9ddda62019-02-19 23:00:50 +0100932 " Variants on test 6 with different context settings
933 call term_sendkeys(buf, ":set diffopt+=context:2\<cr>")
934 call VerifyScreenDump(buf, 'Test_diff_06.2', {})
935 call term_sendkeys(buf, ":set diffopt-=context:2\<cr>")
936 call term_sendkeys(buf, ":set diffopt+=context:1\<cr>")
937 call VerifyScreenDump(buf, 'Test_diff_06.1', {})
938 call term_sendkeys(buf, ":set diffopt-=context:1\<cr>")
939 call term_sendkeys(buf, ":set diffopt+=context:0\<cr>")
940 call VerifyScreenDump(buf, 'Test_diff_06.0', {})
941 call term_sendkeys(buf, ":set diffopt-=context:0\<cr>")
942
Bram Moolenaare828b762018-09-10 17:51:58 +0200943 " Test 7 - 9: Test normal/patience/histogram diff algorithm
Bram Moolenaar785fc652018-09-15 19:17:38 +0200944 call WriteDiffFiles(buf, ['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
Bram Moolenaare828b762018-09-10 17:51:58 +0200945 \ ' int i;', ' for(i = 0; i < 10; i++)', ' {', ' printf("Your answer is: ");',
946 \ ' printf("%d\n", foo);', ' }', '}', '', 'int fact(int n)', '{', ' if(n > 1)', ' {',
947 \ ' return fact(n-1) * n;', ' }', ' return 1;', '}', '', 'int main(int argc, char **argv)',
948 \ '{', ' frobnitz(fact(10));', '}'],
949 \ ['#include <stdio.h>', '', 'int fib(int n)', '{', ' if(n > 2)', ' {',
950 \ ' return fib(n-1) + fib(n-2);', ' }', ' return 1;', '}', '', '// Frobs foo heartily',
951 \ 'int frobnitz(int foo)', '{', ' int i;', ' for(i = 0; i < 10; i++)', ' {',
952 \ ' printf("%d\n", foo);', ' }', '}', '',
953 \ 'int main(int argc, char **argv)', '{', ' frobnitz(fib(10));', '}'])
954 call term_sendkeys(buf, ":diffupdate!\<cr>")
955 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
956 call VerifyScreenDump(buf, 'Test_diff_07', {})
957
958 call term_sendkeys(buf, ":set diffopt+=algorithm:patience\<cr>")
959 call VerifyScreenDump(buf, 'Test_diff_08', {})
960
961 call term_sendkeys(buf, ":set diffopt+=algorithm:histogram\<cr>")
962 call VerifyScreenDump(buf, 'Test_diff_09', {})
963
964 " Test 10-11: normal/indent-heuristic
965 call term_sendkeys(buf, ":set diffopt&vim\<cr>")
Bram Moolenaar785fc652018-09-15 19:17:38 +0200966 call WriteDiffFiles(buf, ['', ' def finalize(values)', '', ' values.each do |v|', ' v.finalize', ' end'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200967 \ ['', ' def finalize(values)', '', ' values.each do |v|', ' v.prepare', ' end', '',
968 \ ' values.each do |v|', ' v.finalize', ' end'])
969 call term_sendkeys(buf, ":diffupdate!\<cr>")
970 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
971 call VerifyScreenDump(buf, 'Test_diff_10', {})
972
Bram Moolenaarb6fc7282018-12-04 22:24:16 +0100973 " Leave trailing : at commandline!
974 call term_sendkeys(buf, ":set diffopt+=indent-heuristic\<cr>:\<cr>")
975 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'one')
976 " shouldn't matter, if indent-algorithm comes before or after the algorithm
977 call term_sendkeys(buf, ":set diffopt&\<cr>")
978 call term_sendkeys(buf, ":set diffopt+=indent-heuristic,algorithm:patience\<cr>:\<cr>")
979 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'two')
980 call term_sendkeys(buf, ":set diffopt&\<cr>")
981 call term_sendkeys(buf, ":set diffopt+=algorithm:patience,indent-heuristic\<cr>:\<cr>")
982 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'three')
Bram Moolenaare828b762018-09-10 17:51:58 +0200983
984 " Test 12: diff the same file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200985 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 +0200986 call VerifyBoth(buf, 'Test_diff_12', '')
987
988 " Test 13: diff an empty file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200989 call WriteDiffFiles(buf, [], [])
Bram Moolenaare828b762018-09-10 17:51:58 +0200990 call VerifyBoth(buf, 'Test_diff_13', '')
991
992 " Test 14: test diffopt+=icase
Bram Moolenaar785fc652018-09-15 19:17:38 +0200993 call WriteDiffFiles(buf, ['a', 'b', 'cd'], ['A', 'b', 'cDe'])
Bram Moolenaare828b762018-09-10 17:51:58 +0200994 call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
995
996 " Test 15-16: test diffopt+=iwhite
Bram Moolenaar785fc652018-09-15 19:17:38 +0200997 call WriteDiffFiles(buf, ['int main()', '{', ' printf("Hello, World!");', ' return 0;', '}'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200998 \ ['int main()', '{', ' if (0)', ' {', ' printf("Hello, World!");', ' return 0;', ' }', '}'])
999 call term_sendkeys(buf, ":diffupdate!\<cr>")
1000 call term_sendkeys(buf, ":set diffopt&vim diffopt+=filler diffopt+=iwhite\<cr>")
1001 call VerifyScreenDump(buf, 'Test_diff_15', {})
1002 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
1003 call VerifyScreenDump(buf, 'Test_diff_16', {})
1004
Bram Moolenaar785fc652018-09-15 19:17:38 +02001005 " Test 17: test diffopt+=iblank
1006 call WriteDiffFiles(buf, ['a', ' ', 'cd', 'ef', 'xxx'], ['a', 'cd', '', 'ef', 'yyy'])
1007 call VerifyInternal(buf, 'Test_diff_17', " diffopt+=iblank")
1008
1009 " Test 18: test diffopt+=iblank,iwhite / iwhiteall / iwhiteeol
1010 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhite")
1011 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteall")
1012 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteeol")
1013
1014 " Test 19: test diffopt+=iwhiteeol
1015 call WriteDiffFiles(buf, ['a ', 'x', 'cd', 'ef', 'xx xx', 'foo', 'bar'], ['a', 'x', 'c d', ' ef', 'xx xx', 'foo', '', 'bar'])
1016 call VerifyInternal(buf, 'Test_diff_19', " diffopt+=iwhiteeol")
1017
1018 " Test 19: test diffopt+=iwhiteall
1019 call VerifyInternal(buf, 'Test_diff_20', " diffopt+=iwhiteall")
1020
Bram Moolenaare828b762018-09-10 17:51:58 +02001021 " clean up
1022 call StopVimInTerminal(buf)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001023 call delete('Xdifile1')
1024 call delete('Xdifile2')
Bram Moolenaare828b762018-09-10 17:51:58 +02001025endfunc
1026
Bram Moolenaar04626c22021-09-01 16:02:07 +02001027func Test_diff_with_scroll_and_change()
1028 CheckScreendump
1029
1030 let lines =<< trim END
1031 call setline(1, range(1, 15))
1032 vnew
1033 call setline(1, range(9, 15))
1034 windo diffthis
1035 wincmd h
1036 exe "normal Gl5\<C-E>"
1037 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001038 call writefile(lines, 'Xtest_scroll_change', 'D')
Bram Moolenaar04626c22021-09-01 16:02:07 +02001039 let buf = RunVimInTerminal('-S Xtest_scroll_change', {})
1040
1041 call VerifyScreenDump(buf, 'Test_diff_scroll_change_01', {})
1042
1043 call term_sendkeys(buf, "ax\<Esc>")
1044 call VerifyScreenDump(buf, 'Test_diff_scroll_change_02', {})
1045
Bram Moolenaar841c2252021-10-22 20:56:55 +01001046 call term_sendkeys(buf, "\<C-W>lay\<Esc>")
1047 call VerifyScreenDump(buf, 'Test_diff_scroll_change_03', {})
1048
Bram Moolenaar04626c22021-09-01 16:02:07 +02001049 " clean up
1050 call StopVimInTerminal(buf)
Bram Moolenaar04626c22021-09-01 16:02:07 +02001051endfunc
1052
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001053func Test_diff_with_cursorline()
Bram Moolenaar3c8ee622019-08-03 22:55:50 +02001054 CheckScreendump
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001055
1056 call writefile([
1057 \ 'hi CursorLine ctermbg=red ctermfg=white',
1058 \ 'set cursorline',
1059 \ 'call setline(1, ["foo","foo","foo","bar"])',
1060 \ 'vnew',
1061 \ 'call setline(1, ["bee","foo","foo","baz"])',
1062 \ 'windo diffthis',
1063 \ '2wincmd w',
Bram Moolenaar59173412022-09-20 22:01:33 +01001064 \ ], 'Xtest_diff_cursorline', 'D')
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001065 let buf = RunVimInTerminal('-S Xtest_diff_cursorline', {})
1066
1067 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_01', {})
1068 call term_sendkeys(buf, "j")
1069 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_02', {})
1070 call term_sendkeys(buf, "j")
1071 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_03', {})
1072
1073 " clean up
1074 call StopVimInTerminal(buf)
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001075endfunc
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001076
Bram Moolenaar127969c2022-03-06 19:54:13 +00001077func Test_diff_with_cursorline_number()
1078 CheckScreendump
1079
1080 let lines =<< trim END
1081 hi CursorLine ctermbg=red ctermfg=white
1082 hi CursorLineNr ctermbg=white ctermfg=black cterm=underline
1083 set cursorline number
1084 call setline(1, ["baz", "foo", "foo", "bar"])
1085 2
1086 vnew
1087 call setline(1, ["foo", "foo", "bar"])
1088 windo diffthis
1089 1wincmd w
1090 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001091 call writefile(lines, 'Xtest_diff_cursorline_number', 'D')
Bram Moolenaar127969c2022-03-06 19:54:13 +00001092 let buf = RunVimInTerminal('-S Xtest_diff_cursorline_number', {})
1093
1094 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_01', {})
1095 call term_sendkeys(buf, ":set cursorlineopt=number\r")
1096 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_02', {})
1097
1098 " clean up
1099 call StopVimInTerminal(buf)
Bram Moolenaar127969c2022-03-06 19:54:13 +00001100endfunc
1101
zeertzjq4f33bc22021-08-05 17:57:02 +02001102func Test_diff_with_cursorline_breakindent()
1103 CheckScreendump
1104
zeertzjq588f20d2023-12-05 15:47:09 +01001105 let lines =<< trim END
1106 hi CursorLine ctermbg=red ctermfg=white
1107 set noequalalways wrap diffopt=followwrap cursorline breakindent
1108 50vnew
1109 call setline(1, [' ', ' ', ' ', ' '])
1110 exe "norm! 20Afoo\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abar\<Esc>"
1111 vnew
1112 call setline(1, [' ', ' ', ' ', ' '])
1113 exe "norm! 20Abee\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abaz\<Esc>"
1114 windo diffthis
1115 2wincmd w
1116 END
1117 call writefile(lines, 'Xtest_diff_cursorline_breakindent', 'D')
zeertzjq4f33bc22021-08-05 17:57:02 +02001118 let buf = RunVimInTerminal('-S Xtest_diff_cursorline_breakindent', {})
1119
1120 call term_sendkeys(buf, "gg0")
1121 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_01', {})
1122 call term_sendkeys(buf, "j")
1123 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_02', {})
1124 call term_sendkeys(buf, "j")
1125 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_03', {})
1126 call term_sendkeys(buf, "j")
1127 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_04', {})
1128
1129 " clean up
1130 call StopVimInTerminal(buf)
zeertzjq4f33bc22021-08-05 17:57:02 +02001131endfunc
1132
zeertzjq588f20d2023-12-05 15:47:09 +01001133func Test_diff_breakindent_after_filler()
1134 CheckScreendump
1135
1136 let lines =<< trim END
zeertzjqf0a9d652024-02-12 22:53:20 +01001137 set laststatus=0 diffopt+=followwrap breakindent breakindentopt=min:0
zeertzjq588f20d2023-12-05 15:47:09 +01001138 call setline(1, ['a', ' ' .. repeat('c', 50)])
1139 vnew
1140 call setline(1, ['a', 'b', ' ' .. repeat('c', 50)])
1141 windo diffthis
1142 norm! G$
1143 END
1144 call writefile(lines, 'Xtest_diff_breakindent_after_filler', 'D')
1145 let buf = RunVimInTerminal('-S Xtest_diff_breakindent_after_filler', #{rows: 8, cols: 45})
1146 call VerifyScreenDump(buf, 'Test_diff_breakindent_after_filler', {})
1147
1148 " clean up
1149 call StopVimInTerminal(buf)
1150endfunc
1151
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001152func Test_diff_with_syntax()
1153 CheckScreendump
1154
1155 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001156 void doNothing() {
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001157 int x = 0;
1158 char *s = "hello";
1159 return 5;
1160 }
1161 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001162 call writefile(lines, 'Xprogram1.c', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001163 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001164 void doSomething() {
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001165 int x = 0;
1166 char *s = "there";
1167 return 5;
1168 }
1169 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001170 call writefile(lines, 'Xprogram2.c', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001171
1172 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001173 edit Xprogram1.c
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001174 diffsplit Xprogram2.c
1175 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001176 call writefile(lines, 'Xtest_diff_syntax', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001177 let buf = RunVimInTerminal('-S Xtest_diff_syntax', {})
1178
1179 call VerifyScreenDump(buf, 'Test_diff_syntax_1', {})
1180
1181 " clean up
1182 call StopVimInTerminal(buf)
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001183endfunc
1184
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001185func Test_diff_of_diff()
Bram Moolenaar3c8ee622019-08-03 22:55:50 +02001186 CheckScreendump
1187 CheckFeature rightleft
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001188
1189 call writefile([
1190 \ 'call setline(1, ["aa","bb","cc","@@ -3,2 +5,7 @@","dd","ee","ff"])',
1191 \ 'vnew',
1192 \ 'call setline(1, ["aa","bb","cc"])',
1193 \ 'windo diffthis',
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01001194 \ '1wincmd w',
1195 \ 'setlocal number',
Bram Moolenaar59173412022-09-20 22:01:33 +01001196 \ ], 'Xtest_diff_diff', 'D')
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001197 let buf = RunVimInTerminal('-S Xtest_diff_diff', {})
1198
1199 call VerifyScreenDump(buf, 'Test_diff_of_diff_01', {})
1200
Bram Moolenaare73f9112019-03-29 18:29:54 +01001201 call term_sendkeys(buf, ":set rightleft\<cr>")
1202 call VerifyScreenDump(buf, 'Test_diff_of_diff_02', {})
1203
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001204 " clean up
1205 call StopVimInTerminal(buf)
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001206endfunc
Bram Moolenaarc8234772019-11-10 21:00:27 +01001207
1208func CloseoffSetup()
1209 enew
1210 call setline(1, ['one', 'two', 'three'])
1211 diffthis
1212 new
1213 call setline(1, ['one', 'tow', 'three'])
1214 diffthis
1215 call assert_equal(1, &diff)
1216 only!
1217endfunc
1218
1219func Test_diff_closeoff()
1220 " "closeoff" included by default: last diff win gets 'diff' reset'
1221 call CloseoffSetup()
1222 call assert_equal(0, &diff)
1223 enew!
1224
1225 " "closeoff" excluded: last diff win keeps 'diff' set'
1226 set diffopt-=closeoff
1227 call CloseoffSetup()
1228 call assert_equal(1, &diff)
1229 diffoff!
1230 enew!
1231endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001232
Bram Moolenaar4223d432021-02-10 13:18:17 +01001233func Test_diff_followwrap()
1234 new
1235 set diffopt+=followwrap
1236 set wrap
1237 diffthis
1238 call assert_equal(1, &wrap)
1239 diffoff
1240 set nowrap
1241 diffthis
1242 call assert_equal(0, &wrap)
1243 diffoff
1244 set diffopt&
1245 bwipe!
1246endfunc
1247
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001248func Test_diff_maintains_change_mark()
Sean Dewarccc16442021-12-29 16:44:48 +00001249 func DiffMaintainsChangeMark()
1250 enew!
1251 call setline(1, ['a', 'b', 'c', 'd'])
1252 diffthis
1253 new
1254 call setline(1, ['a', 'b', 'c', 'e'])
1255 " Set '[ and '] marks
1256 2,3yank
1257 call assert_equal([2, 3], [line("'["), line("']")])
1258 " Verify they aren't affected by the implicit diff
1259 diffthis
1260 call assert_equal([2, 3], [line("'["), line("']")])
1261 " Verify they aren't affected by an explicit diff
1262 diffupdate
1263 call assert_equal([2, 3], [line("'["), line("']")])
1264 bwipe!
1265 bwipe!
1266 endfunc
1267
1268 set diffopt-=internal
1269 call DiffMaintainsChangeMark()
1270 set diffopt+=internal
1271 call DiffMaintainsChangeMark()
Bram Moolenaard9b74a22022-01-16 15:00:08 +00001272
Sean Dewarccc16442021-12-29 16:44:48 +00001273 set diffopt&
Bram Moolenaard9b74a22022-01-16 15:00:08 +00001274 delfunc DiffMaintainsChangeMark
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001275endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001276
1277" Test for 'patchexpr'
1278func Test_patchexpr()
1279 let g:patch_args = []
1280 func TPatch()
1281 call add(g:patch_args, readfile(v:fname_in))
1282 call add(g:patch_args, readfile(v:fname_diff))
1283 call writefile(['output file'], v:fname_out)
1284 endfunc
1285 set patchexpr=TPatch()
1286
Bram Moolenaar59173412022-09-20 22:01:33 +01001287 call writefile(['input file'], 'Xinput', 'D')
1288 call writefile(['diff file'], 'Xdiff', 'D')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001289 %bwipe!
1290 edit Xinput
1291 diffpatch Xdiff
1292 call assert_equal('output file', getline(1))
1293 call assert_equal('Xinput.new', bufname())
1294 call assert_equal(2, winnr('$'))
1295 call assert_true(&diff)
1296
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00001297 " Using a script-local function
1298 func s:NewPatchExpr()
1299 endfunc
1300 set patchexpr=s:NewPatchExpr()
1301 call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1302 set patchexpr=<SID>NewPatchExpr()
1303 call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1304
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001305 set patchexpr&
1306 delfunc TPatch
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00001307 delfunc s:NewPatchExpr
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001308 %bwipe!
1309endfunc
1310
Bram Moolenaar511feec2020-06-18 19:15:27 +02001311func Test_diff_rnu()
1312 CheckScreendump
1313
1314 let content =<< trim END
1315 call setline(1, ['a', 'a', 'a', 'y', 'b', 'b', 'b', 'b', 'b'])
1316 vnew
1317 call setline(1, ['a', 'a', 'a', 'x', 'x', 'x', 'b', 'b', 'b', 'b', 'b'])
1318 windo diffthis
1319 setlocal number rnu foldcolumn=0
1320 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001321 call writefile(content, 'Xtest_diff_rnu', 'D')
Bram Moolenaar511feec2020-06-18 19:15:27 +02001322 let buf = RunVimInTerminal('-S Xtest_diff_rnu', {})
1323
1324 call VerifyScreenDump(buf, 'Test_diff_rnu_01', {})
1325
1326 call term_sendkeys(buf, "j")
1327 call VerifyScreenDump(buf, 'Test_diff_rnu_02', {})
1328 call term_sendkeys(buf, "j")
1329 call VerifyScreenDump(buf, 'Test_diff_rnu_03', {})
1330
1331 " clean up
1332 call StopVimInTerminal(buf)
Bram Moolenaar511feec2020-06-18 19:15:27 +02001333endfunc
1334
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001335func Test_diff_multilineconceal()
1336 new
1337 diffthis
1338
1339 new
1340 call matchadd('Conceal', 'a\nb', 9, -1, {'conceal': 'Y'})
1341 set cole=2 cocu=n
1342 call setline(1, ["a", "b"])
1343 diffthis
1344 redraw
1345endfunc
1346
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001347func Test_diff_and_scroll()
1348 " this was causing an ml_get error
1349 set ls=2
Bram Moolenaar94722c52023-01-28 19:19:03 +00001350 for i in range(winheight(0) * 2)
1351 call setline(i, i < winheight(0) - 10 ? i : i + 10)
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001352 endfor
1353 vnew
Bram Moolenaar94722c52023-01-28 19:19:03 +00001354 for i in range(winheight(0)*2 + 10)
1355 call setline(i, i < winheight(0) - 10 ? 0 : i)
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001356 endfor
1357 diffthis
1358 wincmd p
1359 diffthis
1360 execute 'normal ' . winheight(0) . "\<C-d>"
1361
1362 bwipe!
1363 bwipe!
1364 set ls&
1365endfunc
1366
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001367func Test_diff_filler_cursorcolumn()
1368 CheckScreendump
1369
1370 let content =<< trim END
1371 call setline(1, ['aa', 'bb', 'cc'])
1372 vnew
1373 call setline(1, ['aa', 'cc'])
1374 windo diffthis
1375 wincmd p
1376 setlocal cursorcolumn foldcolumn=0
1377 norm! gg0
1378 redraw!
1379 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001380 call writefile(content, 'Xtest_diff_cuc', 'D')
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001381 let buf = RunVimInTerminal('-S Xtest_diff_cuc', {})
1382
1383 call VerifyScreenDump(buf, 'Test_diff_cuc_01', {})
1384
1385 call term_sendkeys(buf, "l")
1386 call term_sendkeys(buf, "\<C-l>")
1387 call VerifyScreenDump(buf, 'Test_diff_cuc_02', {})
1388 call term_sendkeys(buf, "0j")
1389 call term_sendkeys(buf, "\<C-l>")
1390 call VerifyScreenDump(buf, 'Test_diff_cuc_03', {})
1391 call term_sendkeys(buf, "l")
1392 call term_sendkeys(buf, "\<C-l>")
1393 call VerifyScreenDump(buf, 'Test_diff_cuc_04', {})
1394
1395 " clean up
1396 call StopVimInTerminal(buf)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001397endfunc
1398
Yegappan Lakshmanan30443242021-06-10 21:52:15 +02001399" Test for adding/removing lines inside diff chunks, between diff chunks
1400" and before diff chunks
1401func Test_diff_modify_chunks()
1402 enew!
1403 let w2_id = win_getid()
1404 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1405 new
1406 let w1_id = win_getid()
1407 call setline(1, ['a', '2', '3', 'd', 'e', 'f', '7', '8', 'i'])
1408 windo diffthis
1409
1410 " remove a line between two diff chunks and create a new diff chunk
1411 call win_gotoid(w2_id)
1412 5d
1413 call win_gotoid(w1_id)
1414 call diff_hlID(5, 1)->synIDattr('name')->assert_equal('DiffAdd')
1415
1416 " add a line between two diff chunks
1417 call win_gotoid(w2_id)
1418 normal! 4Goe
1419 call win_gotoid(w1_id)
1420 call diff_hlID(4, 1)->synIDattr('name')->assert_equal('')
1421 call diff_hlID(5, 1)->synIDattr('name')->assert_equal('')
1422
1423 " remove all the lines in a diff chunk.
1424 call win_gotoid(w2_id)
1425 7,8d
1426 call win_gotoid(w1_id)
1427 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1428 call assert_equal(['', 'DiffText', 'DiffText', '', '', '', 'DiffAdd',
1429 \ 'DiffAdd', ''], hl)
1430
1431 " remove lines from one diff chunk to just before the next diff chunk
1432 call win_gotoid(w2_id)
1433 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1434 2,6d
1435 call win_gotoid(w1_id)
1436 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1437 call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', 'DiffAdd',
1438 \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1439
1440 " remove lines just before the top of a diff chunk
1441 call win_gotoid(w2_id)
1442 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1443 5,6d
1444 call win_gotoid(w1_id)
1445 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1446 call assert_equal(['', 'DiffText', 'DiffText', '', 'DiffText', 'DiffText',
1447 \ 'DiffAdd', 'DiffAdd', ''], hl)
1448
1449 " remove line after the end of a diff chunk
1450 call win_gotoid(w2_id)
1451 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1452 4d
1453 call win_gotoid(w1_id)
1454 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1455 call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', '', '', 'DiffText',
1456 \ 'DiffText', ''], hl)
1457
1458 " remove lines starting from the end of one diff chunk and ending inside
1459 " another diff chunk
1460 call win_gotoid(w2_id)
1461 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1462 4,7d
1463 call win_gotoid(w1_id)
1464 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1465 call assert_equal(['', 'DiffText', 'DiffText', 'DiffText', 'DiffAdd',
1466 \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1467
1468 " removing the only remaining diff chunk should make the files equal
1469 call win_gotoid(w2_id)
1470 call setline(1, ['a', '2', '3', 'x', 'd', 'e', 'f', 'x', '7', '8', 'i'])
1471 8d
1472 let hl = range(1, 10)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1473 call assert_equal(['', '', '', 'DiffAdd', '', '', '', '', '', ''], hl)
1474 call win_gotoid(w2_id)
1475 4d
1476 call win_gotoid(w1_id)
1477 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1478 call assert_equal(['', '', '', '', '', '', '', '', ''], hl)
1479
1480 %bw!
1481endfunc
glacambread5c1782021-05-24 14:20:53 +02001482
Bram Moolenaar06f60952021-12-28 18:30:05 +00001483func Test_diff_binary()
1484 CheckScreendump
1485
1486 let content =<< trim END
1487 call setline(1, ['a', 'b', "c\n", 'd', 'e', 'f', 'g'])
1488 vnew
1489 call setline(1, ['A', 'b', 'c', 'd', 'E', 'f', 'g'])
1490 windo diffthis
1491 wincmd p
1492 norm! gg0
1493 redraw!
1494 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001495 call writefile(content, 'Xtest_diff_bin', 'D')
Bram Moolenaar06f60952021-12-28 18:30:05 +00001496 let buf = RunVimInTerminal('-S Xtest_diff_bin', {})
1497
1498 " Test using internal diff
1499 call VerifyScreenDump(buf, 'Test_diff_bin_01', {})
1500
1501 " Test using internal diff and case folding
1502 call term_sendkeys(buf, ":set diffopt+=icase\<cr>")
1503 call term_sendkeys(buf, "\<C-l>")
1504 call VerifyScreenDump(buf, 'Test_diff_bin_02', {})
1505 " Test using external diff
1506 call term_sendkeys(buf, ":set diffopt=filler\<cr>")
1507 call term_sendkeys(buf, "\<C-l>")
1508 call VerifyScreenDump(buf, 'Test_diff_bin_03', {})
1509 " Test using external diff and case folding
1510 call term_sendkeys(buf, ":set diffopt=filler,icase\<cr>")
1511 call term_sendkeys(buf, "\<C-l>")
1512 call VerifyScreenDump(buf, 'Test_diff_bin_04', {})
1513
1514 " clean up
1515 call StopVimInTerminal(buf)
Bram Moolenaar06f60952021-12-28 18:30:05 +00001516 set diffopt&vim
1517endfunc
1518
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001519" Test for using the 'zi' command to invert 'foldenable' in diff windows (test
1520" for the issue fixed by patch 6.2.317)
1521func Test_diff_foldinvert()
1522 %bw!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001523 edit Xdoffile1
1524 new Xdoffile2
1525 new Xdoffile3
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001526 windo diffthis
1527 " open a non-diff window
1528 botright new
1529 1wincmd w
1530 call assert_true(getwinvar(1, '&foldenable'))
1531 call assert_true(getwinvar(2, '&foldenable'))
1532 call assert_true(getwinvar(3, '&foldenable'))
1533 normal zi
1534 call assert_false(getwinvar(1, '&foldenable'))
1535 call assert_false(getwinvar(2, '&foldenable'))
1536 call assert_false(getwinvar(3, '&foldenable'))
1537 normal zi
1538 call assert_true(getwinvar(1, '&foldenable'))
1539 call assert_true(getwinvar(2, '&foldenable'))
1540 call assert_true(getwinvar(3, '&foldenable'))
1541
1542 " If the current window has 'noscrollbind', then 'zi' should not change
1543 " 'foldenable' in other windows.
1544 1wincmd w
1545 set noscrollbind
1546 normal zi
1547 call assert_false(getwinvar(1, '&foldenable'))
1548 call assert_true(getwinvar(2, '&foldenable'))
1549 call assert_true(getwinvar(3, '&foldenable'))
1550
1551 " 'zi' should not change the 'foldenable' for windows with 'noscrollbind'
1552 1wincmd w
1553 set scrollbind
1554 normal zi
1555 call setwinvar(2, '&scrollbind', v:false)
1556 normal zi
1557 call assert_false(getwinvar(1, '&foldenable'))
1558 call assert_true(getwinvar(2, '&foldenable'))
1559 call assert_false(getwinvar(3, '&foldenable'))
1560
1561 %bw!
1562 set scrollbind&
1563endfunc
1564
Bram Moolenaara315ce12022-06-24 12:38:57 +01001565" This was scrolling for 'cursorbind' but 'scrollbind' is more important
1566func Test_diff_scroll()
1567 CheckScreendump
1568
1569 let left =<< trim END
1570 line 1
1571 line 2
1572 line 3
1573 line 4
1574
1575 // Common block
1576 // one
1577 // containing
1578 // four lines
1579
1580 // Common block
1581 // two
1582 // containing
1583 // four lines
1584 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001585 call writefile(left, 'Xleft', 'D')
Bram Moolenaara315ce12022-06-24 12:38:57 +01001586 let right =<< trim END
1587 line 1
1588 line 2
1589 line 3
1590 line 4
1591
1592 Lorem
1593 ipsum
1594 dolor
1595 sit
1596 amet,
1597 consectetur
1598 adipiscing
1599 elit.
1600 Etiam
1601 luctus
1602 lectus
1603 sodales,
1604 dictum
1605
1606 // Common block
1607 // one
1608 // containing
1609 // four lines
1610
1611 Vestibulum
1612 tincidunt
1613 aliquet
1614 nulla.
1615
1616 // Common block
1617 // two
1618 // containing
1619 // four lines
1620 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001621 call writefile(right, 'Xright', 'D')
Bram Moolenaara315ce12022-06-24 12:38:57 +01001622 let buf = RunVimInTerminal('-d Xleft Xright', {'rows': 12})
1623 call term_sendkeys(buf, "\<C-W>\<C-W>jjjj")
1624 call VerifyScreenDump(buf, 'Test_diff_scroll_1', {})
1625 call term_sendkeys(buf, "j")
1626 call VerifyScreenDump(buf, 'Test_diff_scroll_2', {})
1627
1628 call StopVimInTerminal(buf)
Bram Moolenaara315ce12022-06-24 12:38:57 +01001629endfunc
1630
Bram Moolenaar38d867f2023-04-01 19:54:40 +01001631" This was scrolling too many lines.
1632func Test_diff_scroll_wrap_on()
1633 20new
1634 40vsplit
1635 call setline(1, map(range(1, 9), 'repeat(v:val, 200)'))
1636 setlocal number diff so=0
1637 redraw
1638 normal! jj
1639 call assert_equal(1, winsaveview().topline)
1640 normal! j
1641 call assert_equal(2, winsaveview().topline)
zeertzjq05834912023-10-04 20:12:37 +02001642
1643 bwipe!
1644 bwipe!
1645endfunc
1646
1647func Test_diff_scroll_many_filler()
1648 20new
1649 vnew
Luuk van Baalb9f5b952024-03-26 18:46:45 +01001650 call setline(1, range(1, 40))
zeertzjq05834912023-10-04 20:12:37 +02001651 diffthis
1652 setlocal scrolloff=0
1653 wincmd p
Luuk van Baalb9f5b952024-03-26 18:46:45 +01001654 call setline(1, range(1, 20)->reverse() + ['###']->repeat(41) + range(21, 40)->reverse())
zeertzjq05834912023-10-04 20:12:37 +02001655 diffthis
1656 setlocal scrolloff=0
1657 wincmd p
1658 redraw
1659
1660 " Note: need a redraw after each scroll, otherwise the test always passes.
Luuk van Baalb9f5b952024-03-26 18:46:45 +01001661 for _ in range(2)
1662 normal! G
1663 redraw
1664 call assert_equal(40, winsaveview().topline)
1665 call assert_equal(19, winsaveview().topfill)
1666 exe "normal! \<C-B>"
1667 redraw
1668 call assert_equal(22, winsaveview().topline)
1669 call assert_equal(0, winsaveview().topfill)
1670 exe "normal! \<C-B>"
1671 redraw
1672 call assert_equal(4, winsaveview().topline)
1673 call assert_equal(0, winsaveview().topfill)
1674 exe "normal! \<C-B>"
1675 redraw
1676 call assert_equal(1, winsaveview().topline)
1677 call assert_equal(0, winsaveview().topfill)
1678 set smoothscroll
1679 endfor
zeertzjq05834912023-10-04 20:12:37 +02001680
Luuk van Baalb9f5b952024-03-26 18:46:45 +01001681 set smoothscroll&
Luuk van Baalcb204e62024-04-02 20:49:45 +02001682 %bwipe!
Bram Moolenaar38d867f2023-04-01 19:54:40 +01001683endfunc
1684
Bram Moolenaarcd38bb42022-06-26 14:04:07 +01001685" This was trying to update diffs for a buffer being closed
1686func Test_diff_only()
1687 silent! lfile
1688 set diff
1689 lopen
1690 norm o
1691 silent! norm o
1692
1693 set nodiff
1694 %bwipe!
1695endfunc
1696
Bram Moolenaarc101abf2022-06-26 16:53:34 +01001697" This was causing invalid diff block values
1698" FIXME: somehow this causes a valgrind error when run directly but not when
1699" run as a test.
1700func Test_diff_manipulations()
1701 set diff
1702 split 0
1703 sil! norm R doobdeuR doobdeuR doobdeu
1704
1705 set nodiff
1706 %bwipe!
1707endfunc
1708
Bram Moolenaar4e677b92022-07-28 18:44:27 +01001709" This was causing the line number in the diff block to go below one.
1710" FIXME: somehow this causes a valgrind error when run directly but not when
1711" run as a test.
1712func Test_diff_put_and_undo()
1713 set diff
1714 next 0
1715 split 00
1716 sil! norm o0gguudpo0ggJuudp
1717
1718 bwipe!
1719 bwipe!
1720 set nodiff
1721endfunc
1722
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001723" Test for the diff() function
1724def Test_diff_func()
1725 # string is added/removed/modified at the beginning
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001726 assert_equal("@@ -0,0 +1 @@\n+abc\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001727 diff(['def'], ['abc', 'def'], {output: 'unified'}))
1728 assert_equal([{from_idx: 0, from_count: 0, to_idx: 0, to_count: 1}],
1729 diff(['def'], ['abc', 'def'], {output: 'indices'}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001730 assert_equal("@@ -1 +0,0 @@\n-abc\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001731 diff(['abc', 'def'], ['def'], {output: 'unified'}))
1732 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 0}],
1733 diff(['abc', 'def'], ['def'], {output: 'indices'}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001734 assert_equal("@@ -1 +1 @@\n-abc\n+abx\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001735 diff(['abc', 'def'], ['abx', 'def'], {output: 'unified'}))
1736 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1737 diff(['abc', 'def'], ['abx', 'def'], {output: 'indices'}))
1738
1739 # string is added/removed/modified at the end
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001740 assert_equal("@@ -1,0 +2 @@\n+def\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001741 diff(['abc'], ['abc', 'def'], {output: 'unified'}))
1742 assert_equal([{from_idx: 1, from_count: 0, to_idx: 1, to_count: 1}],
1743 diff(['abc'], ['abc', 'def'], {output: 'indices'}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001744 assert_equal("@@ -2 +1,0 @@\n-def\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001745 diff(['abc', 'def'], ['abc'], {output: 'unified'}))
1746 assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 0}],
1747 diff(['abc', 'def'], ['abc'], {output: 'indices'}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001748 assert_equal("@@ -2 +2 @@\n-def\n+xef\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001749 diff(['abc', 'def'], ['abc', 'xef'], {output: 'unified'}))
1750 assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 1}],
1751 diff(['abc', 'def'], ['abc', 'xef'], {output: 'indices'}))
1752
1753 # string is added/removed/modified in the middle
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001754 assert_equal("@@ -2,0 +3 @@\n+xxx\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001755 diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'unified'}))
1756 assert_equal([{from_idx: 2, from_count: 0, to_idx: 2, to_count: 1}],
1757 diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'indices'}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001758 assert_equal("@@ -3 +2,0 @@\n-333\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001759 diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'unified'}))
1760 assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 0}],
1761 diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'indices'}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001762 assert_equal("@@ -3 +3 @@\n-333\n+xxx\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001763 diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'unified'}))
1764 assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 1}],
1765 diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'indices'}))
1766
1767 # new strings are added to an empty List
1768 assert_equal("@@ -0,0 +1,2 @@\n+abc\n+def\n",
1769 diff([], ['abc', 'def'], {output: 'unified'}))
1770 assert_equal([{from_idx: 0, from_count: 0, to_idx: 0, to_count: 2}],
1771 diff([], ['abc', 'def'], {output: 'indices'}))
1772
1773 # all the strings are removed from a List
1774 assert_equal("@@ -1,2 +0,0 @@\n-abc\n-def\n",
1775 diff(['abc', 'def'], [], {output: 'unified'}))
1776 assert_equal([{from_idx: 0, from_count: 2, to_idx: 0, to_count: 0}],
1777 diff(['abc', 'def'], [], {output: 'indices'}))
1778
1779 # First character is added/removed/different
1780 assert_equal("@@ -1 +1 @@\n-abc\n+bc\n",
1781 diff(['abc'], ['bc'], {output: 'unified'}))
1782 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1783 diff(['abc'], ['bc'], {output: 'indices'}))
1784 assert_equal("@@ -1 +1 @@\n-bc\n+abc\n",
1785 diff(['bc'], ['abc'], {output: 'unified'}))
1786 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1787 diff(['bc'], ['abc'], {output: 'indices'}))
1788 assert_equal("@@ -1 +1 @@\n-abc\n+xbc\n",
1789 diff(['abc'], ['xbc'], {output: 'unified'}))
1790 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1791 diff(['abc'], ['xbc'], {output: 'indices'}))
1792
1793 # Last character is added/removed/different
1794 assert_equal("@@ -1 +1 @@\n-abc\n+abcd\n",
1795 diff(['abc'], ['abcd'], {output: 'unified'}))
1796 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1797 diff(['abc'], ['abcd'], {output: 'indices'}))
1798 assert_equal("@@ -1 +1 @@\n-abcd\n+abc\n",
1799 diff(['abcd'], ['abc'], {output: 'unified'}))
1800 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1801 diff(['abcd'], ['abc'], {output: 'indices'}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001802 var diff_unified: string = diff(['abc'], ['abx'], {output: 'unified'})
1803 assert_equal("@@ -1 +1 @@\n-abc\n+abx\n", diff_unified)
1804 var diff_indices: list<dict<number>> =
1805 diff(['abc'], ['abx'], {output: 'indices'})
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001806 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001807 diff_indices)
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001808
1809 # partial string modification at the start and at the end.
1810 var fromlist =<< trim END
1811 one two
1812 three four
1813 five six
1814 END
1815 var tolist =<< trim END
1816 one
1817 six
1818 END
1819 assert_equal("@@ -1,3 +1,2 @@\n-one two\n-three four\n-five six\n+one\n+six\n", diff(fromlist, tolist, {output: 'unified'}))
1820 assert_equal([{from_idx: 0, from_count: 3, to_idx: 0, to_count: 2}],
1821 diff(fromlist, tolist, {output: 'indices'}))
1822
1823 # non-contiguous modifications
1824 fromlist =<< trim END
1825 one two
1826 three four
1827 five abc six
1828 END
1829 tolist =<< trim END
1830 one abc two
1831 three four
1832 five six
1833 END
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001834 assert_equal("@@ -1 +1 @@\n-one two\n+one abc two\n@@ -3 +3 @@\n-five abc six\n+five six\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001835 diff(fromlist, tolist, {output: 'unified'}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001836 assert_equal([{'from_count': 1, 'to_idx': 0, 'to_count': 1, 'from_idx': 0},
1837 {'from_count': 1, 'to_idx': 2, 'to_count': 1, 'from_idx': 2}],
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001838 diff(fromlist, tolist, {output: 'indices'}))
1839
1840 # add/remove blank lines
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001841 assert_equal("@@ -2,2 +1,0 @@\n-\n-\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001842 diff(['one', '', '', 'two'], ['one', 'two'], {output: 'unified'}))
1843 assert_equal([{from_idx: 1, from_count: 2, to_idx: 1, to_count: 0}],
1844 diff(['one', '', '', 'two'], ['one', 'two'], {output: 'indices'}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001845 assert_equal("@@ -1,0 +2,2 @@\n+\n+\n",
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001846 diff(['one', 'two'], ['one', '', '', 'two'], {output: 'unified'}))
1847 assert_equal([{'from_idx': 1, 'from_count': 0, 'to_idx': 1, 'to_count': 2}],
1848 diff(['one', 'two'], ['one', '', '', 'two'], {output: 'indices'}))
1849
1850 # diff ignoring case
1851 assert_equal('', diff(['abc', 'def'], ['ABC', 'DEF'], {icase: true, output: 'unified'}))
1852 assert_equal([], diff(['abc', 'def'], ['ABC', 'DEF'], {icase: true, output: 'indices'}))
1853
1854 # diff ignoring all whitespace changes except leading whitespace changes
1855 assert_equal('', diff(['abc def'], ['abc def '], {iwhite: true}))
1856 assert_equal("@@ -1 +1 @@\n- abc\n+ xxx\n", diff([' abc'], [' xxx'], {iwhite: v:true}))
1857 assert_equal("@@ -1 +1 @@\n- abc\n+ xxx\n", diff([' abc'], [' xxx'], {iwhite: v:false}))
1858 assert_equal("@@ -1 +1 @@\n-abc \n+xxx \n", diff(['abc '], ['xxx '], {iwhite: v:true}))
1859 assert_equal("@@ -1 +1 @@\n-abc \n+xxx \n", diff(['abc '], ['xxx '], {iwhite: v:false}))
1860
1861 # diff ignoring all whitespace changes
1862 assert_equal('', diff(['abc def'], [' abc def '], {iwhiteall: true}))
1863 assert_equal("@@ -1 +1 @@\n- abc \n+ xxx \n", diff([' abc '], [' xxx '], {iwhiteall: v:true}))
1864 assert_equal("@@ -1 +1 @@\n- abc \n+ xxx \n", diff([' abc '], [' xxx '], {iwhiteall: v:false}))
1865
1866 # diff ignoring trailing whitespace changes
1867 assert_equal('', diff(['abc'], ['abc '], {iwhiteeol: true}))
1868
1869 # diff ignoring blank lines
1870 assert_equal('', diff(['one', '', '', 'two'], ['one', 'two'], {iblank: true}))
1871 assert_equal('', diff(['one', 'two'], ['one', '', '', 'two'], {iblank: true}))
1872
1873 # same string
1874 assert_equal('', diff(['abc', 'def', 'ghi'], ['abc', 'def', 'ghi']))
1875 assert_equal('', diff([''], ['']))
1876 assert_equal('', diff([], []))
1877
1878 # different xdiff algorithms
1879 for algo in ['myers', 'minimal', 'patience', 'histogram']
1880 assert_equal("@@ -1 +1 @@\n- abc \n+ xxx \n",
1881 diff([' abc '], [' xxx '], {algorithm: algo, output: 'unified'}))
1882 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1883 diff([' abc '], [' xxx '], {algorithm: algo, output: 'indices'}))
1884 endfor
1885 assert_equal("@@ -1 +1 @@\n- abc \n+ xxx \n",
1886 diff([' abc '], [' xxx '], {indent-heuristic: true, output: 'unified'}))
1887 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1888 diff([' abc '], [' xxx '], {indent-heuristic: true, output: 'indices'}))
1889
1890 # identical strings
1891 assert_equal('', diff(['111', '222'], ['111', '222'], {output: 'unified'}))
1892 assert_equal([], diff(['111', '222'], ['111', '222'], {output: 'indices'}))
1893 assert_equal('', diff([], [], {output: 'unified'}))
1894 assert_equal([], diff([], [], {output: 'indices'}))
1895
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01001896 # If 'diffexpr' is set, it should not be used for diff()
1897 def MyDiffExpr()
1898 enddef
1899 var save_diffexpr = &diffexpr
1900 :set diffexpr=MyDiffExpr()
1901 assert_equal("@@ -1 +1 @@\n-abc\n+\n",
1902 diff(['abc'], [''], {output: 'unified'}))
1903 assert_equal([{'from_idx': 0, 'from_count': 1, 'to_idx': 0, 'to_count': 1}],
1904 diff(['abc'], [''], {output: 'indices'}))
1905 assert_equal('MyDiffExpr()', &diffexpr)
1906 &diffexpr = save_diffexpr
1907
1908 # try different values for unified diff 'context'
1909 assert_equal("@@ -0,0 +1 @@\n+x\n",
1910 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c']))
1911 assert_equal("@@ -0,0 +1 @@\n+x\n",
1912 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 0}))
1913 assert_equal("@@ -1 +1,2 @@\n+x\n a\n",
1914 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 1}))
1915 assert_equal("@@ -1,2 +1,3 @@\n+x\n a\n b\n",
1916 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 2}))
1917 assert_equal("@@ -1,3 +1,4 @@\n+x\n a\n b\n c\n",
1918 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 3}))
1919 assert_equal("@@ -1,3 +1,4 @@\n+x\n a\n b\n c\n",
1920 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 4}))
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001921 assert_equal("@@ -0,0 +1 @@\n+x\n",
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01001922 diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: -1}))
1923
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001924 # Error cases
1925 assert_fails('call diff({}, ["a"])', 'E1211:')
1926 assert_fails('call diff(["a"], {})', 'E1211:')
1927 assert_fails('call diff(["a"], ["a"], [])', 'E1206:')
1928 assert_fails('call diff(["a"], ["a"], {output: "xyz"})', 'E106: Unsupported diff output format: xyz')
Yegappan Lakshmananbe156a32024-02-11 17:08:29 +01001929 assert_fails('call diff(["a"], ["a"], {context: []})', 'E745: Using a List as a Number')
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001930enddef
Bram Moolenaara315ce12022-06-24 12:38:57 +01001931
Yegappan Lakshmanana0010a12024-02-12 20:21:26 +01001932" Test for using the diff() function with 'diffexpr'
1933func Test_diffexpr_with_diff_func()
1934 CheckScreendump
1935
1936 let lines =<< trim END
1937 def DiffFuncExpr()
1938 var in: list<string> = readfile(v:fname_in)
1939 var new: list<string> = readfile(v:fname_new)
1940 var out: string = diff(in, new)
1941 writefile(split(out, "\n"), v:fname_out)
1942 enddef
1943 set diffexpr=DiffFuncExpr()
1944
1945 edit Xdifffunc1.txt
1946 diffthis
1947 vert split Xdifffunc2.txt
1948 diffthis
1949 END
1950 call writefile(lines, 'XsetupDiffFunc.vim', 'D')
1951
1952 call writefile(['zero', 'one', 'two', 'three'], 'Xdifffunc1.txt', 'D')
1953 call writefile(['one', 'twox', 'three', 'four'], 'Xdifffunc2.txt', 'D')
1954
1955 let buf = RunVimInTerminal('-S XsetupDiffFunc.vim', {'rows': 12})
1956 call VerifyScreenDump(buf, 'Test_difffunc_diffexpr_1', {})
1957 call StopVimInTerminal(buf)
1958endfunc
1959
zeertzjq9e7f1fc2024-03-16 09:40:22 +01001960func Test_diff_toggle_wrap_skipcol_leftcol()
1961 61vnew
1962 call setline(1, 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.')
1963 30vnew
1964 call setline(1, 'ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.')
1965 let win1 = win_getid()
1966 setlocal smoothscroll
1967 exe "normal! $\<C-E>"
1968 wincmd l
1969 let win2 = win_getid()
1970 setlocal smoothscroll
1971 exe "normal! $\<C-E>"
1972 call assert_equal([
1973 \ '<<<sadipscing elitr, sed diam |<<<tetur sadipscing elitr, sed|',
1974 \ 'nonumy eirmod tempor invidunt | diam nonumy eirmod tempor inv|',
1975 \ 'ut labore et dolore magna aliq|idunt ut labore et dolore magn|',
1976 \ 'uyam erat, sed diam voluptua. |a aliquyam erat, sed diam volu|',
1977 \ '~ |ptua. |',
1978 \ ], ScreenLines([1, 5], 62))
1979 call assert_equal({'col': 29, 'row': 4, 'endcol': 29, 'curscol': 29},
1980 \ screenpos(win1, line('.', win1), col('.', win1)))
1981 call assert_equal({'col': 36, 'row': 5, 'endcol': 36, 'curscol': 36},
1982 \ screenpos(win2, line('.', win2), col('.', win2)))
1983
1984 wincmd h
1985 diffthis
1986 wincmd l
1987 diffthis
1988 normal! 0
1989 call assert_equal([
1990 \ ' ipsum dolor sit amet, conset| Lorem ipsum dolor sit amet, |',
1991 \ '~ |~ |',
1992 \ ], ScreenLines([1, 2], 62))
1993 call assert_equal({'col': 3, 'row': 1, 'endcol': 3, 'curscol': 3},
1994 \ screenpos(win1, line('.', win1), col('.', win1)))
1995 call assert_equal({'col': 34, 'row': 1, 'endcol': 34, 'curscol': 34},
1996 \ screenpos(win2, line('.', win2), col('.', win2)))
1997
1998 normal! $
1999 call assert_equal([
2000 \ ' voluptua. | diam voluptua. |',
2001 \ '~ |~ |',
2002 \ ], ScreenLines([1, 2], 62))
2003 call assert_equal({'col': 11, 'row': 1, 'endcol': 11, 'curscol': 11},
2004 \ screenpos(win1, line('.', win1), col('.', win1)))
2005 call assert_equal({'col': 48, 'row': 1, 'endcol': 48, 'curscol': 48},
2006 \ screenpos(win2, line('.', win2), col('.', win2)))
2007
2008 diffoff!
2009 call assert_equal([
2010 \ 'ipsum dolor sit amet, consetet|Lorem ipsum dolor sit amet, co|',
2011 \ 'ur sadipscing elitr, sed diam |nsetetur sadipscing elitr, sed|',
2012 \ 'nonumy eirmod tempor invidunt | diam nonumy eirmod tempor inv|',
2013 \ 'ut labore et dolore magna aliq|idunt ut labore et dolore magn|',
2014 \ 'uyam erat, sed diam voluptua. |a aliquyam erat, sed diam volu|',
2015 \ '~ |ptua. |',
2016 \ ], ScreenLines([1, 6], 62))
2017 call assert_equal({'col': 29, 'row': 5, 'endcol': 29, 'curscol': 29},
2018 \ screenpos(win1, line('.', win1), col('.', win1)))
2019 call assert_equal({'col': 36, 'row': 6, 'endcol': 36, 'curscol': 36},
2020 \ screenpos(win2, line('.', win2), col('.', win2)))
2021
2022 bwipe!
2023 bwipe!
2024endfunc
2025
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01002026" vim: shiftwidth=2 sts=2 expandtab