2017-01-29 13:22:50 -07:00
|
|
|
-- Test for Vim overrides of lua built-ins
|
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
2017-01-29 17:30:45 -07:00
|
|
|
local Screen = require('test.functional.ui.screen')
|
2017-01-29 13:22:50 -07:00
|
|
|
|
|
|
|
local eq = helpers.eq
|
2024-01-12 04:28:20 -07:00
|
|
|
local NIL = vim.NIL
|
2017-01-29 17:30:45 -07:00
|
|
|
local feed = helpers.feed
|
2017-01-29 13:22:50 -07:00
|
|
|
local clear = helpers.clear
|
2024-01-12 10:59:57 -07:00
|
|
|
local fn = helpers.fn
|
|
|
|
local api = helpers.api
|
2017-01-29 13:22:50 -07:00
|
|
|
local command = helpers.command
|
|
|
|
local write_file = helpers.write_file
|
2021-09-19 02:29:37 -07:00
|
|
|
local exec_capture = helpers.exec_capture
|
2019-06-29 11:15:12 -07:00
|
|
|
local exec_lua = helpers.exec_lua
|
2021-09-19 02:29:37 -07:00
|
|
|
local pcall_err = helpers.pcall_err
|
2022-11-21 17:13:30 -07:00
|
|
|
local is_os = helpers.is_os
|
2017-01-29 13:22:50 -07:00
|
|
|
|
|
|
|
local fname = 'Xtest-functional-lua-overrides-luafile'
|
|
|
|
|
|
|
|
before_each(clear)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove(fname)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('print', function()
|
|
|
|
it('returns nothing', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(NIL, fn.luaeval('print("abc")'))
|
|
|
|
eq(0, fn.luaeval('select("#", print("abc"))'))
|
2017-01-29 13:22:50 -07:00
|
|
|
end)
|
|
|
|
it('allows catching printed text with :execute', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('\nabc', fn.execute('lua print("abc")'))
|
|
|
|
eq('\nabc', fn.execute('luado print("abc")'))
|
|
|
|
eq('\nabc', fn.execute('call luaeval("print(\'abc\')")'))
|
2017-01-29 13:22:50 -07:00
|
|
|
write_file(fname, 'print("abc")')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('\nabc', fn.execute('luafile ' .. fname))
|
2017-01-29 13:22:50 -07:00
|
|
|
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('abc', exec_capture('lua print("abc")'))
|
|
|
|
eq('abc', exec_capture('luado print("abc")'))
|
|
|
|
eq('abc', exec_capture('call luaeval("print(\'abc\')")'))
|
2017-01-29 13:22:50 -07:00
|
|
|
write_file(fname, 'print("abc")')
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('abc', exec_capture('luafile ' .. fname))
|
2017-01-29 13:22:50 -07:00
|
|
|
end)
|
|
|
|
it('handles errors in __tostring', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
write_file(
|
|
|
|
fname,
|
|
|
|
[[
|
2017-01-29 13:22:50 -07:00
|
|
|
local meta_nilerr = { __tostring = function() error(nil) end }
|
|
|
|
local meta_abcerr = { __tostring = function() error("abc") end }
|
|
|
|
local meta_tblout = { __tostring = function() return {"TEST"} end }
|
|
|
|
v_nilerr = setmetatable({}, meta_nilerr)
|
|
|
|
v_abcerr = setmetatable({}, meta_abcerr)
|
|
|
|
v_tblout = setmetatable({}, meta_tblout)
|
2024-01-02 18:09:18 -07:00
|
|
|
]]
|
|
|
|
)
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture('luafile ' .. fname))
|
2019-10-30 12:53:09 -07:00
|
|
|
-- TODO(bfredl): these look weird, print() should not use "E5114:" style errors..
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'Vim(lua):E5108: Error executing lua E5114: Error while converting print argument #2: [NULL]',
|
|
|
|
pcall_err(command, 'lua print("foo", v_nilerr, "bar")')
|
|
|
|
)
|
|
|
|
eq(
|
|
|
|
'Vim(lua):E5108: Error executing lua E5114: Error while converting print argument #2: Xtest-functional-lua-overrides-luafile:2: abc',
|
|
|
|
pcall_err(command, 'lua print("foo", v_abcerr, "bar")')
|
|
|
|
)
|
|
|
|
eq(
|
|
|
|
'Vim(lua):E5108: Error executing lua E5114: Error while converting print argument #2: <Unknown error: lua_tolstring returned NULL for tostring result>',
|
|
|
|
pcall_err(command, 'lua print("foo", v_tblout, "bar")')
|
|
|
|
)
|
2017-01-29 13:22:50 -07:00
|
|
|
end)
|
2022-02-19 04:56:50 -07:00
|
|
|
it('coerces error values into strings', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
write_file(
|
|
|
|
fname,
|
|
|
|
[[
|
2022-02-19 04:56:50 -07:00
|
|
|
function string_error() error("my mistake") end
|
|
|
|
function number_error() error(1234) end
|
|
|
|
function nil_error() error(nil) end
|
|
|
|
function table_error() error({message = "my mistake"}) end
|
|
|
|
function custom_error()
|
|
|
|
local err = {message = "my mistake", code = 11234}
|
|
|
|
setmetatable(err, {
|
|
|
|
__tostring = function(t)
|
|
|
|
return "Internal Error [" .. t.code .. "] " .. t.message
|
|
|
|
end
|
|
|
|
})
|
|
|
|
error(err)
|
|
|
|
end
|
|
|
|
function bad_custom_error()
|
|
|
|
local err = {message = "my mistake", code = 11234}
|
|
|
|
setmetatable(err, {
|
|
|
|
-- intentionally not a function, downstream programmer has made an mistake
|
|
|
|
__tostring = "Internal Error [" .. err.code .. "] " .. err.message
|
|
|
|
})
|
|
|
|
error(err)
|
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
]]
|
|
|
|
)
|
2022-02-19 04:56:50 -07:00
|
|
|
eq('', exec_capture('luafile ' .. fname))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'Vim(lua):E5108: Error executing lua Xtest-functional-lua-overrides-luafile:1: my mistake',
|
|
|
|
pcall_err(command, 'lua string_error()')
|
|
|
|
)
|
|
|
|
eq(
|
|
|
|
'Vim(lua):E5108: Error executing lua Xtest-functional-lua-overrides-luafile:2: 1234',
|
|
|
|
pcall_err(command, 'lua number_error()')
|
|
|
|
)
|
|
|
|
eq('Vim(lua):E5108: Error executing lua [NULL]', pcall_err(command, 'lua nil_error()'))
|
|
|
|
eq('Vim(lua):E5108: Error executing lua [NULL]', pcall_err(command, 'lua table_error()'))
|
|
|
|
eq(
|
|
|
|
'Vim(lua):E5108: Error executing lua Internal Error [11234] my mistake',
|
|
|
|
pcall_err(command, 'lua custom_error()')
|
|
|
|
)
|
|
|
|
eq('Vim(lua):E5108: Error executing lua [NULL]', pcall_err(command, 'lua bad_custom_error()'))
|
2022-02-19 04:56:50 -07:00
|
|
|
end)
|
2017-01-29 13:22:50 -07:00
|
|
|
it('prints strings with NULs and NLs correctly', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_option_value('more', true, {})
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'abc ^@ def\nghi^@^@^@jkl\nTEST\n\n\nT\n',
|
|
|
|
exec_capture([[lua print("abc \0 def\nghi\0\0\0jkl\nTEST\n\n\nT\n")]])
|
|
|
|
)
|
|
|
|
eq(
|
|
|
|
'abc ^@ def\nghi^@^@^@jkl\nTEST\n\n\nT^@',
|
|
|
|
exec_capture([[lua print("abc \0 def\nghi\0\0\0jkl\nTEST\n\n\nT\0")]])
|
|
|
|
)
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('T^@', exec_capture([[lua print("T\0")]]))
|
|
|
|
eq('T\n', exec_capture([[lua print("T\n")]]))
|
2017-01-29 13:22:50 -07:00
|
|
|
end)
|
2017-08-13 08:37:35 -07:00
|
|
|
it('prints empty strings correctly', function()
|
|
|
|
-- Regression: first test used to crash
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture('lua print("")'))
|
|
|
|
eq(' def', exec_capture('lua print("", "def")'))
|
|
|
|
eq('abc ', exec_capture('lua print("abc", "")'))
|
|
|
|
eq('abc def', exec_capture('lua print("abc", "", "def")'))
|
2017-08-13 08:37:35 -07:00
|
|
|
end)
|
2019-06-29 11:15:12 -07:00
|
|
|
it('defers printing in luv event handlers', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
exec_lua(
|
|
|
|
[[
|
2019-06-29 11:15:12 -07:00
|
|
|
local cmd = ...
|
|
|
|
function test()
|
2023-06-03 03:06:00 -07:00
|
|
|
local timer = vim.uv.new_timer()
|
2019-06-29 11:15:12 -07:00
|
|
|
local done = false
|
|
|
|
timer:start(10, 0, function()
|
|
|
|
print("very fast")
|
|
|
|
timer:close()
|
|
|
|
done = true
|
|
|
|
end)
|
|
|
|
-- be kind to slow travis OS X jobs:
|
|
|
|
-- loop until we know for sure the callback has been executed
|
|
|
|
while not done do
|
|
|
|
os.execute(cmd)
|
2023-06-03 03:06:00 -07:00
|
|
|
vim.uv.run("nowait") -- fake os_breakcheck()
|
2019-06-29 11:15:12 -07:00
|
|
|
end
|
|
|
|
print("very slow")
|
|
|
|
vim.api.nvim_command("sleep 1m") -- force deferred event processing
|
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
(is_os('win') and 'timeout 1') or 'sleep 0.1'
|
|
|
|
)
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('very slow\nvery fast', exec_capture('lua test()'))
|
2019-06-29 11:15:12 -07:00
|
|
|
end)
|
2023-07-03 16:19:02 -07:00
|
|
|
|
|
|
|
it('blank line in message works', function()
|
|
|
|
local screen = Screen.new(40, 8)
|
|
|
|
screen:attach()
|
|
|
|
screen:set_default_attr_ids({
|
2024-01-02 18:09:18 -07:00
|
|
|
[0] = { bold = true, foreground = Screen.colors.Blue },
|
|
|
|
[1] = { bold = true, foreground = Screen.colors.SeaGreen },
|
|
|
|
[2] = { bold = true, reverse = true },
|
2023-07-03 16:19:02 -07:00
|
|
|
})
|
|
|
|
feed([[:lua print('\na')<CR>]])
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2023-07-03 16:19:02 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*3
|
2023-07-03 16:19:02 -07:00
|
|
|
{2: }|
|
|
|
|
|
|
|
|
|
a |
|
|
|
|
{1:Press ENTER or type command to continue}^ |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2023-07-03 16:19:02 -07:00
|
|
|
feed('<CR>')
|
|
|
|
feed([[:lua print('b\n\nc')<CR>]])
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2023-07-03 16:19:02 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*2
|
2023-07-03 16:19:02 -07:00
|
|
|
{2: }|
|
|
|
|
b |
|
|
|
|
|
|
|
|
|
c |
|
|
|
|
{1:Press ENTER or type command to continue}^ |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2023-07-03 16:19:02 -07:00
|
|
|
end)
|
2017-01-29 13:22:50 -07:00
|
|
|
end)
|
2017-01-29 17:30:45 -07:00
|
|
|
|
|
|
|
describe('debug.debug', function()
|
2024-03-25 12:06:28 -07:00
|
|
|
local screen --- @type test.functional.ui.screen
|
2023-07-03 16:19:02 -07:00
|
|
|
|
2017-01-29 17:30:45 -07:00
|
|
|
before_each(function()
|
|
|
|
screen = Screen.new()
|
|
|
|
screen:attach()
|
2022-08-17 07:19:35 -07:00
|
|
|
screen:set_default_attr_ids {
|
2024-01-02 18:09:18 -07:00
|
|
|
[0] = { bold = true, foreground = 255 },
|
|
|
|
[1] = { bold = true, reverse = true },
|
|
|
|
E = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
|
|
|
|
cr = { bold = true, foreground = Screen.colors.SeaGreen4 },
|
2022-08-17 07:19:35 -07:00
|
|
|
}
|
2017-01-29 17:30:45 -07:00
|
|
|
end)
|
2022-08-17 07:19:35 -07:00
|
|
|
|
2017-01-29 17:30:45 -07:00
|
|
|
it('works', function()
|
2017-01-29 17:35:45 -07:00
|
|
|
command([[lua
|
2017-01-29 17:30:45 -07:00
|
|
|
function Test(a)
|
|
|
|
print(a)
|
|
|
|
debug.debug()
|
|
|
|
print(a * 100)
|
|
|
|
end
|
|
|
|
]])
|
|
|
|
feed(':lua Test()\n')
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2022-08-17 07:19:35 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*10
|
2022-08-17 07:19:35 -07:00
|
|
|
{1: }|
|
2017-01-29 17:30:45 -07:00
|
|
|
nil |
|
|
|
|
lua_debug> ^ |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2017-01-29 17:30:45 -07:00
|
|
|
feed('print("TEST")\n')
|
|
|
|
screen:expect([[
|
2022-08-17 07:19:35 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*8
|
2022-08-17 07:19:35 -07:00
|
|
|
{1: }|
|
2017-01-29 17:30:45 -07:00
|
|
|
nil |
|
|
|
|
lua_debug> print("TEST") |
|
|
|
|
TEST |
|
|
|
|
lua_debug> ^ |
|
|
|
|
]])
|
|
|
|
feed('<C-c>')
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2022-08-17 07:19:35 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*2
|
2022-08-17 07:19:35 -07:00
|
|
|
{1: }|
|
2017-01-29 17:30:45 -07:00
|
|
|
nil |
|
|
|
|
lua_debug> print("TEST") |
|
|
|
|
TEST |
|
|
|
|
|
|
2019-10-30 12:53:09 -07:00
|
|
|
{E:E5108: Error executing lua [string ":lua"]:5: attempt}|
|
|
|
|
{E: to perform arithmetic on local 'a' (a nil value)} |
|
2021-11-06 07:26:10 -07:00
|
|
|
{E:stack traceback:} |
|
|
|
|
{E: [string ":lua"]:5: in function 'Test'} |
|
|
|
|
{E: [string ":lua"]:1: in main chunk} |
|
2017-01-29 17:30:45 -07:00
|
|
|
Interrupt: {cr:Press ENTER or type command to continue}^ |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2017-01-29 17:30:45 -07:00
|
|
|
feed('<C-l>:lua Test()\n')
|
|
|
|
screen:expect([[
|
2022-08-17 07:19:35 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*10
|
2022-08-17 07:19:35 -07:00
|
|
|
{1: }|
|
2017-01-29 17:30:45 -07:00
|
|
|
nil |
|
|
|
|
lua_debug> ^ |
|
|
|
|
]])
|
|
|
|
feed('\n')
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2022-08-17 07:19:35 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*4
|
2022-08-17 07:19:35 -07:00
|
|
|
{1: }|
|
2017-01-29 17:30:45 -07:00
|
|
|
nil |
|
|
|
|
lua_debug> |
|
2019-10-30 12:53:09 -07:00
|
|
|
{E:E5108: Error executing lua [string ":lua"]:5: attempt}|
|
|
|
|
{E: to perform arithmetic on local 'a' (a nil value)} |
|
2021-11-06 07:26:10 -07:00
|
|
|
{E:stack traceback:} |
|
|
|
|
{E: [string ":lua"]:5: in function 'Test'} |
|
|
|
|
{E: [string ":lua"]:1: in main chunk} |
|
2017-01-29 17:30:45 -07:00
|
|
|
{cr:Press ENTER or type command to continue}^ |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2017-01-29 17:30:45 -07:00
|
|
|
end)
|
2019-08-04 10:42:31 -07:00
|
|
|
|
|
|
|
it("can be safely exited with 'cont'", function()
|
|
|
|
feed('<cr>')
|
|
|
|
feed(':lua debug.debug() print("x")<cr>')
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2019-08-04 10:42:31 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*12
|
2019-08-04 10:42:31 -07:00
|
|
|
lua_debug> ^ |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2019-08-04 10:42:31 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
feed('conttt<cr>') -- misspelled cont; invalid syntax
|
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2022-08-17 07:19:35 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*8
|
2022-08-17 07:19:35 -07:00
|
|
|
{1: }|
|
2019-08-04 10:42:31 -07:00
|
|
|
lua_debug> conttt |
|
|
|
|
{E:E5115: Error while loading debug string: (debug comma}|
|
|
|
|
{E:nd):1: '=' expected near '<eof>'} |
|
|
|
|
lua_debug> ^ |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2019-08-04 10:42:31 -07:00
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
feed('cont<cr>') -- exactly "cont", exit now
|
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2022-08-17 07:19:35 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*6
|
2022-08-17 07:19:35 -07:00
|
|
|
{1: }|
|
2019-08-04 10:42:31 -07:00
|
|
|
lua_debug> conttt |
|
|
|
|
{E:E5115: Error while loading debug string: (debug comma}|
|
|
|
|
{E:nd):1: '=' expected near '<eof>'} |
|
|
|
|
lua_debug> cont |
|
|
|
|
x |
|
|
|
|
{cr:Press ENTER or type command to continue}^ |
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2019-08-04 10:42:31 -07:00
|
|
|
|
|
|
|
feed('<cr>')
|
2024-01-02 18:09:18 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2019-08-04 10:42:31 -07:00
|
|
|
^ |
|
2023-12-09 05:42:00 -07:00
|
|
|
{0:~ }|*12
|
2019-08-04 10:42:31 -07:00
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
]],
|
|
|
|
}
|
2019-08-04 10:42:31 -07:00
|
|
|
end)
|
2017-01-29 17:30:45 -07:00
|
|
|
end)
|
2017-05-22 14:46:57 -07:00
|
|
|
|
2019-03-06 21:41:40 -07:00
|
|
|
describe('os.getenv', function()
|
2019-03-16 12:28:52 -07:00
|
|
|
it('returns nothing for undefined env var', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(NIL, fn.luaeval('os.getenv("XTEST_1")'))
|
2019-03-06 21:41:40 -07:00
|
|
|
end)
|
|
|
|
it('returns env var set by the parent process', function()
|
2019-03-16 12:28:52 -07:00
|
|
|
local value = 'foo'
|
2024-01-02 18:09:18 -07:00
|
|
|
clear({ env = { ['XTEST_1'] = value } })
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(value, fn.luaeval('os.getenv("XTEST_1")'))
|
2019-03-06 21:41:40 -07:00
|
|
|
end)
|
|
|
|
it('returns env var set by let', function()
|
|
|
|
local value = 'foo'
|
2024-01-12 06:11:28 -07:00
|
|
|
command('let $XTEST_1 = "' .. value .. '"')
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(value, fn.luaeval('os.getenv("XTEST_1")'))
|
2019-03-06 21:41:40 -07:00
|
|
|
end)
|
|
|
|
end)
|
2022-11-28 14:43:10 -07:00
|
|
|
|
|
|
|
-- "bit" module is always available, regardless if nvim is built with
|
|
|
|
-- luajit or PUC lua 5.1.
|
|
|
|
describe('bit module', function()
|
|
|
|
it('works', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(9, exec_lua [[ return require'bit'.band(11,13) ]])
|
2022-11-28 14:43:10 -07:00
|
|
|
end)
|
|
|
|
end)
|