blob: 2211ba05a2c3dfc9373b2cf2741874fd00177e42 [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
Bram Moolenaar23a971d2023-04-04 22:04:53 +0100739" FIXME: test fails, the Xresult file can't be read
740func No_Test_diffpatch_restricted()
741 let lines =<< trim END
742 call assert_fails('diffpatch NoSuchDiff', 'E145:')
743
744 call writefile(v:errors, 'Xresult')
745 qa!
746 END
747 call writefile(lines, 'Xrestricted', 'D')
748 if RunVim([], [], '-Z --clean -S Xrestricted')
749 call assert_equal([], readfile('Xresult'))
750 endif
751 call delete('Xresult')
752endfunc
753
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100754func Test_diff_too_many_buffers()
755 for i in range(1, 8)
756 exe "new Xtest" . i
757 diffthis
758 endfor
759 new Xtest9
760 call assert_fails('diffthis', 'E96:')
761 %bwipe!
762endfunc
763
764func Test_diff_nomodifiable()
765 new
766 call setline(1, [1, 2, 3, 4])
767 setl nomodifiable
768 diffthis
769 vnew
770 call setline(1, ['1x', 2, 3, 3, 4])
771 diffthis
772 call assert_fails('norm dp', 'E793:')
773 setl nomodifiable
774 call assert_fails('norm do', 'E21:')
775 %bwipe!
776endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100777
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200778func Test_diff_hlID()
779 new
780 call setline(1, [1, 2, 3])
781 diffthis
782 vnew
783 call setline(1, ['1x', 2, 'x', 3])
784 diffthis
785 redraw
786
Bram Moolenaara74e4942019-08-04 17:35:53 +0200787 call diff_hlID(-1, 1)->synIDattr("name")->assert_equal("")
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200788
Bram Moolenaara74e4942019-08-04 17:35:53 +0200789 call diff_hlID(1, 1)->synIDattr("name")->assert_equal("DiffChange")
790 call diff_hlID(1, 2)->synIDattr("name")->assert_equal("DiffText")
791 call diff_hlID(2, 1)->synIDattr("name")->assert_equal("")
792 call diff_hlID(3, 1)->synIDattr("name")->assert_equal("DiffAdd")
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200793 eval 4->diff_hlID(1)->synIDattr("name")->assert_equal("")
Bram Moolenaar97fbc402017-09-26 19:41:46 +0200794
795 wincmd w
796 call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange")
797 call assert_equal(synIDattr(diff_hlID(2, 1), "name"), "")
798 call assert_equal(synIDattr(diff_hlID(3, 1), "name"), "")
799
800 %bwipe!
801endfunc
802
803func Test_diff_filler()
804 new
805 call setline(1, [1, 2, 3, 'x', 4])
806 diffthis
807 vnew
808 call setline(1, [1, 2, 'y', 'y', 3, 4])
809 diffthis
810 redraw
811
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200812 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 +0200813 wincmd w
814 call assert_equal([0, 0, 0, 0, 2, 0, 0, 0], map(range(-1, 6), 'diff_filler(v:val)'))
815
816 %bwipe!
817endfunc
818
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100819func Test_diff_lastline()
820 enew!
821 only!
822 call setline(1, ['This is a ', 'line with five ', 'rows'])
823 diffthis
824 botright vert new
825 call setline(1, ['This is', 'a line with ', 'four rows'])
826 diffthis
827 1
828 call feedkeys("Je a\<CR>", 'tx')
829 call feedkeys("Je a\<CR>", 'tx')
830 let w1lines = winline()
831 wincmd w
832 $
833 let w2lines = winline()
834 call assert_equal(w2lines, w1lines)
835 bwipe!
836 bwipe!
837endfunc
Bram Moolenaare828b762018-09-10 17:51:58 +0200838
Bram Moolenaar785fc652018-09-15 19:17:38 +0200839func WriteDiffFiles(buf, list1, list2)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100840 call writefile(a:list1, 'Xdifile1')
841 call writefile(a:list2, 'Xdifile2')
Bram Moolenaar785fc652018-09-15 19:17:38 +0200842 if a:buf
843 call term_sendkeys(a:buf, ":checktime\<CR>")
844 endif
Bram Moolenaare828b762018-09-10 17:51:58 +0200845endfunc
846
Bram Moolenaar785fc652018-09-15 19:17:38 +0200847" Verify a screendump with both the internal and external diff.
Bram Moolenaare828b762018-09-10 17:51:58 +0200848func VerifyBoth(buf, dumpfile, extra)
Bram Moolenaare828b762018-09-10 17:51:58 +0200849 " trailing : for leaving the cursor on the command line
Bram Moolenaar785fc652018-09-15 19:17:38 +0200850 for cmd in [":set diffopt=filler" . a:extra . "\<CR>:", ":set diffopt+=internal\<CR>:"]
Bram Moolenaare828b762018-09-10 17:51:58 +0200851 call term_sendkeys(a:buf, cmd)
852 if VerifyScreenDump(a:buf, a:dumpfile, {}, cmd =~ 'internal' ? 'internal' : 'external')
Bram Moolenaar485b6272021-05-18 19:19:03 +0200853 " don't let the next iteration overwrite the "failed" file.
854 return
Bram Moolenaare828b762018-09-10 17:51:58 +0200855 endif
856 endfor
Bram Moolenaar485b6272021-05-18 19:19:03 +0200857
858 " also test unified diff
859 call term_sendkeys(a:buf, ":call SetupUnified()\<CR>:")
glacambread5c1782021-05-24 14:20:53 +0200860 call term_sendkeys(a:buf, ":redraw!\<CR>:")
Bram Moolenaar485b6272021-05-18 19:19:03 +0200861 call VerifyScreenDump(a:buf, a:dumpfile, {}, 'unified')
862 call term_sendkeys(a:buf, ":call StopUnified()\<CR>:")
Bram Moolenaare828b762018-09-10 17:51:58 +0200863endfunc
864
Bram Moolenaar785fc652018-09-15 19:17:38 +0200865" Verify a screendump with the internal diff only.
866func VerifyInternal(buf, dumpfile, extra)
867 call term_sendkeys(a:buf, ":diffupdate!\<CR>")
868 " trailing : for leaving the cursor on the command line
869 call term_sendkeys(a:buf, ":set diffopt=internal,filler" . a:extra . "\<CR>:")
870 call VerifyScreenDump(a:buf, a:dumpfile, {})
871endfunc
872
Bram Moolenaare828b762018-09-10 17:51:58 +0200873func Test_diff_screen()
rhysde93d5ca2024-02-01 21:22:14 +0100874 if has('osx') && !empty($CI) && system('uname -m') =~# 'arm64'
875 throw 'Skipped: FIXME: This test fails on M1 Mac on GitHub Actions'
876 endif
877
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100878 let g:test_is_flaky = 1
Bram Moolenaar3c8ee622019-08-03 22:55:50 +0200879 CheckScreendump
880 CheckFeature menu
881
Bram Moolenaar485b6272021-05-18 19:19:03 +0200882 let lines =<< trim END
883 func UnifiedDiffExpr()
884 " Prepend some text to check diff type detection
885 call writefile(['warning', ' message'], v:fname_out)
glacambread5c1782021-05-24 14:20:53 +0200886 silent exe '!diff -U0 ' .. v:fname_in .. ' ' .. v:fname_new .. '>>' .. v:fname_out
Bram Moolenaar485b6272021-05-18 19:19:03 +0200887 endfunc
888 func SetupUnified()
889 set diffexpr=UnifiedDiffExpr()
glacambread5c1782021-05-24 14:20:53 +0200890 diffupdate
Bram Moolenaar485b6272021-05-18 19:19:03 +0200891 endfunc
892 func StopUnified()
893 set diffexpr=
894 endfunc
895 END
Bram Moolenaar59173412022-09-20 22:01:33 +0100896 call writefile(lines, 'XdiffSetup', 'D')
Bram Moolenaar485b6272021-05-18 19:19:03 +0200897
Bram Moolenaare828b762018-09-10 17:51:58 +0200898 " clean up already existing swap files, just in case
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100899 call delete('.Xdifile1.swp')
900 call delete('.Xdifile2.swp')
Bram Moolenaare828b762018-09-10 17:51:58 +0200901
902 " Test 1: Add a line in beginning of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200903 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 +0100904 let buf = RunVimInTerminal('-d -S XdiffSetup Xdifile1 Xdifile2', {})
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100905 " Set autoread mode, so that Vim won't complain once we re-write the test
Bram Moolenaare828b762018-09-10 17:51:58 +0200906 " files
Bram Moolenaar785fc652018-09-15 19:17:38 +0200907 call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
Bram Moolenaare828b762018-09-10 17:51:58 +0200908
909 call VerifyBoth(buf, 'Test_diff_01', '')
910
911 " Test 2: Add a line in beginning of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200912 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 +0200913 call VerifyBoth(buf, 'Test_diff_02', '')
914
915 " Test 3: Add a line at the end of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200916 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 +0200917 call VerifyBoth(buf, 'Test_diff_03', '')
918
919 " Test 4: Add a line at the end of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200920 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 +0200921 call VerifyBoth(buf, 'Test_diff_04', '')
922
923 " 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 +0200924 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 +0200925 call VerifyBoth(buf, 'Test_diff_05', '')
926
927 " 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 +0200928 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 +0200929 call VerifyBoth(buf, 'Test_diff_06', '')
930
Bram Moolenaarb9ddda62019-02-19 23:00:50 +0100931 " Variants on test 6 with different context settings
932 call term_sendkeys(buf, ":set diffopt+=context:2\<cr>")
933 call VerifyScreenDump(buf, 'Test_diff_06.2', {})
934 call term_sendkeys(buf, ":set diffopt-=context:2\<cr>")
935 call term_sendkeys(buf, ":set diffopt+=context:1\<cr>")
936 call VerifyScreenDump(buf, 'Test_diff_06.1', {})
937 call term_sendkeys(buf, ":set diffopt-=context:1\<cr>")
938 call term_sendkeys(buf, ":set diffopt+=context:0\<cr>")
939 call VerifyScreenDump(buf, 'Test_diff_06.0', {})
940 call term_sendkeys(buf, ":set diffopt-=context:0\<cr>")
941
Bram Moolenaare828b762018-09-10 17:51:58 +0200942 " Test 7 - 9: Test normal/patience/histogram diff algorithm
Bram Moolenaar785fc652018-09-15 19:17:38 +0200943 call WriteDiffFiles(buf, ['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
Bram Moolenaare828b762018-09-10 17:51:58 +0200944 \ ' int i;', ' for(i = 0; i < 10; i++)', ' {', ' printf("Your answer is: ");',
945 \ ' printf("%d\n", foo);', ' }', '}', '', 'int fact(int n)', '{', ' if(n > 1)', ' {',
946 \ ' return fact(n-1) * n;', ' }', ' return 1;', '}', '', 'int main(int argc, char **argv)',
947 \ '{', ' frobnitz(fact(10));', '}'],
948 \ ['#include <stdio.h>', '', 'int fib(int n)', '{', ' if(n > 2)', ' {',
949 \ ' return fib(n-1) + fib(n-2);', ' }', ' return 1;', '}', '', '// Frobs foo heartily',
950 \ 'int frobnitz(int foo)', '{', ' int i;', ' for(i = 0; i < 10; i++)', ' {',
951 \ ' printf("%d\n", foo);', ' }', '}', '',
952 \ 'int main(int argc, char **argv)', '{', ' frobnitz(fib(10));', '}'])
953 call term_sendkeys(buf, ":diffupdate!\<cr>")
954 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
955 call VerifyScreenDump(buf, 'Test_diff_07', {})
956
957 call term_sendkeys(buf, ":set diffopt+=algorithm:patience\<cr>")
958 call VerifyScreenDump(buf, 'Test_diff_08', {})
959
960 call term_sendkeys(buf, ":set diffopt+=algorithm:histogram\<cr>")
961 call VerifyScreenDump(buf, 'Test_diff_09', {})
962
963 " Test 10-11: normal/indent-heuristic
964 call term_sendkeys(buf, ":set diffopt&vim\<cr>")
Bram Moolenaar785fc652018-09-15 19:17:38 +0200965 call WriteDiffFiles(buf, ['', ' def finalize(values)', '', ' values.each do |v|', ' v.finalize', ' end'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200966 \ ['', ' def finalize(values)', '', ' values.each do |v|', ' v.prepare', ' end', '',
967 \ ' values.each do |v|', ' v.finalize', ' end'])
968 call term_sendkeys(buf, ":diffupdate!\<cr>")
969 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
970 call VerifyScreenDump(buf, 'Test_diff_10', {})
971
Bram Moolenaarb6fc7282018-12-04 22:24:16 +0100972 " Leave trailing : at commandline!
973 call term_sendkeys(buf, ":set diffopt+=indent-heuristic\<cr>:\<cr>")
974 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'one')
975 " shouldn't matter, if indent-algorithm comes before or after the algorithm
976 call term_sendkeys(buf, ":set diffopt&\<cr>")
977 call term_sendkeys(buf, ":set diffopt+=indent-heuristic,algorithm:patience\<cr>:\<cr>")
978 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'two')
979 call term_sendkeys(buf, ":set diffopt&\<cr>")
980 call term_sendkeys(buf, ":set diffopt+=algorithm:patience,indent-heuristic\<cr>:\<cr>")
981 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'three')
Bram Moolenaare828b762018-09-10 17:51:58 +0200982
983 " Test 12: diff the same file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200984 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 +0200985 call VerifyBoth(buf, 'Test_diff_12', '')
986
987 " Test 13: diff an empty file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200988 call WriteDiffFiles(buf, [], [])
Bram Moolenaare828b762018-09-10 17:51:58 +0200989 call VerifyBoth(buf, 'Test_diff_13', '')
990
991 " Test 14: test diffopt+=icase
Bram Moolenaar785fc652018-09-15 19:17:38 +0200992 call WriteDiffFiles(buf, ['a', 'b', 'cd'], ['A', 'b', 'cDe'])
Bram Moolenaare828b762018-09-10 17:51:58 +0200993 call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
994
995 " Test 15-16: test diffopt+=iwhite
Bram Moolenaar785fc652018-09-15 19:17:38 +0200996 call WriteDiffFiles(buf, ['int main()', '{', ' printf("Hello, World!");', ' return 0;', '}'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200997 \ ['int main()', '{', ' if (0)', ' {', ' printf("Hello, World!");', ' return 0;', ' }', '}'])
998 call term_sendkeys(buf, ":diffupdate!\<cr>")
999 call term_sendkeys(buf, ":set diffopt&vim diffopt+=filler diffopt+=iwhite\<cr>")
1000 call VerifyScreenDump(buf, 'Test_diff_15', {})
1001 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
1002 call VerifyScreenDump(buf, 'Test_diff_16', {})
1003
Bram Moolenaar785fc652018-09-15 19:17:38 +02001004 " Test 17: test diffopt+=iblank
1005 call WriteDiffFiles(buf, ['a', ' ', 'cd', 'ef', 'xxx'], ['a', 'cd', '', 'ef', 'yyy'])
1006 call VerifyInternal(buf, 'Test_diff_17', " diffopt+=iblank")
1007
1008 " Test 18: test diffopt+=iblank,iwhite / iwhiteall / iwhiteeol
1009 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhite")
1010 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteall")
1011 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteeol")
1012
1013 " Test 19: test diffopt+=iwhiteeol
1014 call WriteDiffFiles(buf, ['a ', 'x', 'cd', 'ef', 'xx xx', 'foo', 'bar'], ['a', 'x', 'c d', ' ef', 'xx xx', 'foo', '', 'bar'])
1015 call VerifyInternal(buf, 'Test_diff_19', " diffopt+=iwhiteeol")
1016
1017 " Test 19: test diffopt+=iwhiteall
1018 call VerifyInternal(buf, 'Test_diff_20', " diffopt+=iwhiteall")
1019
Bram Moolenaare828b762018-09-10 17:51:58 +02001020 " clean up
1021 call StopVimInTerminal(buf)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001022 call delete('Xdifile1')
1023 call delete('Xdifile2')
Bram Moolenaare828b762018-09-10 17:51:58 +02001024endfunc
1025
Bram Moolenaar04626c22021-09-01 16:02:07 +02001026func Test_diff_with_scroll_and_change()
1027 CheckScreendump
1028
1029 let lines =<< trim END
1030 call setline(1, range(1, 15))
1031 vnew
1032 call setline(1, range(9, 15))
1033 windo diffthis
1034 wincmd h
1035 exe "normal Gl5\<C-E>"
1036 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001037 call writefile(lines, 'Xtest_scroll_change', 'D')
Bram Moolenaar04626c22021-09-01 16:02:07 +02001038 let buf = RunVimInTerminal('-S Xtest_scroll_change', {})
1039
1040 call VerifyScreenDump(buf, 'Test_diff_scroll_change_01', {})
1041
1042 call term_sendkeys(buf, "ax\<Esc>")
1043 call VerifyScreenDump(buf, 'Test_diff_scroll_change_02', {})
1044
Bram Moolenaar841c2252021-10-22 20:56:55 +01001045 call term_sendkeys(buf, "\<C-W>lay\<Esc>")
1046 call VerifyScreenDump(buf, 'Test_diff_scroll_change_03', {})
1047
Bram Moolenaar04626c22021-09-01 16:02:07 +02001048 " clean up
1049 call StopVimInTerminal(buf)
Bram Moolenaar04626c22021-09-01 16:02:07 +02001050endfunc
1051
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001052func Test_diff_with_cursorline()
Bram Moolenaar3c8ee622019-08-03 22:55:50 +02001053 CheckScreendump
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001054
1055 call writefile([
1056 \ 'hi CursorLine ctermbg=red ctermfg=white',
1057 \ 'set cursorline',
1058 \ 'call setline(1, ["foo","foo","foo","bar"])',
1059 \ 'vnew',
1060 \ 'call setline(1, ["bee","foo","foo","baz"])',
1061 \ 'windo diffthis',
1062 \ '2wincmd w',
Bram Moolenaar59173412022-09-20 22:01:33 +01001063 \ ], 'Xtest_diff_cursorline', 'D')
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001064 let buf = RunVimInTerminal('-S Xtest_diff_cursorline', {})
1065
1066 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_01', {})
1067 call term_sendkeys(buf, "j")
1068 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_02', {})
1069 call term_sendkeys(buf, "j")
1070 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_03', {})
1071
1072 " clean up
1073 call StopVimInTerminal(buf)
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001074endfunc
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001075
Bram Moolenaar127969c2022-03-06 19:54:13 +00001076func Test_diff_with_cursorline_number()
1077 CheckScreendump
1078
1079 let lines =<< trim END
1080 hi CursorLine ctermbg=red ctermfg=white
1081 hi CursorLineNr ctermbg=white ctermfg=black cterm=underline
1082 set cursorline number
1083 call setline(1, ["baz", "foo", "foo", "bar"])
1084 2
1085 vnew
1086 call setline(1, ["foo", "foo", "bar"])
1087 windo diffthis
1088 1wincmd w
1089 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001090 call writefile(lines, 'Xtest_diff_cursorline_number', 'D')
Bram Moolenaar127969c2022-03-06 19:54:13 +00001091 let buf = RunVimInTerminal('-S Xtest_diff_cursorline_number', {})
1092
1093 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_01', {})
1094 call term_sendkeys(buf, ":set cursorlineopt=number\r")
1095 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_02', {})
1096
1097 " clean up
1098 call StopVimInTerminal(buf)
Bram Moolenaar127969c2022-03-06 19:54:13 +00001099endfunc
1100
zeertzjq4f33bc22021-08-05 17:57:02 +02001101func Test_diff_with_cursorline_breakindent()
1102 CheckScreendump
1103
zeertzjq588f20d2023-12-05 15:47:09 +01001104 let lines =<< trim END
1105 hi CursorLine ctermbg=red ctermfg=white
1106 set noequalalways wrap diffopt=followwrap cursorline breakindent
1107 50vnew
1108 call setline(1, [' ', ' ', ' ', ' '])
1109 exe "norm! 20Afoo\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abar\<Esc>"
1110 vnew
1111 call setline(1, [' ', ' ', ' ', ' '])
1112 exe "norm! 20Abee\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abaz\<Esc>"
1113 windo diffthis
1114 2wincmd w
1115 END
1116 call writefile(lines, 'Xtest_diff_cursorline_breakindent', 'D')
zeertzjq4f33bc22021-08-05 17:57:02 +02001117 let buf = RunVimInTerminal('-S Xtest_diff_cursorline_breakindent', {})
1118
1119 call term_sendkeys(buf, "gg0")
1120 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_01', {})
1121 call term_sendkeys(buf, "j")
1122 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_02', {})
1123 call term_sendkeys(buf, "j")
1124 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_03', {})
1125 call term_sendkeys(buf, "j")
1126 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_04', {})
1127
1128 " clean up
1129 call StopVimInTerminal(buf)
zeertzjq4f33bc22021-08-05 17:57:02 +02001130endfunc
1131
zeertzjq588f20d2023-12-05 15:47:09 +01001132func Test_diff_breakindent_after_filler()
1133 CheckScreendump
1134
1135 let lines =<< trim END
1136 set laststatus=0 diffopt+=followwrap breakindent
1137 call setline(1, ['a', ' ' .. repeat('c', 50)])
1138 vnew
1139 call setline(1, ['a', 'b', ' ' .. repeat('c', 50)])
1140 windo diffthis
1141 norm! G$
1142 END
1143 call writefile(lines, 'Xtest_diff_breakindent_after_filler', 'D')
1144 let buf = RunVimInTerminal('-S Xtest_diff_breakindent_after_filler', #{rows: 8, cols: 45})
1145 call VerifyScreenDump(buf, 'Test_diff_breakindent_after_filler', {})
1146
1147 " clean up
1148 call StopVimInTerminal(buf)
1149endfunc
1150
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001151func Test_diff_with_syntax()
1152 CheckScreendump
1153
1154 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001155 void doNothing() {
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001156 int x = 0;
1157 char *s = "hello";
1158 return 5;
1159 }
1160 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001161 call writefile(lines, 'Xprogram1.c', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001162 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001163 void doSomething() {
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001164 int x = 0;
1165 char *s = "there";
1166 return 5;
1167 }
1168 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001169 call writefile(lines, 'Xprogram2.c', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001170
1171 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001172 edit Xprogram1.c
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001173 diffsplit Xprogram2.c
1174 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001175 call writefile(lines, 'Xtest_diff_syntax', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001176 let buf = RunVimInTerminal('-S Xtest_diff_syntax', {})
1177
1178 call VerifyScreenDump(buf, 'Test_diff_syntax_1', {})
1179
1180 " clean up
1181 call StopVimInTerminal(buf)
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001182endfunc
1183
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001184func Test_diff_of_diff()
Bram Moolenaar3c8ee622019-08-03 22:55:50 +02001185 CheckScreendump
1186 CheckFeature rightleft
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001187
1188 call writefile([
1189 \ 'call setline(1, ["aa","bb","cc","@@ -3,2 +5,7 @@","dd","ee","ff"])',
1190 \ 'vnew',
1191 \ 'call setline(1, ["aa","bb","cc"])',
1192 \ 'windo diffthis',
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01001193 \ '1wincmd w',
1194 \ 'setlocal number',
Bram Moolenaar59173412022-09-20 22:01:33 +01001195 \ ], 'Xtest_diff_diff', 'D')
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001196 let buf = RunVimInTerminal('-S Xtest_diff_diff', {})
1197
1198 call VerifyScreenDump(buf, 'Test_diff_of_diff_01', {})
1199
Bram Moolenaare73f9112019-03-29 18:29:54 +01001200 call term_sendkeys(buf, ":set rightleft\<cr>")
1201 call VerifyScreenDump(buf, 'Test_diff_of_diff_02', {})
1202
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001203 " clean up
1204 call StopVimInTerminal(buf)
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001205endfunc
Bram Moolenaarc8234772019-11-10 21:00:27 +01001206
1207func CloseoffSetup()
1208 enew
1209 call setline(1, ['one', 'two', 'three'])
1210 diffthis
1211 new
1212 call setline(1, ['one', 'tow', 'three'])
1213 diffthis
1214 call assert_equal(1, &diff)
1215 only!
1216endfunc
1217
1218func Test_diff_closeoff()
1219 " "closeoff" included by default: last diff win gets 'diff' reset'
1220 call CloseoffSetup()
1221 call assert_equal(0, &diff)
1222 enew!
1223
1224 " "closeoff" excluded: last diff win keeps 'diff' set'
1225 set diffopt-=closeoff
1226 call CloseoffSetup()
1227 call assert_equal(1, &diff)
1228 diffoff!
1229 enew!
1230endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001231
Bram Moolenaar4223d432021-02-10 13:18:17 +01001232func Test_diff_followwrap()
1233 new
1234 set diffopt+=followwrap
1235 set wrap
1236 diffthis
1237 call assert_equal(1, &wrap)
1238 diffoff
1239 set nowrap
1240 diffthis
1241 call assert_equal(0, &wrap)
1242 diffoff
1243 set diffopt&
1244 bwipe!
1245endfunc
1246
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001247func Test_diff_maintains_change_mark()
Sean Dewarccc16442021-12-29 16:44:48 +00001248 func DiffMaintainsChangeMark()
1249 enew!
1250 call setline(1, ['a', 'b', 'c', 'd'])
1251 diffthis
1252 new
1253 call setline(1, ['a', 'b', 'c', 'e'])
1254 " Set '[ and '] marks
1255 2,3yank
1256 call assert_equal([2, 3], [line("'["), line("']")])
1257 " Verify they aren't affected by the implicit diff
1258 diffthis
1259 call assert_equal([2, 3], [line("'["), line("']")])
1260 " Verify they aren't affected by an explicit diff
1261 diffupdate
1262 call assert_equal([2, 3], [line("'["), line("']")])
1263 bwipe!
1264 bwipe!
1265 endfunc
1266
1267 set diffopt-=internal
1268 call DiffMaintainsChangeMark()
1269 set diffopt+=internal
1270 call DiffMaintainsChangeMark()
Bram Moolenaard9b74a22022-01-16 15:00:08 +00001271
Sean Dewarccc16442021-12-29 16:44:48 +00001272 set diffopt&
Bram Moolenaard9b74a22022-01-16 15:00:08 +00001273 delfunc DiffMaintainsChangeMark
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001274endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001275
1276" Test for 'patchexpr'
1277func Test_patchexpr()
1278 let g:patch_args = []
1279 func TPatch()
1280 call add(g:patch_args, readfile(v:fname_in))
1281 call add(g:patch_args, readfile(v:fname_diff))
1282 call writefile(['output file'], v:fname_out)
1283 endfunc
1284 set patchexpr=TPatch()
1285
Bram Moolenaar59173412022-09-20 22:01:33 +01001286 call writefile(['input file'], 'Xinput', 'D')
1287 call writefile(['diff file'], 'Xdiff', 'D')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001288 %bwipe!
1289 edit Xinput
1290 diffpatch Xdiff
1291 call assert_equal('output file', getline(1))
1292 call assert_equal('Xinput.new', bufname())
1293 call assert_equal(2, winnr('$'))
1294 call assert_true(&diff)
1295
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00001296 " Using a script-local function
1297 func s:NewPatchExpr()
1298 endfunc
1299 set patchexpr=s:NewPatchExpr()
1300 call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1301 set patchexpr=<SID>NewPatchExpr()
1302 call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1303
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001304 set patchexpr&
1305 delfunc TPatch
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00001306 delfunc s:NewPatchExpr
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001307 %bwipe!
1308endfunc
1309
Bram Moolenaar511feec2020-06-18 19:15:27 +02001310func Test_diff_rnu()
1311 CheckScreendump
1312
1313 let content =<< trim END
1314 call setline(1, ['a', 'a', 'a', 'y', 'b', 'b', 'b', 'b', 'b'])
1315 vnew
1316 call setline(1, ['a', 'a', 'a', 'x', 'x', 'x', 'b', 'b', 'b', 'b', 'b'])
1317 windo diffthis
1318 setlocal number rnu foldcolumn=0
1319 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001320 call writefile(content, 'Xtest_diff_rnu', 'D')
Bram Moolenaar511feec2020-06-18 19:15:27 +02001321 let buf = RunVimInTerminal('-S Xtest_diff_rnu', {})
1322
1323 call VerifyScreenDump(buf, 'Test_diff_rnu_01', {})
1324
1325 call term_sendkeys(buf, "j")
1326 call VerifyScreenDump(buf, 'Test_diff_rnu_02', {})
1327 call term_sendkeys(buf, "j")
1328 call VerifyScreenDump(buf, 'Test_diff_rnu_03', {})
1329
1330 " clean up
1331 call StopVimInTerminal(buf)
Bram Moolenaar511feec2020-06-18 19:15:27 +02001332endfunc
1333
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001334func Test_diff_multilineconceal()
1335 new
1336 diffthis
1337
1338 new
1339 call matchadd('Conceal', 'a\nb', 9, -1, {'conceal': 'Y'})
1340 set cole=2 cocu=n
1341 call setline(1, ["a", "b"])
1342 diffthis
1343 redraw
1344endfunc
1345
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001346func Test_diff_and_scroll()
1347 " this was causing an ml_get error
1348 set ls=2
Bram Moolenaar94722c52023-01-28 19:19:03 +00001349 for i in range(winheight(0) * 2)
1350 call setline(i, i < winheight(0) - 10 ? i : i + 10)
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001351 endfor
1352 vnew
Bram Moolenaar94722c52023-01-28 19:19:03 +00001353 for i in range(winheight(0)*2 + 10)
1354 call setline(i, i < winheight(0) - 10 ? 0 : i)
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001355 endfor
1356 diffthis
1357 wincmd p
1358 diffthis
1359 execute 'normal ' . winheight(0) . "\<C-d>"
1360
1361 bwipe!
1362 bwipe!
1363 set ls&
1364endfunc
1365
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001366func Test_diff_filler_cursorcolumn()
1367 CheckScreendump
1368
1369 let content =<< trim END
1370 call setline(1, ['aa', 'bb', 'cc'])
1371 vnew
1372 call setline(1, ['aa', 'cc'])
1373 windo diffthis
1374 wincmd p
1375 setlocal cursorcolumn foldcolumn=0
1376 norm! gg0
1377 redraw!
1378 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001379 call writefile(content, 'Xtest_diff_cuc', 'D')
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001380 let buf = RunVimInTerminal('-S Xtest_diff_cuc', {})
1381
1382 call VerifyScreenDump(buf, 'Test_diff_cuc_01', {})
1383
1384 call term_sendkeys(buf, "l")
1385 call term_sendkeys(buf, "\<C-l>")
1386 call VerifyScreenDump(buf, 'Test_diff_cuc_02', {})
1387 call term_sendkeys(buf, "0j")
1388 call term_sendkeys(buf, "\<C-l>")
1389 call VerifyScreenDump(buf, 'Test_diff_cuc_03', {})
1390 call term_sendkeys(buf, "l")
1391 call term_sendkeys(buf, "\<C-l>")
1392 call VerifyScreenDump(buf, 'Test_diff_cuc_04', {})
1393
1394 " clean up
1395 call StopVimInTerminal(buf)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001396endfunc
1397
Yegappan Lakshmanan30443242021-06-10 21:52:15 +02001398" Test for adding/removing lines inside diff chunks, between diff chunks
1399" and before diff chunks
1400func Test_diff_modify_chunks()
1401 enew!
1402 let w2_id = win_getid()
1403 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1404 new
1405 let w1_id = win_getid()
1406 call setline(1, ['a', '2', '3', 'd', 'e', 'f', '7', '8', 'i'])
1407 windo diffthis
1408
1409 " remove a line between two diff chunks and create a new diff chunk
1410 call win_gotoid(w2_id)
1411 5d
1412 call win_gotoid(w1_id)
1413 call diff_hlID(5, 1)->synIDattr('name')->assert_equal('DiffAdd')
1414
1415 " add a line between two diff chunks
1416 call win_gotoid(w2_id)
1417 normal! 4Goe
1418 call win_gotoid(w1_id)
1419 call diff_hlID(4, 1)->synIDattr('name')->assert_equal('')
1420 call diff_hlID(5, 1)->synIDattr('name')->assert_equal('')
1421
1422 " remove all the lines in a diff chunk.
1423 call win_gotoid(w2_id)
1424 7,8d
1425 call win_gotoid(w1_id)
1426 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1427 call assert_equal(['', 'DiffText', 'DiffText', '', '', '', 'DiffAdd',
1428 \ 'DiffAdd', ''], hl)
1429
1430 " remove lines from one diff chunk to just before the next diff chunk
1431 call win_gotoid(w2_id)
1432 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1433 2,6d
1434 call win_gotoid(w1_id)
1435 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1436 call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', 'DiffAdd',
1437 \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1438
1439 " remove lines just before the top of a diff chunk
1440 call win_gotoid(w2_id)
1441 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1442 5,6d
1443 call win_gotoid(w1_id)
1444 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1445 call assert_equal(['', 'DiffText', 'DiffText', '', 'DiffText', 'DiffText',
1446 \ 'DiffAdd', 'DiffAdd', ''], hl)
1447
1448 " remove line after the end of a diff chunk
1449 call win_gotoid(w2_id)
1450 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1451 4d
1452 call win_gotoid(w1_id)
1453 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1454 call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', '', '', 'DiffText',
1455 \ 'DiffText', ''], hl)
1456
1457 " remove lines starting from the end of one diff chunk and ending inside
1458 " another diff chunk
1459 call win_gotoid(w2_id)
1460 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1461 4,7d
1462 call win_gotoid(w1_id)
1463 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1464 call assert_equal(['', 'DiffText', 'DiffText', 'DiffText', 'DiffAdd',
1465 \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1466
1467 " removing the only remaining diff chunk should make the files equal
1468 call win_gotoid(w2_id)
1469 call setline(1, ['a', '2', '3', 'x', 'd', 'e', 'f', 'x', '7', '8', 'i'])
1470 8d
1471 let hl = range(1, 10)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1472 call assert_equal(['', '', '', 'DiffAdd', '', '', '', '', '', ''], hl)
1473 call win_gotoid(w2_id)
1474 4d
1475 call win_gotoid(w1_id)
1476 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1477 call assert_equal(['', '', '', '', '', '', '', '', ''], hl)
1478
1479 %bw!
1480endfunc
glacambread5c1782021-05-24 14:20:53 +02001481
Bram Moolenaar06f60952021-12-28 18:30:05 +00001482func Test_diff_binary()
1483 CheckScreendump
1484
1485 let content =<< trim END
1486 call setline(1, ['a', 'b', "c\n", 'd', 'e', 'f', 'g'])
1487 vnew
1488 call setline(1, ['A', 'b', 'c', 'd', 'E', 'f', 'g'])
1489 windo diffthis
1490 wincmd p
1491 norm! gg0
1492 redraw!
1493 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001494 call writefile(content, 'Xtest_diff_bin', 'D')
Bram Moolenaar06f60952021-12-28 18:30:05 +00001495 let buf = RunVimInTerminal('-S Xtest_diff_bin', {})
1496
1497 " Test using internal diff
1498 call VerifyScreenDump(buf, 'Test_diff_bin_01', {})
1499
1500 " Test using internal diff and case folding
1501 call term_sendkeys(buf, ":set diffopt+=icase\<cr>")
1502 call term_sendkeys(buf, "\<C-l>")
1503 call VerifyScreenDump(buf, 'Test_diff_bin_02', {})
1504 " Test using external diff
1505 call term_sendkeys(buf, ":set diffopt=filler\<cr>")
1506 call term_sendkeys(buf, "\<C-l>")
1507 call VerifyScreenDump(buf, 'Test_diff_bin_03', {})
1508 " Test using external diff and case folding
1509 call term_sendkeys(buf, ":set diffopt=filler,icase\<cr>")
1510 call term_sendkeys(buf, "\<C-l>")
1511 call VerifyScreenDump(buf, 'Test_diff_bin_04', {})
1512
1513 " clean up
1514 call StopVimInTerminal(buf)
Bram Moolenaar06f60952021-12-28 18:30:05 +00001515 set diffopt&vim
1516endfunc
1517
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001518" Test for using the 'zi' command to invert 'foldenable' in diff windows (test
1519" for the issue fixed by patch 6.2.317)
1520func Test_diff_foldinvert()
1521 %bw!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001522 edit Xdoffile1
1523 new Xdoffile2
1524 new Xdoffile3
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001525 windo diffthis
1526 " open a non-diff window
1527 botright new
1528 1wincmd w
1529 call assert_true(getwinvar(1, '&foldenable'))
1530 call assert_true(getwinvar(2, '&foldenable'))
1531 call assert_true(getwinvar(3, '&foldenable'))
1532 normal zi
1533 call assert_false(getwinvar(1, '&foldenable'))
1534 call assert_false(getwinvar(2, '&foldenable'))
1535 call assert_false(getwinvar(3, '&foldenable'))
1536 normal zi
1537 call assert_true(getwinvar(1, '&foldenable'))
1538 call assert_true(getwinvar(2, '&foldenable'))
1539 call assert_true(getwinvar(3, '&foldenable'))
1540
1541 " If the current window has 'noscrollbind', then 'zi' should not change
1542 " 'foldenable' in other windows.
1543 1wincmd w
1544 set noscrollbind
1545 normal zi
1546 call assert_false(getwinvar(1, '&foldenable'))
1547 call assert_true(getwinvar(2, '&foldenable'))
1548 call assert_true(getwinvar(3, '&foldenable'))
1549
1550 " 'zi' should not change the 'foldenable' for windows with 'noscrollbind'
1551 1wincmd w
1552 set scrollbind
1553 normal zi
1554 call setwinvar(2, '&scrollbind', v:false)
1555 normal zi
1556 call assert_false(getwinvar(1, '&foldenable'))
1557 call assert_true(getwinvar(2, '&foldenable'))
1558 call assert_false(getwinvar(3, '&foldenable'))
1559
1560 %bw!
1561 set scrollbind&
1562endfunc
1563
Bram Moolenaara315ce12022-06-24 12:38:57 +01001564" This was scrolling for 'cursorbind' but 'scrollbind' is more important
1565func Test_diff_scroll()
1566 CheckScreendump
1567
1568 let left =<< trim END
1569 line 1
1570 line 2
1571 line 3
1572 line 4
1573
1574 // Common block
1575 // one
1576 // containing
1577 // four lines
1578
1579 // Common block
1580 // two
1581 // containing
1582 // four lines
1583 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001584 call writefile(left, 'Xleft', 'D')
Bram Moolenaara315ce12022-06-24 12:38:57 +01001585 let right =<< trim END
1586 line 1
1587 line 2
1588 line 3
1589 line 4
1590
1591 Lorem
1592 ipsum
1593 dolor
1594 sit
1595 amet,
1596 consectetur
1597 adipiscing
1598 elit.
1599 Etiam
1600 luctus
1601 lectus
1602 sodales,
1603 dictum
1604
1605 // Common block
1606 // one
1607 // containing
1608 // four lines
1609
1610 Vestibulum
1611 tincidunt
1612 aliquet
1613 nulla.
1614
1615 // Common block
1616 // two
1617 // containing
1618 // four lines
1619 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001620 call writefile(right, 'Xright', 'D')
Bram Moolenaara315ce12022-06-24 12:38:57 +01001621 let buf = RunVimInTerminal('-d Xleft Xright', {'rows': 12})
1622 call term_sendkeys(buf, "\<C-W>\<C-W>jjjj")
1623 call VerifyScreenDump(buf, 'Test_diff_scroll_1', {})
1624 call term_sendkeys(buf, "j")
1625 call VerifyScreenDump(buf, 'Test_diff_scroll_2', {})
1626
1627 call StopVimInTerminal(buf)
Bram Moolenaara315ce12022-06-24 12:38:57 +01001628endfunc
1629
Bram Moolenaar38d867f2023-04-01 19:54:40 +01001630" This was scrolling too many lines.
1631func Test_diff_scroll_wrap_on()
1632 20new
1633 40vsplit
1634 call setline(1, map(range(1, 9), 'repeat(v:val, 200)'))
1635 setlocal number diff so=0
1636 redraw
1637 normal! jj
1638 call assert_equal(1, winsaveview().topline)
1639 normal! j
1640 call assert_equal(2, winsaveview().topline)
zeertzjq05834912023-10-04 20:12:37 +02001641
1642 bwipe!
1643 bwipe!
1644endfunc
1645
1646func Test_diff_scroll_many_filler()
1647 20new
1648 vnew
1649 call setline(1, ['^^^', '^^^', '$$$', '$$$'])
1650 diffthis
1651 setlocal scrolloff=0
1652 wincmd p
1653 call setline(1, ['^^^', '^^^'] + repeat(['###'], 41) + ['$$$', '$$$'])
1654 diffthis
1655 setlocal scrolloff=0
1656 wincmd p
1657 redraw
1658
1659 " Note: need a redraw after each scroll, otherwise the test always passes.
1660 normal! G
1661 redraw
1662 call assert_equal(3, winsaveview().topline)
1663 call assert_equal(18, winsaveview().topfill)
1664 exe "normal! \<C-B>"
1665 redraw
1666 call assert_equal(3, winsaveview().topline)
1667 call assert_equal(19, winsaveview().topfill)
1668 exe "normal! \<C-B>"
1669 redraw
1670 call assert_equal(2, winsaveview().topline)
1671 call assert_equal(0, winsaveview().topfill)
1672 exe "normal! \<C-B>"
1673 redraw
1674 call assert_equal(1, winsaveview().topline)
1675 call assert_equal(0, winsaveview().topfill)
1676
Bram Moolenaar38d867f2023-04-01 19:54:40 +01001677 bwipe!
1678 bwipe!
1679endfunc
1680
Bram Moolenaarcd38bb42022-06-26 14:04:07 +01001681" This was trying to update diffs for a buffer being closed
1682func Test_diff_only()
1683 silent! lfile
1684 set diff
1685 lopen
1686 norm o
1687 silent! norm o
1688
1689 set nodiff
1690 %bwipe!
1691endfunc
1692
Bram Moolenaarc101abf2022-06-26 16:53:34 +01001693" This was causing invalid diff block values
1694" FIXME: somehow this causes a valgrind error when run directly but not when
1695" run as a test.
1696func Test_diff_manipulations()
1697 set diff
1698 split 0
1699 sil! norm R doobdeuR doobdeuR doobdeu
1700
1701 set nodiff
1702 %bwipe!
1703endfunc
1704
Bram Moolenaar4e677b92022-07-28 18:44:27 +01001705" This was causing the line number in the diff block to go below one.
1706" FIXME: somehow this causes a valgrind error when run directly but not when
1707" run as a test.
1708func Test_diff_put_and_undo()
1709 set diff
1710 next 0
1711 split 00
1712 sil! norm o0gguudpo0ggJuudp
1713
1714 bwipe!
1715 bwipe!
1716 set nodiff
1717endfunc
1718
Yegappan Lakshmananfa378352024-02-01 22:05:27 +01001719" Test for the diff() function
1720def Test_diff_func()
1721 # string is added/removed/modified at the beginning
1722 assert_equal("@@ -0,0 +1 @@\n+abc\n",
1723 diff(['def'], ['abc', 'def'], {output: 'unified'}))
1724 assert_equal([{from_idx: 0, from_count: 0, to_idx: 0, to_count: 1}],
1725 diff(['def'], ['abc', 'def'], {output: 'indices'}))
1726 assert_equal("@@ -1 +0,0 @@\n-abc\n",
1727 diff(['abc', 'def'], ['def'], {output: 'unified'}))
1728 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 0}],
1729 diff(['abc', 'def'], ['def'], {output: 'indices'}))
1730 assert_equal("@@ -1 +1 @@\n-abc\n+abx\n",
1731 diff(['abc', 'def'], ['abx', 'def'], {output: 'unified'}))
1732 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1733 diff(['abc', 'def'], ['abx', 'def'], {output: 'indices'}))
1734
1735 # string is added/removed/modified at the end
1736 assert_equal("@@ -1,0 +2 @@\n+def\n",
1737 diff(['abc'], ['abc', 'def'], {output: 'unified'}))
1738 assert_equal([{from_idx: 1, from_count: 0, to_idx: 1, to_count: 1}],
1739 diff(['abc'], ['abc', 'def'], {output: 'indices'}))
1740 assert_equal("@@ -2 +1,0 @@\n-def\n",
1741 diff(['abc', 'def'], ['abc'], {output: 'unified'}))
1742 assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 0}],
1743 diff(['abc', 'def'], ['abc'], {output: 'indices'}))
1744 assert_equal("@@ -2 +2 @@\n-def\n+xef\n",
1745 diff(['abc', 'def'], ['abc', 'xef'], {output: 'unified'}))
1746 assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 1}],
1747 diff(['abc', 'def'], ['abc', 'xef'], {output: 'indices'}))
1748
1749 # string is added/removed/modified in the middle
1750 assert_equal("@@ -2,0 +3 @@\n+xxx\n",
1751 diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'unified'}))
1752 assert_equal([{from_idx: 2, from_count: 0, to_idx: 2, to_count: 1}],
1753 diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'indices'}))
1754 assert_equal("@@ -3 +2,0 @@\n-333\n",
1755 diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'unified'}))
1756 assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 0}],
1757 diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'indices'}))
1758 assert_equal("@@ -3 +3 @@\n-333\n+xxx\n",
1759 diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'unified'}))
1760 assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 1}],
1761 diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'indices'}))
1762
1763 # new strings are added to an empty List
1764 assert_equal("@@ -0,0 +1,2 @@\n+abc\n+def\n",
1765 diff([], ['abc', 'def'], {output: 'unified'}))
1766 assert_equal([{from_idx: 0, from_count: 0, to_idx: 0, to_count: 2}],
1767 diff([], ['abc', 'def'], {output: 'indices'}))
1768
1769 # all the strings are removed from a List
1770 assert_equal("@@ -1,2 +0,0 @@\n-abc\n-def\n",
1771 diff(['abc', 'def'], [], {output: 'unified'}))
1772 assert_equal([{from_idx: 0, from_count: 2, to_idx: 0, to_count: 0}],
1773 diff(['abc', 'def'], [], {output: 'indices'}))
1774
1775 # First character is added/removed/different
1776 assert_equal("@@ -1 +1 @@\n-abc\n+bc\n",
1777 diff(['abc'], ['bc'], {output: 'unified'}))
1778 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1779 diff(['abc'], ['bc'], {output: 'indices'}))
1780 assert_equal("@@ -1 +1 @@\n-bc\n+abc\n",
1781 diff(['bc'], ['abc'], {output: 'unified'}))
1782 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1783 diff(['bc'], ['abc'], {output: 'indices'}))
1784 assert_equal("@@ -1 +1 @@\n-abc\n+xbc\n",
1785 diff(['abc'], ['xbc'], {output: 'unified'}))
1786 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1787 diff(['abc'], ['xbc'], {output: 'indices'}))
1788
1789 # Last character is added/removed/different
1790 assert_equal("@@ -1 +1 @@\n-abc\n+abcd\n",
1791 diff(['abc'], ['abcd'], {output: 'unified'}))
1792 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1793 diff(['abc'], ['abcd'], {output: 'indices'}))
1794 assert_equal("@@ -1 +1 @@\n-abcd\n+abc\n",
1795 diff(['abcd'], ['abc'], {output: 'unified'}))
1796 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1797 diff(['abcd'], ['abc'], {output: 'indices'}))
1798 assert_equal("@@ -1 +1 @@\n-abc\n+abx\n",
1799 diff(['abc'], ['abx'], {output: 'unified'}))
1800 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1801 diff(['abc'], ['abx'], {output: 'indices'}))
1802
1803 # partial string modification at the start and at the end.
1804 var fromlist =<< trim END
1805 one two
1806 three four
1807 five six
1808 END
1809 var tolist =<< trim END
1810 one
1811 six
1812 END
1813 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'}))
1814 assert_equal([{from_idx: 0, from_count: 3, to_idx: 0, to_count: 2}],
1815 diff(fromlist, tolist, {output: 'indices'}))
1816
1817 # non-contiguous modifications
1818 fromlist =<< trim END
1819 one two
1820 three four
1821 five abc six
1822 END
1823 tolist =<< trim END
1824 one abc two
1825 three four
1826 five six
1827 END
1828 assert_equal("@@ -1 +1 @@\n-one two\n+one abc two\n@@ -3 +3 @@\n-five abc six\n+five six\n",
1829 diff(fromlist, tolist, {output: 'unified'}))
1830 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1},
1831 {from_idx: 2, from_count: 1, to_idx: 2, to_count: 1}],
1832 diff(fromlist, tolist, {output: 'indices'}))
1833
1834 # add/remove blank lines
1835 assert_equal("@@ -2,2 +1,0 @@\n-\n-\n",
1836 diff(['one', '', '', 'two'], ['one', 'two'], {output: 'unified'}))
1837 assert_equal([{from_idx: 1, from_count: 2, to_idx: 1, to_count: 0}],
1838 diff(['one', '', '', 'two'], ['one', 'two'], {output: 'indices'}))
1839 assert_equal("@@ -1,0 +2,2 @@\n+\n+\n",
1840 diff(['one', 'two'], ['one', '', '', 'two'], {output: 'unified'}))
1841 assert_equal([{'from_idx': 1, 'from_count': 0, 'to_idx': 1, 'to_count': 2}],
1842 diff(['one', 'two'], ['one', '', '', 'two'], {output: 'indices'}))
1843
1844 # diff ignoring case
1845 assert_equal('', diff(['abc', 'def'], ['ABC', 'DEF'], {icase: true, output: 'unified'}))
1846 assert_equal([], diff(['abc', 'def'], ['ABC', 'DEF'], {icase: true, output: 'indices'}))
1847
1848 # diff ignoring all whitespace changes except leading whitespace changes
1849 assert_equal('', diff(['abc def'], ['abc def '], {iwhite: true}))
1850 assert_equal("@@ -1 +1 @@\n- abc\n+ xxx\n", diff([' abc'], [' xxx'], {iwhite: v:true}))
1851 assert_equal("@@ -1 +1 @@\n- abc\n+ xxx\n", diff([' abc'], [' xxx'], {iwhite: v:false}))
1852 assert_equal("@@ -1 +1 @@\n-abc \n+xxx \n", diff(['abc '], ['xxx '], {iwhite: v:true}))
1853 assert_equal("@@ -1 +1 @@\n-abc \n+xxx \n", diff(['abc '], ['xxx '], {iwhite: v:false}))
1854
1855 # diff ignoring all whitespace changes
1856 assert_equal('', diff(['abc def'], [' abc def '], {iwhiteall: true}))
1857 assert_equal("@@ -1 +1 @@\n- abc \n+ xxx \n", diff([' abc '], [' xxx '], {iwhiteall: v:true}))
1858 assert_equal("@@ -1 +1 @@\n- abc \n+ xxx \n", diff([' abc '], [' xxx '], {iwhiteall: v:false}))
1859
1860 # diff ignoring trailing whitespace changes
1861 assert_equal('', diff(['abc'], ['abc '], {iwhiteeol: true}))
1862
1863 # diff ignoring blank lines
1864 assert_equal('', diff(['one', '', '', 'two'], ['one', 'two'], {iblank: true}))
1865 assert_equal('', diff(['one', 'two'], ['one', '', '', 'two'], {iblank: true}))
1866
1867 # same string
1868 assert_equal('', diff(['abc', 'def', 'ghi'], ['abc', 'def', 'ghi']))
1869 assert_equal('', diff([''], ['']))
1870 assert_equal('', diff([], []))
1871
1872 # different xdiff algorithms
1873 for algo in ['myers', 'minimal', 'patience', 'histogram']
1874 assert_equal("@@ -1 +1 @@\n- abc \n+ xxx \n",
1875 diff([' abc '], [' xxx '], {algorithm: algo, output: 'unified'}))
1876 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1877 diff([' abc '], [' xxx '], {algorithm: algo, output: 'indices'}))
1878 endfor
1879 assert_equal("@@ -1 +1 @@\n- abc \n+ xxx \n",
1880 diff([' abc '], [' xxx '], {indent-heuristic: true, output: 'unified'}))
1881 assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
1882 diff([' abc '], [' xxx '], {indent-heuristic: true, output: 'indices'}))
1883
1884 # identical strings
1885 assert_equal('', diff(['111', '222'], ['111', '222'], {output: 'unified'}))
1886 assert_equal([], diff(['111', '222'], ['111', '222'], {output: 'indices'}))
1887 assert_equal('', diff([], [], {output: 'unified'}))
1888 assert_equal([], diff([], [], {output: 'indices'}))
1889
1890 # Error cases
1891 assert_fails('call diff({}, ["a"])', 'E1211:')
1892 assert_fails('call diff(["a"], {})', 'E1211:')
1893 assert_fails('call diff(["a"], ["a"], [])', 'E1206:')
1894 assert_fails('call diff(["a"], ["a"], {output: "xyz"})', 'E106: Unsupported diff output format: xyz')
1895enddef
Bram Moolenaara315ce12022-06-24 12:38:57 +01001896
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001897" vim: shiftwidth=2 sts=2 expandtab