robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local neq , eq , command =t . neq , t . eq , n . command 7local clear =n . clear 8local expect , eval =n . expect , n . eval 9local exec_lua =n . exec_lua 10local insert , pcall_err =n . insert , t . pcall_err 11local matches =t . matches 12local api =n . api 13local feed =n . feed 14 15describe ( 'eval-API' , function () 16before_each ( clear ) 17 18it ( 'work' , function () 19command ( "call nvim_command('let g:test = 1')" ) 20eq ( 1 , eval ( "nvim_get_var('test')" )) 21 22local buf =eval ( 'nvim_get_current_buf()' ) 23command ( 'call nvim_buf_set_lines(' .. buf .. ", 0, -1, v:true, ['aa', 'bb'])" ) 24expect ( [[ 25aa 26bb]] ) 27 28command ( 'call nvim_win_set_cursor(0, [1, 1])' ) 29command ( "call nvim_input('ax<esc>')" ) 30expect ( [[ 31aax 32bb]] ) 33end ) 34 35it ( 'throw errors for invalid arguments' , function () 36local err =pcall_err ( command , 'call nvim_get_current_buf("foo")' ) 37eq ( 'Vim(call):E118: Too many arguments for function: nvim_get_current_buf' , err ) 38 39err =pcall_err ( command , 'call nvim_set_option_value("hlsearch")' ) 40eq ( 'Vim(call):E119: Not enough arguments for function: nvim_set_option_value' , err ) 41 42err =pcall_err ( command , 'call nvim_buf_set_lines(1, 0, -1, [], ["list"])' ) 43eq ( 44'Vim(call):E5555: API call: Wrong type for argument 4 when calling nvim_buf_set_lines, expecting Boolean' , 45err 46) 47 48err =pcall_err ( command , 'call nvim_buf_set_lines(0, 0, -1, v:true, "string")' ) 49eq ( 50'Vim(call):E5555: API call: Wrong type for argument 5 when calling nvim_buf_set_lines, expecting ArrayOf(String)' , 51err 52) 53 54err =pcall_err ( command , 'call nvim_buf_get_number("0")' ) 55eq ( 56'Vim(call):E5555: API call: Wrong type for argument 1 when calling nvim_buf_get_number, expecting Buffer' , 57err 58) 59 60err =pcall_err ( command , 'call nvim_buf_line_count(17)' ) 61eq ( 'Vim(call):E5555: API call: Invalid buffer id: 17' , err ) 62end ) 63 64it ( 'cannot change text or window if textlock' , function () 65command ( 'autocmd TextYankPost <buffer> ++once call nvim_buf_set_lines(0, 0, -1, v:false, [])' ) 66matches ( 67'Vim%(call%):E5555: API call: E565: Not allowed to change text or change window$' , 68pcall_err ( command , 'normal! yy' ) 69) 70 71command ( 'autocmd TextYankPost <buffer> ++once call nvim_open_term(0, {})' ) 72matches ( 73'Vim%(call%):E5555: API call: E565: Not allowed to change text or change window$' , 74pcall_err ( command , 'normal! yy' ) 75) 76 77-- Functions checking textlock should also not be usable from <expr> mappings. 78command ( 'inoremap <expr> <f2> nvim_win_close(0, 1)' ) 79eq ( 80'Vim(normal):E5555: API call: E565: Not allowed to change text or change window' , 81pcall_err ( command , [[execute "normal i\<f2>"]] ) 82) 83 84-- Text-changing functions gave a "Failed to save undo information" error when called from an 85-- <expr> mapping outside do_cmdline() (msg_list == NULL), so use feed() to test this. 86command ( "inoremap <expr> <f2> nvim_buf_set_text(0, 0, 0, 0, 0, ['hi'])" ) 87api . nvim_set_vvar ( 'errmsg' , '' ) 88feed ( 'i<f2><esc>' ) 89eq ( 90'E5555: API call: E565: Not allowed to change text or change window' , 91api . nvim_get_vvar ( 'errmsg' ) 92) 93 94-- cmdwin (#40312, #40484): E11 "Invalid in command-line window" restriction was removed. 95local old_win =api . nvim_get_current_win () 96feed ( 'q:' ) 97local cmdwin_win =api . nvim_get_current_win () 98neq ( old_win , cmdwin_win ) 99-- Switching to another window from the cmdwin now works (previously E11). 100api . nvim_set_current_win ( old_win ) 101eq ( old_win , api . nvim_get_current_win ()) 102-- ...and back into the cmdwin. 103api . nvim_set_current_win ( cmdwin_win ) 104eq ( cmdwin_win , api . nvim_get_current_win ()) 105 106-- nvim_buf_set_lines() in the cmdwin buffer is OK. 107api . nvim_buf_set_lines ( 0 , 0 , - 1 , 1 , { 'wow!' } ) 108eq ( { 'wow!' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 1 )) 109 110-- Turning the cmdwin buffer into a terminal buffer also works (#41199) 111api . nvim_open_term ( 0 , {} ) 112api . nvim_buf_delete ( 0 , { force =true } ) 113 114feed ( 'q:' ) 115exec_lua ( [[ 116local cmdwin_buf = vim.api.nvim_get_current_buf() 117vim._with({buf = vim.api.nvim_create_buf(false, true)}, function() 118vim.api.nvim_open_term(cmdwin_buf, {}) 119end) 120]] ) 121 122-- And turning a different buffer into a terminal from the cmdwin is OK. 123local term_buf =api . nvim_create_buf ( false , true ) 124api . nvim_open_term ( term_buf , {} ) 125eq ( 'terminal' , api . nvim_get_option_value ( 'buftype' , { buf =term_buf } )) 126end ) 127 128it ( 'use buffer numbers and windows ids as handles' , function () 129local bnr =eval ( "bufnr('')" ) 130local bhnd =eval ( 'nvim_get_current_buf()' ) 131local wid =eval ( 'win_getid()' ) 132local whnd =eval ( 'nvim_get_current_win()' ) 133eq ( bnr , bhnd ) 134eq ( wid , whnd ) 135 136command ( 'new' ) -- creates new buffer and new window 137local bnr2 =eval ( "bufnr('')" ) 138local bhnd2 =eval ( 'nvim_get_current_buf()' ) 139local wid2 =eval ( 'win_getid()' ) 140local whnd2 =eval ( 'nvim_get_current_win()' ) 141eq ( bnr2 , bhnd2 ) 142eq ( wid2 , whnd2 ) 143neq ( bnr , bnr2 ) 144neq ( wid , wid2 ) 145-- 0 is synonymous to the current buffer 146eq ( bnr2 , eval ( 'nvim_buf_get_number(0)' )) 147 148command ( 'bn' ) -- show old buffer in new window 149eq ( bnr , eval ( 'nvim_get_current_buf()' )) 150eq ( bnr , eval ( "bufnr('')" )) 151eq ( bnr , eval ( 'nvim_buf_get_number(0)' )) 152eq ( wid2 , eval ( 'win_getid()' )) 153eq ( whnd2 , eval ( 'nvim_get_current_win()' )) 154end ) 155 156it ( 'get_lines and set_lines use NL to represent NUL' , function () 157api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { 'aa\0' , 'b\0b' } ) 158eq ( { 'aa\n' , 'b\nb' } , eval ( 'nvim_buf_get_lines(0, 0, -1, 1)' )) 159 160command ( 'call nvim_buf_set_lines(0, 1, 2, v:true, ["xx", "\\nyy"])' ) 161eq ( { 'aa\0' , 'xx' , '\0yy' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , 1 )) 162end ) 163 164it ( 'that are FUNC_ATTR_NOEVAL cannot be called' , function () 165-- Deprecated vim_ prefix is not exported. 166local err =pcall_err ( command , 'call vim_get_current_buffer("foo")' ) 167eq ( 'Vim(call):E117: Unknown function: vim_get_current_buffer' , err ) 168 169-- Deprecated buffer_ prefix is not exported. 170err =pcall_err ( command , 'call buffer_line_count(0)' ) 171eq ( 'Vim(call):E117: Unknown function: buffer_line_count' , err ) 172 173-- Functions deprecated before the api functions became available 174-- in vimscript are not exported. 175err =pcall_err ( command , 'call buffer_get_line(0, 1)' ) 176eq ( 'Vim(call):E117: Unknown function: buffer_get_line' , err ) 177 178-- some api functions are only useful from a msgpack-rpc channel 179err =pcall_err ( command , 'call nvim_set_client_info()' ) 180eq ( 'Vim(call):E117: Unknown function: nvim_set_client_info' , err ) 181end ) 182 183it ( 'have metadata accessible with api_info()' , function () 184local api_keys =eval ( 'sort(keys(api_info()))' ) 185eq ( { 'error_types' , 'functions' , 'types' , 'ui_events' , 'ui_options' , 'version' } , api_keys ) 186end ) 187 188it ( 'are highlighted by vim.vim syntax file' , function () 189local screen =Screen . new ( 40 , 8 ) 190 191command ( 'set ft=vim' ) 192n . add_builddir_to_rtp () 193command ( 'syntax on' ) 194insert ( [[ 195call bufnr('%') 196call nvim_input('typing...') 197call not_a_function(42)]] ) 198 199screen : expect ( [[ 200{15:call} {25:bufnr}{16:(}{26:'%'}{16:)} | 201{15:call} {25:nvim_input}{16:(}{26:'typing...'}{16:)} | 202{15:call} {9:not_a_function}{16:(}{26:42}{16:^)} | 203{1:~ }|*4 204| 205]] ) 206end ) 207 208it ( 'cannot be called from sandbox' , function () 209eq ( 210'Vim(call):E48: Not allowed in sandbox' , 211pcall_err ( command , "sandbox call nvim_input('ievil')" ) 212) 213eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 214end ) 215 216it ( 'converts blobs to API strings' , function () 217command ( 'let g:v1 = nvim__id(0z68656c6c6f)' ) 218command ( 'let g:v2 = nvim__id(v:_null_blob)' ) 219eq ( 1 , eval ( 'type(g:v1)' )) 220eq ( 1 , eval ( 'type(g:v2)' )) 221eq ( 'hello' , eval ( 'g:v1' )) 222eq ( '' , eval ( 'g:v2' )) 223end ) 224 225it ( 'supports omitting optional trailing opts' , function () 226eq ( 0 , eval ( 'nvim_get_hl_ns()' )) 227end ) 228end )