2017-01-20 14:00:47 -07:00
|
|
|
-- Test suite for checking :lua* commands
|
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
2019-01-22 11:55:31 -07:00
|
|
|
local Screen = require('test.functional.ui.screen')
|
2017-01-20 14:00:47 -07:00
|
|
|
|
|
|
|
local eq = helpers.eq
|
2024-01-12 04:28:20 -07:00
|
|
|
local NIL = vim.NIL
|
2019-01-22 11:55:31 -07:00
|
|
|
local eval = helpers.eval
|
|
|
|
local feed = helpers.feed
|
2017-01-28 18:11:18 -07:00
|
|
|
local clear = helpers.clear
|
2023-04-28 18:23:31 -07:00
|
|
|
local matches = helpers.matches
|
2024-01-12 10:59:57 -07:00
|
|
|
local api = helpers.api
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
local exec_lua = helpers.exec_lua
|
|
|
|
local exec_capture = helpers.exec_capture
|
2024-01-12 10:59:57 -07:00
|
|
|
local fn = helpers.fn
|
2017-01-28 18:11:18 -07:00
|
|
|
local source = helpers.source
|
|
|
|
local dedent = helpers.dedent
|
2019-01-22 11:55:31 -07:00
|
|
|
local command = helpers.command
|
2017-01-28 18:11:18 -07:00
|
|
|
local exc_exec = helpers.exc_exec
|
2019-10-30 12:53:09 -07:00
|
|
|
local pcall_err = helpers.pcall_err
|
2017-01-29 09:51:55 -07:00
|
|
|
local write_file = helpers.write_file
|
2021-11-06 07:26:10 -07:00
|
|
|
local remove_trace = helpers.remove_trace
|
2017-01-28 18:11:18 -07:00
|
|
|
|
|
|
|
before_each(clear)
|
|
|
|
|
2024-01-27 11:40:30 -07:00
|
|
|
describe(':lua', function()
|
2017-01-28 18:11:18 -07:00
|
|
|
it('works', function()
|
|
|
|
eq('', exec_capture('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TEST"})'))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '', 'TEST' }, api.nvim_buf_get_lines(0, 0, 100, false))
|
2023-04-28 18:23:31 -07:00
|
|
|
source([[
|
2017-01-28 18:11:18 -07:00
|
|
|
lua << EOF
|
|
|
|
vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TSET"})
|
2023-04-28 18:23:31 -07:00
|
|
|
EOF]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '', 'TSET' }, api.nvim_buf_get_lines(0, 0, 100, false))
|
2023-04-28 18:23:31 -07:00
|
|
|
source([[
|
2017-01-28 18:11:18 -07:00
|
|
|
lua << EOF
|
2023-04-28 18:23:31 -07:00
|
|
|
vim.api.nvim_buf_set_lines(1, 1, 2, false, {"SETT"})]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '', 'SETT' }, api.nvim_buf_get_lines(0, 0, 100, false))
|
2023-04-28 18:23:31 -07:00
|
|
|
source([[
|
2017-01-28 18:11:18 -07:00
|
|
|
lua << EOF
|
|
|
|
vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
|
|
|
|
vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
|
|
|
|
vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
|
2023-04-28 18:23:31 -07:00
|
|
|
EOF]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '', 'ETTS', 'TTSE', 'STTE' }, api.nvim_buf_get_lines(0, 0, 100, false))
|
2023-04-28 18:23:31 -07:00
|
|
|
matches(
|
|
|
|
'.*Vim%(lua%):E15: Invalid expression: .*',
|
|
|
|
pcall_err(
|
|
|
|
source,
|
|
|
|
[[
|
|
|
|
lua << eval EOF
|
|
|
|
{}
|
|
|
|
EOF
|
|
|
|
]]
|
|
|
|
)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-01-28 18:11:18 -07:00
|
|
|
end)
|
2024-01-27 11:40:30 -07:00
|
|
|
|
2017-01-28 18:11:18 -07:00
|
|
|
it('throws catchable errors', function()
|
2024-02-01 22:14:10 -07:00
|
|
|
eq('Vim(lua):E471: Argument required', pcall_err(command, 'lua'))
|
2020-09-12 19:04:22 -07:00
|
|
|
eq(
|
|
|
|
[[Vim(lua):E5107: Error loading lua [string ":lua"]:0: unexpected symbol near ')']],
|
2019-10-30 12:53:09 -07:00
|
|
|
pcall_err(command, 'lua ()')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2019-10-30 12:53:09 -07:00
|
|
|
eq(
|
|
|
|
[[Vim(lua):E5108: Error executing lua [string ":lua"]:1: TEST]],
|
2021-11-06 07:26:10 -07:00
|
|
|
remove_trace(exc_exec('lua error("TEST")'))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2020-01-14 01:21:10 -07:00
|
|
|
eq(
|
|
|
|
[[Vim(lua):E5108: Error executing lua [string ":lua"]:1: Invalid buffer id: -10]],
|
2021-11-06 07:26:10 -07:00
|
|
|
remove_trace(exc_exec('lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})'))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '' }, api.nvim_buf_get_lines(0, 0, 100, false))
|
2017-01-28 18:11:18 -07:00
|
|
|
end)
|
2024-01-27 11:40:30 -07:00
|
|
|
|
2017-01-29 13:22:50 -07:00
|
|
|
it('works with NULL errors', function()
|
2019-10-30 12:53:09 -07:00
|
|
|
eq([=[Vim(lua):E5108: Error executing lua [NULL]]=], exc_exec('lua error(nil)'))
|
2017-01-29 13:22:50 -07:00
|
|
|
end)
|
2024-01-27 11:40:30 -07:00
|
|
|
|
2017-01-28 18:11:18 -07:00
|
|
|
it('accepts embedded NLs without heredoc', function()
|
|
|
|
-- Such code is usually used for `:execute 'lua' {generated_string}`:
|
|
|
|
-- heredocs do not work in this case.
|
2024-01-12 06:11:28 -07:00
|
|
|
command([[
|
2017-01-28 18:11:18 -07:00
|
|
|
lua
|
|
|
|
vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
|
|
|
|
vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
|
|
|
|
vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
|
|
|
|
]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '', 'ETTS', 'TTSE', 'STTE' }, api.nvim_buf_get_lines(0, 0, 100, false))
|
2017-01-28 18:11:18 -07:00
|
|
|
end)
|
2024-01-27 11:40:30 -07:00
|
|
|
|
2017-01-28 19:09:54 -07:00
|
|
|
it('preserves global and not preserves local variables', function()
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture('lua gvar = 42'))
|
|
|
|
eq('', exec_capture('lua local lvar = 100500'))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(NIL, fn.luaeval('lvar'))
|
|
|
|
eq(42, fn.luaeval('gvar'))
|
2017-01-28 19:09:54 -07:00
|
|
|
end)
|
2024-01-27 11:40:30 -07:00
|
|
|
|
2017-01-29 09:32:01 -07:00
|
|
|
it('works with long strings', function()
|
|
|
|
local s = ('x'):rep(100500)
|
|
|
|
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(lua):E5107: Error loading lua [string ":lua"]:0: unfinished string near \'<eof>\'',
|
|
|
|
pcall_err(command, ('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s})'):format(s))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
2017-01-29 09:32:01 -07:00
|
|
|
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture(('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s"})'):format(s)))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '', s }, api.nvim_buf_get_lines(0, 0, -1, false))
|
2017-01-29 09:32:01 -07:00
|
|
|
end)
|
2019-01-22 11:55:31 -07:00
|
|
|
|
|
|
|
it('can show multiline error messages', function()
|
2019-10-30 12:53:09 -07:00
|
|
|
local screen = Screen.new(40, 10)
|
2019-01-22 11:55:31 -07:00
|
|
|
screen:attach()
|
|
|
|
screen:set_default_attr_ids({
|
|
|
|
[1] = { bold = true, foreground = Screen.colors.Blue1 },
|
|
|
|
[2] = { bold = true, reverse = true },
|
|
|
|
[3] = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
|
|
|
|
[4] = { bold = true, foreground = Screen.colors.SeaGreen4 },
|
|
|
|
})
|
|
|
|
|
|
|
|
feed(':lua error("fail\\nmuch error\\nsuch details")<cr>')
|
2019-10-30 12:53:09 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
|
|
|
{2: }|
|
|
|
|
{3:E5108: Error executing lua [string ":lua}|
|
|
|
|
{3:"]:1: fail} |
|
|
|
|
{3:much error} |
|
|
|
|
{3:such details} |
|
2021-11-06 07:26:10 -07:00
|
|
|
{3:stack traceback:} |
|
|
|
|
{3: [C]: in function 'error'} |
|
|
|
|
{3: [string ":lua"]:1: in main chunk}|
|
|
|
|
|
|
2019-10-30 12:53:09 -07:00
|
|
|
{4:Press ENTER or type command to continue}^ |
|
|
|
|
]],
|
|
|
|
}
|
2019-01-22 11:55:31 -07:00
|
|
|
feed('<cr>')
|
2019-10-30 12:53:09 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
|
|
|
^ |
|
2023-12-09 05:42:00 -07:00
|
|
|
{1:~ }|*8
|
2019-10-30 12:53:09 -07:00
|
|
|
|
|
|
|
|
]],
|
|
|
|
}
|
2021-11-06 07:26:10 -07:00
|
|
|
eq(
|
|
|
|
'E5108: Error executing lua [string ":lua"]:1: fail\nmuch error\nsuch details',
|
|
|
|
remove_trace(eval('v:errmsg'))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2019-01-22 11:55:31 -07:00
|
|
|
|
|
|
|
local status, err = pcall(command, 'lua error("some error\\nin a\\nAPI command")')
|
2019-10-30 12:53:09 -07:00
|
|
|
local expected =
|
|
|
|
'Vim(lua):E5108: Error executing lua [string ":lua"]:1: some error\nin a\nAPI command'
|
2019-01-22 11:55:31 -07:00
|
|
|
eq(false, status)
|
2021-11-06 07:26:10 -07:00
|
|
|
eq(expected, string.sub(remove_trace(err), -string.len(expected)))
|
2019-01-22 11:55:31 -07:00
|
|
|
|
|
|
|
feed(':messages<cr>')
|
2019-10-30 12:53:09 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
|
|
|
{2: }|
|
|
|
|
{3:E5108: Error executing lua [string ":lua}|
|
|
|
|
{3:"]:1: fail} |
|
|
|
|
{3:much error} |
|
|
|
|
{3:such details} |
|
2021-11-06 07:26:10 -07:00
|
|
|
{3:stack traceback:} |
|
|
|
|
{3: [C]: in function 'error'} |
|
|
|
|
{3: [string ":lua"]:1: in main chunk}|
|
|
|
|
|
|
2019-10-30 12:53:09 -07:00
|
|
|
{4:Press ENTER or type command to continue}^ |
|
|
|
|
]],
|
|
|
|
}
|
2019-01-22 11:55:31 -07:00
|
|
|
end)
|
2022-01-03 22:05:15 -07:00
|
|
|
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
it('prints result of =expr', function()
|
|
|
|
exec_lua('x = 5')
|
|
|
|
eq('5', exec_capture(':lua =x'))
|
2023-03-20 13:11:10 -07:00
|
|
|
eq('5', exec_capture('=x'))
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
exec_lua("function x() return 'hello' end")
|
|
|
|
eq('hello', exec_capture(':lua = x()'))
|
|
|
|
exec_lua('x = {a = 1, b = 2}')
|
|
|
|
eq('{\n a = 1,\n b = 2\n}', exec_capture(':lua =x'))
|
|
|
|
exec_lua([[function x(success)
|
2022-01-06 11:42:31 -07:00
|
|
|
if success then
|
|
|
|
return true, "Return value"
|
|
|
|
else
|
|
|
|
return false, nil, "Error message"
|
|
|
|
end
|
|
|
|
end]])
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
eq(
|
|
|
|
dedent [[
|
|
|
|
true
|
|
|
|
Return value]],
|
|
|
|
exec_capture(':lua =x(true)')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
refactor!: rename vim.pretty_print => vim.print
Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
(except external projects like penlight or python?), which reduces
discoverability, and degrades signaling about best practices.
Solution:
- Rename to `vim.print`.
- Change the behavior so that
1. strings are printed without quotes
2. each arg is printed on its own line
3. tables are indented with 2 instead of 4 spaces
- Example:
:lua ='a', 'b', 42, {a=3}
a
b
42
{
a = 3
}
Comparison of alternatives:
- `vim.print`:
- pro: consistent with Lua's `print()`
- pro: aligns with potential `nvim_print` API function which will
replace nvim_echo, nvim_notify, etc.
- con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
- pro: `:echo` has similar "pretty print" behavior.
- con: inconsistent with Lua idioms.
- `vim.p`:
- pro: very short, fits with `vim.o`, etc.
- con: not as discoverable as "echo"
- con: less opportunity for `local p = vim.p` because of potential shadowing.
2023-03-07 08:04:57 -07:00
|
|
|
eq(
|
|
|
|
dedent [[
|
|
|
|
false
|
|
|
|
nil
|
|
|
|
Error message]],
|
2023-03-20 13:11:10 -07:00
|
|
|
exec_capture('=x(false)')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2022-01-03 22:05:15 -07:00
|
|
|
end)
|
2024-01-26 18:00:50 -07:00
|
|
|
|
2024-01-27 11:40:30 -07:00
|
|
|
it('with range', function()
|
2024-01-26 18:00:50 -07:00
|
|
|
local screen = Screen.new(40, 10)
|
|
|
|
screen:attach()
|
2024-01-27 11:40:30 -07:00
|
|
|
api.nvim_buf_set_lines(0, 0, 0, 0, { 'nonsense', 'function x() print "hello" end', 'x()' })
|
|
|
|
|
|
|
|
-- ":{range}lua" fails on invalid Lua code.
|
|
|
|
eq(
|
|
|
|
[[:{range}lua: Vim(lua):E5107: Error loading lua [string ":{range}lua"]:0: '=' expected near '<eof>']],
|
|
|
|
pcall_err(command, '1lua')
|
|
|
|
)
|
|
|
|
|
|
|
|
-- ":{range}lua" executes valid Lua code.
|
|
|
|
feed(':2,3lua<CR>')
|
2024-01-26 18:00:50 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2024-01-27 11:40:30 -07:00
|
|
|
nonsense |
|
2024-01-26 18:00:50 -07:00
|
|
|
function x() print "hello" end |
|
|
|
|
x() |
|
|
|
|
^ |
|
2024-01-27 11:40:30 -07:00
|
|
|
{1:~ }|*5
|
2024-01-26 18:00:50 -07:00
|
|
|
hello |
|
|
|
|
]],
|
|
|
|
attr_ids = {
|
|
|
|
[1] = { foreground = Screen.colors.Blue, bold = true },
|
|
|
|
},
|
|
|
|
}
|
2024-02-01 22:14:10 -07:00
|
|
|
|
|
|
|
-- ":{range}lua {code}" executes {code}, ignoring {range}
|
|
|
|
eq('', exec_capture('1lua gvar = 42'))
|
|
|
|
eq(42, fn.luaeval('gvar'))
|
2024-01-26 18:00:50 -07:00
|
|
|
end)
|
2017-01-28 18:11:18 -07:00
|
|
|
end)
|
2017-01-29 09:26:22 -07:00
|
|
|
|
|
|
|
describe(':luado command', function()
|
|
|
|
it('works', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_buf_set_lines(0, 0, 1, false, { 'ABC', 'def', 'gHi' })
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture('luado lines = (lines or {}) lines[#lines + 1] = {linenr, line}'))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ 'ABC', 'def', 'gHi' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
|
|
|
eq({ { 1, 'ABC' }, { 2, 'def' }, { 3, 'gHi' } }, fn.luaeval('lines'))
|
2017-01-29 09:26:22 -07:00
|
|
|
|
|
|
|
-- Automatic transformation of numbers
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture('luado return linenr'))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '1', '2', '3' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
2017-01-29 09:26:22 -07:00
|
|
|
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture('luado return ("<%02x>"):format(line:byte())'))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '<31>', '<32>', '<33>' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
2017-01-29 09:26:22 -07:00
|
|
|
end)
|
2024-01-29 17:09:25 -07:00
|
|
|
|
2017-01-29 09:26:22 -07:00
|
|
|
it('stops processing lines when suddenly out of lines', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_buf_set_lines(0, 0, 1, false, { 'ABC', 'def', 'gHi' })
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture('2,$luado runs = ((runs or 0) + 1) vim.api.nvim_command("%d")'))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
|
|
|
eq(1, fn.luaeval('runs'))
|
2024-01-29 17:09:25 -07:00
|
|
|
|
|
|
|
api.nvim_buf_set_lines(0, 0, -1, false, { 'one', 'two', 'three' })
|
|
|
|
eq('', exec_capture('luado vim.api.nvim_command("%d")'))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
2024-01-29 17:09:25 -07:00
|
|
|
|
|
|
|
api.nvim_buf_set_lines(0, 0, -1, false, { 'one', 'two', 'three' })
|
|
|
|
eq('', exec_capture('luado vim.api.nvim_command("1,2d")'))
|
|
|
|
eq({ 'three' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
|
|
|
|
|
|
|
api.nvim_buf_set_lines(0, 0, -1, false, { 'one', 'two', 'three' })
|
|
|
|
eq('', exec_capture('luado vim.api.nvim_command("2,3d"); return "REPLACED"'))
|
|
|
|
eq({ 'REPLACED' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
|
|
|
|
|
|
|
api.nvim_buf_set_lines(0, 0, -1, false, { 'one', 'two', 'three' })
|
|
|
|
eq('', exec_capture('2,3luado vim.api.nvim_command("1,2d"); return "REPLACED"'))
|
|
|
|
eq({ 'three' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
2017-01-29 09:26:22 -07:00
|
|
|
end)
|
2024-01-29 17:09:25 -07:00
|
|
|
|
2017-01-29 09:26:22 -07:00
|
|
|
it('fails on errors', function()
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
[[Vim(luado):E5109: Error loading lua: [string ":luado"]:0: unexpected symbol near ')']],
|
|
|
|
pcall_err(command, 'luado ()')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
[[Vim(luado):E5111: Error calling lua: [string ":luado"]:0: attempt to perform arithmetic on global 'liness' (a nil value)]],
|
|
|
|
pcall_err(command, 'luado return liness + 1')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-01-29 09:26:22 -07:00
|
|
|
end)
|
2024-01-29 17:09:25 -07:00
|
|
|
|
2017-01-29 13:22:50 -07:00
|
|
|
it('works with NULL errors', function()
|
2019-10-30 12:53:09 -07:00
|
|
|
eq([=[Vim(luado):E5111: Error calling lua: [NULL]]=], exc_exec('luado error(nil)'))
|
2017-01-29 13:22:50 -07:00
|
|
|
end)
|
2024-01-29 17:09:25 -07:00
|
|
|
|
2017-01-29 09:26:22 -07:00
|
|
|
it('fails in sandbox when needed', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_buf_set_lines(0, 0, 1, false, { 'ABC', 'def', 'gHi' })
|
2023-05-04 15:32:17 -07:00
|
|
|
eq(
|
|
|
|
'Vim(luado):E48: Not allowed in sandbox: sandbox luado runs = (runs or 0) + 1',
|
2021-09-19 02:29:37 -07:00
|
|
|
pcall_err(command, 'sandbox luado runs = (runs or 0) + 1')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(NIL, fn.luaeval('runs'))
|
2017-01-29 09:26:22 -07:00
|
|
|
end)
|
2024-01-29 17:09:25 -07:00
|
|
|
|
2017-01-29 09:26:22 -07:00
|
|
|
it('works with long strings', function()
|
|
|
|
local s = ('x'):rep(100500)
|
2017-01-29 09:32:01 -07:00
|
|
|
|
2021-09-19 02:29:37 -07:00
|
|
|
eq(
|
|
|
|
'Vim(luado):E5109: Error loading lua: [string ":luado"]:0: unfinished string near \'<eof>\'',
|
|
|
|
pcall_err(command, ('luado return "%s'):format(s))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
|
2017-01-29 09:32:01 -07:00
|
|
|
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture(('luado return "%s"'):format(s)))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ s }, api.nvim_buf_get_lines(0, 0, -1, false))
|
2017-01-29 09:26:22 -07:00
|
|
|
end)
|
|
|
|
end)
|
2017-01-29 09:51:55 -07:00
|
|
|
|
|
|
|
describe(':luafile', function()
|
|
|
|
local fname = 'Xtest-functional-lua-commands-luafile'
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
os.remove(fname)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('works', function()
|
|
|
|
write_file(
|
|
|
|
fname,
|
|
|
|
[[
|
|
|
|
vim.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})
|
|
|
|
vim.api.nvim_buf_set_lines(1, 2, 3, false, {"TTSE"})
|
|
|
|
vim.api.nvim_buf_set_lines(1, 3, 4, false, {"STTE"})
|
|
|
|
]]
|
|
|
|
)
|
2021-09-19 02:29:37 -07:00
|
|
|
eq('', exec_capture('luafile ' .. fname))
|
2024-01-12 10:59:57 -07:00
|
|
|
eq({ '', 'ETTS', 'TTSE', 'STTE' }, api.nvim_buf_get_lines(0, 0, 100, false))
|
2017-01-29 09:51:55 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('correctly errors out', function()
|
|
|
|
write_file(fname, '()')
|
|
|
|
eq(
|
|
|
|
("Vim(luafile):E5112: Error while creating lua chunk: %s:1: unexpected symbol near ')'"):format(
|
|
|
|
fname
|
|
|
|
),
|
|
|
|
exc_exec('luafile ' .. fname)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-01-29 09:51:55 -07:00
|
|
|
write_file(fname, 'vimm.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})')
|
|
|
|
eq(
|
|
|
|
("Vim(luafile):E5113: Error while calling lua chunk: %s:1: attempt to index global 'vimm' (a nil value)"):format(
|
|
|
|
fname
|
|
|
|
),
|
2021-11-06 07:26:10 -07:00
|
|
|
remove_trace(exc_exec('luafile ' .. fname))
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-01-29 09:51:55 -07:00
|
|
|
end)
|
2024-01-29 17:09:25 -07:00
|
|
|
|
2017-01-29 13:22:50 -07:00
|
|
|
it('works with NULL errors', function()
|
|
|
|
write_file(fname, 'error(nil)')
|
|
|
|
eq(
|
|
|
|
[=[Vim(luafile):E5113: Error while calling lua chunk: [NULL]]=],
|
|
|
|
exc_exec('luafile ' .. fname)
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-01-29 13:22:50 -07:00
|
|
|
end)
|
2017-01-29 09:51:55 -07:00
|
|
|
end)
|