robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Test suite for checking :lua* commands 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 NIL =vim . NIL 9local eval =n . eval 10local feed =n . feed 11local clear =n . clear 12local matches =t . matches 13local api =n . api 14local exec_lua =n . exec_lua 15local exec_capture =n . exec_capture 16local fn =n . fn 17local source =n . source 18local dedent =t . dedent 19local command =n . command 20local pcall_err =t . pcall_err 21local write_file =t . write_file 22local remove_trace =t . remove_trace 23 24before_each ( clear ) 25 26describe ( ':lua' , function () 27it ( 'works' , function () 28eq ( '' , exec_capture ( 'lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TEST"})' )) 29eq ( { '' , 'TEST' } , api . nvim_buf_get_lines ( 0 , 0 , 100 , false )) 30source ( [[ 31lua << EOF 32vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TSET"}) 33EOF]] ) 34eq ( { '' , 'TSET' } , api . nvim_buf_get_lines ( 0 , 0 , 100 , false )) 35source ( [[ 36lua << EOF 37vim.api.nvim_buf_set_lines(1, 1, 2, false, {"SETT"})]] ) 38eq ( { '' , 'SETT' } , api . nvim_buf_get_lines ( 0 , 0 , 100 , false )) 39source ( [[ 40lua << EOF 41vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"}) 42vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"}) 43vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"}) 44EOF]] ) 45eq ( { '' , 'ETTS' , 'TTSE' , 'STTE' } , api . nvim_buf_get_lines ( 0 , 0 , 100 , false )) 46matches ( 47'.*Vim%(lua%):E15: Invalid expression: .*' , 48pcall_err ( 49source , 50[[ 51lua << eval EOF 52{} 53EOF 54]] 55) 56) 57end ) 58 59it ( 'throws catchable errors' , function () 60eq ( 'Vim(lua):E471: Argument required' , pcall_err ( command , 'lua' )) 61eq ( 62[[Vim(lua):E5107: Lua: [string ":lua"]:0: unexpected symbol near ')']] , 63pcall_err ( command , 'lua ()' ) 64) 65eq ( 66[[Vim(lua):E5108: Lua: [string ":lua"]:0: TEST]] , 67remove_trace ( pcall_err ( command , 'lua error("TEST")' )) 68) 69eq ( 70[[Vim(lua):E5108: Lua: [string ":lua"]:0: Invalid buffer id: -10]] , 71remove_trace ( pcall_err ( command , 'lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})' )) 72) 73eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , 100 , false )) 74end ) 75 76it ( 'works with nil errors' , function () 77eq ( [=[Vim(lua):E5108: Lua: nil]=] , pcall_err ( command , 'lua error(nil)' )) 78end ) 79 80it ( 'works with errors with __tostring failure' , function () 81eq ( 82[=[Vim(lua):E5108: Lua: [UNPRINTABLE ERROR]]=] , 83pcall_err ( command , 'lua error(setmetatable({}, {__tostring=error}))' ) 84) 85end ) 86 87it ( 'works with printable errors' , function () 88eq ( [=[Vim(lua):E5108: Lua: false]=] , pcall_err ( command , 'lua error(false)' )) 89-- numbers get the location prefixed because numbers are almost strings (know the workplace rules) 90eq ( [=[Vim(lua):E5108: Lua: [string ":lua"]:0: 39]=] , pcall_err ( command , 'lua error(39)' )) 91matches ( [=[Vim%(lua%):E5108: Lua: table: 0x%x+]=] , pcall_err ( command , 'lua error({})' )) 92end ) 93 94it ( 'accepts embedded NLs without heredoc' , function () 95-- Such code is usually used for `:execute 'lua' {generated_string}`: 96-- heredocs do not work in this case. 97command ( [[ 98lua 99vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"}) 100vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"}) 101vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"}) 102]] ) 103eq ( { '' , 'ETTS' , 'TTSE' , 'STTE' } , api . nvim_buf_get_lines ( 0 , 0 , 100 , false )) 104end ) 105 106it ( 'preserves global and not preserves local variables' , function () 107eq ( '' , exec_capture ( 'lua gvar = 42' )) 108eq ( '' , exec_capture ( 'lua local lvar = 100500' )) 109eq ( NIL , fn . luaeval ( 'lvar' )) 110eq ( 42 , fn . luaeval ( 'gvar' )) 111end ) 112 113it ( 'works with long strings' , function () 114local s =( 'x' ): rep ( 100500 ) 115 116eq ( 117'Vim(lua):E5107: Lua: [string ":lua"]:0: unfinished string near \'<eof>\'' , 118pcall_err ( command , ( 'lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s})' ): format ( s )) 119) 120eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 121 122eq ( '' , exec_capture (( 'lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s"})' ): format ( s ))) 123eq ( { '' , s } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 124end ) 125 126it ( 'can show multiline error messages' , function () 127local screen =Screen . new ( 40 , 10 ) 128screen : set_default_attr_ids ( { 129[ 1 ] ={ bold =true , foreground =Screen . colors . Blue1 } , 130[ 2 ] ={ bold =true , reverse =true } , 131[ 3 ] ={ foreground =Screen . colors . Grey100 , background =Screen . colors . Red } , 132[ 4 ] ={ bold =true , foreground =Screen . colors . SeaGreen4 } , 133} ) 134 135feed ( ':lua error("fail\\nmuch error\\nsuch details")<cr>' ) 136screen : expect ( [[ 137| 138{2: }| 139{3:E5108: Lua: [string ":lua"]:1: fail} | 140{3:much error} | 141{3:such details} | 142{3:stack traceback:} | 143{3: [C]: in function 'error'} | 144{3: [string ":lua"]:1: in main chunk}| 145| 146{4:Press ENTER or type command to continue}^ | 147]] ) 148feed ( '<cr>' ) 149screen : expect { 150grid =[[ 151^ | 152{1:~ }|*8 153| 154]] , 155} 156eq ( 157'E5108: Lua: [string ":lua"]:1: fail\nmuch error\nsuch details' , 158remove_trace ( eval ( 'v:errmsg' )) 159) 160 161local expected ='Vim(lua):E5108: Lua: [string ":lua"]:0: some error\nin a\nAPI command' 162eq ( expected , remove_trace ( pcall_err ( command , 'lua error("some error\\nin a\\nAPI command")' ))) 163 164feed ( ':messages<cr>' ) 165screen : expect ( [[ 166| 167{2: }| 168{3:E5108: Lua: [string ":lua"]:1: fail} | 169{3:much error} | 170{3:such details} | 171{3:stack traceback:} | 172{3: [C]: in function 'error'} | 173{3: [string ":lua"]:1: in main chunk}| 174| 175{4:Press ENTER or type command to continue}^ | 176]] ) 177end ) 178 179it ( 'prints result of =expr' , function () 180exec_lua ( 'x = 5' ) 181eq ( '5' , exec_capture ( ':lua =x' )) 182eq ( '5' , exec_capture ( '=x' )) 183exec_lua ( 'x = "5"' ) 184eq ( '"5"' , exec_capture ( ':lua =x' )) 185eq ( '"5"' , exec_capture ( '=x' )) 186exec_lua ( "function x() return 'hello' end" ) 187eq ( '"hello"' , exec_capture ( ':lua = x()' )) 188exec_lua ( "function x() return 'hello ' end" ) 189eq ( '"hello "' , exec_capture ( ':lua = x()' )) 190exec_lua ( 'x = {a = 1, b = 2}' ) 191eq ( '{\n a = 1,\n b = 2\n}' , exec_capture ( ':lua =x' )) 192exec_lua ( function () 193function _G . x ( success ) 194if success then 195return true , 'Return value' 196else 197return false , nil , 'Error message' 198end 199end 200end ) 201eq ( 202dedent [[ 203true 204"Return value"]] , 205exec_capture ( ':lua =x(true)' ) 206) 207eq ( 208dedent [[ 209false 210nil 211"Error message"]] , 212exec_capture ( '=x(false)' ) 213) 214end ) 215 216it ( 'with range' , function () 217local screen =Screen . new ( 40 , 10 ) 218api . nvim_buf_set_lines ( 0 , 0 , 0 , 0 , { 'nonsense' , 'function x() print "hello" end' , 'x()' } ) 219 220-- ":{range}lua" fails on invalid Lua code. 221eq ( 222[[:{range}lua buffer=1: Vim(lua):E5107: Lua: ]] 223.. [[[string ":{range}lua buffer=1"]:0: '=' expected near '<eof>']] , 224pcall_err ( command , '1lua' ) 225) 226 227-- ":{range}lua" executes valid Lua code. 228feed ( ':2,3lua<CR>' ) 229screen : expect { 230grid =[[ 231nonsense | 232function x() print "hello" end | 233x() | 234^ | 235{1:~ }|*5 236hello | 237]] , 238attr_ids ={ 239[ 1 ] ={ foreground =Screen . colors . Blue , bold =true } , 240} , 241} 242 243-- ":{range}lua {code}" executes {code}, ignoring {range} 244eq ( '' , exec_capture ( '1lua gvar = 42' )) 245eq ( 42 , fn . luaeval ( 'gvar' )) 246end ) 247end ) 248 249describe ( ':luado command' , function () 250it ( 'works' , function () 251api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 'ABC' , 'def' , 'gHi' } ) 252eq ( '' , exec_capture ( 'luado lines = (lines or {}) lines[#lines + 1] = {linenr, line}' )) 253eq ( { 'ABC' , 'def' , 'gHi' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 254eq ( { { 1 , 'ABC' } , { 2 , 'def' } , { 3 , 'gHi' } } , fn . luaeval ( 'lines' )) 255 256-- Automatic transformation of numbers 257eq ( '' , exec_capture ( 'luado return linenr' )) 258eq ( { '1' , '2' , '3' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 259 260eq ( '' , exec_capture ( 'luado return ("<%02x>"):format(line:byte())' )) 261eq ( { '<31>' , '<32>' , '<33>' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 262end ) 263 264it ( 'stops processing lines when suddenly out of lines' , function () 265api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 'ABC' , 'def' , 'gHi' } ) 266eq ( '' , exec_capture ( '2,$luado runs = ((runs or 0) + 1) vim.api.nvim_command("%d")' )) 267eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 268eq ( 1 , fn . luaeval ( 'runs' )) 269 270api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'one' , 'two' , 'three' } ) 271eq ( '' , exec_capture ( 'luado vim.api.nvim_command("%d")' )) 272eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 273 274api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'one' , 'two' , 'three' } ) 275eq ( '' , exec_capture ( 'luado vim.api.nvim_command("1,2d")' )) 276eq ( { 'three' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 277 278api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'one' , 'two' , 'three' } ) 279eq ( '' , exec_capture ( 'luado vim.api.nvim_command("2,3d"); return "REPLACED"' )) 280eq ( { 'REPLACED' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 281 282api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'one' , 'two' , 'three' } ) 283eq ( '' , exec_capture ( '2,3luado vim.api.nvim_command("1,2d"); return "REPLACED"' )) 284eq ( { 'three' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 285end ) 286 287it ( 'fails on errors' , function () 288eq ( 289[[Vim(luado):E5109: Lua: [string ":luado"]:0: unexpected symbol near ')']] , 290pcall_err ( command , 'luado ()' ) 291) 292eq ( 293[[Vim(luado):E5111: Lua: [string ":luado"]:0: attempt to perform arithmetic on global 'liness' (a nil value)]] , 294pcall_err ( command , 'luado return liness + 1' ) 295) 296end ) 297 298it ( 'works with nil errors' , function () 299eq ( [=[Vim(luado):E5111: Lua: nil]=] , pcall_err ( command , 'luado error(nil)' )) 300end ) 301 302it ( 'fails in sandbox when needed' , function () 303api . nvim_buf_set_lines ( 0 , 0 , 1 , false , { 'ABC' , 'def' , 'gHi' } ) 304eq ( 305'Vim(luado):E48: Not allowed in sandbox: sandbox luado runs = (runs or 0) + 1' , 306pcall_err ( command , 'sandbox luado runs = (runs or 0) + 1' ) 307) 308eq ( NIL , fn . luaeval ( 'runs' )) 309end ) 310 311it ( 'works with long strings' , function () 312local s =( 'x' ): rep ( 100500 ) 313 314eq ( 315'Vim(luado):E5109: Lua: [string ":luado"]:0: unfinished string near \'<eof>\'' , 316pcall_err ( command , ( 'luado return "%s' ): format ( s )) 317) 318eq ( { '' } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 319 320eq ( '' , exec_capture (( 'luado return "%s"' ): format ( s ))) 321eq ( { s } , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )) 322end ) 323end ) 324 325describe ( ':luafile' , function () 326local fname ='Xtest-functional-lua-commands-luafile' 327 328after_each ( function () 329os . remove ( fname ) 330end ) 331 332it ( 'works' , function () 333write_file ( 334fname , 335[[ 336vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"}) 337vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"}) 338vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"}) 339]] 340) 341eq ( '' , exec_capture ( 'luafile ' .. fname )) 342eq ( { '' , 'ETTS' , 'TTSE' , 'STTE' } , api . nvim_buf_get_lines ( 0 , 0 , 100 , false )) 343end ) 344 345it ( 'correctly errors out' , function () 346write_file ( fname , '()' ) 347eq ( 348( "Vim(luafile):E5112: Lua chunk: %s:1: unexpected symbol near ')'" ): format ( fname ), 349pcall_err ( command , 'luafile ' .. fname ) 350) 351write_file ( fname , 'vimm.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})' ) 352eq ( 353( "Vim(luafile):E5113: Lua chunk: %s:1: attempt to index global 'vimm' (a nil value)" ): format ( 354fname 355), 356remove_trace ( pcall_err ( command , 'luafile ' .. fname )) 357) 358end ) 359 360it ( 'works with nil errors' , function () 361write_file ( fname , 'error(nil)' ) 362eq ( [=[Vim(luafile):E5113: Lua chunk: nil]=] , pcall_err ( command , 'luafile ' .. fname )) 363end ) 364end )