robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
51d59b2244
master
1" Tests for cursor() and other functions that get/set the cursor position 2 3source check.vim 4 5func Test_wrong_arguments () 6call assert_fails ( 'call cursor(1. 3)' , 'E474:' ) 7call assert_fails ( 'call cursor(v:_null_list)' , 'E474:' ) 8endfunc 9 10func Test_move_cursor () 11new 12call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' , 'ddd' ]) 13 14call cursor ([ 1 , 1 , 0 , 1 ]) 15call assert_equal ([ 1 , 1 , 0 , 1 ], getcurpos ()[ 1 :]) 16call cursor ([ 4 , 3 , 0 , 3 ]) 17call assert_equal ([ 4 , 3 , 0 , 3 ], getcurpos ()[ 1 :]) 18 19call cursor ( 2 , 2 ) 20call assert_equal ([ 2 , 2 , 0 , 2 ], getcurpos ()[ 1 :]) 21" line number zero keeps the line number 22call cursor ( 0 , 1 ) 23call assert_equal ([ 2 , 1 , 0 , 1 ], getcurpos ()[ 1 :]) 24" col number zero keeps the column 25call cursor ( 3 , 0 ) 26call assert_equal ([ 3 , 1 , 0 , 1 ], getcurpos ()[ 1 :]) 27" below last line goes to last line 28eval [ 9 , 1 ] ->cursor () 29call assert_equal ([ 4 , 1 , 0 , 1 ], getcurpos ()[ 1 :]) 30" pass string arguments 31call cursor ( '3' , '3' ) 32call assert_equal ([ 3 , 3 , 0 , 3 ], getcurpos ()[ 1 :]) 33 34call setline ( 1 , [ "\<TAB>" ]) 35call cursor ( 1 , 1 , 1 ) 36call assert_equal ([ 1 , 1 , 1 ], getcurpos ()[ 1 : 3 ]) 37 38call assert_fails ( 'call cursor(-1, -1)' , 'E475:' ) 39 40quit ! 41endfunc 42 43func Test_curswant_maxcol () 44new 45call setline ( 1 , 'foo' ) 46 47" Test that after "$" command curswant is set to the same value as v:maxcol. 48normal ! 1G$ 49call assert_equal ( v: maxcol , getcurpos ()[ 4 ]) 50call assert_equal ( v: maxcol , winsaveview (). curswant ) 51 52quit ! 53endfunc 54 55" Very short version of what matchparen does. 56function s: Highlight_Matching_Pair () 57let save_cursor = getcurpos () 58eval save_cursor ->setpos ( '.' ) 59endfunc 60 61func Test_curswant_with_autocommand () 62new 63call setline ( 1 , [ 'func()' , '{' , '}' , '----' ]) 64autocmd ! CursorMovedI * call s: Highlight_Matching_Pair () 65exe "normal! 3Ga\<Down>X\<Esc>" 66call assert_equal ( '-X---' , getline ( 4 )) 67autocmd ! CursorMovedI * 68quit ! 69endfunc 70 71" Tests for behavior of curswant with cursorcolumn/line 72func Test_curswant_with_cursorcolumn () 73new 74call setline ( 1 , [ '01234567' , '' ]) 75exe "normal! ggf6j" 76call assert_equal ( 6 , winsaveview (). curswant ) 77set cursorcolumn 78call assert_equal ( 6 , winsaveview (). curswant ) 79quit ! 80endfunc 81 82func Test_curswant_with_cursorline () 83new 84call setline ( 1 , [ '01234567' , '' ]) 85exe "normal! ggf6j" 86call assert_equal ( 6 , winsaveview (). curswant ) 87set cursorline 88call assert_equal ( 6 , winsaveview (). curswant ) 89quit ! 90endfunc 91 92func Test_screenpos () 93if has ( 'gui_running' ) 94set lines = 25 95set columns = 78 96endif 97rightbelow new 98rightbelow 20 vsplit 99call setline ( 1 , [ "\tsome text" , "long wrapping line here" , "next line" ]) 100redraw 101let winid = win_getid () 102let [ winrow , wincol ] = win_screenpos ( winid ) 103call assert_equal ({ 'row' : winrow , 104 \'col' : wincol + 0 , 105 \'curscol' : wincol + 7 , 106 \'endcol' : wincol + 7 }, winid ->screenpos ( 1 , 1 )) 107call assert_equal ({ 'row' : winrow , 108 \'col' : wincol + 13 , 109 \'curscol' : wincol + 13 , 110 \'endcol' : wincol + 13 }, winid ->screenpos ( 1 , 7 )) 111call assert_equal ({ 'row' : winrow + 2 , 112 \'col' : wincol + 1 , 113 \'curscol' : wincol + 1 , 114 \'endcol' : wincol + 1 }, screenpos ( winid , 2 , 22 )) 115setlocal number 116call assert_equal ({ 'row' : winrow + 3 , 117 \'col' : wincol + 9 , 118 \'curscol' : wincol + 9 , 119 \'endcol' : wincol + 9 }, screenpos ( winid , 2 , 22 )) 120 121let wininfo = getwininfo ( winid )[ 0 ] 122call setline ( 3 , [ 'x' ] ->repeat ( wininfo . height )) 123call setline ( line ( '$' ) + 1 , 'x' ->repeat ( wininfo . width * 3 )) 124setlocal nonumber display = lastlineso = 0 125exe "normal G\<C-Y>\<C-Y>" 126redraw 127let winbar_height = get ( wininfo , 'winbar' , 0 ) 128call assert_equal ({ 'row' : winrow + winheight ( winid ) - 1 + winbar_height , 129 \'col' : wincol + 7 , 130 \'curscol' : wincol + 7 , 131 \'endcol' : wincol + 7 }, winid ->screenpos ( line ( '$' ), 8 )) 132call assert_equal ({ 'row' : 0 , 'col' : 0 , 'curscol' : 0 , 'endcol' : 0 }, 133 \winid ->screenpos ( line ( '$' ), 22 )) 134 1351 split 136 137" w_leftcol should be subtracted 138setlocal nowrap 139normal G050zl$ 140redraw 141call assert_equal ({ 'row' : winrow + 0 , 142 \'col' : wincol + 10 - 1 , 143 \'curscol' : wincol + 10 - 1 , 144 \'endcol' : wincol + 10 - 1 }, 145 \screenpos ( win_getid (), line ( '.' ), col ( '.' ))) 146 147" w_skipcol should be taken into account 148setlocal wrap 149normal $ 150redraw 151call assert_equal ({ 'row' : winrow + 0 , 152 \'col' : wincol + 20 - 1 , 153 \'curscol' : wincol + 20 - 1 , 154 \'endcol' : wincol + 20 - 1 }, 155 \screenpos ( win_getid (), line ( '.' ), col ( '.' ))) 156call assert_equal ({ 'row' : 0 , 'col' : 0 , 'curscol' : 0 , 'endcol' : 0 }, 157 \screenpos ( win_getid (), line ( '.' ), col ( '.' ) - 20 )) 158setlocal number 159redraw 160call assert_equal ({ 'row' : winrow + 0 , 161 \'col' : wincol + 16 - 1 , 162 \'curscol' : wincol + 16 - 1 , 163 \'endcol' : wincol + 16 - 1 }, 164 \screenpos ( win_getid (), line ( '.' ), col ( '.' ))) 165call assert_equal ({ 'row' : 0 , 'col' : 0 , 'curscol' : 0 , 'endcol' : 0 }, 166 \screenpos ( win_getid (), line ( '.' ), col ( '.' ) - 16 )) 167set cpoptions += n 168redraw 169call assert_equal ({ 'row' : winrow + 0 , 170 \'col' : wincol + 4 - 1 , 171 \'curscol' : wincol + 4 - 1 , 172 \'endcol' : wincol + 4 - 1 }, 173 \screenpos ( win_getid (), line ( '.' ), col ( '.' ))) 174call assert_equal ({ 'row' : 0 , 'col' : 0 , 'curscol' : 0 , 'endcol' : 0 }, 175 \screenpos ( win_getid (), line ( '.' ), col ( '.' ) - 4 )) 176 177wincmd + 178call setline ( line ( '$' ) + 1 , 'last line' ) 179setlocal smoothscroll 180normal G$ 181redraw 182call assert_equal ({ 'row' : winrow + 1 , 183 \'col' : wincol + 4 + 9 - 1 , 184 \'curscol' : wincol + 4 + 9 - 1 , 185 \'endcol' : wincol + 4 + 9 - 1 }, 186 \screenpos ( win_getid (), line ( '.' ), col ( '.' ))) 187set cpoptions -= n 188redraw 189call assert_equal ({ 'row' : winrow + 1 , 190 \'col' : wincol + 4 + 9 - 1 , 191 \'curscol' : wincol + 4 + 9 - 1 , 192 \'endcol' : wincol + 4 + 9 - 1 }, 193 \screenpos ( win_getid (), line ( '.' ), col ( '.' ))) 194setlocal nonumber 195redraw 196call assert_equal ({ 'row' : winrow + 1 , 197 \'col' : wincol + 9 - 1 , 198 \'curscol' : wincol + 9 - 1 , 199 \'endcol' : wincol + 9 - 1 }, 200 \screenpos ( win_getid (), line ( '.' ), col ( '.' ))) 201 202close 203call assert_equal ({}, screenpos ( 999 , 1 , 1 )) 204 205bwipe ! 206set display & 207 208call assert_equal (#{ col : 1 , row : 1 , endcol : 1 , curscol : 1 }, screenpos ( win_getid (), 1 , 1 )) 209" nmenu WinBar.TEST : 210setlocal winbar = TEST 211call assert_equal (#{ col : 1 , row : 2 , endcol : 1 , curscol : 1 }, screenpos ( win_getid (), 1 , 1 )) 212" nunmenu WinBar.TEST 213setlocal winbar & 214call assert_equal (#{ col : 1 , row : 1 , endcol : 1 , curscol : 1 }, screenpos ( win_getid (), 1 , 1 )) 215 216call assert_equal (#{ col : 0 , row : 0 , endcol : 0 , curscol : 0 }, screenpos ( 0 , 0 , 1 )) 217call assert_equal (#{ col : 0 , row : 0 , endcol : 0 , curscol : 0 }, screenpos ( 0 , -1 , 1 )) 218call assert_equal (#{ col : 1 , row : 1 , endcol : 1 , curscol : 1 }, screenpos ( 0 , 1 , - v: maxcol )) 219endfunc 220 221func Test_screenpos_fold () 222CheckFeature folding 223 224enew ! 225call setline ( 1 , range ( 10 )) 2263 , 5 fold 227redraw 228call assert_equal ( 2 , screenpos ( 1 , 2 , 1 ). row ) 229call assert_equal (#{ col : 1 , row : 3 , endcol : 1 , curscol : 1 }, screenpos ( 1 , 3 , 1 )) 230call assert_equal (#{ col : 1 , row : 3 , endcol : 1 , curscol : 1 }, screenpos ( 1 , 4 , 1 )) 231call assert_equal (#{ col : 1 , row : 3 , endcol : 1 , curscol : 1 }, screenpos ( 1 , 5 , 1 )) 232setlocal number 233call assert_equal (#{ col : 5 , row : 3 , endcol : 5 , curscol : 5 }, screenpos ( 1 , 3 , 1 )) 234call assert_equal (#{ col : 5 , row : 3 , endcol : 5 , curscol : 5 }, screenpos ( 1 , 4 , 1 )) 235call assert_equal (#{ col : 5 , row : 3 , endcol : 5 , curscol : 5 }, screenpos ( 1 , 5 , 1 )) 236call assert_equal ( 4 , screenpos ( 1 , 6 , 1 ). row ) 237bwipe ! 238endfunc 239 240func Test_screenpos_diff () 241CheckFeature diff 242 243enew ! 244call setline ( 1 , [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' ]) 245vnew 246call setline ( 1 , [ 'a' , 'b' , 'c' , 'g' , 'h' , 'i' ]) 247windo diffthis 248wincmd w 249call assert_equal (#{ col : 3 , row : 7 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 4 , 1 )) 250call assert_equal (#{ col : 3 , row : 8 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 5 , 1 )) 251exe "normal! 3\<C-E>" 252call assert_equal (#{ col : 3 , row : 4 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 4 , 1 )) 253call assert_equal (#{ col : 3 , row : 5 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 5 , 1 )) 254exe "normal! \<C-E>" 255call assert_equal (#{ col : 3 , row : 3 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 4 , 1 )) 256call assert_equal (#{ col : 3 , row : 4 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 5 , 1 )) 257exe "normal! \<C-E>" 258call assert_equal (#{ col : 3 , row : 2 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 4 , 1 )) 259call assert_equal (#{ col : 3 , row : 3 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 5 , 1 )) 260exe "normal! \<C-E>" 261call assert_equal (#{ col : 3 , row : 1 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 4 , 1 )) 262call assert_equal (#{ col : 3 , row : 2 , endcol : 3 , curscol : 3 }, screenpos ( 0 , 5 , 1 )) 263 264windo diffoff 265bwipe ! 266bwipe ! 267endfunc 268 269func Test_screenpos_number () 270rightbelow new 271rightbelow 73 vsplit 272call setline ( 1 , repeat ( 'x' , 66 )) 273setlocal number 274redraw 275let winid = win_getid () 276let [ winrow , wincol ] = win_screenpos ( winid ) 277let pos = screenpos ( winid , 1 , 66 ) 278call assert_equal ( winrow , pos . row ) 279call assert_equal ( wincol + 66 + 3 , pos . col ) 280 281call assert_fails ( 'echo screenpos(0, 2, 1)' , 'E966:' ) 282 283close 284bwipe ! 285endfunc 286 287func Test_screenpos_edit_newfile () 288new 28920 vsp 290setl nowrap 291call setline ( 1 , 'abcdefghijklmnopqrstuvwxyz' ) 292call cursor ( 1 , 10 ) 293norm ! 5zl 294call assert_equal (#{ col : 5 , row : 1 , endcol : 5 , curscol : 5 }, screenpos ( win_getid (), 1 , 10 )) 295enew ! 296call assert_equal ( 1 , & l: wrap ) 297call assert_equal (#{ col : 1 , row : 1 , endcol : 1 , curscol : 1 }, screenpos ( win_getid (), 1 , 1 )) 298 299bwipe ! 300endfunc 301 302" Save the visual start character position 303func SaveVisualStartCharPos () 304call add ( g: VisualStartPos , getcharpos ( 'v' )) 305return '' 306endfunc 307 308" Save the current cursor character position in insert mode 309func SaveInsertCurrentCharPos () 310call add ( g: InsertCurrentPos , getcharpos ( '.' )) 311return '' 312endfunc 313 314" Test for the getcharpos() function 315func Test_getcharpos () 316call assert_fails ( 'call getcharpos({})' , 'E731:' ) 317call assert_equal ([ 0 , 0 , 0 , 0 ], getcharpos ( 0 )) 318new 319call setline ( 1 , [ '' , "01\tà4è678" , 'Ⅵ' , '012345678' , ' │ x' ]) 320 321" Test for '.' and '$' 322normal 1G 323call assert_equal ([ 0 , 1 , 1 , 0 ], getcharpos ( '.' )) 324call assert_equal ([ 0 , 5 , 1 , 0 ], getcharpos ( '$' )) 325normal 2G6l 326call assert_equal ([ 0 , 2 , 7 , 0 ], getcharpos ( '.' )) 327normal 3G$ 328call assert_equal ([ 0 , 3 , 1 , 0 ], getcharpos ( '.' )) 329normal 4G$ 330call assert_equal ([ 0 , 4 , 9 , 0 ], getcharpos ( '.' )) 331 332" Test for a mark 333normal 2G7lmmgg 334call assert_equal ([ 0 , 2 , 8 , 0 ], getcharpos ( "'m" )) 335delmarks m 336call assert_equal ([ 0 , 0 , 0 , 0 ], getcharpos ( "'m" )) 337 338" Check mark does not move 339normal 5Gfxma 340call assert_equal ([ 0 , 5 , 5 , 0 ], getcharpos ( "'a" )) 341call assert_equal ([ 0 , 5 , 5 , 0 ], getcharpos ( "'a" )) 342call assert_equal ([ 0 , 5 , 5 , 0 ], getcharpos ( "'a" )) 343 344" Test for the visual start column 345vnoremap <expr> < F3 > SaveVisualStartCharPos () 346let g: VisualStartPos = [] 347exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>" 348call assert_equal ([[ 0 , 2 , 7 , 0 ], [ 0 , 2 , 10 , 0 ], [ 0 , 2 , 5 , 0 ]], g: VisualStartPos ) 349call assert_equal ([ 0 , 2 , 9 , 0 ], getcharpos ( 'v' )) 350let g: VisualStartPos = [] 351exe "normal 3Gv$\<F3>o\<F3>" 352call assert_equal ([[ 0 , 3 , 1 , 0 ], [ 0 , 3 , 2 , 0 ]], g: VisualStartPos ) 353let g: VisualStartPos = [] 354exe "normal 1Gv$\<F3>o\<F3>" 355call assert_equal ([[ 0 , 1 , 1 , 0 ], [ 0 , 1 , 1 , 0 ]], g: VisualStartPos ) 356vunmap <F3> 357 358" Test for getting the position in insert mode with the cursor after the 359" last character in a line 360inoremap <expr> < F3 > SaveInsertCurrentCharPos () 361let g: InsertCurrentPos = [] 362exe "normal 1GA\<F3>" 363exe "normal 2GA\<F3>" 364exe "normal 3GA\<F3>" 365exe "normal 4GA\<F3>" 366exe "normal 2G6li\<F3>" 367call assert_equal ([[ 0 , 1 , 1 , 0 ], [ 0 , 2 , 10 , 0 ], [ 0 , 3 , 2 , 0 ], [ 0 , 4 , 10 , 0 ], 368 \[ 0 , 2 , 7 , 0 ]], g: InsertCurrentPos ) 369iunmap <F3> 370 371 %bw ! 372endfunc 373 374" Test for the setcharpos() function 375func Test_setcharpos () 376call assert_equal ( -1 , setcharpos ( '.' , v: _null_list )) 377new 378call setline ( 1 , [ '' , "01\tà4è678" , 'Ⅵ' , '012345678' ]) 379call setcharpos ( '.' , [ 0 , 1 , 1 , 0 ]) 380call assert_equal ([ 1 , 1 ], [ line ( '.' ), col ( '.' )]) 381call setcharpos ( '.' , [ 0 , 2 , 7 , 0 ]) 382call assert_equal ([ 2 , 9 ], [ line ( '.' ), col ( '.' )]) 383call setcharpos ( '.' , [ 0 , 3 , 4 , 0 ]) 384call assert_equal ([ 3 , 1 ], [ line ( '.' ), col ( '.' )]) 385call setcharpos ( '.' , [ 0 , 3 , 1 , 0 ]) 386call assert_equal ([ 3 , 1 ], [ line ( '.' ), col ( '.' )]) 387call setcharpos ( '.' , [ 0 , 4 , 0 , 0 ]) 388call assert_equal ([ 4 , 1 ], [ line ( '.' ), col ( '.' )]) 389call setcharpos ( '.' , [ 0 , 4 , 20 , 0 ]) 390call assert_equal ([ 4 , 9 ], [ line ( '.' ), col ( '.' )]) 391 392" Test for mark 393delmarks m 394call setcharpos ( "'m" , [ 0 , 2 , 9 , 0 ]) 395normal `m 396call assert_equal ([ 2 , 11 ], [ line ( '.' ), col ( '.' )]) 397" unload the buffer and try to set the mark 398let bnr = bufnr () 399enew ! 400call assert_equal ( -1 , setcharpos ( "'m" , [ bnr , 2 , 2 , 0 ])) 401 402 %bw ! 403call assert_equal ( -1 , setcharpos ( '.' , [ 10 , 3 , 1 , 0 ])) 404endfunc 405 406func SaveVisualStartCharCol () 407call add ( g: VisualStartCol , charcol ( 'v' )) 408return '' 409endfunc 410 411func SaveInsertCurrentCharCol () 412call add ( g: InsertCurrentCol , charcol ( '.' )) 413return '' 414endfunc 415 416" Test for the charcol() function 417func Test_charcol () 418call assert_fails ( 'call charcol({})' , 'E1222:' ) 419call assert_fails ( 'call charcol(".", [])' , 'E1210:' ) 420call assert_fails ( 'call charcol(0)' , 'E1222:' ) 421new 422call setline ( 1 , [ '' , "01\tà4è678" , 'Ⅵ' , '012345678' ]) 423 424" Test for '.' and '$' 425normal 1G 426call assert_equal ( 1 , charcol ( '.' )) 427call assert_equal ( 1 , charcol ( '$' )) 428normal 2G6l 429call assert_equal ( 7 , charcol ( '.' )) 430call assert_equal ( 10 , charcol ( '$' )) 431normal 3G$ 432call assert_equal ( 1 , charcol ( '.' )) 433call assert_equal ( 2 , charcol ( '$' )) 434normal 4G$ 435call assert_equal ( 9 , charcol ( '.' )) 436call assert_equal ( 10 , charcol ( '$' )) 437 438" Test for [lnum, '$'] 439call assert_equal ( 1 , charcol ([ 1 , '$' ])) 440call assert_equal ( 10 , charcol ([ 2 , '$' ])) 441call assert_equal ( 2 , charcol ([ 3 , '$' ])) 442call assert_equal ( 0 , charcol ([ 5 , '$' ])) 443 444" Test for a mark 445normal 2G7lmmgg 446call assert_equal ( 8 , charcol ( "'m" )) 447delmarks m 448call assert_equal ( 0 , charcol ( "'m" )) 449 450" Test for the visual start column 451vnoremap <expr> < F3 > SaveVisualStartCharCol () 452let g: VisualStartCol = [] 453exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>" 454call assert_equal ([ 7 , 10 , 5 ], g: VisualStartCol ) 455call assert_equal ( 9 , charcol ( 'v' )) 456let g: VisualStartCol = [] 457exe "normal 3Gv$\<F3>o\<F3>" 458call assert_equal ([ 1 , 2 ], g: VisualStartCol ) 459let g: VisualStartCol = [] 460exe "normal 1Gv$\<F3>o\<F3>" 461call assert_equal ([ 1 , 1 ], g: VisualStartCol ) 462vunmap <F3> 463 464" Test for getting the column number in insert mode with the cursor after 465" the last character in a line 466inoremap <expr> < F3 > SaveInsertCurrentCharCol () 467let g: InsertCurrentCol = [] 468exe "normal 1GA\<F3>" 469exe "normal 2GA\<F3>" 470exe "normal 3GA\<F3>" 471exe "normal 4GA\<F3>" 472exe "normal 2G6li\<F3>" 473call assert_equal ([ 1 , 10 , 2 , 10 , 7 ], g: InsertCurrentCol ) 474iunmap <F3> 475 476" Test for getting the column number in another window. 477let winid = win_getid () 478new 479call win_execute ( winid , 'normal 1G' ) 480call assert_equal ( 1 , charcol ( '.' , winid )) 481call assert_equal ( 1 , charcol ( '$' , winid )) 482call win_execute ( winid , 'normal 2G6l' ) 483call assert_equal ( 7 , charcol ( '.' , winid )) 484call assert_equal ( 10 , charcol ( '$' , winid )) 485 486" calling from another tab page also works 487tabnew 488call assert_equal ( 7 , charcol ( '.' , winid )) 489call assert_equal ( 10 , charcol ( '$' , winid )) 490tabclose 491 492" unknown window ID 493call assert_equal ( 0 , charcol ( '.' , 10001 )) 494 495 %bw ! 496endfunc 497 498func SaveInsertCursorCharPos () 499call add ( g: InsertCursorPos , getcursorcharpos ( '.' )) 500return '' 501endfunc 502 503" Test for getcursorcharpos() 504func Test_getcursorcharpos () 505call assert_equal ( getcursorcharpos (), getcursorcharpos ( 0 )) 506call assert_equal ([ 0 , 0 , 0 , 0 , 0 ], getcursorcharpos ( -1 )) 507call assert_equal ([ 0 , 0 , 0 , 0 , 0 ], getcursorcharpos ( 1999 )) 508 509new 510call setline ( 1 , [ '' , "01\tà4è678" , 'Ⅵ' , '012345678' ]) 511normal 1G9l 512call assert_equal ([ 0 , 1 , 1 , 0 , 1 ], getcursorcharpos ()) 513normal 2G9l 514call assert_equal ([ 0 , 2 , 9 , 0 , 14 ], getcursorcharpos ()) 515normal 3G9l 516call assert_equal ([ 0 , 3 , 1 , 0 , 1 ], getcursorcharpos ()) 517normal 4G9l 518call assert_equal ([ 0 , 4 , 9 , 0 , 9 ], getcursorcharpos ()) 519 520" Test for getting the cursor position in insert mode with the cursor after 521" the last character in a line 522inoremap <expr> < F3 > SaveInsertCursorCharPos () 523let g: InsertCursorPos = [] 524exe "normal 1GA\<F3>" 525exe "normal 2GA\<F3>" 526exe "normal 3GA\<F3>" 527exe "normal 4GA\<F3>" 528exe "normal 2G6li\<F3>" 529call assert_equal ([[ 0 , 1 , 1 , 0 , 1 ], [ 0 , 2 , 10 , 0 , 15 ], [ 0 , 3 , 2 , 0 , 2 ], 530 \[ 0 , 4 , 10 , 0 , 10 ], [ 0 , 2 , 7 , 0 , 12 ]], g: InsertCursorPos ) 531iunmap <F3> 532 533let winid = win_getid () 534normal 2G5l 535wincmd w 536call assert_equal ([ 0 , 2 , 6 , 0 , 11 ], getcursorcharpos ( winid )) 537 %bw ! 538endfunc 539 540" Test for setcursorcharpos() 541func Test_setcursorcharpos () 542call assert_fails ( 'call setcursorcharpos(v:_null_list)' , 'E474:' ) 543call assert_fails ( 'call setcursorcharpos([1])' , 'E474:' ) 544call assert_fails ( 'call setcursorcharpos([1, 1, 1, 1, 1])' , 'E474:' ) 545new 546call setline ( 1 , [ '' , "01\tà4è678" , 'Ⅵ' , '012345678' ]) 547normal G 548call setcursorcharpos ([ 1 , 1 ]) 549call assert_equal ([ 1 , 1 ], [ line ( '.' ), col ( '.' )]) 550 551call setcursorcharpos ([ 2 , 7 , 0 ]) 552call assert_equal ([ 2 , 9 ], [ line ( '.' ), col ( '.' )]) 553call setcursorcharpos ([ 0 , 7 , 0 ]) 554call assert_equal ([ 2 , 9 ], [ line ( '.' ), col ( '.' )]) 555call setcursorcharpos ( 0 , 7 , 0 ) 556call assert_equal ([ 2 , 9 ], [ line ( '.' ), col ( '.' )]) 557 558call setcursorcharpos ( 3 , 4 ) 559call assert_equal ([ 3 , 1 ], [ line ( '.' ), col ( '.' )]) 560call setcursorcharpos ([ 3 , 1 ]) 561call assert_equal ([ 3 , 1 ], [ line ( '.' ), col ( '.' )]) 562call setcursorcharpos ([ 4 , 0 , 0 , 0 ]) 563call assert_equal ([ 4 , 1 ], [ line ( '.' ), col ( '.' )]) 564call setcursorcharpos ([ 4 , 20 ]) 565call assert_equal ([ 4 , 9 ], [ line ( '.' ), col ( '.' )]) 566normal 1G 567call setcursorcharpos ([ 100 , 100 , 100 , 100 ]) 568call assert_equal ([ 4 , 9 ], [ line ( '.' ), col ( '.' )]) 569normal 1G 570call setcursorcharpos ( '$' , 1 ) 571call assert_equal ([ 4 , 1 ], [ line ( '.' ), col ( '.' )]) 572 573 %bw ! 574endfunc 575 576" Test for virtcol2col() 577func Test_virtcol2col () 578new 579call setline ( 1 , [ "a\tb\tc" ]) 580call assert_equal ( 1 , virtcol2col ( 0 , 1 , 1 )) 581call assert_equal ( 2 , virtcol2col ( 0 , 1 , 2 )) 582call assert_equal ( 2 , virtcol2col ( 0 , 1 , 8 )) 583call assert_equal ( 3 , virtcol2col ( 0 , 1 , 9 )) 584call assert_equal ( 4 , virtcol2col ( 0 , 1 , 10 )) 585call assert_equal ( 4 , virtcol2col ( 0 , 1 , 16 )) 586call assert_equal ( 5 , virtcol2col ( 0 , 1 , 17 )) 587call assert_equal ( -1 , virtcol2col ( 10 , 1 , 1 )) 588call assert_equal ( -1 , virtcol2col ( 0 , 10 , 1 )) 589call assert_equal ( -1 , virtcol2col ( 0 , -1 , 1 )) 590call assert_equal ( -1 , virtcol2col ( 0 , 1 , -1 )) 591call assert_equal ( 5 , virtcol2col ( 0 , 1 , 20 )) 592 593" Multibyte character 594call setline ( 1 , [ 'a✅✅✅' ]) 595call assert_equal ( 1 , virtcol2col ( 0 , 1 , 1 )) 596call assert_equal ( 2 , virtcol2col ( 0 , 1 , 3 )) 597call assert_equal ( 5 , virtcol2col ( 0 , 1 , 5 )) 598call assert_equal ( 8 , virtcol2col ( 0 , 1 , 7 )) 599call assert_equal ( 8 , virtcol2col ( 0 , 1 , 8 )) 600 601" These used to cause invalid memory access 602call setline ( 1 , '' ) 603call assert_equal ( 0 , virtcol2col ( 0 , 1 , 1 )) 604call assert_equal ( 0 , virtcol2col ( 0 , 1 , 2 )) 605 606let w = winwidth ( 0 ) 607call setline ( 2 , repeat ( 'a' , w + 2 )) 608let win_nosbr = win_getid () 609split 610setlocal showbreak = !! 611let win_sbr = win_getid () 612call assert_equal ( w , virtcol2col ( win_nosbr , 2 , w )) 613call assert_equal ( w + 1 , virtcol2col ( win_nosbr , 2 , w + 1 )) 614call assert_equal ( w + 2 , virtcol2col ( win_nosbr , 2 , w + 2 )) 615call assert_equal ( w + 2 , virtcol2col ( win_nosbr , 2 , w + 3 )) 616call assert_equal ( w , virtcol2col ( win_sbr , 2 , w )) 617call assert_equal ( w + 1 , virtcol2col ( win_sbr , 2 , w + 1 )) 618call assert_equal ( w + 1 , virtcol2col ( win_sbr , 2 , w + 2 )) 619call assert_equal ( w + 1 , virtcol2col ( win_sbr , 2 , w + 3 )) 620call assert_equal ( w + 2 , virtcol2col ( win_sbr , 2 , w + 4 )) 621call assert_equal ( w + 2 , virtcol2col ( win_sbr , 2 , w + 5 )) 622close 623 624call assert_fails ( 'echo virtcol2col("0", 1, 20)' , 'E1210:' ) 625call assert_fails ( 'echo virtcol2col(0, "1", 20)' , 'E1210:' ) 626call assert_fails ( 'echo virtcol2col(0, 1, "1")' , 'E1210:' ) 627 628bw ! 629endfunc 630 631" vim: shiftwidth=2 sts=2 expandtab