blob: e05d91601352ad39a1a74b4ade29813f47a91586 [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()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100874 let g:test_is_flaky = 1
Bram Moolenaar3c8ee622019-08-03 22:55:50 +0200875 CheckScreendump
876 CheckFeature menu
877
Bram Moolenaar485b6272021-05-18 19:19:03 +0200878 let lines =<< trim END
879 func UnifiedDiffExpr()
880 " Prepend some text to check diff type detection
881 call writefile(['warning', ' message'], v:fname_out)
glacambread5c1782021-05-24 14:20:53 +0200882 silent exe '!diff -U0 ' .. v:fname_in .. ' ' .. v:fname_new .. '>>' .. v:fname_out
Bram Moolenaar485b6272021-05-18 19:19:03 +0200883 endfunc
884 func SetupUnified()
885 set diffexpr=UnifiedDiffExpr()
glacambread5c1782021-05-24 14:20:53 +0200886 diffupdate
Bram Moolenaar485b6272021-05-18 19:19:03 +0200887 endfunc
888 func StopUnified()
889 set diffexpr=
890 endfunc
891 END
Bram Moolenaar59173412022-09-20 22:01:33 +0100892 call writefile(lines, 'XdiffSetup', 'D')
Bram Moolenaar485b6272021-05-18 19:19:03 +0200893
Bram Moolenaare828b762018-09-10 17:51:58 +0200894 " clean up already existing swap files, just in case
Bram Moolenaar61abe7d2022-08-30 21:46:08 +0100895 call delete('.Xdifile1.swp')
896 call delete('.Xdifile2.swp')
Bram Moolenaare828b762018-09-10 17:51:58 +0200897
898 " Test 1: Add a line in beginning of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200899 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 +0100900 let buf = RunVimInTerminal('-d -S XdiffSetup Xdifile1 Xdifile2', {})
Bram Moolenaar8ee4c012019-03-29 18:08:18 +0100901 " Set autoread mode, so that Vim won't complain once we re-write the test
Bram Moolenaare828b762018-09-10 17:51:58 +0200902 " files
Bram Moolenaar785fc652018-09-15 19:17:38 +0200903 call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w")
Bram Moolenaare828b762018-09-10 17:51:58 +0200904
905 call VerifyBoth(buf, 'Test_diff_01', '')
906
907 " Test 2: Add a line in beginning of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200908 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 +0200909 call VerifyBoth(buf, 'Test_diff_02', '')
910
911 " Test 3: Add a line at the end of file 2
Bram Moolenaar785fc652018-09-15 19:17:38 +0200912 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 +0200913 call VerifyBoth(buf, 'Test_diff_03', '')
914
915 " Test 4: Add a line at the end of file 1
Bram Moolenaar785fc652018-09-15 19:17:38 +0200916 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 +0200917 call VerifyBoth(buf, 'Test_diff_04', '')
918
919 " 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 +0200920 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 +0200921 call VerifyBoth(buf, 'Test_diff_05', '')
922
923 " 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 +0200924 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 +0200925 call VerifyBoth(buf, 'Test_diff_06', '')
926
Bram Moolenaarb9ddda62019-02-19 23:00:50 +0100927 " Variants on test 6 with different context settings
928 call term_sendkeys(buf, ":set diffopt+=context:2\<cr>")
929 call VerifyScreenDump(buf, 'Test_diff_06.2', {})
930 call term_sendkeys(buf, ":set diffopt-=context:2\<cr>")
931 call term_sendkeys(buf, ":set diffopt+=context:1\<cr>")
932 call VerifyScreenDump(buf, 'Test_diff_06.1', {})
933 call term_sendkeys(buf, ":set diffopt-=context:1\<cr>")
934 call term_sendkeys(buf, ":set diffopt+=context:0\<cr>")
935 call VerifyScreenDump(buf, 'Test_diff_06.0', {})
936 call term_sendkeys(buf, ":set diffopt-=context:0\<cr>")
937
Bram Moolenaare828b762018-09-10 17:51:58 +0200938 " Test 7 - 9: Test normal/patience/histogram diff algorithm
Bram Moolenaar785fc652018-09-15 19:17:38 +0200939 call WriteDiffFiles(buf, ['#include <stdio.h>', '', '// Frobs foo heartily', 'int frobnitz(int foo)', '{',
Bram Moolenaare828b762018-09-10 17:51:58 +0200940 \ ' int i;', ' for(i = 0; i < 10; i++)', ' {', ' printf("Your answer is: ");',
941 \ ' printf("%d\n", foo);', ' }', '}', '', 'int fact(int n)', '{', ' if(n > 1)', ' {',
942 \ ' return fact(n-1) * n;', ' }', ' return 1;', '}', '', 'int main(int argc, char **argv)',
943 \ '{', ' frobnitz(fact(10));', '}'],
944 \ ['#include <stdio.h>', '', 'int fib(int n)', '{', ' if(n > 2)', ' {',
945 \ ' return fib(n-1) + fib(n-2);', ' }', ' return 1;', '}', '', '// Frobs foo heartily',
946 \ 'int frobnitz(int foo)', '{', ' int i;', ' for(i = 0; i < 10; i++)', ' {',
947 \ ' printf("%d\n", foo);', ' }', '}', '',
948 \ 'int main(int argc, char **argv)', '{', ' frobnitz(fib(10));', '}'])
949 call term_sendkeys(buf, ":diffupdate!\<cr>")
950 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
951 call VerifyScreenDump(buf, 'Test_diff_07', {})
952
953 call term_sendkeys(buf, ":set diffopt+=algorithm:patience\<cr>")
954 call VerifyScreenDump(buf, 'Test_diff_08', {})
955
956 call term_sendkeys(buf, ":set diffopt+=algorithm:histogram\<cr>")
957 call VerifyScreenDump(buf, 'Test_diff_09', {})
958
959 " Test 10-11: normal/indent-heuristic
960 call term_sendkeys(buf, ":set diffopt&vim\<cr>")
Bram Moolenaar785fc652018-09-15 19:17:38 +0200961 call WriteDiffFiles(buf, ['', ' def finalize(values)', '', ' values.each do |v|', ' v.finalize', ' end'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200962 \ ['', ' def finalize(values)', '', ' values.each do |v|', ' v.prepare', ' end', '',
963 \ ' values.each do |v|', ' v.finalize', ' end'])
964 call term_sendkeys(buf, ":diffupdate!\<cr>")
965 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
966 call VerifyScreenDump(buf, 'Test_diff_10', {})
967
Bram Moolenaarb6fc7282018-12-04 22:24:16 +0100968 " Leave trailing : at commandline!
969 call term_sendkeys(buf, ":set diffopt+=indent-heuristic\<cr>:\<cr>")
970 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'one')
971 " shouldn't matter, if indent-algorithm comes before or after the algorithm
972 call term_sendkeys(buf, ":set diffopt&\<cr>")
973 call term_sendkeys(buf, ":set diffopt+=indent-heuristic,algorithm:patience\<cr>:\<cr>")
974 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'two')
975 call term_sendkeys(buf, ":set diffopt&\<cr>")
976 call term_sendkeys(buf, ":set diffopt+=algorithm:patience,indent-heuristic\<cr>:\<cr>")
977 call VerifyScreenDump(buf, 'Test_diff_11', {}, 'three')
Bram Moolenaare828b762018-09-10 17:51:58 +0200978
979 " Test 12: diff the same file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200980 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 +0200981 call VerifyBoth(buf, 'Test_diff_12', '')
982
983 " Test 13: diff an empty file
Bram Moolenaar785fc652018-09-15 19:17:38 +0200984 call WriteDiffFiles(buf, [], [])
Bram Moolenaare828b762018-09-10 17:51:58 +0200985 call VerifyBoth(buf, 'Test_diff_13', '')
986
987 " Test 14: test diffopt+=icase
Bram Moolenaar785fc652018-09-15 19:17:38 +0200988 call WriteDiffFiles(buf, ['a', 'b', 'cd'], ['A', 'b', 'cDe'])
Bram Moolenaare828b762018-09-10 17:51:58 +0200989 call VerifyBoth(buf, 'Test_diff_14', " diffopt+=filler diffopt+=icase")
990
991 " Test 15-16: test diffopt+=iwhite
Bram Moolenaar785fc652018-09-15 19:17:38 +0200992 call WriteDiffFiles(buf, ['int main()', '{', ' printf("Hello, World!");', ' return 0;', '}'],
Bram Moolenaare828b762018-09-10 17:51:58 +0200993 \ ['int main()', '{', ' if (0)', ' {', ' printf("Hello, World!");', ' return 0;', ' }', '}'])
994 call term_sendkeys(buf, ":diffupdate!\<cr>")
995 call term_sendkeys(buf, ":set diffopt&vim diffopt+=filler diffopt+=iwhite\<cr>")
996 call VerifyScreenDump(buf, 'Test_diff_15', {})
997 call term_sendkeys(buf, ":set diffopt+=internal\<cr>")
998 call VerifyScreenDump(buf, 'Test_diff_16', {})
999
Bram Moolenaar785fc652018-09-15 19:17:38 +02001000 " Test 17: test diffopt+=iblank
1001 call WriteDiffFiles(buf, ['a', ' ', 'cd', 'ef', 'xxx'], ['a', 'cd', '', 'ef', 'yyy'])
1002 call VerifyInternal(buf, 'Test_diff_17', " diffopt+=iblank")
1003
1004 " Test 18: test diffopt+=iblank,iwhite / iwhiteall / iwhiteeol
1005 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhite")
1006 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteall")
1007 call VerifyInternal(buf, 'Test_diff_18', " diffopt+=iblank,iwhiteeol")
1008
1009 " Test 19: test diffopt+=iwhiteeol
1010 call WriteDiffFiles(buf, ['a ', 'x', 'cd', 'ef', 'xx xx', 'foo', 'bar'], ['a', 'x', 'c d', ' ef', 'xx xx', 'foo', '', 'bar'])
1011 call VerifyInternal(buf, 'Test_diff_19', " diffopt+=iwhiteeol")
1012
1013 " Test 19: test diffopt+=iwhiteall
1014 call VerifyInternal(buf, 'Test_diff_20', " diffopt+=iwhiteall")
1015
Bram Moolenaare828b762018-09-10 17:51:58 +02001016 " clean up
1017 call StopVimInTerminal(buf)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001018 call delete('Xdifile1')
1019 call delete('Xdifile2')
Bram Moolenaare828b762018-09-10 17:51:58 +02001020endfunc
1021
Bram Moolenaar04626c22021-09-01 16:02:07 +02001022func Test_diff_with_scroll_and_change()
1023 CheckScreendump
1024
1025 let lines =<< trim END
1026 call setline(1, range(1, 15))
1027 vnew
1028 call setline(1, range(9, 15))
1029 windo diffthis
1030 wincmd h
1031 exe "normal Gl5\<C-E>"
1032 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001033 call writefile(lines, 'Xtest_scroll_change', 'D')
Bram Moolenaar04626c22021-09-01 16:02:07 +02001034 let buf = RunVimInTerminal('-S Xtest_scroll_change', {})
1035
1036 call VerifyScreenDump(buf, 'Test_diff_scroll_change_01', {})
1037
1038 call term_sendkeys(buf, "ax\<Esc>")
1039 call VerifyScreenDump(buf, 'Test_diff_scroll_change_02', {})
1040
Bram Moolenaar841c2252021-10-22 20:56:55 +01001041 call term_sendkeys(buf, "\<C-W>lay\<Esc>")
1042 call VerifyScreenDump(buf, 'Test_diff_scroll_change_03', {})
1043
Bram Moolenaar04626c22021-09-01 16:02:07 +02001044 " clean up
1045 call StopVimInTerminal(buf)
Bram Moolenaar04626c22021-09-01 16:02:07 +02001046endfunc
1047
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001048func Test_diff_with_cursorline()
Bram Moolenaar3c8ee622019-08-03 22:55:50 +02001049 CheckScreendump
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001050
1051 call writefile([
1052 \ 'hi CursorLine ctermbg=red ctermfg=white',
1053 \ 'set cursorline',
1054 \ 'call setline(1, ["foo","foo","foo","bar"])',
1055 \ 'vnew',
1056 \ 'call setline(1, ["bee","foo","foo","baz"])',
1057 \ 'windo diffthis',
1058 \ '2wincmd w',
Bram Moolenaar59173412022-09-20 22:01:33 +01001059 \ ], 'Xtest_diff_cursorline', 'D')
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001060 let buf = RunVimInTerminal('-S Xtest_diff_cursorline', {})
1061
1062 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_01', {})
1063 call term_sendkeys(buf, "j")
1064 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_02', {})
1065 call term_sendkeys(buf, "j")
1066 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_03', {})
1067
1068 " clean up
1069 call StopVimInTerminal(buf)
Bram Moolenaar4a5abbd2018-10-02 18:26:10 +02001070endfunc
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001071
Bram Moolenaar127969c2022-03-06 19:54:13 +00001072func Test_diff_with_cursorline_number()
1073 CheckScreendump
1074
1075 let lines =<< trim END
1076 hi CursorLine ctermbg=red ctermfg=white
1077 hi CursorLineNr ctermbg=white ctermfg=black cterm=underline
1078 set cursorline number
1079 call setline(1, ["baz", "foo", "foo", "bar"])
1080 2
1081 vnew
1082 call setline(1, ["foo", "foo", "bar"])
1083 windo diffthis
1084 1wincmd w
1085 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001086 call writefile(lines, 'Xtest_diff_cursorline_number', 'D')
Bram Moolenaar127969c2022-03-06 19:54:13 +00001087 let buf = RunVimInTerminal('-S Xtest_diff_cursorline_number', {})
1088
1089 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_01', {})
1090 call term_sendkeys(buf, ":set cursorlineopt=number\r")
1091 call VerifyScreenDump(buf, 'Test_diff_with_cursorline_number_02', {})
1092
1093 " clean up
1094 call StopVimInTerminal(buf)
Bram Moolenaar127969c2022-03-06 19:54:13 +00001095endfunc
1096
zeertzjq4f33bc22021-08-05 17:57:02 +02001097func Test_diff_with_cursorline_breakindent()
1098 CheckScreendump
1099
1100 call writefile([
1101 \ 'hi CursorLine ctermbg=red ctermfg=white',
1102 \ 'set noequalalways wrap diffopt=followwrap cursorline breakindent',
1103 \ '50vnew',
1104 \ 'call setline(1, [" "," "," "," "])',
1105 \ 'exe "norm 20Afoo\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abar\<Esc>"',
1106 \ 'vnew',
1107 \ 'call setline(1, [" "," "," "," "])',
1108 \ 'exe "norm 20Abee\<Esc>j20Afoo\<Esc>j20Afoo\<Esc>j20Abaz\<Esc>"',
1109 \ 'windo diffthis',
1110 \ '2wincmd w',
Bram Moolenaar59173412022-09-20 22:01:33 +01001111 \ ], 'Xtest_diff_cursorline_breakindent', 'D')
zeertzjq4f33bc22021-08-05 17:57:02 +02001112 let buf = RunVimInTerminal('-S Xtest_diff_cursorline_breakindent', {})
1113
1114 call term_sendkeys(buf, "gg0")
1115 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_01', {})
1116 call term_sendkeys(buf, "j")
1117 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_02', {})
1118 call term_sendkeys(buf, "j")
1119 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_03', {})
1120 call term_sendkeys(buf, "j")
1121 call VerifyScreenDump(buf, 'Test_diff_with_cul_bri_04', {})
1122
1123 " clean up
1124 call StopVimInTerminal(buf)
zeertzjq4f33bc22021-08-05 17:57:02 +02001125endfunc
1126
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001127func Test_diff_with_syntax()
1128 CheckScreendump
1129
1130 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001131 void doNothing() {
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001132 int x = 0;
1133 char *s = "hello";
1134 return 5;
1135 }
1136 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001137 call writefile(lines, 'Xprogram1.c', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001138 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001139 void doSomething() {
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001140 int x = 0;
1141 char *s = "there";
1142 return 5;
1143 }
1144 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001145 call writefile(lines, 'Xprogram2.c', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001146
1147 let lines =<< trim END
Bram Moolenaar94722c52023-01-28 19:19:03 +00001148 edit Xprogram1.c
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001149 diffsplit Xprogram2.c
1150 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001151 call writefile(lines, 'Xtest_diff_syntax', 'D')
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001152 let buf = RunVimInTerminal('-S Xtest_diff_syntax', {})
1153
1154 call VerifyScreenDump(buf, 'Test_diff_syntax_1', {})
1155
1156 " clean up
1157 call StopVimInTerminal(buf)
Bram Moolenaar248fdb32019-09-15 19:31:28 +02001158endfunc
1159
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001160func Test_diff_of_diff()
Bram Moolenaar3c8ee622019-08-03 22:55:50 +02001161 CheckScreendump
1162 CheckFeature rightleft
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001163
1164 call writefile([
1165 \ 'call setline(1, ["aa","bb","cc","@@ -3,2 +5,7 @@","dd","ee","ff"])',
1166 \ 'vnew',
1167 \ 'call setline(1, ["aa","bb","cc"])',
1168 \ 'windo diffthis',
Bram Moolenaar8ee4c012019-03-29 18:08:18 +01001169 \ '1wincmd w',
1170 \ 'setlocal number',
Bram Moolenaar59173412022-09-20 22:01:33 +01001171 \ ], 'Xtest_diff_diff', 'D')
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001172 let buf = RunVimInTerminal('-S Xtest_diff_diff', {})
1173
1174 call VerifyScreenDump(buf, 'Test_diff_of_diff_01', {})
1175
Bram Moolenaare73f9112019-03-29 18:29:54 +01001176 call term_sendkeys(buf, ":set rightleft\<cr>")
1177 call VerifyScreenDump(buf, 'Test_diff_of_diff_02', {})
1178
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001179 " clean up
1180 call StopVimInTerminal(buf)
Bram Moolenaarf7acf2b2018-11-01 21:14:53 +01001181endfunc
Bram Moolenaarc8234772019-11-10 21:00:27 +01001182
1183func CloseoffSetup()
1184 enew
1185 call setline(1, ['one', 'two', 'three'])
1186 diffthis
1187 new
1188 call setline(1, ['one', 'tow', 'three'])
1189 diffthis
1190 call assert_equal(1, &diff)
1191 only!
1192endfunc
1193
1194func Test_diff_closeoff()
1195 " "closeoff" included by default: last diff win gets 'diff' reset'
1196 call CloseoffSetup()
1197 call assert_equal(0, &diff)
1198 enew!
1199
1200 " "closeoff" excluded: last diff win keeps 'diff' set'
1201 set diffopt-=closeoff
1202 call CloseoffSetup()
1203 call assert_equal(1, &diff)
1204 diffoff!
1205 enew!
1206endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001207
Bram Moolenaar4223d432021-02-10 13:18:17 +01001208func Test_diff_followwrap()
1209 new
1210 set diffopt+=followwrap
1211 set wrap
1212 diffthis
1213 call assert_equal(1, &wrap)
1214 diffoff
1215 set nowrap
1216 diffthis
1217 call assert_equal(0, &wrap)
1218 diffoff
1219 set diffopt&
1220 bwipe!
1221endfunc
1222
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001223func Test_diff_maintains_change_mark()
Sean Dewarccc16442021-12-29 16:44:48 +00001224 func DiffMaintainsChangeMark()
1225 enew!
1226 call setline(1, ['a', 'b', 'c', 'd'])
1227 diffthis
1228 new
1229 call setline(1, ['a', 'b', 'c', 'e'])
1230 " Set '[ and '] marks
1231 2,3yank
1232 call assert_equal([2, 3], [line("'["), line("']")])
1233 " Verify they aren't affected by the implicit diff
1234 diffthis
1235 call assert_equal([2, 3], [line("'["), line("']")])
1236 " Verify they aren't affected by an explicit diff
1237 diffupdate
1238 call assert_equal([2, 3], [line("'["), line("']")])
1239 bwipe!
1240 bwipe!
1241 endfunc
1242
1243 set diffopt-=internal
1244 call DiffMaintainsChangeMark()
1245 set diffopt+=internal
1246 call DiffMaintainsChangeMark()
Bram Moolenaard9b74a22022-01-16 15:00:08 +00001247
Sean Dewarccc16442021-12-29 16:44:48 +00001248 set diffopt&
Bram Moolenaard9b74a22022-01-16 15:00:08 +00001249 delfunc DiffMaintainsChangeMark
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01001250endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001251
1252" Test for 'patchexpr'
1253func Test_patchexpr()
1254 let g:patch_args = []
1255 func TPatch()
1256 call add(g:patch_args, readfile(v:fname_in))
1257 call add(g:patch_args, readfile(v:fname_diff))
1258 call writefile(['output file'], v:fname_out)
1259 endfunc
1260 set patchexpr=TPatch()
1261
Bram Moolenaar59173412022-09-20 22:01:33 +01001262 call writefile(['input file'], 'Xinput', 'D')
1263 call writefile(['diff file'], 'Xdiff', 'D')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001264 %bwipe!
1265 edit Xinput
1266 diffpatch Xdiff
1267 call assert_equal('output file', getline(1))
1268 call assert_equal('Xinput.new', bufname())
1269 call assert_equal(2, winnr('$'))
1270 call assert_true(&diff)
1271
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00001272 " Using a script-local function
1273 func s:NewPatchExpr()
1274 endfunc
1275 set patchexpr=s:NewPatchExpr()
1276 call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1277 set patchexpr=<SID>NewPatchExpr()
1278 call assert_equal(expand('<SID>') .. 'NewPatchExpr()', &patchexpr)
1279
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001280 set patchexpr&
1281 delfunc TPatch
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +00001282 delfunc s:NewPatchExpr
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001283 %bwipe!
1284endfunc
1285
Bram Moolenaar511feec2020-06-18 19:15:27 +02001286func Test_diff_rnu()
1287 CheckScreendump
1288
1289 let content =<< trim END
1290 call setline(1, ['a', 'a', 'a', 'y', 'b', 'b', 'b', 'b', 'b'])
1291 vnew
1292 call setline(1, ['a', 'a', 'a', 'x', 'x', 'x', 'b', 'b', 'b', 'b', 'b'])
1293 windo diffthis
1294 setlocal number rnu foldcolumn=0
1295 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001296 call writefile(content, 'Xtest_diff_rnu', 'D')
Bram Moolenaar511feec2020-06-18 19:15:27 +02001297 let buf = RunVimInTerminal('-S Xtest_diff_rnu', {})
1298
1299 call VerifyScreenDump(buf, 'Test_diff_rnu_01', {})
1300
1301 call term_sendkeys(buf, "j")
1302 call VerifyScreenDump(buf, 'Test_diff_rnu_02', {})
1303 call term_sendkeys(buf, "j")
1304 call VerifyScreenDump(buf, 'Test_diff_rnu_03', {})
1305
1306 " clean up
1307 call StopVimInTerminal(buf)
Bram Moolenaar511feec2020-06-18 19:15:27 +02001308endfunc
1309
Bram Moolenaarfc838d62020-06-25 22:23:48 +02001310func Test_diff_multilineconceal()
1311 new
1312 diffthis
1313
1314 new
1315 call matchadd('Conceal', 'a\nb', 9, -1, {'conceal': 'Y'})
1316 set cole=2 cocu=n
1317 call setline(1, ["a", "b"])
1318 diffthis
1319 redraw
1320endfunc
1321
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001322func Test_diff_and_scroll()
1323 " this was causing an ml_get error
1324 set ls=2
Bram Moolenaar94722c52023-01-28 19:19:03 +00001325 for i in range(winheight(0) * 2)
1326 call setline(i, i < winheight(0) - 10 ? i : i + 10)
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001327 endfor
1328 vnew
Bram Moolenaar94722c52023-01-28 19:19:03 +00001329 for i in range(winheight(0)*2 + 10)
1330 call setline(i, i < winheight(0) - 10 ? 0 : i)
Bram Moolenaar8455c5e2020-07-14 21:22:30 +02001331 endfor
1332 diffthis
1333 wincmd p
1334 diffthis
1335 execute 'normal ' . winheight(0) . "\<C-d>"
1336
1337 bwipe!
1338 bwipe!
1339 set ls&
1340endfunc
1341
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001342func Test_diff_filler_cursorcolumn()
1343 CheckScreendump
1344
1345 let content =<< trim END
1346 call setline(1, ['aa', 'bb', 'cc'])
1347 vnew
1348 call setline(1, ['aa', 'cc'])
1349 windo diffthis
1350 wincmd p
1351 setlocal cursorcolumn foldcolumn=0
1352 norm! gg0
1353 redraw!
1354 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001355 call writefile(content, 'Xtest_diff_cuc', 'D')
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001356 let buf = RunVimInTerminal('-S Xtest_diff_cuc', {})
1357
1358 call VerifyScreenDump(buf, 'Test_diff_cuc_01', {})
1359
1360 call term_sendkeys(buf, "l")
1361 call term_sendkeys(buf, "\<C-l>")
1362 call VerifyScreenDump(buf, 'Test_diff_cuc_02', {})
1363 call term_sendkeys(buf, "0j")
1364 call term_sendkeys(buf, "\<C-l>")
1365 call VerifyScreenDump(buf, 'Test_diff_cuc_03', {})
1366 call term_sendkeys(buf, "l")
1367 call term_sendkeys(buf, "\<C-l>")
1368 call VerifyScreenDump(buf, 'Test_diff_cuc_04', {})
1369
1370 " clean up
1371 call StopVimInTerminal(buf)
Bram Moolenaarfabc3ca2020-11-05 19:07:21 +01001372endfunc
1373
Yegappan Lakshmanan30443242021-06-10 21:52:15 +02001374" Test for adding/removing lines inside diff chunks, between diff chunks
1375" and before diff chunks
1376func Test_diff_modify_chunks()
1377 enew!
1378 let w2_id = win_getid()
1379 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1380 new
1381 let w1_id = win_getid()
1382 call setline(1, ['a', '2', '3', 'd', 'e', 'f', '7', '8', 'i'])
1383 windo diffthis
1384
1385 " remove a line between two diff chunks and create a new diff chunk
1386 call win_gotoid(w2_id)
1387 5d
1388 call win_gotoid(w1_id)
1389 call diff_hlID(5, 1)->synIDattr('name')->assert_equal('DiffAdd')
1390
1391 " add a line between two diff chunks
1392 call win_gotoid(w2_id)
1393 normal! 4Goe
1394 call win_gotoid(w1_id)
1395 call diff_hlID(4, 1)->synIDattr('name')->assert_equal('')
1396 call diff_hlID(5, 1)->synIDattr('name')->assert_equal('')
1397
1398 " remove all the lines in a diff chunk.
1399 call win_gotoid(w2_id)
1400 7,8d
1401 call win_gotoid(w1_id)
1402 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1403 call assert_equal(['', 'DiffText', 'DiffText', '', '', '', 'DiffAdd',
1404 \ 'DiffAdd', ''], hl)
1405
1406 " remove lines from one diff chunk to just before the next diff chunk
1407 call win_gotoid(w2_id)
1408 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1409 2,6d
1410 call win_gotoid(w1_id)
1411 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1412 call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', 'DiffAdd',
1413 \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1414
1415 " remove lines just before the top of a diff chunk
1416 call win_gotoid(w2_id)
1417 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1418 5,6d
1419 call win_gotoid(w1_id)
1420 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1421 call assert_equal(['', 'DiffText', 'DiffText', '', 'DiffText', 'DiffText',
1422 \ 'DiffAdd', 'DiffAdd', ''], hl)
1423
1424 " remove line after the end of a diff chunk
1425 call win_gotoid(w2_id)
1426 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1427 4d
1428 call win_gotoid(w1_id)
1429 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1430 call assert_equal(['', 'DiffText', 'DiffText', 'DiffAdd', '', '', 'DiffText',
1431 \ 'DiffText', ''], hl)
1432
1433 " remove lines starting from the end of one diff chunk and ending inside
1434 " another diff chunk
1435 call win_gotoid(w2_id)
1436 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
1437 4,7d
1438 call win_gotoid(w1_id)
1439 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1440 call assert_equal(['', 'DiffText', 'DiffText', 'DiffText', 'DiffAdd',
1441 \ 'DiffAdd', 'DiffAdd', 'DiffAdd', ''], hl)
1442
1443 " removing the only remaining diff chunk should make the files equal
1444 call win_gotoid(w2_id)
1445 call setline(1, ['a', '2', '3', 'x', 'd', 'e', 'f', 'x', '7', '8', 'i'])
1446 8d
1447 let hl = range(1, 10)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1448 call assert_equal(['', '', '', 'DiffAdd', '', '', '', '', '', ''], hl)
1449 call win_gotoid(w2_id)
1450 4d
1451 call win_gotoid(w1_id)
1452 let hl = range(1, 9)->map({_, lnum -> diff_hlID(lnum, 1)->synIDattr('name')})
1453 call assert_equal(['', '', '', '', '', '', '', '', ''], hl)
1454
1455 %bw!
1456endfunc
glacambread5c1782021-05-24 14:20:53 +02001457
Bram Moolenaar06f60952021-12-28 18:30:05 +00001458func Test_diff_binary()
1459 CheckScreendump
1460
1461 let content =<< trim END
1462 call setline(1, ['a', 'b', "c\n", 'd', 'e', 'f', 'g'])
1463 vnew
1464 call setline(1, ['A', 'b', 'c', 'd', 'E', 'f', 'g'])
1465 windo diffthis
1466 wincmd p
1467 norm! gg0
1468 redraw!
1469 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001470 call writefile(content, 'Xtest_diff_bin', 'D')
Bram Moolenaar06f60952021-12-28 18:30:05 +00001471 let buf = RunVimInTerminal('-S Xtest_diff_bin', {})
1472
1473 " Test using internal diff
1474 call VerifyScreenDump(buf, 'Test_diff_bin_01', {})
1475
1476 " Test using internal diff and case folding
1477 call term_sendkeys(buf, ":set diffopt+=icase\<cr>")
1478 call term_sendkeys(buf, "\<C-l>")
1479 call VerifyScreenDump(buf, 'Test_diff_bin_02', {})
1480 " Test using external diff
1481 call term_sendkeys(buf, ":set diffopt=filler\<cr>")
1482 call term_sendkeys(buf, "\<C-l>")
1483 call VerifyScreenDump(buf, 'Test_diff_bin_03', {})
1484 " Test using external diff and case folding
1485 call term_sendkeys(buf, ":set diffopt=filler,icase\<cr>")
1486 call term_sendkeys(buf, "\<C-l>")
1487 call VerifyScreenDump(buf, 'Test_diff_bin_04', {})
1488
1489 " clean up
1490 call StopVimInTerminal(buf)
Bram Moolenaar06f60952021-12-28 18:30:05 +00001491 set diffopt&vim
1492endfunc
1493
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001494" Test for using the 'zi' command to invert 'foldenable' in diff windows (test
1495" for the issue fixed by patch 6.2.317)
1496func Test_diff_foldinvert()
1497 %bw!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01001498 edit Xdoffile1
1499 new Xdoffile2
1500 new Xdoffile3
Yegappan Lakshmananb0ad2d92022-01-27 13:16:59 +00001501 windo diffthis
1502 " open a non-diff window
1503 botright new
1504 1wincmd w
1505 call assert_true(getwinvar(1, '&foldenable'))
1506 call assert_true(getwinvar(2, '&foldenable'))
1507 call assert_true(getwinvar(3, '&foldenable'))
1508 normal zi
1509 call assert_false(getwinvar(1, '&foldenable'))
1510 call assert_false(getwinvar(2, '&foldenable'))
1511 call assert_false(getwinvar(3, '&foldenable'))
1512 normal zi
1513 call assert_true(getwinvar(1, '&foldenable'))
1514 call assert_true(getwinvar(2, '&foldenable'))
1515 call assert_true(getwinvar(3, '&foldenable'))
1516
1517 " If the current window has 'noscrollbind', then 'zi' should not change
1518 " 'foldenable' in other windows.
1519 1wincmd w
1520 set noscrollbind
1521 normal zi
1522 call assert_false(getwinvar(1, '&foldenable'))
1523 call assert_true(getwinvar(2, '&foldenable'))
1524 call assert_true(getwinvar(3, '&foldenable'))
1525
1526 " 'zi' should not change the 'foldenable' for windows with 'noscrollbind'
1527 1wincmd w
1528 set scrollbind
1529 normal zi
1530 call setwinvar(2, '&scrollbind', v:false)
1531 normal zi
1532 call assert_false(getwinvar(1, '&foldenable'))
1533 call assert_true(getwinvar(2, '&foldenable'))
1534 call assert_false(getwinvar(3, '&foldenable'))
1535
1536 %bw!
1537 set scrollbind&
1538endfunc
1539
Bram Moolenaara315ce12022-06-24 12:38:57 +01001540" This was scrolling for 'cursorbind' but 'scrollbind' is more important
1541func Test_diff_scroll()
1542 CheckScreendump
1543
1544 let left =<< trim END
1545 line 1
1546 line 2
1547 line 3
1548 line 4
1549
1550 // Common block
1551 // one
1552 // containing
1553 // four lines
1554
1555 // Common block
1556 // two
1557 // containing
1558 // four lines
1559 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001560 call writefile(left, 'Xleft', 'D')
Bram Moolenaara315ce12022-06-24 12:38:57 +01001561 let right =<< trim END
1562 line 1
1563 line 2
1564 line 3
1565 line 4
1566
1567 Lorem
1568 ipsum
1569 dolor
1570 sit
1571 amet,
1572 consectetur
1573 adipiscing
1574 elit.
1575 Etiam
1576 luctus
1577 lectus
1578 sodales,
1579 dictum
1580
1581 // Common block
1582 // one
1583 // containing
1584 // four lines
1585
1586 Vestibulum
1587 tincidunt
1588 aliquet
1589 nulla.
1590
1591 // Common block
1592 // two
1593 // containing
1594 // four lines
1595 END
Bram Moolenaar59173412022-09-20 22:01:33 +01001596 call writefile(right, 'Xright', 'D')
Bram Moolenaara315ce12022-06-24 12:38:57 +01001597 let buf = RunVimInTerminal('-d Xleft Xright', {'rows': 12})
1598 call term_sendkeys(buf, "\<C-W>\<C-W>jjjj")
1599 call VerifyScreenDump(buf, 'Test_diff_scroll_1', {})
1600 call term_sendkeys(buf, "j")
1601 call VerifyScreenDump(buf, 'Test_diff_scroll_2', {})
1602
1603 call StopVimInTerminal(buf)
Bram Moolenaara315ce12022-06-24 12:38:57 +01001604endfunc
1605
Bram Moolenaar38d867f2023-04-01 19:54:40 +01001606" This was scrolling too many lines.
1607func Test_diff_scroll_wrap_on()
1608 20new
1609 40vsplit
1610 call setline(1, map(range(1, 9), 'repeat(v:val, 200)'))
1611 setlocal number diff so=0
1612 redraw
1613 normal! jj
1614 call assert_equal(1, winsaveview().topline)
1615 normal! j
1616 call assert_equal(2, winsaveview().topline)
zeertzjq05834912023-10-04 20:12:37 +02001617
1618 bwipe!
1619 bwipe!
1620endfunc
1621
1622func Test_diff_scroll_many_filler()
1623 20new
1624 vnew
1625 call setline(1, ['^^^', '^^^', '$$$', '$$$'])
1626 diffthis
1627 setlocal scrolloff=0
1628 wincmd p
1629 call setline(1, ['^^^', '^^^'] + repeat(['###'], 41) + ['$$$', '$$$'])
1630 diffthis
1631 setlocal scrolloff=0
1632 wincmd p
1633 redraw
1634
1635 " Note: need a redraw after each scroll, otherwise the test always passes.
1636 normal! G
1637 redraw
1638 call assert_equal(3, winsaveview().topline)
1639 call assert_equal(18, winsaveview().topfill)
1640 exe "normal! \<C-B>"
1641 redraw
1642 call assert_equal(3, winsaveview().topline)
1643 call assert_equal(19, winsaveview().topfill)
1644 exe "normal! \<C-B>"
1645 redraw
1646 call assert_equal(2, winsaveview().topline)
1647 call assert_equal(0, winsaveview().topfill)
1648 exe "normal! \<C-B>"
1649 redraw
1650 call assert_equal(1, winsaveview().topline)
1651 call assert_equal(0, winsaveview().topfill)
1652
Bram Moolenaar38d867f2023-04-01 19:54:40 +01001653 bwipe!
1654 bwipe!
1655endfunc
1656
Bram Moolenaarcd38bb42022-06-26 14:04:07 +01001657" This was trying to update diffs for a buffer being closed
1658func Test_diff_only()
1659 silent! lfile
1660 set diff
1661 lopen
1662 norm o
1663 silent! norm o
1664
1665 set nodiff
1666 %bwipe!
1667endfunc
1668
Bram Moolenaarc101abf2022-06-26 16:53:34 +01001669" This was causing invalid diff block values
1670" FIXME: somehow this causes a valgrind error when run directly but not when
1671" run as a test.
1672func Test_diff_manipulations()
1673 set diff
1674 split 0
1675 sil! norm R doobdeuR doobdeuR doobdeu
1676
1677 set nodiff
1678 %bwipe!
1679endfunc
1680
Bram Moolenaar4e677b92022-07-28 18:44:27 +01001681" This was causing the line number in the diff block to go below one.
1682" FIXME: somehow this causes a valgrind error when run directly but not when
1683" run as a test.
1684func Test_diff_put_and_undo()
1685 set diff
1686 next 0
1687 split 00
1688 sil! norm o0gguudpo0ggJuudp
1689
1690 bwipe!
1691 bwipe!
1692 set nodiff
1693endfunc
1694
Bram Moolenaara315ce12022-06-24 12:38:57 +01001695
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001696" vim: shiftwidth=2 sts=2 expandtab