blob: 0384b4fd003a50a2a8418d9c3afdd5fe327fa32f [file] [log] [blame]
Bram Moolenaarded27822017-01-02 14:27:34 +01001" Test for folding
2
Bram Moolenaar907dad72018-07-10 15:07:15 +02003source view_util.vim
Bram Moolenaar7701f302018-10-02 21:20:32 +02004source screendump.vim
Bram Moolenaar907dad72018-07-10 15:07:15 +02005
Bram Moolenaar94be6192017-04-22 22:40:11 +02006func PrepIndent(arg)
Bram Moolenaar88d298a2017-03-14 21:53:58 +01007 return [a:arg] + repeat(["\t".a:arg], 5)
8endfu
9
Bram Moolenaar94be6192017-04-22 22:40:11 +020010func Test_address_fold()
Bram Moolenaarded27822017-01-02 14:27:34 +010011 new
12 call setline(1, ['int FuncName() {/*{{{*/', 1, 2, 3, 4, 5, '}/*}}}*/',
13 \ 'after fold 1', 'after fold 2', 'after fold 3'])
14 setl fen fdm=marker
Bram Moolenaar518c9b12017-03-21 11:48:39 +010015 " The next commands should all copy the same part of the buffer,
16 " regardless of the addressing type, since the part to be copied
Bram Moolenaarded27822017-01-02 14:27:34 +010017 " is folded away
18 :1y
19 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
20 :.y
21 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
22 :.+y
23 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
24 :.,.y
25 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
26 :sil .1,.y
27 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
28 " use silent to make E493 go away
29 :sil .+,.y
30 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
31 :,y
32 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
33 :,+y
34 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/','after fold 1'], getreg(0,1,1))
35 " using .+3 as second address should copy the whole folded line + the next 3
36 " lines
37 :.,+3y
38 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/',
39 \ 'after fold 1', 'after fold 2', 'after fold 3'], getreg(0,1,1))
40 :sil .,-2y
41 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3', '4', '5', '}/*}}}*/'], getreg(0,1,1))
42
43 " now test again with folding disabled
44 set nofoldenable
45 :1y
46 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
47 :.y
48 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
49 :.+y
50 call assert_equal(['1'], getreg(0,1,1))
51 :.,.y
52 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
53 " use silent to make E493 go away
54 :sil .1,.y
55 call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
56 " use silent to make E493 go away
57 :sil .+,.y
58 call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
59 :,y
60 call assert_equal(['int FuncName() {/*{{{*/'], getreg(0,1,1))
61 :,+y
62 call assert_equal(['int FuncName() {/*{{{*/', '1'], getreg(0,1,1))
63 " using .+3 as second address should copy the whole folded line + the next 3
64 " lines
65 :.,+3y
66 call assert_equal(['int FuncName() {/*{{{*/', '1', '2', '3'], getreg(0,1,1))
67 :7
68 :sil .,-2y
69 call assert_equal(['4', '5', '}/*}}}*/'], getreg(0,1,1))
70
71 quit!
Bram Moolenaar1159b162017-02-28 21:53:56 +010072endfunc
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010073
Bram Moolenaar94be6192017-04-22 22:40:11 +020074func Test_indent_fold()
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010075 new
76 call setline(1, ['', 'a', ' b', ' c'])
77 setl fen fdm=indent
78 2
79 norm! >>
80 let a=map(range(1,4), 'foldclosed(v:val)')
81 call assert_equal([-1,-1,-1,-1], a)
82 bw!
Bram Moolenaar1159b162017-02-28 21:53:56 +010083endfunc
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010084
Bram Moolenaar94be6192017-04-22 22:40:11 +020085func Test_indent_fold2()
Bram Moolenaar54b2bfa2017-01-02 14:57:08 +010086 new
87 call setline(1, ['', '{{{', '}}}', '{{{', '}}}'])
88 setl fen fdm=marker
89 2
90 norm! >>
91 let a=map(range(1,5), 'foldclosed(v:val)')
92 call assert_equal([-1,-1,-1,4,4], a)
93 bw!
Bram Moolenaar1159b162017-02-28 21:53:56 +010094endfunc
95
96func Test_manual_fold_with_filter()
97 if !executable('cat')
98 return
99 endif
Bram Moolenaar3f3897e2017-03-04 15:28:53 +0100100 for type in ['manual', 'marker']
101 exe 'set foldmethod=' . type
102 new
103 call setline(1, range(1, 20))
104 4,$fold
105 %foldopen
106 10,$fold
107 %foldopen
108 " This filter command should not have an effect
109 1,8! cat
110 call feedkeys('5ggzdzMGdd', 'xt')
111 call assert_equal(['1', '2', '3', '4', '5', '6', '7', '8', '9'], getline(1, '$'))
112
113 bwipe!
114 set foldmethod&
115 endfor
Bram Moolenaar1159b162017-02-28 21:53:56 +0100116endfunc
Bram Moolenaareadbc2b2017-03-04 18:42:39 +0100117
Bram Moolenaar94be6192017-04-22 22:40:11 +0200118func Test_indent_fold_with_read()
Bram Moolenaareadbc2b2017-03-04 18:42:39 +0100119 new
120 set foldmethod=indent
121 call setline(1, repeat(["\<Tab>a"], 4))
122 for n in range(1, 4)
123 call assert_equal(1, foldlevel(n))
124 endfor
125
126 call writefile(["a", "", "\<Tab>a"], 'Xfile')
127 foldopen
128 2read Xfile
129 %foldclose
130 call assert_equal(1, foldlevel(1))
131 call assert_equal(2, foldclosedend(1))
132 call assert_equal(0, foldlevel(3))
133 call assert_equal(0, foldlevel(4))
134 call assert_equal(1, foldlevel(5))
135 call assert_equal(7, foldclosedend(5))
136
137 bwipe!
138 set foldmethod&
139 call delete('Xfile')
140endfunc
141
142func Test_combining_folds_indent()
143 new
144 let one = "\<Tab>a"
145 let zero = 'a'
146 call setline(1, [one, one, zero, zero, zero, one, one, one])
147 set foldmethod=indent
148 3,5d
149 %foldclose
150 call assert_equal(5, foldclosedend(1))
151
152 set foldmethod&
153 bwipe!
154endfunc
155
156func Test_combining_folds_marker()
157 new
158 call setline(1, ['{{{', '}}}', '', '', '', '{{{', '', '}}}'])
159 set foldmethod=marker
160 3,5d
161 %foldclose
162 call assert_equal(2, foldclosedend(1))
163
164 set foldmethod&
165 bwipe!
166endfunc
167
Bram Moolenaar025a6b72017-03-12 20:37:21 +0100168func Test_folds_marker_in_comment()
169 new
170 call setline(1, ['" foo', 'bar', 'baz'])
171 setl fen fdm=marker
172 setl com=sO:\"\ -,mO:\"\ \ ,eO:\"\",:\" cms=\"%s
173 norm! zf2j
174 setl nofen
175 :1y
176 call assert_equal(['" foo{{{'], getreg(0,1,1))
177 :+2y
178 call assert_equal(['baz"}}}'], getreg(0,1,1))
179
180 set foldmethod&
181 bwipe!
182endfunc
183
Bram Moolenaareadbc2b2017-03-04 18:42:39 +0100184func s:TestFoldExpr(lnum)
185 let thisline = getline(a:lnum)
186 if thisline == 'a'
187 return 1
188 elseif thisline == 'b'
189 return 0
190 elseif thisline == 'c'
191 return '<1'
192 elseif thisline == 'd'
193 return '>1'
194 endif
195 return 0
196endfunction
197
198func Test_update_folds_expr_read()
199 new
200 call setline(1, ['a', 'a', 'a', 'a', 'a', 'a'])
201 set foldmethod=expr
202 set foldexpr=s:TestFoldExpr(v:lnum)
203 2
204 foldopen
205 call writefile(['b', 'b', 'a', 'a', 'd', 'a', 'a', 'c'], 'Xfile')
206 read Xfile
207 %foldclose
208 call assert_equal(2, foldclosedend(1))
209 call assert_equal(0, foldlevel(3))
210 call assert_equal(0, foldlevel(4))
211 call assert_equal(6, foldclosedend(5))
212 call assert_equal(10, foldclosedend(7))
213 call assert_equal(14, foldclosedend(11))
214
215 call delete('Xfile')
216 bwipe!
217 set foldmethod& foldexpr&
218endfunc
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100219
Bram Moolenaar94be6192017-04-22 22:40:11 +0200220func Check_foldlevels(expected)
221 call assert_equal(a:expected, map(range(1, line('$')), 'foldlevel(v:val)'))
222endfunc
223
224func Test_move_folds_around_manual()
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100225 new
226 let input = PrepIndent("a") + PrepIndent("b") + PrepIndent("c")
227 call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
228 let folds=[-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14]
229 " all folds closed
230 set foldenable foldlevel=0 fdm=indent
231 " needs a forced redraw
232 redraw!
233 set fdm=manual
234 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
235 call assert_equal(input, getline(1, '$'))
236 7,12m0
237 call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
238 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
239 10,12m0
240 call assert_equal(PrepIndent("a")[1:] + PrepIndent("b") + ["a"] + PrepIndent("c"), getline(1, '$'))
241 call assert_equal([1, 1, 1, 1, 1, -1, 7, 7, 7, 7, 7, -1, -1, 14, 14, 14, 14, 14], map(range(1, line('$')), 'foldclosed(v:val)'))
242 " moving should not close the folds
243 %d
244 call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
245 set fdm=indent
246 redraw!
247 set fdm=manual
248 call cursor(2, 1)
Bram Moolenaar40ebc0a2017-03-16 15:59:14 +0100249 %foldopen
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100250 7,12m0
251 let folds=repeat([-1], 18)
252 call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
253 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
254 norm! zM
255 " folds are not corrupted and all have been closed
256 call assert_equal([-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14], map(range(1, line('$')), 'foldclosed(v:val)'))
257 %d
258 call setline(1, ["a", "\tb", "\tc", "\td", "\te"])
259 set fdm=indent
260 redraw!
261 set fdm=manual
262 %foldopen
263 3m4
264 %foldclose
265 call assert_equal(["a", "\tb", "\td", "\tc", "\te"], getline(1, '$'))
266 call assert_equal([-1, 5, 5, 5, 5], map(range(1, line('$')), 'foldclosedend(v:val)'))
267 %d
268 call setline(1, ["a", "\tb", "\tc", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"])
269 set fdm=indent foldlevel=0
270 set fdm=manual
271 %foldopen
272 3m1
273 %foldclose
274 call assert_equal(["a", "\tc", "\tb", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"], getline(1, '$'))
275 call assert_equal(0, foldlevel(2))
276 call assert_equal(5, foldclosedend(3))
277 call assert_equal([-1, -1, 3, 3, 3, -1, 7, 7, 7, 7], map(range(1, line('$')), 'foldclosed(v:val)'))
278 2,6m$
279 %foldclose
280 call assert_equal(5, foldclosedend(2))
281 call assert_equal(0, foldlevel(6))
282 call assert_equal(9, foldclosedend(7))
283 call assert_equal([-1, 2, 2, 2, 2, -1, 7, 7, 7, -1], map(range(1, line('$')), 'foldclosed(v:val)'))
Bram Moolenaar495b7dd2017-09-16 17:19:22 +0200284
Bram Moolenaar40ebc0a2017-03-16 15:59:14 +0100285 %d
286 " Ensure moving around the edges still works.
287 call setline(1, PrepIndent("a") + repeat(["a"], 3) + ["\ta"])
288 set fdm=indent foldlevel=0
289 set fdm=manual
290 %foldopen
291 6m$
292 " The first fold has been truncated to the 5'th line.
293 " Second fold has been moved up because the moved line is now below it.
Bram Moolenaar94be6192017-04-22 22:40:11 +0200294 call Check_foldlevels([0, 1, 1, 1, 1, 0, 0, 0, 1, 0])
295
296 %delete
297 set fdm=indent foldlevel=0
298 call setline(1, [
299 \ "a",
300 \ "\ta",
301 \ "\t\ta",
302 \ "\t\ta",
303 \ "\t\ta",
304 \ "a",
305 \ "a"])
306 set fdm=manual
307 %foldopen!
308 4,5m6
309 call Check_foldlevels([0, 1, 2, 0, 0, 0, 0])
310
311 %delete
312 set fdm=indent
313 call setline(1, [
314 \ "\ta",
315 \ "\t\ta",
316 \ "\t\ta",
317 \ "\t\ta",
318 \ "\ta",
319 \ "\t\ta",
320 \ "\t\ta",
321 \ "\t\ta",
322 \ "\ta",
323 \ "\t\ta",
324 \ "\t\ta",
325 \ "\t\ta",
326 \ "\t\ta",
327 \ "\ta",
328 \ "a"])
329 set fdm=manual
330 %foldopen!
331 13m7
332 call Check_foldlevels([1, 2, 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 0])
333
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100334 bw!
335endfunc
336
Bram Moolenaar94be6192017-04-22 22:40:11 +0200337func Test_move_folds_around_indent()
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100338 new
339 let input = PrepIndent("a") + PrepIndent("b") + PrepIndent("c")
340 call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
341 let folds=[-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14]
342 " all folds closed
343 set fdm=indent
344 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
345 call assert_equal(input, getline(1, '$'))
346 7,12m0
347 call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
348 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
349 10,12m0
350 call assert_equal(PrepIndent("a")[1:] + PrepIndent("b") + ["a"] + PrepIndent("c"), getline(1, '$'))
351 call assert_equal([1, 1, 1, 1, 1, -1, 7, 7, 7, 7, 7, -1, -1, 14, 14, 14, 14, 14], map(range(1, line('$')), 'foldclosed(v:val)'))
352 " moving should not close the folds
353 %d
354 call setline(1, PrepIndent("a") + PrepIndent("b") + PrepIndent("c"))
355 set fdm=indent
356 call cursor(2, 1)
Bram Moolenaar40ebc0a2017-03-16 15:59:14 +0100357 %foldopen
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100358 7,12m0
359 let folds=repeat([-1], 18)
360 call assert_equal(PrepIndent("b") + PrepIndent("a") + PrepIndent("c"), getline(1, '$'))
361 call assert_equal(folds, map(range(1, line('$')), 'foldclosed(v:val)'))
362 norm! zM
363 " folds are not corrupted and all have been closed
364 call assert_equal([-1, 2, 2, 2, 2, 2, -1, 8, 8, 8, 8, 8, -1, 14, 14, 14, 14, 14], map(range(1, line('$')), 'foldclosed(v:val)'))
365 %d
366 call setline(1, ["a", "\tb", "\tc", "\td", "\te"])
367 set fdm=indent
368 %foldopen
369 3m4
370 %foldclose
371 call assert_equal(["a", "\tb", "\td", "\tc", "\te"], getline(1, '$'))
372 call assert_equal([-1, 5, 5, 5, 5], map(range(1, line('$')), 'foldclosedend(v:val)'))
373 %d
374 call setline(1, ["a", "\tb", "\tc", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"])
375 set fdm=indent foldlevel=0
376 %foldopen
377 3m1
378 %foldclose
379 call assert_equal(["a", "\tc", "\tb", "\td", "\te", "z", "\ty", "\tx", "\tw", "\tv"], getline(1, '$'))
380 call assert_equal(1, foldlevel(2))
381 call assert_equal(5, foldclosedend(3))
382 call assert_equal([-1, 2, 2, 2, 2, -1, 7, 7, 7, 7], map(range(1, line('$')), 'foldclosed(v:val)'))
383 2,6m$
384 %foldclose
385 call assert_equal(9, foldclosedend(2))
386 call assert_equal(1, foldlevel(6))
387 call assert_equal(9, foldclosedend(7))
388 call assert_equal([-1, 2, 2, 2, 2, 2, 2, 2, 2, -1], map(range(1, line('$')), 'foldclosed(v:val)'))
Bram Moolenaar40ebc0a2017-03-16 15:59:14 +0100389 " Ensure moving around the edges still works.
390 %d
391 call setline(1, PrepIndent("a") + repeat(["a"], 3) + ["\ta"])
392 set fdm=indent foldlevel=0
393 %foldopen
394 6m$
395 " The first fold has been truncated to the 5'th line.
396 " Second fold has been moved up because the moved line is now below it.
Bram Moolenaar94be6192017-04-22 22:40:11 +0200397 call Check_foldlevels([0, 1, 1, 1, 1, 0, 0, 0, 1, 1])
Bram Moolenaar88d298a2017-03-14 21:53:58 +0100398 bw!
399endfunc
Bram Moolenaar518c9b12017-03-21 11:48:39 +0100400
401func Test_folddoopen_folddoclosed()
402 new
403 call setline(1, range(1, 9))
404 set foldmethod=manual
405 1,3 fold
406 6,8 fold
407
408 " Test without range.
409 folddoopen s/$/o/
410 folddoclosed s/$/c/
411 call assert_equal(['1c', '2c', '3c',
412 \ '4o', '5o',
413 \ '6c', '7c', '8c',
414 \ '9o'], getline(1, '$'))
415
416 " Test with range.
417 call setline(1, range(1, 9))
418 1,8 folddoopen s/$/o/
419 4,$ folddoclosed s/$/c/
420 call assert_equal(['1', '2', '3',
421 \ '4o', '5o',
422 \ '6c', '7c', '8c',
423 \ '9'], getline(1, '$'))
424
425 set foldmethod&
426 bw!
427endfunc
428
429func Test_fold_error()
430 new
431 call setline(1, [1, 2])
432
433 for fm in ['indent', 'expr', 'syntax', 'diff']
434 exe 'set foldmethod=' . fm
435 call assert_fails('norm zf', 'E350:')
436 call assert_fails('norm zd', 'E351:')
437 call assert_fails('norm zE', 'E352:')
438 endfor
439
440 set foldmethod=manual
441 call assert_fails('norm zd', 'E490:')
442 call assert_fails('norm zo', 'E490:')
443 call assert_fails('3fold', 'E16:')
444
445 set foldmethod=marker
446 set nomodifiable
447 call assert_fails('1,2fold', 'E21:')
448
449 set modifiable&
450 set foldmethod&
451 bw!
452endfunc
Bram Moolenaar495b7dd2017-09-16 17:19:22 +0200453
454func Test_foldtext_recursive()
455 new
456 call setline(1, ['{{{', 'some text', '}}}'])
457 setlocal foldenable foldmethod=marker foldtext=foldtextresult(v\:foldstart)
458 " This was crashing because of endless recursion.
459 2foldclose
460 redraw
461 call assert_equal(1, foldlevel(2))
462 call assert_equal(1, foldclosed(2))
463 call assert_equal(3, foldclosedend(2))
464 bwipe!
465endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +0100466
467" Various fold related tests
468
469" Basic test if a fold can be created, opened, moving to the end and closed
470func Test_fold_manual()
471 enew!
472 set fdm=manual
473
474 let content = ['1 aa', '2 bb', '3 cc']
475 call append(0, content)
476 call cursor(1, 1)
477 normal zf2j
478 call assert_equal('1 aa', getline(foldclosed('.')))
479 normal zo
480 call assert_equal(-1, foldclosed('.'))
481 normal ]z
482 call assert_equal('3 cc', getline('.'))
483 normal zc
484 call assert_equal('1 aa', getline(foldclosed('.')))
485
486 set fdm&
487 enew!
488endfunc
489
490" test folding with markers.
491func Test_fold_marker()
492 enew!
493 set fdm=marker fdl=1 fdc=3
494
495 let content = ['4 dd {{{', '5 ee {{{ }}}', '6 ff }}}']
496 call append(0, content)
497 call cursor(2, 1)
498 call assert_equal(2, foldlevel('.'))
499 normal [z
500 call assert_equal(1, foldlevel('.'))
501 exe "normal jo{{ \<Esc>r{jj"
502 call assert_equal(1, foldlevel('.'))
503 normal kYpj
504 call assert_equal(0, foldlevel('.'))
505
506 set fdm& fdl& fdc&
507 enew!
508endfunc
509
510" test folding with indent
511func Test_fold_indent()
512 enew!
513 set fdm=indent sw=2
514
515 let content = ['1 aa', '2 bb', '3 cc']
516 call append(0, content)
517 call cursor(2, 1)
518 exe "normal i \<Esc>jI "
519 call assert_equal(2, foldlevel('.'))
520 normal k
521 call assert_equal(1, foldlevel('.'))
522
523 set fdm& sw&
524 enew!
525endfunc
526
527" test syntax folding
528func Test_fold_syntax()
529 if !has('syntax')
530 return
531 endif
532
533 enew!
534 set fdm=syntax fdl=0
535
536 syn region Hup start="dd" end="ii" fold contains=Fd1,Fd2,Fd3
537 syn region Fd1 start="ee" end="ff" fold contained
538 syn region Fd2 start="gg" end="hh" fold contained
539 syn region Fd3 start="commentstart" end="commentend" fold contained
540 let content = ['3 cc', '4 dd {{{', '5 ee {{{ }}}', '{{{{', '6 ff }}}',
541 \ '6 ff }}}', '7 gg', '8 hh', '9 ii']
542 call append(0, content)
543 normal Gzk
544 call assert_equal('9 ii', getline('.'))
545 normal k
546 call assert_equal('3 cc', getline('.'))
547 exe "normal jAcommentstart \<Esc>Acommentend"
548 set fdl=1
549 normal 3j
550 call assert_equal('7 gg', getline('.'))
551 set fdl=0
552 exe "normal zO\<C-L>j"
553 call assert_equal('8 hh', getline('.'))
554 syn clear Fd1 Fd2 Fd3 Hup
555
556 set fdm& fdl&
557 enew!
558endfunc
559
560func Flvl()
561 let l = getline(v:lnum)
562 if l =~ "bb$"
563 return 2
564 elseif l =~ "gg$"
565 return "s1"
566 elseif l =~ "ii$"
567 return ">2"
568 elseif l =~ "kk$"
569 return "0"
570 endif
571 return "="
572endfun
573
574" test expression folding
575func Test_fold_expr()
576 enew!
577 set fdm=expr fde=Flvl()
578
579 let content = ['1 aa',
580 \ '2 bb',
581 \ '3 cc',
582 \ '4 dd {{{commentstart commentend',
583 \ '5 ee {{{ }}}',
584 \ '{{{',
585 \ '6 ff }}}',
586 \ '6 ff }}}',
587 \ ' 7 gg',
588 \ ' 8 hh',
589 \ '9 ii',
590 \ 'a jj',
591 \ 'b kk']
592 call append(0, content)
593 call cursor(1, 1)
594 exe "normal /bb$\<CR>"
595 call assert_equal(2, foldlevel('.'))
596 exe "normal /hh$\<CR>"
597 call assert_equal(1, foldlevel('.'))
598 exe "normal /ii$\<CR>"
599 call assert_equal(2, foldlevel('.'))
600 exe "normal /kk$\<CR>"
601 call assert_equal(0, foldlevel('.'))
602
603 set fdm& fde&
604 enew!
605endfunc
606
607" Bug with fdm=indent and moving folds
608" Moving a fold a few times, messes up the folds below the moved fold.
609" Fixed by 7.4.700
610func Test_fold_move()
611 enew!
612 set fdm=indent sw=2 fdl=0
613
614 let content = ['', '', 'Line1', ' Line2', ' Line3',
615 \ 'Line4', ' Line5', ' Line6',
616 \ 'Line7', ' Line8', ' Line9']
617 call append(0, content)
618 normal zM
619 call cursor(4, 1)
620 move 2
621 move 1
622 call assert_equal(7, foldclosed(7))
623 call assert_equal(8, foldclosedend(7))
624 call assert_equal(0, foldlevel(9))
625 call assert_equal(10, foldclosed(10))
626 call assert_equal(11, foldclosedend(10))
627 call assert_equal('+-- 2 lines: Line2', foldtextresult(2))
628 call assert_equal('+-- 2 lines: Line8', foldtextresult(10))
629
630 set fdm& sw& fdl&
631 enew!
632endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100633
634" test for patch 7.3.637
635" Cannot catch the error caused by a foldopen when there is no fold.
636func Test_foldopen_exception()
637 enew!
638 let a = 'No error caught'
639 try
640 foldopen
641 catch
642 let a = matchstr(v:exception,'^[^ ]*')
643 endtry
644 call assert_equal('Vim(foldopen):E490:', a)
645
646 let a = 'No error caught'
647 try
648 foobar
649 catch
650 let a = matchstr(v:exception,'^[^ ]*')
651 endtry
652 call assert_match('E492:', a)
653endfunc
Bram Moolenaar907dad72018-07-10 15:07:15 +0200654
655func Test_fold_last_line_with_pagedown()
656 enew!
657 set fdm=manual
658
659 let expect = '+-- 11 lines: 9---'
660 let content = range(1,19)
661 call append(0, content)
662 normal dd9G
663 normal zfG
664 normal zt
665 call assert_equal('9', getline(foldclosed('.')))
666 call assert_equal('19', getline(foldclosedend('.')))
667 call assert_equal(expect, ScreenLines(1, len(expect))[0])
668 call feedkeys("\<C-F>", 'xt')
669 call assert_equal(expect, ScreenLines(1, len(expect))[0])
670 call feedkeys("\<C-F>", 'xt')
671 call assert_equal(expect, ScreenLines(1, len(expect))[0])
672 call feedkeys("\<C-B>\<C-F>\<C-F>", 'xt')
673 call assert_equal(expect, ScreenLines(1, len(expect))[0])
674
675 set fdm&
676 enew!
677endfunc
Bram Moolenaar7701f302018-10-02 21:20:32 +0200678
679func Test_folds_with_rnu()
680 if !CanRunVimInTerminal()
681 return
682 endif
683
684 call writefile([
685 \ 'set fdm=marker rnu foldcolumn=2',
686 \ 'call setline(1, ["{{{1", "nline 1", "{{{1", "line 2"])',
687 \ ], 'Xtest_folds_with_rnu')
688 let buf = RunVimInTerminal('-S Xtest_folds_with_rnu', {})
689
690 call VerifyScreenDump(buf, 'Test_folds_with_rnu_01', {})
691 call term_sendkeys(buf, "j")
692 call VerifyScreenDump(buf, 'Test_folds_with_rnu_02', {})
693
694 " clean up
695 call StopVimInTerminal(buf)
696 call delete('Xtest_folds_with_rnu')
697endfunc