1" Test for variable tabstops 2 3source check.vim 4CheckFeature vartabs 5 6source view_util.vim 7 8func s:compare_lines(expect, actual) 9 call assert_equal(join(a:expect, "\n"), join(a:actual, "\n")) 10endfunc 11 12func 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 vlaues 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! 147endfunc 148 149func Test_vartabs_breakindent() 150 if !exists("+breakindent") 151 return 152 endif 153 new 154 %d 155 156 " Test normal operation of tabstops ... 157 set ts=4 158 call setline(1, join(split('aaaaa', '\zs'), "\t")) 159 retab 8 160 let expect = "a a\<tab>a a\<tab>a" 161 call assert_equal(expect, getline(1)) 162 163 " ... and softtabstops 164 set ts=8 sts=6 165 exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b" 166 let expect = "b b\<tab> b\<tab> b\<tab>b" 167 call assert_equal(expect, getline(1)) 168 169 " Test variable tabstops. 170 set sts=0 vts=4,8,4,8 171 exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c" 172 retab 8 173 let expect = "c c\<tab> c\<tab>c\<tab>c\<tab>c" 174 call assert_equal(expect, getline(1)) 175 176 set et vts=4,8,4,8 177 exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d" 178 let expect = "d d d d d d" 179 call assert_equal(expect, getline(1)) 180 181 " Changing ts should have no effect if vts is in use. 182 call cursor(1, 1) 183 set ts=6 184 exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e" 185 let expect = "e e e e e e" 186 call assert_equal(expect, getline(1)) 187 188 " Clearing vts should revert to using ts. 189 set vts= 190 exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f" 191 let expect = "f f f f f f" 192 call assert_equal(expect, getline(1)) 193 194 " Test variable softtabstops. 195 set noet ts=8 vsts=12,2,6 196 exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g" 197 let expect = "g\<tab> g g\<tab> g\<tab> g\<tab>g" 198 call assert_equal(expect, getline(1)) 199 200 " Variable tabstops and softtabstops combined. 201 set vsts=6,12,8 vts=4,6,8 202 exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h" 203 let expect = "h\<tab> h\<tab>\<tab>h\<tab>h\<tab>h" 204 call assert_equal(expect, getline(1)) 205 206 " Retab with a single value, not using vts. 207 set ts=8 sts=0 vts= vsts= 208 exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i" 209 retab 4 210 let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i" 211 call assert_equal(expect, getline(1)) 212 213 " Retab with a single value, using vts. 214 set ts=8 sts=0 vts=6 vsts= 215 exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j" 216 retab 4 217 let expect = "j\<tab> j\<tab>\<tab>j\<tab> j\<tab>\<tab>j" 218 call assert_equal(expect, getline(1)) 219 220 " Retab with multiple values, not using vts. 221 set ts=6 sts=0 vts= vsts= 222 exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k" 223 retab 4,8 224 let expect = "k\<tab> k\<tab>k k\<tab> k\<tab> k" 225 call assert_equal(expect, getline(1)) 226 227 " Retab with multiple values, using vts. 228 set ts=8 sts=0 vts=6 vsts= 229 exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l" 230 retab 4,8 231 let expect = "l\<tab> l\<tab>l l\<tab> l\<tab> l" 232 call assert_equal(expect, getline(1)) 233 234 " Check that global and local values are set. 235 set ts=4 vts=6 sts=8 vsts=10 236 call assert_equal(&ts, 4) 237 call assert_equal(&vts, '6') 238 call assert_equal(&sts, 8) 239 call assert_equal(&vsts, '10') 240 new 241 call assert_equal(&ts, 4) 242 call assert_equal(&vts, '6') 243 call assert_equal(&sts, 8) 244 call assert_equal(&vsts, '10') 245 bwipeout! 246 247 " Check that local values only are set. 248 setlocal ts=5 vts=7 sts=9 vsts=11 249 call assert_equal(&ts, 5) 250 call assert_equal(&vts, '7') 251 call assert_equal(&sts, 9) 252 call assert_equal(&vsts, '11') 253 new 254 call assert_equal(&ts, 4) 255 call assert_equal(&vts, '6') 256 call assert_equal(&sts, 8) 257 call assert_equal(&vsts, '10') 258 bwipeout! 259 260 " Check that global values only are set. 261 setglobal ts=6 vts=8 sts=10 vsts=12 262 call assert_equal(&ts, 5) 263 call assert_equal(&vts, '7') 264 call assert_equal(&sts, 9) 265 call assert_equal(&vsts, '11') 266 new 267 call assert_equal(&ts, 6) 268 call assert_equal(&vts, '8') 269 call assert_equal(&sts, 10) 270 call assert_equal(&vsts, '12') 271 bwipeout! 272 273 bwipeout! 274endfunc 275 276func Test_vartabs_linebreak() 277 if winwidth(0) < 40 278 return 279 endif 280 new 281 40vnew 282 %d 283 setl linebreak vartabstop=10,20,30,40 284 call setline(1, "\tx\tx\tx\tx") 285 286 let expect = [' x ', 287 \ 'x x ', 288 \ 'x '] 289 let lines = ScreenLines([1, 3], winwidth(0)) 290 call s:compare_lines(expect, lines) 291 setl list listchars=tab:>- 292 let expect = ['>---------x>------------------ ', 293 \ 'x>------------------x>------------------', 294 \ 'x '] 295 let lines = ScreenLines([1, 3], winwidth(0)) 296 call s:compare_lines(expect, lines) 297 setl linebreak vartabstop=40 298 let expect = ['>---------------------------------------', 299 \ 'x>--------------------------------------', 300 \ 'x>--------------------------------------', 301 \ 'x>--------------------------------------', 302 \ 'x '] 303 let lines = ScreenLines([1, 5], winwidth(0)) 304 call s:compare_lines(expect, lines) 305 306 " cleanup 307 bw! 308 bw! 309 set nolist listchars&vim 310endfunc 311 312func Test_vartabs_shiftwidth() 313 "return 314 if winwidth(0) < 40 315 return 316 endif 317 new 318 40vnew 319 %d 320" setl varsofttabstop=10,20,30,40 321 setl shiftwidth=0 vartabstop=10,20,30,40 322 call setline(1, "x") 323 324 " Check without any change. 325 let expect = ['x '] 326 let lines = ScreenLines(1, winwidth(0)) 327 call s:compare_lines(expect, lines) 328 " Test 1: 329 " shiftwidth depends on the indent, first check with cursor at the end of the 330 " line (which is the same as the start of the line, since there is only one 331 " character). 332 norm! $>> 333 let expect1 = [' x '] 334 let lines = ScreenLines(1, winwidth(0)) 335 call s:compare_lines(expect1, lines) 336 call assert_equal(10, shiftwidth()) 337 call assert_equal(10, shiftwidth(1)) 338 call assert_equal(20, shiftwidth(virtcol('.'))) 339 norm! $>> 340 let expect2 = [' x ', '~ '] 341 let lines = ScreenLines([1, 2], winwidth(0)) 342 call s:compare_lines(expect2, lines) 343 call assert_equal(20, shiftwidth(virtcol('.')-2)) 344 call assert_equal(30, virtcol('.')->shiftwidth()) 345 norm! $>> 346 let expect3 = [' ', ' x ', '~ '] 347 let lines = ScreenLines([1, 3], winwidth(0)) 348 call s:compare_lines(expect3, lines) 349 call assert_equal(30, shiftwidth(virtcol('.')-2)) 350 call assert_equal(40, shiftwidth(virtcol('.'))) 351 norm! $>> 352 let expect4 = [' ', ' ', ' x '] 353 let lines = ScreenLines([1, 3], winwidth(0)) 354 call assert_equal(40, shiftwidth(virtcol('.'))) 355 call s:compare_lines(expect4, lines) 356 357 " Test 2: Put the cursor at the first column, result should be the same 358 call setline(1, "x") 359 norm! 0>> 360 let lines = ScreenLines(1, winwidth(0)) 361 call s:compare_lines(expect1, lines) 362 norm! 0>> 363 let lines = ScreenLines([1, 2], winwidth(0)) 364 call s:compare_lines(expect2, lines) 365 norm! 0>> 366 let lines = ScreenLines([1, 3], winwidth(0)) 367 call s:compare_lines(expect3, lines) 368 norm! 0>> 369 let lines = ScreenLines([1, 3], winwidth(0)) 370 call s:compare_lines(expect4, lines) 371 372 call assert_fails('call shiftwidth([])', 'E745:') 373 374 " cleanup 375 bw! 376 bw! 377endfunc 378 379func Test_vartabs_failures() 380 call assert_fails('set vts=8,') 381 call assert_fails('set vsts=8,') 382 call assert_fails('set vts=8,,8') 383 call assert_fails('set vsts=8,,8') 384 call assert_fails('set vts=8,,8,') 385 call assert_fails('set vsts=8,,8,') 386 call assert_fails('set vts=,8') 387 call assert_fails('set vsts=,8') 388endfunc 389 390func Test_vartabs_reset() 391 set vts=8 392 set all& 393 call assert_equal('', &vts) 394endfunc 395 396func s:SaveCol(l) 397 call add(a:l, [col('.'), virtcol('.')]) 398 return '' 399endfunc 400 401" Test for 'varsofttabstop' 402func Test_varsofttabstop() 403 new 404 inoremap <expr> <F2> s:SaveCol(g:cols) 405 406 set backspace=indent,eol,start 407 set varsofttabstop=6,2,5,3 408 let g:cols = [] 409 call feedkeys("a\t\<F2>\t\<F2>\t\<F2>\t\<F2> ", 'xt') 410 call assert_equal("\t\t ", getline(1)) 411 call assert_equal([[7, 7], [2, 9], [7, 14], [3, 17]], g:cols) 412 413 let g:cols = [] 414 call feedkeys("a\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>", 'xt') 415 call assert_equal('', getline(1)) 416 call assert_equal([[3, 17], [7, 14], [2, 9], [7, 7], [1, 1]], g:cols) 417 418 set varsofttabstop& 419 set backspace& 420 iunmap <F2> 421 close! 422endfunc 423 424" vim: shiftwidth=2 sts=2 expandtab 425