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