test_vartabs.vim (15554B)
1 " Test for variable tabstops 2 3 source check.vim 4 CheckFeature vartabs 5 6 source view_util.vim 7 8 func s:compare_lines(expect, actual) 9 call assert_equal(join(a:expect, "\n"), join(a:actual, "\n")) 10 endfunc 11 12 func Test_vartabs() 13 new 14 %d 15 16 " Test normal operation of tabstops ... 17 set ts=4 18 call setline(1, join(split('aaaaa', '\zs'), "\t")) 19 retab 8 20 let expect = "a a\<tab>a a\<tab>a" 21 call assert_equal(expect, getline(1)) 22 23 " ... and softtabstops 24 set ts=8 sts=6 25 exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b" 26 let expect = "b b\<tab> b\<tab> b\<tab>b" 27 call assert_equal(expect, getline(1)) 28 29 " Test variable tabstops. 30 set sts=0 vts=4,8,4,8 31 exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c" 32 retab 8 33 let expect = "c c\<tab> c\<tab>c\<tab>c\<tab>c" 34 call assert_equal(expect, getline(1)) 35 36 set et vts=4,8,4,8 37 exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d" 38 let expect = "d d d d d d" 39 call assert_equal(expect, getline(1)) 40 41 " Changing ts should have no effect if vts is in use. 42 call cursor(1, 1) 43 set ts=6 44 exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e" 45 let expect = "e e e e e e" 46 call assert_equal(expect, getline(1)) 47 48 " Clearing vts should revert to using ts. 49 set vts= 50 exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f" 51 let expect = "f f f f f f" 52 call assert_equal(expect, getline(1)) 53 54 " Test variable softtabstops. 55 set noet ts=8 vsts=12,2,6 56 exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g" 57 let expect = "g\<tab> g g\<tab> g\<tab> g\<tab>g" 58 call assert_equal(expect, getline(1)) 59 60 " Variable tabstops and softtabstops combined. 61 set vsts=6,12,8 vts=4,6,8 62 exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h" 63 let expect = "h\<tab> h\<tab>\<tab>h\<tab>h\<tab>h" 64 call assert_equal(expect, getline(1)) 65 66 " Retab with a single value, not using vts. 67 set ts=8 sts=0 vts= vsts= 68 exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i" 69 retab 4 70 let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i" 71 call assert_equal(expect, getline(1)) 72 73 " Retab with a single value, using vts. 74 set ts=8 sts=0 vts=6 vsts= 75 exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j" 76 retab 4 77 let expect = "j\<tab> j\<tab>\<tab>j\<tab> j\<tab>\<tab>j" 78 call assert_equal(expect, getline(1)) 79 80 " Retab with multiple values, not using vts. 81 set ts=6 sts=0 vts= vsts= 82 exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k" 83 retab 4,8 84 let expect = "k\<tab> k\<tab>k k\<tab> k\<tab> k" 85 call assert_equal(expect, getline(1)) 86 87 " Retab with multiple values, using vts. 88 set ts=8 sts=0 vts=6 vsts= 89 exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l" 90 retab 4,8 91 let expect = "l\<tab> l\<tab>l l\<tab> l\<tab> l" 92 call assert_equal(expect, getline(1)) 93 94 " Test for 'retab' with vts 95 set ts=8 sts=0 vts=5,3,6,2 vsts= 96 exe "norm! S l" 97 .retab! 98 call assert_equal("\t\t\t\tl", getline(1)) 99 100 " Test for 'retab' with same values as vts 101 set ts=8 sts=0 vts=5,3,6,2 vsts= 102 exe "norm! S l" 103 .retab! 5,3,6,2 104 call assert_equal("\t\t\t\tl", getline(1)) 105 106 " Check that global and local values are set. 107 set ts=4 vts=6 sts=8 vsts=10 108 call assert_equal(&ts, 4) 109 call assert_equal(&vts, '6') 110 call assert_equal(&sts, 8) 111 call assert_equal(&vsts, '10') 112 new 113 call assert_equal(&ts, 4) 114 call assert_equal(&vts, '6') 115 call assert_equal(&sts, 8) 116 call assert_equal(&vsts, '10') 117 bwipeout! 118 119 " Check that local values only are set. 120 setlocal ts=5 vts=7 sts=9 vsts=11 121 call assert_equal(&ts, 5) 122 call assert_equal(&vts, '7') 123 call assert_equal(&sts, 9) 124 call assert_equal(&vsts, '11') 125 new 126 call assert_equal(&ts, 4) 127 call assert_equal(&vts, '6') 128 call assert_equal(&sts, 8) 129 call assert_equal(&vsts, '10') 130 bwipeout! 131 132 " Check that global values only are set. 133 setglobal ts=6 vts=8 sts=10 vsts=12 134 call assert_equal(&ts, 5) 135 call assert_equal(&vts, '7') 136 call assert_equal(&sts, 9) 137 call assert_equal(&vsts, '11') 138 new 139 call assert_equal(&ts, 6) 140 call assert_equal(&vts, '8') 141 call assert_equal(&sts, 10) 142 call assert_equal(&vsts, '12') 143 bwipeout! 144 145 set ts& vts& sts& vsts& et& 146 bwipeout! 147 endfunc 148 149 func Test_retab_invalid_arg() 150 new 151 call setline(1, "\ttext") 152 retab 0 153 call assert_fails("retab -8", 'E487: Argument must be positive') 154 call assert_fails("retab 10000", 'E475:') 155 call assert_fails("retab 720575940379279360", 'E475:') 156 bwipe! 157 endfunc 158 159 func Test_vartabs_breakindent() 160 if !exists("+breakindent") 161 return 162 endif 163 new 164 %d 165 166 " Test normal operation of tabstops ... 167 set ts=4 168 call setline(1, join(split('aaaaa', '\zs'), "\t")) 169 retab 8 170 let expect = "a a\<tab>a a\<tab>a" 171 call assert_equal(expect, getline(1)) 172 173 " ... and softtabstops 174 set ts=8 sts=6 175 exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b" 176 let expect = "b b\<tab> b\<tab> b\<tab>b" 177 call assert_equal(expect, getline(1)) 178 179 " Test variable tabstops. 180 set sts=0 vts=4,8,4,8 181 exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c" 182 retab 8 183 let expect = "c c\<tab> c\<tab>c\<tab>c\<tab>c" 184 call assert_equal(expect, getline(1)) 185 186 set et vts=4,8,4,8 187 exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d" 188 let expect = "d d d d d d" 189 call assert_equal(expect, getline(1)) 190 191 " Changing ts should have no effect if vts is in use. 192 call cursor(1, 1) 193 set ts=6 194 exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e" 195 let expect = "e e e e e e" 196 call assert_equal(expect, getline(1)) 197 198 " Clearing vts should revert to using ts. 199 set vts= 200 exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f" 201 let expect = "f f f f f f" 202 call assert_equal(expect, getline(1)) 203 204 " Test variable softtabstops. 205 set noet ts=8 vsts=12,2,6 206 exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g" 207 let expect = "g\<tab> g g\<tab> g\<tab> g\<tab>g" 208 call assert_equal(expect, getline(1)) 209 210 " Variable tabstops and softtabstops combined. 211 set vsts=6,12,8 vts=4,6,8 212 exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h" 213 let expect = "h\<tab> h\<tab>\<tab>h\<tab>h\<tab>h" 214 call assert_equal(expect, getline(1)) 215 216 " Retab with a single value, not using vts. 217 set ts=8 sts=0 vts= vsts= 218 exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i" 219 retab 4 220 let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i" 221 call assert_equal(expect, getline(1)) 222 223 " Retab with a single value, using vts. 224 set ts=8 sts=0 vts=6 vsts= 225 exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j" 226 retab 4 227 let expect = "j\<tab> j\<tab>\<tab>j\<tab> j\<tab>\<tab>j" 228 call assert_equal(expect, getline(1)) 229 230 " Retab with multiple values, not using vts. 231 set ts=6 sts=0 vts= vsts= 232 exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k" 233 retab 4,8 234 let expect = "k\<tab> k\<tab>k k\<tab> k\<tab> k" 235 call assert_equal(expect, getline(1)) 236 237 " Retab with multiple values, using vts. 238 set ts=8 sts=0 vts=6 vsts= 239 exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l" 240 retab 4,8 241 let expect = "l\<tab> l\<tab>l l\<tab> l\<tab> l" 242 call assert_equal(expect, getline(1)) 243 244 " Check that global and local values are set. 245 set ts=4 vts=6 sts=8 vsts=10 246 call assert_equal(&ts, 4) 247 call assert_equal(&vts, '6') 248 call assert_equal(&sts, 8) 249 call assert_equal(&vsts, '10') 250 new 251 call assert_equal(&ts, 4) 252 call assert_equal(&vts, '6') 253 call assert_equal(&sts, 8) 254 call assert_equal(&vsts, '10') 255 bwipeout! 256 257 " Check that local values only are set. 258 setlocal ts=5 vts=7 sts=9 vsts=11 259 call assert_equal(&ts, 5) 260 call assert_equal(&vts, '7') 261 call assert_equal(&sts, 9) 262 call assert_equal(&vsts, '11') 263 new 264 call assert_equal(&ts, 4) 265 call assert_equal(&vts, '6') 266 call assert_equal(&sts, 8) 267 call assert_equal(&vsts, '10') 268 bwipeout! 269 270 " Check that global values only are set. 271 setglobal ts=6 vts=8 sts=10 vsts=12 272 call assert_equal(&ts, 5) 273 call assert_equal(&vts, '7') 274 call assert_equal(&sts, 9) 275 call assert_equal(&vsts, '11') 276 new 277 call assert_equal(&ts, 6) 278 call assert_equal(&vts, '8') 279 call assert_equal(&sts, 10) 280 call assert_equal(&vsts, '12') 281 bwipeout! 282 283 bwipeout! 284 endfunc 285 286 func Test_vartabs_linebreak() 287 if winwidth(0) < 40 288 return 289 endif 290 new 291 40vnew 292 %d 293 setl linebreak vartabstop=10,20,30,40 294 call setline(1, "\tx\tx\tx\tx") 295 296 let expect = [' x ', 297 \ 'x x ', 298 \ 'x '] 299 let lines = ScreenLines([1, 3], winwidth(0)) 300 call s:compare_lines(expect, lines) 301 setl list listchars=tab:>- 302 let expect = ['>---------x>------------------ ', 303 \ 'x>------------------x>------------------', 304 \ 'x '] 305 let lines = ScreenLines([1, 3], winwidth(0)) 306 call s:compare_lines(expect, lines) 307 setl linebreak vartabstop=40 308 let expect = ['>---------------------------------------', 309 \ 'x>--------------------------------------', 310 \ 'x>--------------------------------------', 311 \ 'x>--------------------------------------', 312 \ 'x '] 313 let lines = ScreenLines([1, 5], winwidth(0)) 314 call s:compare_lines(expect, lines) 315 316 " cleanup 317 bw! 318 bw! 319 set nolist listchars&vim 320 endfunc 321 322 func Test_vartabs_shiftwidth() 323 "return 324 if winwidth(0) < 40 325 return 326 endif 327 new 328 40vnew 329 %d 330 " setl varsofttabstop=10,20,30,40 331 setl shiftwidth=0 vartabstop=10,20,30,40 332 call setline(1, "x") 333 334 " Check without any change. 335 let expect = ['x '] 336 let lines = ScreenLines(1, winwidth(0)) 337 call s:compare_lines(expect, lines) 338 " Test 1: 339 " shiftwidth depends on the indent, first check with cursor at the end of the 340 " line (which is the same as the start of the line, since there is only one 341 " character). 342 norm! $>> 343 let expect1 = [' x '] 344 let lines = ScreenLines(1, winwidth(0)) 345 call s:compare_lines(expect1, lines) 346 call assert_equal(10, shiftwidth()) 347 call assert_equal(10, shiftwidth(1)) 348 call assert_equal(20, shiftwidth(virtcol('.'))) 349 norm! $>> 350 let expect2 = [' x ', '~ '] 351 let lines = ScreenLines([1, 2], winwidth(0)) 352 call s:compare_lines(expect2, lines) 353 call assert_equal(20, shiftwidth(virtcol('.')-2)) 354 call assert_equal(30, virtcol('.')->shiftwidth()) 355 norm! $>> 356 let expect3 = [' ', ' x ', '~ '] 357 let lines = ScreenLines([1, 3], winwidth(0)) 358 call s:compare_lines(expect3, lines) 359 call assert_equal(30, shiftwidth(virtcol('.')-2)) 360 call assert_equal(40, shiftwidth(virtcol('.'))) 361 norm! $>> 362 let expect4 = [' ', ' ', ' x '] 363 let lines = ScreenLines([1, 3], winwidth(0)) 364 call assert_equal(40, shiftwidth(virtcol('.'))) 365 call s:compare_lines(expect4, lines) 366 367 " Test 2: Put the cursor at the first column, result should be the same 368 call setline(1, "x") 369 norm! 0>> 370 let lines = ScreenLines(1, winwidth(0)) 371 call s:compare_lines(expect1, lines) 372 norm! 0>> 373 let lines = ScreenLines([1, 2], winwidth(0)) 374 call s:compare_lines(expect2, lines) 375 norm! 0>> 376 let lines = ScreenLines([1, 3], winwidth(0)) 377 call s:compare_lines(expect3, lines) 378 norm! 0>> 379 let lines = ScreenLines([1, 3], winwidth(0)) 380 call s:compare_lines(expect4, lines) 381 382 call assert_fails('call shiftwidth([])', 'E745:') 383 384 " cleanup 385 bw! 386 bw! 387 endfunc 388 389 func Test_vartabs_failures() 390 call assert_fails('set vts=8,', 'E475: Invalid argument: 8,') 391 call assert_fails('set vsts=8,', 'E475: Invalid argument: 8,') 392 call assert_fails('set vts=8,,8', 'E474: Invalid argument: vts=8,,8') 393 call assert_fails('set vsts=8,,8', 'E474: Invalid argument: vsts=8,,8') 394 call assert_fails('set vts=8,,8,', 'E474: Invalid argument: vts=8,,8,') 395 call assert_fails('set vsts=8,,8,', 'E474: Invalid argument: vsts=8,,8,') 396 call assert_fails('set vts=,8', 'E474: Invalid argument: vts=,8') 397 call assert_fails('set vsts=,8', 'E474: Invalid argument: vsts=,8') 398 endfunc 399 400 func Test_vartabs_reset() 401 set vts=8 402 set all& 403 call assert_equal('', &vts) 404 endfunc 405 406 func s:SaveCol(l) 407 call add(a:l, [col('.'), virtcol('.')]) 408 return '' 409 endfunc 410 411 " Test for 'varsofttabstop' 412 func Test_varsofttabstop() 413 new 414 inoremap <expr> <F2> s:SaveCol(g:cols) 415 416 set backspace=indent,eol,start 417 set varsofttabstop=6,2,5,3 418 let g:cols = [] 419 call feedkeys("a\t\<F2>\t\<F2>\t\<F2>\t\<F2> ", 'xt') 420 call assert_equal("\t\t ", getline(1)) 421 call assert_equal([[7, 7], [2, 9], [7, 14], [3, 17]], g:cols) 422 423 let g:cols = [] 424 call feedkeys("a\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>", 'xt') 425 call assert_equal('', getline(1)) 426 call assert_equal([[3, 17], [7, 14], [2, 9], [7, 7], [1, 1]], g:cols) 427 428 set varsofttabstop& 429 set backspace& 430 iunmap <F2> 431 close! 432 endfunc 433 434 " Setting 'shiftwidth' to a negative value, should set it to either the value 435 " of 'tabstop' (if 'vartabstop' is not set) or to the first value in 436 " 'vartabstop' 437 func Test_shiftwidth_vartabstop() 438 throw 'Skipped: Nvim removed this behavior in #6377' 439 setlocal tabstop=7 vartabstop= 440 call assert_fails('set shiftwidth=-1', 'E487:') 441 call assert_equal(7, &shiftwidth) 442 setlocal tabstop=7 vartabstop=5,7,10 443 call assert_fails('set shiftwidth=-1', 'E487:') 444 call assert_equal(5, &shiftwidth) 445 setlocal shiftwidth& vartabstop& tabstop& 446 endfunc 447 448 func Test_vartabstop_latin1() 449 throw "Skipped: Nvim does not support 'compatible'" 450 let save_encoding = &encoding 451 new 452 set encoding=iso8859-1 453 set compatible linebreak list revins smarttab 454 set vartabstop=400 455 exe "norm i00\t\<C-D>" 456 bwipe! 457 let &encoding = save_encoding 458 set nocompatible linebreak& list& revins& smarttab& vartabstop& 459 endfunc 460 461 " Verify that right-shifting and left-shifting adjust lines to the proper 462 " tabstops. 463 func Test_vartabstop_shift_right_left() 464 new 465 set expandtab 466 set shiftwidth=0 467 set vartabstop=17,11,7 468 exe "norm! aword" 469 let expect = "word" 470 call assert_equal(expect, getline(1)) 471 472 " Shift to first tabstop. 473 norm! >> 474 let expect = " word" 475 call assert_equal(expect, getline(1)) 476 477 " Shift to second tabstop. 478 norm! >> 479 let expect = " word" 480 call assert_equal(expect, getline(1)) 481 482 " Shift to third tabstop. 483 norm! >> 484 let expect = " word" 485 call assert_equal(expect, getline(1)) 486 487 " Shift to fourth tabstop, repeating the third shift width. 488 norm! >> 489 let expect = " word" 490 call assert_equal(expect, getline(1)) 491 492 " Shift back to the third tabstop. 493 norm! << 494 let expect = " word" 495 call assert_equal(expect, getline(1)) 496 497 " Shift back to the second tabstop. 498 norm! << 499 let expect = " word" 500 call assert_equal(expect, getline(1)) 501 502 " Shift back to the first tabstop. 503 norm! << 504 let expect = " word" 505 call assert_equal(expect, getline(1)) 506 507 " Shift back to the left margin. 508 norm! << 509 let expect = "word" 510 call assert_equal(expect, getline(1)) 511 512 " Shift again back to the left margin. 513 norm! << 514 let expect = "word" 515 call assert_equal(expect, getline(1)) 516 517 bwipeout! 518 endfunc 519 520 521 " vim: shiftwidth=2 sts=2 expandtab