robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
038eb01b41
master
1" Tests for setbufline(), getbufline(), appendbufline(), deletebufline() 2 3source shared.vim 4source screendump.vim 5source check.vim 6 7func Test_setbufline_getbufline () 8" similar to Test_set_get_bufline() 9new 10let b = bufnr ( '%' ) 11hide 12call assert_equal ( 0 , setbufline ( b , 1 , [ 'foo' , 'bar' ])) 13call assert_equal ([ 'foo' ], getbufline ( b , 1 )) 14call assert_equal ( 'foo' , getbufoneline ( b , 1 )) 15call assert_equal ([ 'bar' ], getbufline ( b , '$' )) 16call assert_equal ( 'bar' , getbufoneline ( b , '$' )) 17call assert_equal ([ 'foo' , 'bar' ], getbufline ( b , 1 , 2 )) 18exe "bd!" b 19call assert_equal ([], getbufline ( b , 1 , 2 )) 20 21split Xtest 22call setline ( 1 , [ 'a' , 'b' , 'c' ]) 23let b = bufnr ( '%' ) 24wincmd w 25 26call assert_equal ( 1 , setbufline ( b , 5 , 'x' )) 27call assert_equal ( 1 , setbufline ( b , 5 , [ 'x' ])) 28call assert_equal ( 0 , setbufline ( b , 5 , [])) 29call assert_equal ( 0 , setbufline ( b , 5 , v: _null_list )) 30 31call assert_equal ( 1 , 'x' ->setbufline ( bufnr ( '$' ) + 1 , 1 )) 32call assert_equal ( 1 , [ 'x' ] ->setbufline ( bufnr ( '$' ) + 1 , 1 )) 33call assert_equal ( 1 , [] ->setbufline ( bufnr ( '$' ) + 1 , 1 )) 34call assert_equal ( 1 , v: _null_list ->setbufline ( bufnr ( '$' ) + 1 , 1 )) 35 36call assert_equal ([ 'a' , 'b' , 'c' ], getbufline ( b , 1 , '$' )) 37 38call assert_equal ( 0 , setbufline ( b , 4 , [ 'd' , 'e' ])) 39call assert_equal ([ 'c' ], b ->getbufline ( 3 )) 40call assert_equal ( 'c' , b ->getbufoneline ( 3 )) 41call assert_equal ([ 'd' ], getbufline ( b , 4 )) 42call assert_equal ( 'd' , getbufoneline ( b , 4 )) 43call assert_equal ([ 'e' ], getbufline ( b , 5 )) 44call assert_equal ( 'e' , getbufoneline ( b , 5 )) 45call assert_equal ([], getbufline ( b , 6 )) 46call assert_equal ([], getbufline ( b , 2 , 1 )) 47 48if has ( 'job' ) 49call setbufline ( b , 2 , [ function ( 'eval' ), #{ key : 123 }, test_null_job ()]) 50call assert_equal ([ "function('eval')" , 51 \"{'key': 123}" , 52 \"no process" ], 53 \getbufline ( b , 2 , 4 )) 54endif 55exe "bwipe! " . b 56endfunc 57 58func Test_setbufline_getbufline_fold () 59split Xtest 60setlocal foldmethod = exprfoldexpr = 0 61let b = bufnr ( '%' ) 62new 63call assert_equal ( 0 , setbufline ( b , 1 , [ 'foo' , 'bar' ])) 64call assert_equal ([ 'foo' ], getbufline ( b , 1 )) 65call assert_equal ([ 'bar' ], getbufline ( b , 2 )) 66call assert_equal ([ 'foo' , 'bar' ], getbufline ( b , 1 , 2 )) 67exe "bwipe!" b 68bwipe ! 69endfunc 70 71func Test_setbufline_getbufline_fold_tab () 72split Xtest 73setlocal foldmethod = exprfoldexpr = 0 74let b = bufnr ( '%' ) 75tab new 76call assert_equal ( 0 , setbufline ( b , 1 , [ 'foo' , 'bar' ])) 77call assert_equal ([ 'foo' ], getbufline ( b , 1 )) 78call assert_equal ([ 'bar' ], getbufline ( b , 2 )) 79call assert_equal ([ 'foo' , 'bar' ], getbufline ( b , 1 , 2 )) 80exe "bwipe!" b 81bwipe ! 82endfunc 83 84func Test_setline_startup () 85let cmd = GetVimCommand ( 'Xscript' ) 86if cmd == '' 87return 88endif 89call writefile ([ 'call setline(1, "Hello")' , 'silent w Xtest' , 'q!' ], 'Xscript' , 'D' ) 90call system ( cmd ) 91sleep 50m 92call assert_equal ([ 'Hello' ], readfile ( 'Xtest' )) 93 94call assert_equal ( 0 , setline ( 1 , [])) 95call assert_equal ( 0 , setline ( 1 , v: _null_list )) 96call assert_equal ( 0 , setline ( 5 , [])) 97call assert_equal ( 0 , setline ( 6 , v: _null_list )) 98 99call delete ( 'Xtest' ) 100endfunc 101 102func Test_appendbufline () 103new 104let b = bufnr ( '%' ) 105hide 106 107new 108call setline ( 1 , [ 'line1' , 'line2' , 'line3' ]) 109normal ! 2gggg 110call assert_equal ( 2 , line ( "''" )) 111 112call assert_equal ( 0 , appendbufline ( b , 0 , [ 'foo' , 'bar' ])) 113call assert_equal ([ 'foo' ], getbufline ( b , 1 )) 114call assert_equal ([ 'bar' ], getbufline ( b , 2 )) 115call assert_equal ([ 'foo' , 'bar' ], getbufline ( b , 1 , 2 )) 116call assert_equal ( 0 , appendbufline ( b , 0 , 'baz' )) 117call assert_equal ([ 'baz' , 'foo' , 'bar' ], getbufline ( b , 1 , 3 )) 118 119" appendbufline() in a hidden buffer shouldn't move marks in current window. 120call assert_equal ( 2 , line ( "''" )) 121bwipe ! 122 123exe "bd!" b 124call assert_equal ([], getbufline ( b , 1 , 3 )) 125 126split Xtest 127call setline ( 1 , [ 'a' , 'b' , 'c' ]) 128let b = bufnr ( '%' ) 129wincmd w 130 131call assert_equal ( 1 , appendbufline ( b , -1 , 'x' )) 132call assert_equal ( 1 , appendbufline ( b , -1 , [ 'x' ])) 133call assert_equal ( 1 , appendbufline ( b , -1 , [])) 134call assert_equal ( 1 , appendbufline ( b , -1 , v: _null_list )) 135 136call assert_equal ( 1 , appendbufline ( b , 4 , 'x' )) 137call assert_equal ( 1 , appendbufline ( b , 4 , [ 'x' ])) 138call assert_equal ( 0 , appendbufline ( b , 4 , [])) 139call assert_equal ( 0 , appendbufline ( b , 4 , v: _null_list )) 140 141call assert_equal ( 1 , appendbufline ( 1234 , 1 , 'x' )) 142call assert_equal ( 1 , appendbufline ( 1234 , 1 , [ 'x' ])) 143call assert_equal ( 1 , appendbufline ( 1234 , 1 , [])) 144call assert_equal ( 1 , appendbufline ( 1234 , 1 , v: _null_list )) 145 146call assert_equal ( 0 , appendbufline ( b , 1 , [])) 147call assert_equal ( 0 , appendbufline ( b , 1 , v: _null_list )) 148call assert_equal ( 0 , appendbufline ( b , 3 , [])) 149call assert_equal ( 0 , appendbufline ( b , 3 , v: _null_list )) 150 151call assert_equal ([ 'a' , 'b' , 'c' ], getbufline ( b , 1 , '$' )) 152 153call assert_equal ( 0 , appendbufline ( b , 3 , [ 'd' , 'e' ])) 154call assert_equal ([ 'c' ], getbufline ( b , 3 )) 155call assert_equal ([ 'd' ], getbufline ( b , 4 )) 156call assert_equal ([ 'e' ], getbufline ( b , 5 )) 157call assert_equal ([], getbufline ( b , 6 )) 158exe "bwipe! " . b 159endfunc 160 161func Test_appendbufline_no_E315 () 162let after = [ 163 \'set stl=%f ls=2' , 164 \'new' , 165 \'let buf = bufnr("%")' , 166 \'quit' , 167 \'vsp' , 168 \'exec "buffer" buf' , 169 \'wincmd w' , 170 \'call appendbufline(buf, 0, "abc")' , 171 \'redraw' , 172 \'while getbufline(buf, 1)[0] =~ "^\\s*$"' , 173 \' sleep 10m' , 174 \'endwhile' , 175 \'au VimLeavePre * call writefile([v:errmsg], "Xerror")' , 176 \'au VimLeavePre * call writefile(["done"], "Xdone")' , 177 \'qall!' , 178 \] 179if ! RunVim ([], after , '--clean' ) 180return 181endif 182call assert_notmatch ( "^E315:" , readfile ( "Xerror" )[ 0 ]) 183call assert_equal ( "done" , readfile ( "Xdone" )[ 0 ]) 184call delete ( "Xerror" ) 185call delete ( "Xdone" ) 186endfunc 187 188func Test_deletebufline () 189new 190let b = bufnr ( '%' ) 191call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' ]) 192hide 193 194new 195call setline ( 1 , [ 'line1' , 'line2' , 'line3' ]) 196normal ! 2gggg 197call assert_equal ( 2 , line ( "''" )) 198 199call assert_equal ( 0 , deletebufline ( b , 2 )) 200call assert_equal ([ 'aaa' , 'ccc' ], getbufline ( b , 1 , 2 )) 201call assert_equal ( 0 , deletebufline ( b , 2 , 8 )) 202call assert_equal ([ 'aaa' ], getbufline ( b , 1 , 2 )) 203 204" deletebufline() in a hidden buffer shouldn't move marks in current window. 205call assert_equal ( 2 , line ( "''" )) 206bwipe ! 207 208exe "bd!" b 209call assert_equal ( 1 , b ->deletebufline ( 1 )) 210 211call assert_equal ( 1 , deletebufline ( -1 , 1 )) 212 213split Xtest 214call setline ( 1 , [ 'a' , 'b' , 'c' ]) 215call cursor ( line ( '$' ), 1 ) 216let b = bufnr ( '%' ) 217wincmd w 218call assert_equal ( 1 , deletebufline ( b , 4 )) 219call assert_equal ( 0 , deletebufline ( b , 1 )) 220call assert_equal ([ 'b' , 'c' ], getbufline ( b , 1 , 2 )) 221exe "bwipe! " . b 222 223edit XbufOne 224let one = bufnr () 225call setline ( 1 , [ 'a' , 'b' , 'c' ]) 226setlocal nomodifiable 227split XbufTwo 228let two = bufnr () 229call assert_fails ( 'call deletebufline(one, 1)' , 'E21:' ) 230call assert_equal ( two , bufnr ()) 231bwipe ! XbufTwo 232bwipe ! XbufOne 233endfunc 234 235func Test_appendbufline_redraw () 236CheckScreendump 237 238let lines =<< trim END 239new foo 240let winnr = 'foo'->bufwinnr() 241let buf = bufnr('foo') 242wincmd p 243call appendbufline(buf, '$', range(1,200)) 244exe winnr .. 'wincmd w' 245norm! G 246wincmd p 247call deletebufline(buf, 1, '$') 248call appendbufline(buf, '$', 'Hello Vim world...') 249END 250call writefile ( lines , 'XscriptMatchCommon' , 'D' ) 251let buf = RunVimInTerminal ( '-S XscriptMatchCommon' , #{ rows : 10 }) 252call VerifyScreenDump ( buf , 'Test_appendbufline_1' , {}) 253 254call StopVimInTerminal ( buf ) 255endfunc 256 257func Test_setbufline_select_mode () 258new 259call setline ( 1 , [ 'foo' , 'bar' ]) 260call feedkeys ( "j^v2l\<C-G>" , 'nx' ) 261 262let bufnr = bufadd ( 'Xdummy' ) 263call bufload ( bufnr ) 264call setbufline ( bufnr , 1 , [ 'abc' ]) 265 266call feedkeys ( "x" , 'nx' ) 267call assert_equal ([ 'foo' , 'x' ], getline ( 1 , 2 )) 268 269exe "bwipe! " .. bufnr 270bwipe ! 271endfunc 272 273func Test_deletebufline_select_mode () 274new 275call setline ( 1 , [ 'foo' , 'bar' ]) 276call feedkeys ( "j^v2l\<C-G>" , 'nx' ) 277 278let bufnr = bufadd ( 'Xdummy' ) 279call bufload ( bufnr ) 280call setbufline ( bufnr , 1 , [ 'abc' , 'def' ]) 281call deletebufline ( bufnr , 1 ) 282 283call feedkeys ( "x" , 'nx' ) 284call assert_equal ([ 'foo' , 'x' ], getline ( 1 , 2 )) 285 286exe "bwipe! " .. bufnr 287bwipe ! 288endfunc 289 290func Test_setbufline_startup_nofile () 291let before =<< trim [CODE] 292set shortmess+=F 293file Xresult 294set buftype=nofile 295call setbufline('', 1, 'success') 296[CODE] 297let after =<< trim [CODE] 298set buftype= 299write 300quit 301[CODE] 302 303if ! RunVim ( before , after , '--clean' ) 304return 305endif 306call assert_equal ([ 'success' ], readfile ( 'Xresult' )) 307call delete ( 'Xresult' ) 308endfunc 309 310" Test that setbufline(), appendbufline() and deletebufline() should fail and 311" return 1 when "textlock" is active. 312func Test_change_bufline_with_textlock () 313new 314inoremap <buffer> <expr> < F2 > setbufline ( '' , 1 , '' ) 315call assert_fails ( "normal a\<F2>" , 'E565:' ) 316call assert_equal ( '1' , getline ( 1 )) 317inoremap <buffer> <expr> < F2 > appendbufline ( '' , 1 , '' ) 318call assert_fails ( "normal a\<F2>" , 'E565:' ) 319call assert_equal ( '11' , getline ( 1 )) 320inoremap <buffer> <expr> < F2 > deletebufline ( '' , 1 ) 321call assert_fails ( "normal a\<F2>" , 'E565:' ) 322call assert_equal ( '111' , getline ( 1 )) 323bwipe ! 324endfunc 325 326" vim: shiftwidth=2 sts=2 expandtab