blob: e09bcaa44fbd04765925e27cc9b0b1357e6ea7a7 [file] [log] [blame]
Bram Moolenaar42093c02016-07-30 16:16:54 +02001" Tests for diff mode
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01002set belloff=all
Bram Moolenaar42093c02016-07-30 16:16:54 +02003
4func Test_diff_fold_sync()
5 enew!
6 let l = range(50)
7 call setline(1, l)
8 diffthis
9 let winone = win_getid()
10 new
11 let l[25] = 'diff'
12 call setline(1, l)
13 diffthis
14 let wintwo = win_getid()
15 " line 15 is inside the closed fold
16 call assert_equal(19, foldclosedend(10))
17 call win_gotoid(winone)
18 call assert_equal(19, foldclosedend(10))
19 " open the fold
20 normal zv
21 call assert_equal(-1, foldclosedend(10))
22 " fold in other window must have opened too
23 call win_gotoid(wintwo)
24 call assert_equal(-1, foldclosedend(10))
25
26 " cursor position is in sync
27 normal 23G
28 call win_gotoid(winone)
29 call assert_equal(23, getcurpos()[1])
30
31 windo diffoff
32 close!
33 set nomodified
34endfunc
35
36func Test_vert_split()
37 " Disable the title to avoid xterm keeping the wrong one.
38 set notitle noicon
39 new
40 let l = ['1 aa', '2 bb', '3 cc', '4 dd', '5 ee']
41 call setline(1, l)
42 w! Xtest
43 normal dd
44 $
45 put
46 normal kkrXoxxx
47 w! Xtest2
48 file Nop
49 normal ggoyyyjjjozzzz
50 set foldmethod=marker foldcolumn=4
51 call assert_equal(0, &diff)
52 call assert_equal('marker', &foldmethod)
53 call assert_equal(4, &foldcolumn)
54 call assert_equal(0, &scrollbind)
55 call assert_equal(0, &cursorbind)
56 call assert_equal(1, &wrap)
57
58 vert diffsplit Xtest
59 vert diffsplit Xtest2
60 call assert_equal(1, &diff)
61 call assert_equal('diff', &foldmethod)
62 call assert_equal(2, &foldcolumn)
63 call assert_equal(1, &scrollbind)
64 call assert_equal(1, &cursorbind)
65 call assert_equal(0, &wrap)
66
67 let diff_fdm = &fdm
68 let diff_fdc = &fdc
69 " repeat entering diff mode here to see if this saves the wrong settings
70 diffthis
71 " jump to second window for a moment to have filler line appear at start of
72 " first window
73 wincmd w
74 normal gg
75 wincmd p
76 normal gg
77 call assert_equal(2, winline())
78 normal j
79 call assert_equal(4, winline())
80 normal j
81 call assert_equal(5, winline())
82 normal j
83 call assert_equal(6, winline())
84 normal j
85 call assert_equal(8, winline())
86 normal j
87 call assert_equal(9, winline())
88
89 wincmd w
90 normal gg
91 call assert_equal(1, winline())
92 normal j
93 call assert_equal(2, winline())
94 normal j
95 call assert_equal(4, winline())
96 normal j
97 call assert_equal(5, winline())
98 normal j
99 call assert_equal(8, winline())
100
101 wincmd w
102 normal gg
103 call assert_equal(2, winline())
104 normal j
105 call assert_equal(3, winline())
106 normal j
107 call assert_equal(4, winline())
108 normal j
109 call assert_equal(5, winline())
110 normal j
111 call assert_equal(6, winline())
112 normal j
113 call assert_equal(7, winline())
114 normal j
115 call assert_equal(8, winline())
116
117 " Test diffoff
118 diffoff!
119 1wincmd 2
120 let &diff = 1
121 let &fdm = diff_fdm
122 let &fdc = diff_fdc
123 4wincmd w
124 diffoff!
125 1wincmd w
126 call assert_equal(0, &diff)
127 call assert_equal('marker', &foldmethod)
128 call assert_equal(4, &foldcolumn)
129 call assert_equal(0, &scrollbind)
130 call assert_equal(0, &cursorbind)
131 call assert_equal(1, &wrap)
132
133 wincmd w
134 call assert_equal(0, &diff)
135 call assert_equal('marker', &foldmethod)
136 call assert_equal(4, &foldcolumn)
137 call assert_equal(0, &scrollbind)
138 call assert_equal(0, &cursorbind)
139 call assert_equal(1, &wrap)
140
141 wincmd w
142 call assert_equal(0, &diff)
143 call assert_equal('marker', &foldmethod)
144 call assert_equal(4, &foldcolumn)
145 call assert_equal(0, &scrollbind)
146 call assert_equal(0, &cursorbind)
147 call assert_equal(1, &wrap)
148
Bram Moolenaar623cf882016-07-30 16:36:01 +0200149 call delete('Xtest')
150 call delete('Xtest2')
Bram Moolenaar42093c02016-07-30 16:16:54 +0200151 windo bw!
152endfunc
153
154func Test_filler_lines()
155 " Test that diffing shows correct filler lines
156 enew!
157 put =range(4,10)
158 1d _
159 vnew
160 put =range(1,10)
161 1d _
162 windo diffthis
163 wincmd h
164 call assert_equal(1, line('w0'))
165 unlet! diff_fdm diff_fdc
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200166 windo diffoff
167 bwipe!
168 enew!
169endfunc
Bram Moolenaar42093c02016-07-30 16:16:54 +0200170
Bram Moolenaar90d121f2016-07-30 19:11:25 +0200171func Test_diffget_diffput()
172 enew!
173 let l = range(50)
174 call setline(1, l)
175 call assert_fails('diffget', 'E99:')
176 diffthis
177 call assert_fails('diffget', 'E100:')
178 new
179 let l[10] = 'one'
180 let l[20] = 'two'
181 let l[30] = 'three'
182 let l[40] = 'four'
183 call setline(1, l)
184 diffthis
185 call assert_equal('one', getline(11))
186 11diffget
187 call assert_equal('10', getline(11))
188 21diffput
189 wincmd w
190 call assert_equal('two', getline(21))
191 normal 31Gdo
192 call assert_equal('three', getline(31))
193 call assert_equal('40', getline(41))
194 normal 41Gdp
195 wincmd w
196 call assert_equal('40', getline(41))
197 new
198 diffthis
199 call assert_fails('diffget', 'E101:')
200
201 windo diffoff
202 bwipe!
203 bwipe!
204 enew!
Bram Moolenaar42093c02016-07-30 16:16:54 +0200205endfunc
Bram Moolenaare67d5462016-08-27 22:40:42 +0200206
207func Test_diffoff()
208 enew!
209 call setline(1, ['Two', 'Three'])
210 let normattr = screenattr(1, 1)
211 diffthis
212 botright vert new
213 call setline(1, ['One', '', 'Two', 'Three'])
214 diffthis
215 redraw
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100216 call assert_notequal(normattr, screenattr(1, 1))
Bram Moolenaare67d5462016-08-27 22:40:42 +0200217 diffoff!
218 redraw
219 call assert_equal(normattr, screenattr(1, 1))
220 bwipe!
221 bwipe!
222endfunc
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200223
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100224func Test_diffoff_hidden()
225 set diffopt=filler,foldcolumn:0
226 e! one
227 call setline(1, ['Two', 'Three'])
228 let normattr = screenattr(1, 1)
229 diffthis
230 botright vert new two
231 call setline(1, ['One', 'Four'])
232 diffthis
233 redraw
234 call assert_notequal(normattr, screenattr(1, 1))
235 set hidden
236 close
237 redraw
238 " diffing with hidden buffer two
239 call assert_notequal(normattr, screenattr(1, 1))
240 diffoff
241 redraw
242 call assert_equal(normattr, screenattr(1, 1))
243 diffthis
244 redraw
245 " still diffing with hidden buffer two
246 call assert_notequal(normattr, screenattr(1, 1))
247 diffoff!
248 redraw
249 call assert_equal(normattr, screenattr(1, 1))
250 diffthis
251 redraw
252 " no longer diffing with hidden buffer two
253 call assert_equal(normattr, screenattr(1, 1))
254
255 bwipe!
256 bwipe!
257 set hidden& diffopt&
258endfunc
259
Bram Moolenaar025e3e02016-10-18 14:50:18 +0200260func Test_setting_cursor()
261 new Xtest1
262 put =range(1,90)
263 wq
264 new Xtest2
265 put =range(1,100)
266 wq
267
268 tabe Xtest2
269 $
270 diffsp Xtest1
271 tabclose
272
273 call delete('Xtest1')
274 call delete('Xtest2')
275endfunc
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100276
277func Test_diff_move_to()
278 new
279 call setline(1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
280 diffthis
281 vnew
282 call setline(1, [1, '2x', 3, 4, 4, 5, '6x', 7, '8x', 9, '10x'])
283 diffthis
284 norm ]c
285 call assert_equal(2, line('.'))
286 norm 3]c
287 call assert_equal(9, line('.'))
288 norm 10]c
289 call assert_equal(11, line('.'))
290 norm [c
291 call assert_equal(9, line('.'))
292 norm 2[c
293 call assert_equal(5, line('.'))
294 norm 10[c
295 call assert_equal(2, line('.'))
296 %bwipe!
297endfunc
298
299func Test_diffpatch()
300 " The patch program on MS-Windows may fail or hang.
301 if !executable('patch') || !has('unix')
302 return
303 endif
304 new
305 insert
306***************
307*** 1,3 ****
308 1
309! 2
310 3
311--- 1,4 ----
312 1
313! 2x
314 3
315+ 4
316.
317 saveas Xpatch
318 bwipe!
319 new
320 call assert_fails('diffpatch Xpatch', 'E816:')
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100321
Bram Moolenaara95ab322017-03-11 19:21:53 +0100322 for name in ['Xpatch', 'Xpatch$HOME', 'Xpa''tch']
Bram Moolenaar1ef73e32017-03-09 19:21:30 +0100323 call setline(1, ['1', '2', '3'])
324 if name != 'Xpatch'
325 call rename('Xpatch', name)
326 endif
327 exe 'diffpatch ' . escape(name, '$')
328 call assert_equal(['1', '2x', '3', '4'], getline(1, '$'))
329 if name != 'Xpatch'
330 call rename(name, 'Xpatch')
331 endif
332 bwipe!
333 endfor
334
Bram Moolenaaraeb661e2017-02-26 19:59:59 +0100335 call delete('Xpatch')
336 bwipe!
337endfunc
338
339func Test_diff_too_many_buffers()
340 for i in range(1, 8)
341 exe "new Xtest" . i
342 diffthis
343 endfor
344 new Xtest9
345 call assert_fails('diffthis', 'E96:')
346 %bwipe!
347endfunc
348
349func Test_diff_nomodifiable()
350 new
351 call setline(1, [1, 2, 3, 4])
352 setl nomodifiable
353 diffthis
354 vnew
355 call setline(1, ['1x', 2, 3, 3, 4])
356 diffthis
357 call assert_fails('norm dp', 'E793:')
358 setl nomodifiable
359 call assert_fails('norm do', 'E21:')
360 %bwipe!
361endfunc
Bram Moolenaarf58a8472017-03-05 18:03:04 +0100362
363func Test_diff_lastline()
364 enew!
365 only!
366 call setline(1, ['This is a ', 'line with five ', 'rows'])
367 diffthis
368 botright vert new
369 call setline(1, ['This is', 'a line with ', 'four rows'])
370 diffthis
371 1
372 call feedkeys("Je a\<CR>", 'tx')
373 call feedkeys("Je a\<CR>", 'tx')
374 let w1lines = winline()
375 wincmd w
376 $
377 let w2lines = winline()
378 call assert_equal(w2lines, w1lines)
379 bwipe!
380 bwipe!
381endfunc