robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Test for Vim overrides of lua built-ins 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4local Screen =require ( 'test.functional.ui.screen' ) 5 6local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 7local eq =t . eq 8local matches =t . matches 9local NIL =vim . NIL 10local feed =n . feed 11local clear =n . clear 12local fn =n . fn 13local api =n . api 14local command =n . command 15local write_file =t . write_file 16local exec_capture =n . exec_capture 17local exec_lua =n . exec_lua 18local pcall_err =t . pcall_err 19local is_os =t . is_os 20 21local fname ='Xtest-functional-lua-overrides-luafile' 22 23before_each ( clear ) 24 25after_each ( function () 26os . remove ( fname ) 27end ) 28 29describe ( 'print' , function () 30it ( 'returns nothing' , function () 31eq ( NIL , fn . luaeval ( 'print("abc")' )) 32eq ( 0 , fn . luaeval ( 'select("#", print("abc"))' )) 33end ) 34it ( 'allows catching printed text with :execute' , function () 35eq ( '\nabc' , fn . execute ( 'lua print("abc")' )) 36eq ( '\nabc' , fn . execute ( 'luado print("abc")' )) 37eq ( '\nabc' , fn . execute ( 'call luaeval("print(\'abc\')")' )) 38write_file ( fname , 'print("abc")' ) 39eq ( '\nabc' , fn . execute ( 'luafile ' .. fname )) 40 41eq ( 'abc' , exec_capture ( 'lua print("abc")' )) 42eq ( 'abc' , exec_capture ( 'luado print("abc")' )) 43eq ( 'abc' , exec_capture ( 'call luaeval("print(\'abc\')")' )) 44write_file ( fname , 'print("abc")' ) 45eq ( 'abc' , exec_capture ( 'luafile ' .. fname )) 46end ) 47it ( 'handles errors in __tostring' , function () 48write_file ( 49fname , 50[[ 51local meta_nilerr = { __tostring = function() error(nil) end } 52local meta_abcerr = { __tostring = function() error("abc") end } 53local meta_tblout = { __tostring = function() return {"TEST"} end } 54v_nilerr = setmetatable({}, meta_nilerr) 55v_abcerr = setmetatable({}, meta_abcerr) 56v_tblout = setmetatable({}, meta_tblout) 57]] 58) 59eq ( '' , exec_capture ( 'luafile ' .. fname )) 60-- TODO(bfredl): these look weird, print() should not use "E5114:" style errors.. 61eq ( 62'Vim(lua):E5108: Lua: E5114: Converting print argument #2: nil' , 63pcall_err ( command , 'lua print("foo", v_nilerr, "bar")' ) 64) 65eq ( 66'Vim(lua):E5108: Lua: E5114: Converting print argument #2: Xtest-functional-lua-overrides-luafile:2: abc' , 67pcall_err ( command , 'lua print("foo", v_abcerr, "bar")' ) 68) 69matches ( 70'^Vim%(lua%):E5108: Lua: E5114: Converting print argument #2: table: 0x%x+$' , 71pcall_err ( command , 'lua print("foo", v_tblout, "bar")' ) 72) 73end ) 74it ( 'coerces error values into strings' , function () 75write_file ( 76fname , 77[[ 78function string_error() error("my mistake") end 79function number_error() error(1234) end 80function nil_error() error(nil) end 81function table_error() error({message = "my mistake"}) end 82function custom_error() 83local err = {message = "my mistake", code = 11234} 84setmetatable(err, { 85__tostring = function(t) 86return "Internal Error [" .. t.code .. "] " .. t.message 87end 88}) 89error(err) 90end 91function bad_custom_error() 92local err = {message = "my mistake", code = 11234} 93setmetatable(err, { 94-- intentionally not a function, downstream programmer has made an mistake 95__tostring = "Internal Error [" .. err.code .. "] " .. err.message 96}) 97error(err) 98end 99]] 100) 101eq ( '' , exec_capture ( 'luafile ' .. fname )) 102eq ( 103'Vim(lua):E5108: Lua: Xtest-functional-lua-overrides-luafile:1: my mistake' , 104pcall_err ( command , 'lua string_error()' ) 105) 106eq ( 107'Vim(lua):E5108: Lua: Xtest-functional-lua-overrides-luafile:2: 1234' , 108pcall_err ( command , 'lua number_error()' ) 109) 110eq ( 'Vim(lua):E5108: Lua: nil' , pcall_err ( command , 'lua nil_error()' )) 111matches ( '^Vim%(lua%):E5108: Lua: table: 0x%x+$' , pcall_err ( command , 'lua table_error()' )) 112eq ( 113'Vim(lua):E5108: Lua: Internal Error [11234] my mistake' , 114pcall_err ( command , 'lua custom_error()' ) 115) 116eq ( 'Vim(lua):E5108: Lua: [UNPRINTABLE ERROR]' , pcall_err ( command , 'lua bad_custom_error()' )) 117end ) 118it ( 'prints strings with NULs and NLs correctly' , function () 119api . nvim_set_option_value ( 'more' , true , {} ) 120eq ( 121'abc ^@ def\nghi^@^@^@jkl\nTEST\n\n\nT\n' , 122exec_capture ( [[lua print("abc \0 def\nghi\0\0\0jkl\nTEST\n\n\nT\n")]] ) 123) 124eq ( 125'abc ^@ def\nghi^@^@^@jkl\nTEST\n\n\nT^@' , 126exec_capture ( [[lua print("abc \0 def\nghi\0\0\0jkl\nTEST\n\n\nT\0")]] ) 127) 128eq ( 'T^@' , exec_capture ( [[lua print("T\0")]] )) 129eq ( 'T\n' , exec_capture ( [[lua print("T\n")]] )) 130end ) 131it ( 'prints empty strings correctly' , function () 132-- Regression: first test used to crash 133eq ( '' , exec_capture ( 'lua print("")' )) 134eq ( ' def' , exec_capture ( 'lua print("", "def")' )) 135eq ( 'abc ' , exec_capture ( 'lua print("abc", "")' )) 136eq ( 'abc def' , exec_capture ( 'lua print("abc", "", "def")' )) 137end ) 138it ( 'defers printing in luv event handlers' , function () 139exec_lua ( function ( cmd ) 140function test () 141local timer =vim . uv . new_timer () 142local done =false 143timer : start ( 10 , 0 , function () 144( 'very fast' ) 145timer : close () 146done =true 147end ) 148-- be kind to slow travis OS X jobs: 149-- loop until we know for sure the callback has been executed 150while not done do 151os . execute ( cmd ) 152vim . uv . run ( 'nowait' ) -- fake os_breakcheck() 153end 154( 'very slow' ) 155vim . api . nvim_command ( 'sleep 1m' ) -- force deferred event processing 156end 157end , ( is_os ( 'win' ) and 'timeout 1' ) or 'sleep 0.1' ) 158eq ( 'very slow\nvery fast' , exec_capture ( 'lua test()' )) 159end ) 160 161it ( 'blank line in message works' , function () 162local screen =Screen . new ( 40 , 8 ) 163feed ( [[:lua print('\na')<CR>]] ) 164screen : expect { 165grid =[[ 166| 167{1:~ }|*3 168{3: }| 169| 170a | 171{6:Press ENTER or type command to continue}^ | 172]] , 173} 174feed ( '<CR>' ) 175feed ( [[:lua print('b\n\nc')<CR>]] ) 176screen : expect { 177grid =[[ 178| 179{1:~ }|*2 180{3: }| 181b | 182| 183c | 184{6:Press ENTER or type command to continue}^ | 185]] , 186} 187end ) 188end ) 189 190describe ( '_G.debug' , function () 191local screen --- @type test.functional.ui.screen 192 193before_each ( function () 194screen =Screen . new () 195end ) 196 197it ( 'if broken, Nvim reports it instead of crashing #41504' , function () 198-- If user code breaks `_G.debug.traceback`, Nvim should function enough to allow ":qa!". 199-- But `_G.debug=nil` panics immediately, and that is intentional/wontfix. 200for _ , override in ipairs ( { 'debug.traceback = nil' , '_G.debug = {}' } ) do 201clear () 202eq ( 203true , 204exec_lua (( [[ 205%s 206local called = false 207-- nvim_buf_call() is the shortest path from Lua to nlua_pcall(). 208vim.api.nvim_buf_call(0, function() called = true end) 209return called 210]] ): format ( override )) 211) 212-- Error reports the broken situation. 213matches ( 214'boom\nNo traceback: debug.traceback is not a function' , 215pcall_err ( exec_lua , [[vim.api.nvim_buf_call(0, function() error('boom') end)]] ) 216) 217-- vim.on_key() callbacks run on every keystroke, they should not break either. 218exec_lua ( [[ 219_G.keys = 0 220vim.on_key(function() _G.keys = _G.keys + 1 end) 221]] ) 222feed ( 'jjj' ) 223eq ( 3 , exec_lua ( 'return _G.keys' )) 224end 225 226-- We accept non-broken `debug.traceback` overrides (e.g. tui_spec.lua does this). 227matches ( 228'boom %(custom traceback%)' , 229pcall_err ( 230exec_lua , 231[[ 232debug.traceback = function(msg) return msg .. ' (custom traceback)' end 233vim.api.nvim_buf_call(0, function() error('boom') end) 234]] 235) 236) 237end ) 238 239it ( 'debug() works' , function () 240command ( [[lua 241function Test(a) 242print(a) 243debug.debug() 244print(a * 100) 245end 246]] ) 247feed ( ':lua Test()\n' ) 248screen : expect { 249grid =[[ 250| 251{1:~ }|*10 252{3: }| 253nil | 254lua_debug> ^ | 255]] , 256} 257feed ( 'print("TEST")\n' ) 258screen : expect ( [[ 259| 260{1:~ }|*8 261{3: }| 262nil | 263lua_debug> print("TEST") | 264TEST | 265lua_debug> ^ | 266]] ) 267feed ( '<C-c>' ) 268screen : expect ( [[ 269| 270{1:~ }|*2 271{3: }| 272nil | 273lua_debug> print("TEST") | 274TEST | 275| 276{9:E5108: Lua: [string ":lua"]:5: attempt to perform ari}| 277{9:thmetic on local 'a' (a nil value)} | 278{9:stack traceback:} | 279{9: [string ":lua"]:5: in function 'Test'} | 280{9: [string ":lua"]:1: in main chunk} | 281Interrupt: {6:Press ENTER or type command to continue}^ | 282]] ) 283feed ( '<C-l>:lua Test()\n' ) 284screen : expect ( [[ 285| 286{1:~ }|*10 287{3: }| 288nil | 289lua_debug> ^ | 290]] ) 291feed ( '\n' ) 292screen : expect ( [[ 293| 294{1:~ }|*4 295{3: }| 296nil | 297lua_debug> | 298{9:E5108: Lua: [string ":lua"]:5: attempt to perform ari}| 299{9:thmetic on local 'a' (a nil value)} | 300{9:stack traceback:} | 301{9: [string ":lua"]:5: in function 'Test'} | 302{9: [string ":lua"]:1: in main chunk} | 303{6:Press ENTER or type command to continue}^ | 304]] ) 305end ) 306 307it ( "debug() can be safely exited with 'cont'" , function () 308feed ( '<cr>' ) 309feed ( ':lua debug.debug() print("x")<cr>' ) 310screen : expect { 311grid =[[ 312| 313{1:~ }|*12 314lua_debug> ^ | 315]] , 316} 317 318feed ( 'conttt<cr>' ) -- misspelled cont; invalid syntax 319screen : expect ( [[ 320| 321{1:~ }|*8 322{3: }| 323lua_debug> conttt | 324{9:E5115: Loading Lua debug string: (debug command):1: '}| 325{9:=' expected near '<eof>'} | 326lua_debug> ^ | 327]] ) 328 329feed ( 'cont<cr>' ) -- exactly "cont", exit now 330screen : expect ( [[ 331| 332{1:~ }|*6 333{3: }| 334lua_debug> conttt | 335{9:E5115: Loading Lua debug string: (debug command):1: '}| 336{9:=' expected near '<eof>'} | 337lua_debug> cont | 338x | 339{6:Press ENTER or type command to continue}^ | 340]] ) 341 342feed ( '<cr>' ) 343screen : expect { 344grid =[[ 345^ | 346{1:~ }|*12 347| 348]] , 349} 350end ) 351end ) 352 353describe ( 'os.getenv' , function () 354it ( 'returns nothing for undefined env var' , function () 355eq ( NIL , fn . luaeval ( 'os.getenv("XTEST_1")' )) 356end ) 357it ( 'returns env var set by the parent process' , function () 358local value ='foo' 359clear ( { env ={ [ 'XTEST_1' ] =value } } ) 360eq ( value , fn . luaeval ( 'os.getenv("XTEST_1")' )) 361end ) 362it ( 'returns env var set by let' , function () 363local value ='foo' 364command ( 'let $XTEST_1 = "' .. value .. '"' ) 365eq ( value , fn . luaeval ( 'os.getenv("XTEST_1")' )) 366end ) 367end ) 368 369-- "bit" module is always available, regardless if nvim is built with 370-- luajit or PUC lua 5.1. 371describe ( 'bit module' , function () 372it ( 'works' , function () 373eq ( 9 , exec_lua [[ return require'bit'.band(11,13) ]] ) 374end ) 375end )