fold_spec.lua (9853B)
1 -- Tests for folding. 2 3 local n = require('test.functional.testnvim')() 4 local Screen = require('test.functional.ui.screen') 5 6 local feed, insert, feed_command, expect_any = n.feed, n.insert, n.feed_command, n.expect_any 7 local command = n.command 8 local exec = n.exec 9 10 describe('folding', function() 11 local screen 12 13 before_each(function() 14 n.clear() 15 16 screen = Screen.new(45, 8) 17 end) 18 19 it('creation, opening, moving (to the end) and closing', function() 20 insert([[ 21 1 aa 22 2 bb 23 3 cc 24 last 25 ]]) 26 27 -- Basic test if a fold can be created, opened, moving to the end and 28 -- closed. 29 feed_command('1') 30 feed('zf2j') 31 feed_command('call append("$", "manual " . getline(foldclosed(".")))') 32 feed('zo') 33 feed_command('call append("$", foldclosed("."))') 34 feed(']z') 35 feed_command('call append("$", getline("."))') 36 feed('zc') 37 feed_command('call append("$", getline(foldclosed(".")))') 38 39 expect_any([[ 40 manual 1 aa 41 -1 42 3 cc 43 1 aa]]) 44 end) 45 46 it('foldmethod=marker', function() 47 screen:try_resize(20, 10) 48 insert([[ 49 dd {{{ 50 ee {{{ }}} 51 ff }}} 52 ]]) 53 feed_command('set fdm=marker fdl=1') 54 feed_command('2') 55 feed_command('call append("$", "line 2 foldlevel=" . foldlevel("."))') 56 feed('[z') 57 feed_command('call append("$", foldlevel("."))') 58 feed('jo{{ <esc>r{jj') -- writes '{{{' and moves 2 lines bot 59 feed_command('call append("$", foldlevel("."))') 60 feed('kYpj') 61 feed_command('call append("$", foldlevel("."))') 62 63 n.poke_eventloop() 64 screen:expect([[ 65 dd {{{ | 66 ee {{{ }}} | 67 {{{ | 68 ff }}} |*2 69 ^ | 70 line 2 foldlevel=2 | 71 1 |*2 72 | 73 ]]) 74 end) 75 76 it('foldmethod=indent', function() 77 screen:try_resize(20, 8) 78 feed_command('set fdm=indent sw=2') 79 insert([[ 80 aa 81 bb 82 cc 83 last 84 ]]) 85 feed_command('call append("$", "foldlevel line3=" . foldlevel(3))') 86 feed_command('call append("$", foldlevel(2))') 87 feed('zR') 88 89 n.poke_eventloop() 90 screen:expect([[ 91 aa | 92 bb | 93 cc | 94 last | 95 ^ | 96 foldlevel line3=2 | 97 1 | 98 | 99 ]]) 100 end) 101 102 it('foldmethod=syntax', function() 103 screen:try_resize(35, 15) 104 insert([[ 105 1 aa 106 2 bb 107 3 cc 108 4 dd {{{ 109 5 ee {{{ }}} 110 6 ff }}} 111 7 gg 112 8 hh 113 9 ii 114 a jj 115 b kk 116 last]]) 117 feed_command('set fdm=syntax fdl=0') 118 feed_command('syn region Hup start="dd" end="ii" fold contains=Fd1,Fd2,Fd3') 119 feed_command('syn region Fd1 start="ee" end="ff" fold contained') 120 feed_command('syn region Fd2 start="gg" end="hh" fold contained') 121 feed_command('syn region Fd3 start="commentstart" end="commentend" fold contained') 122 feed('Gzk') 123 feed_command('call append("$", "folding " . getline("."))') 124 feed('k') 125 feed_command('call append("$", getline("."))') 126 feed('jAcommentstart <esc>Acommentend<esc>') 127 feed_command('set fdl=1') 128 feed('3j') 129 feed_command('call append("$", getline("."))') 130 feed_command('set fdl=0') 131 feed('zO<C-L>j') -- <C-L> redraws screen 132 feed_command('call append("$", getline("."))') 133 feed_command('set fdl=0') 134 expect_any([[ 135 folding 9 ii 136 3 cc 137 9 ii 138 a jj]]) 139 end) 140 141 it('foldmethod=expression', function() 142 insert([[ 143 1 aa 144 2 bb 145 3 cc 146 4 dd {{{ 147 5 ee {{{ }}} 148 6 ff }}} 149 7 gg 150 8 hh 151 9 ii 152 a jj 153 b kk 154 last ]]) 155 156 feed_command([[ 157 fun Flvl() 158 let l = getline(v:lnum) 159 if l =~ "bb$" 160 return 2 161 elseif l =~ "gg$" 162 return "s1" 163 elseif l =~ "ii$" 164 return ">2" 165 elseif l =~ "kk$" 166 return "0" 167 endif 168 return "=" 169 endfun 170 ]]) 171 feed_command('set fdm=expr fde=Flvl()') 172 feed_command('/bb$') 173 feed_command('call append("$", "expr " . foldlevel("."))') 174 feed_command('/hh$') 175 feed_command('call append("$", foldlevel("."))') 176 feed_command('/ii$') 177 feed_command('call append("$", foldlevel("."))') 178 feed_command('/kk$') 179 feed_command('call append("$", foldlevel("."))') 180 181 expect_any([[ 182 expr 2 183 1 184 2 185 0]]) 186 end) 187 188 it('can be opened after :move', function() 189 -- luacheck: ignore 190 screen:try_resize(35, 8) 191 insert([[ 192 Test fdm=indent and :move bug END 193 line2 194 Test fdm=indent START 195 line3 196 line4]]) 197 feed_command('set noai nosta ') 198 feed_command('set fdm=indent') 199 feed_command('1m1') 200 feed('2jzc') 201 feed_command('m0') 202 feed('zR') 203 204 expect_any([[ 205 Test fdm=indent START 206 line3 207 line4 208 Test fdm=indent and :move bug END 209 line2]]) 210 end) 211 212 -- oldtest: Test_folds_with_rnu() 213 it('with relative line numbers', function() 214 command('set fdm=marker rnu foldcolumn=2') 215 command('call setline(1, ["{{{1", "nline 1", "{{{1", "line 2"])') 216 217 screen:expect([[ 218 {7:+ }{8: 0 }{13:^+-- 2 lines: ·························}| 219 {7:+ }{8: 1 }{13:+-- 2 lines: ·························}| 220 {1:~ }|*5 221 | 222 ]]) 223 feed('j') 224 screen:expect([[ 225 {7:+ }{8: 1 }{13:+-- 2 lines: ·························}| 226 {7:+ }{8: 0 }{13:^+-- 2 lines: ·························}| 227 {1:~ }|*5 228 | 229 ]]) 230 end) 231 232 -- oldtest: Test_foldclose_opt() 233 it('foldclose=all', function() 234 exec([[ 235 set foldmethod=manual foldclose=all foldopen=all 236 call setline(1, ['one', 'two', 'three', 'four']) 237 2,3fold 238 ]]) 239 240 screen:expect([[ 241 ^one | 242 {13:+-- 2 lines: two····························}| 243 four | 244 {1:~ }|*4 245 | 246 ]]) 247 feed('2G') 248 screen:expect([[ 249 one | 250 ^two | 251 three | 252 four | 253 {1:~ }|*3 254 | 255 ]]) 256 feed('4G') 257 screen:expect([[ 258 one | 259 {13:+-- 2 lines: two····························}| 260 ^four | 261 {1:~ }|*4 262 | 263 ]]) 264 feed('3G') 265 screen:expect([[ 266 one | 267 two | 268 ^three | 269 four | 270 {1:~ }|*3 271 | 272 ]]) 273 feed('1G') 274 screen:expect([[ 275 ^one | 276 {13:+-- 2 lines: two····························}| 277 four | 278 {1:~ }|*4 279 | 280 ]]) 281 feed('2G') 282 screen:expect([[ 283 one | 284 ^two | 285 three | 286 four | 287 {1:~ }|*3 288 | 289 ]]) 290 feed('k') 291 screen:expect([[ 292 ^one | 293 {13:+-- 2 lines: two····························}| 294 four | 295 {1:~ }|*4 296 | 297 ]]) 298 end) 299 300 -- oldtest: Test_foldtext_and_fillchars_rightleft() 301 it("fold text is displayed properly with 'rightleft'", function() 302 screen:try_resize(70, 5) 303 exec([[ 304 let longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing' 305 let g:multibyte = 'Lorem ipsum dolor sit amet' 306 307 call setline(1, [longtext, longtext, longtext]) 308 1,2fold 309 310 setlocal rightleft 311 set noshowmode noshowcmd 312 ]]) 313 screen:expect([[ 314 {13:······gnicsipida rutetcesnoc ,tema tis rolod muspi meroL :senil 2 --^+}| 315 gnicsipida rutetcesnoc ,tema tis rolod muspi meroL| 316 {1: ~}|*2 317 | 318 ]]) 319 command('call setline(1, [g:multibyte, g:multibyte, g:multibyte])') 320 screen:expect([[ 321 {13:········tema tis rolod muspi meroL :senil 2 -^-+}| 322 tema tis rolod muspi meroL| 323 {1: ~}|*2 324 | 325 ]]) 326 end) 327 end)