neovim/test/functional/api/command_spec.lua

776 lines
19 KiB
Lua
Raw Normal View History

local t = require('test.testutil')
local n = require('test.functional.testnvim')()
2018-02-24 03:14:51 -07:00
local NIL = vim.NIL
local clear = n.clear
local command = n.command
local eq = t.eq
local api = n.api
local matches = t.matches
local source = n.source
local pcall_err = t.pcall_err
local exec_lua = n.exec_lua
local assert_alive = n.assert_alive
local feed = n.feed
local fn = n.fn
2018-02-24 03:14:51 -07:00
describe('nvim_get_commands', function()
2024-01-02 18:09:18 -07:00
local cmd_dict = {
addr = NIL,
bang = false,
bar = false,
complete = NIL,
complete_arg = NIL,
count = NIL,
definition = 'echo "Hello World"',
name = 'Hello',
nargs = '1',
preview = false,
range = NIL,
register = false,
keepscript = false,
script_id = 0,
}
local cmd_dict2 = {
addr = NIL,
bang = false,
bar = false,
complete = NIL,
complete_arg = NIL,
count = NIL,
definition = 'pwd',
name = 'Pwd',
nargs = '?',
preview = false,
range = NIL,
register = false,
keepscript = false,
script_id = 0,
}
2018-02-24 03:14:51 -07:00
before_each(clear)
2018-02-24 03:14:51 -07:00
it('gets empty list if no commands were defined', function()
eq({}, api.nvim_get_commands({ builtin = false }))
2018-02-24 03:14:51 -07:00
end)
it('validation', function()
eq('builtin=true not implemented', pcall_err(api.nvim_get_commands, { builtin = true }))
eq("Invalid key: 'foo'", pcall_err(api.nvim_get_commands, { foo = 'blah' }))
end)
it('gets global user-defined commands', function()
-- Define a command.
command('command -nargs=1 Hello echo "Hello World"')
eq({ Hello = cmd_dict }, api.nvim_get_commands({ builtin = false }))
-- Define another command.
2024-01-02 18:09:18 -07:00
command('command -nargs=? Pwd pwd')
eq({ Hello = cmd_dict, Pwd = cmd_dict2 }, api.nvim_get_commands({ builtin = false }))
-- Delete a command.
2018-02-24 03:14:51 -07:00
command('delcommand Pwd')
eq({ Hello = cmd_dict }, api.nvim_get_commands({ builtin = false }))
2018-02-24 03:14:51 -07:00
end)
it('gets buffer-local user-defined commands', function()
-- Define a buffer-local command.
command('command -buffer -nargs=1 Hello echo "Hello World"')
eq({ Hello = cmd_dict }, api.nvim_buf_get_commands(0, { builtin = false }))
-- Define another buffer-local command.
command('command -buffer -nargs=? Pwd pwd')
eq({ Hello = cmd_dict, Pwd = cmd_dict2 }, api.nvim_buf_get_commands(0, { builtin = false }))
-- Delete a command.
2018-02-24 03:14:51 -07:00
command('delcommand Pwd')
eq({ Hello = cmd_dict }, api.nvim_buf_get_commands(0, { builtin = false }))
-- {builtin=true} always returns empty for buffer-local case.
eq({}, api.nvim_buf_get_commands(0, { builtin = true }))
2018-02-24 03:14:51 -07:00
end)
it('gets various command attributes', function()
2024-01-02 18:09:18 -07:00
local cmd0 = {
addr = 'arguments',
bang = false,
bar = false,
complete = 'dir',
complete_arg = NIL,
count = '10',
definition = 'pwd <args>',
name = 'TestCmd',
nargs = '1',
preview = false,
range = '10',
register = false,
keepscript = false,
script_id = 0,
}
local cmd1 = {
addr = NIL,
bang = false,
bar = false,
complete = 'custom',
complete_arg = 'ListUsers',
count = NIL,
definition = '!finger <args>',
name = 'Finger',
nargs = '+',
preview = false,
range = NIL,
register = false,
keepscript = false,
script_id = 1,
}
local cmd2 = {
addr = NIL,
bang = true,
bar = false,
complete = NIL,
complete_arg = NIL,
count = NIL,
definition = 'call \128\253R2_foo(<q-args>)',
name = 'Cmd2',
nargs = '*',
preview = false,
range = NIL,
register = false,
keepscript = false,
script_id = 2,
}
local cmd3 = {
addr = NIL,
bang = false,
bar = true,
complete = NIL,
complete_arg = NIL,
count = NIL,
definition = 'call \128\253R3_ohyeah()',
name = 'Cmd3',
nargs = '0',
preview = false,
range = NIL,
register = false,
keepscript = false,
script_id = 3,
}
local cmd4 = {
addr = NIL,
bang = false,
bar = false,
complete = NIL,
complete_arg = NIL,
count = NIL,
definition = 'call \128\253R4_just_great()',
name = 'Cmd4',
nargs = '0',
preview = false,
range = NIL,
register = true,
keepscript = false,
script_id = 4,
}
source([[
let s:foo = 1
command -complete=custom,ListUsers -nargs=+ Finger !finger <args>
]])
eq({ Finger = cmd1 }, api.nvim_get_commands({ builtin = false }))
vim-patch:8.2.3141: no error when using :complete for :command without -nargs Problem: No error when using :complete for :command without -nargs. Solution: Give an error. (Martin Tournoij, closes vim/vim#8544, closes vim/vim#8541) https://github.com/vim/vim/commit/de69a7353e9bec552e15dbe3706a9f4e88080fce N/A patches for version.c: vim-patch:8.1.1801: cannot build without the +eval feature Problem: Cannot build without the +eval feature. Solution: Always define funcexe_T. https://github.com/vim/vim/commit/505e43a20eb25674b18d73971fe3b51dad917f9a vim-patch:8.1.1818: unused variable Problem: Unused variable. Solution: Remove the variable. (Mike Williams) https://github.com/vim/vim/commit/b4a88a0441a65a0c9411c294825a08ca703f541e vim-patch:8.2.1464: Vim9: build warning for unused variable Problem: Vim9: build warning for unused variable. Solution: Delete the variable declaration. https://github.com/vim/vim/commit/829ac868b7615d73dbfb536f7fcd44fc7c5b7c1d vim-patch:8.2.2639: build failure when fsync() is not available Problem: Build failure when fsync() is not available. Solution: Add #ifdef. https://github.com/vim/vim/commit/5ea79a2599d35f75e1ae8a75d2711c754c4cb7c4 vim-patch:8.2.2814: Vim9: unused variable Problem: Vim9: unused variable. (John Marriott) Solution: Adjust #ifdef. https://github.com/vim/vim/commit/b06b50dfa06e1cbefd634e2735e7cd5ddd5b911c vim-patch:8.2.2947: build failure without the channel feature Problem: Build failure without the channel feature. Solution: Add back #ifdef. (John Marriott) https://github.com/vim/vim/commit/f5bfa8faa7bbe025c10148d37e8b47217a430a3b vim-patch:8.2.2976: build failure without the +eval feature Problem: Build failure without the +eval feature. Solution: Add #ifdefs. https://github.com/vim/vim/commit/8de901e1f1b051e02a61ae76ad7c925e4c0642e5 vim-patch:8.2.2986: build failure without the profile feature Problem: Build failure without the profile feature. Solution: Add #ifdef. https://github.com/vim/vim/commit/d9f31c13d217b4b97f724774a67a6d1f8640e8ae vim-patch:8.2.3114: Amiga-like systems: build error using stat() Problem: Amiga-like systems: build error using stat(). Solution: Only build swapfile_process_running() on systems where it is actually used. (Ola Söder, closes vim/vim#8519) https://github.com/vim/vim/commit/599a6e5b3629d943a795cd69e4d3d19886f86405
2021-08-02 21:57:38 -07:00
command('command -nargs=1 -complete=dir -addr=arguments -count=10 TestCmd pwd <args>')
eq({ Finger = cmd1, TestCmd = cmd0 }, api.nvim_get_commands({ builtin = false }))
source([[
function! s:foo() abort
endfunction
command -bang -nargs=* Cmd2 call <SID>foo(<q-args>)
]])
source([[
function! s:ohyeah() abort
endfunction
command -bar -nargs=0 Cmd3 call <SID>ohyeah()
]])
source([[
function! s:just_great() abort
endfunction
command -register Cmd4 call <SID>just_great()
]])
-- TODO(justinmk): Order is stable but undefined. Sort before return?
2024-01-02 18:09:18 -07:00
eq(
{ Cmd2 = cmd2, Cmd3 = cmd3, Cmd4 = cmd4, Finger = cmd1, TestCmd = cmd0 },
api.nvim_get_commands({ builtin = false })
2024-01-02 18:09:18 -07:00
)
2018-02-24 03:14:51 -07:00
end)
end)
describe('nvim_create_user_command', function()
before_each(clear)
it('works with strings', function()
api.nvim_create_user_command('SomeCommand', 'let g:command_fired = <args>', { nargs = 1 })
command('SomeCommand 42')
eq(42, api.nvim_eval('g:command_fired'))
end)
it('works with Lua functions', function()
exec_lua [[
result = {}
vim.api.nvim_create_user_command('CommandWithLuaCallback', function(opts)
result = opts
end, {
nargs = "*",
bang = true,
count = 2,
})
]]
2024-01-02 18:09:18 -07:00
eq(
{
name = 'CommandWithLuaCallback',
args = [[this\ is a\ test]],
fargs = { 'this ', 'is', 'a test' },
bang = false,
line1 = 1,
line2 = 1,
mods = '',
smods = {
browse = false,
confirm = false,
emsg_silent = false,
hide = false,
horizontal = false,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = '',
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
},
range = 0,
count = 2,
reg = '',
},
2024-01-02 18:09:18 -07:00
exec_lua [=[
vim.api.nvim_command([[CommandWithLuaCallback this\ is a\ test]])
return result
2024-01-02 18:09:18 -07:00
]=]
)
eq(
{
name = 'CommandWithLuaCallback',
args = [[this includes\ a backslash: \\]],
fargs = { 'this', 'includes a', 'backslash:', '\\' },
bang = false,
line1 = 1,
line2 = 1,
mods = '',
smods = {
browse = false,
confirm = false,
emsg_silent = false,
hide = false,
horizontal = false,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = '',
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
},
range = 0,
count = 2,
reg = '',
},
2024-01-02 18:09:18 -07:00
exec_lua [=[
vim.api.nvim_command([[CommandWithLuaCallback this includes\ a backslash: \\]])
return result
2024-01-02 18:09:18 -07:00
]=]
)
eq(
{
name = 'CommandWithLuaCallback',
args = 'a\\b',
fargs = { 'a\\b' },
bang = false,
line1 = 1,
line2 = 1,
mods = '',
smods = {
browse = false,
confirm = false,
emsg_silent = false,
hide = false,
horizontal = false,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = '',
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
},
range = 0,
count = 2,
reg = '',
},
2024-01-02 18:09:18 -07:00
exec_lua [=[
vim.api.nvim_command('CommandWithLuaCallback a\\b')
return result
2024-01-02 18:09:18 -07:00
]=]
)
eq(
{
name = 'CommandWithLuaCallback',
args = 'h\tey ',
fargs = { [[h]], [[ey]] },
bang = true,
line1 = 10,
line2 = 10,
mods = 'confirm unsilent botright horizontal',
smods = {
browse = false,
confirm = true,
emsg_silent = false,
hide = false,
horizontal = true,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = 'botright',
tab = -1,
unsilent = true,
verbose = -1,
vertical = false,
},
range = 1,
count = 10,
reg = '',
},
2024-01-02 18:09:18 -07:00
exec_lua [=[
vim.api.nvim_command('unsilent horizontal botright confirm 10CommandWithLuaCallback! h\tey ')
return result
2024-01-02 18:09:18 -07:00
]=]
)
eq(
{
name = 'CommandWithLuaCallback',
args = 'h',
fargs = { 'h' },
bang = false,
line1 = 1,
line2 = 42,
mods = '',
smods = {
browse = false,
confirm = false,
emsg_silent = false,
hide = false,
horizontal = false,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = '',
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
},
range = 1,
count = 42,
reg = '',
},
2024-01-02 18:09:18 -07:00
exec_lua [[
vim.api.nvim_command('CommandWithLuaCallback 42 h')
return result
2024-01-02 18:09:18 -07:00
]]
)
eq(
{
name = 'CommandWithLuaCallback',
args = '',
fargs = {}, -- fargs works without args
bang = false,
line1 = 1,
line2 = 1,
mods = '',
smods = {
browse = false,
confirm = false,
emsg_silent = false,
hide = false,
horizontal = false,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = '',
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
},
range = 0,
count = 2,
reg = '',
},
2024-01-02 18:09:18 -07:00
exec_lua [[
vim.api.nvim_command('CommandWithLuaCallback')
return result
2024-01-02 18:09:18 -07:00
]]
)
-- f-args doesn't split when command nargs is 1 or "?"
exec_lua [[
result = {}
vim.api.nvim_create_user_command('CommandWithOneOrNoArg', function(opts)
result = opts
end, {
nargs = "?",
bang = true,
count = 2,
})
]]
2024-01-02 18:09:18 -07:00
eq(
{
name = 'CommandWithOneOrNoArg',
args = "hello I'm one argument",
fargs = { "hello I'm one argument" }, -- Doesn't split args
bang = false,
line1 = 1,
line2 = 1,
mods = '',
smods = {
browse = false,
confirm = false,
emsg_silent = false,
hide = false,
horizontal = false,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = '',
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
},
range = 0,
count = 2,
reg = '',
},
2024-01-02 18:09:18 -07:00
exec_lua [[
vim.api.nvim_command('CommandWithOneOrNoArg hello I\'m one argument')
return result
2024-01-02 18:09:18 -07:00
]]
)
-- f-args is an empty table if no args were passed
2024-01-02 18:09:18 -07:00
eq(
{
name = 'CommandWithOneOrNoArg',
args = '',
fargs = {},
bang = false,
line1 = 1,
line2 = 1,
mods = '',
smods = {
browse = false,
confirm = false,
emsg_silent = false,
hide = false,
horizontal = false,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = '',
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
},
range = 0,
count = 2,
reg = '',
},
2024-01-02 18:09:18 -07:00
exec_lua [[
vim.api.nvim_command('CommandWithOneOrNoArg')
return result
2024-01-02 18:09:18 -07:00
]]
)
-- f-args is an empty table when the command nargs=0
exec_lua [[
result = {}
vim.api.nvim_create_user_command('CommandWithNoArgs', function(opts)
result = opts
end, {
nargs = 0,
bang = true,
count = 2,
register = true,
})
]]
2024-01-02 18:09:18 -07:00
eq(
{
name = 'CommandWithNoArgs',
args = '',
fargs = {},
bang = false,
line1 = 1,
line2 = 1,
mods = '',
smods = {
browse = false,
confirm = false,
emsg_silent = false,
hide = false,
horizontal = false,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = '',
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
},
range = 0,
count = 2,
reg = '',
},
2024-01-02 18:09:18 -07:00
exec_lua [[
vim.cmd('CommandWithNoArgs')
return result
2024-01-02 18:09:18 -07:00
]]
)
-- register can be specified
2024-01-02 18:09:18 -07:00
eq(
{
name = 'CommandWithNoArgs',
args = '',
fargs = {},
bang = false,
line1 = 1,
line2 = 1,
mods = '',
smods = {
browse = false,
confirm = false,
emsg_silent = false,
hide = false,
horizontal = false,
keepalt = false,
keepjumps = false,
keepmarks = false,
keeppatterns = false,
lockmarks = false,
noautocmd = false,
noswapfile = false,
sandbox = false,
silent = false,
split = '',
tab = -1,
unsilent = false,
verbose = -1,
vertical = false,
},
range = 0,
count = 2,
reg = '+',
},
2024-01-02 18:09:18 -07:00
exec_lua [[
vim.cmd('CommandWithNoArgs +')
return result
2024-01-02 18:09:18 -07:00
]]
)
end)
it('can define buffer-local commands', function()
local bufnr = api.nvim_create_buf(false, false)
api.nvim_buf_create_user_command(bufnr, 'Hello', '', {})
matches('Not an editor command: Hello', pcall_err(command, 'Hello'))
api.nvim_set_current_buf(bufnr)
command('Hello')
assert_alive()
end)
it('can use a Lua complete function', function()
exec_lua [[
vim.api.nvim_create_user_command('Test', '', {
nargs = "*",
complete = function(arg, cmdline, pos)
local options = {"aaa", "bbb", "ccc"}
local t = {}
for _, v in ipairs(options) do
if string.find(v, "^" .. arg) then
table.insert(t, v)
end
end
return t
end,
})
]]
feed(':Test a<Tab>')
eq('Test aaa', fn.getcmdline())
feed('<C-U>Test b<Tab>')
eq('Test bbb', fn.getcmdline())
end)
it('does not allow invalid command names', function()
2024-01-02 18:09:18 -07:00
eq(
"Invalid command name (must start with uppercase): 'test'",
pcall_err(
exec_lua,
[[
vim.api.nvim_create_user_command('test', 'echo "hi"', {})
2024-01-02 18:09:18 -07:00
]]
)
)
eq(
"Invalid command name: 't@'",
pcall_err(
exec_lua,
[[
vim.api.nvim_create_user_command('t@', 'echo "hi"', {})
2024-01-02 18:09:18 -07:00
]]
)
)
eq(
"Invalid command name: 'T@st'",
pcall_err(
exec_lua,
[[
vim.api.nvim_create_user_command('T@st', 'echo "hi"', {})
2024-01-02 18:09:18 -07:00
]]
)
)
eq(
"Invalid command name: 'Test!'",
pcall_err(
exec_lua,
[[
vim.api.nvim_create_user_command('Test!', 'echo "hi"', {})
2024-01-02 18:09:18 -07:00
]]
)
)
eq(
"Invalid command name: '💩'",
pcall_err(
exec_lua,
[[
vim.api.nvim_create_user_command('💩', 'echo "hi"', {})
2024-01-02 18:09:18 -07:00
]]
)
)
end)
it('smods can be used with nvim_cmd', function()
2024-01-02 18:09:18 -07:00
exec_lua [[
vim.api.nvim_create_user_command('MyEcho', function(opts)
vim.api.nvim_cmd({ cmd = 'echo', args = { '&verbose' }, mods = opts.smods }, {})
end, {})
]]
eq('3', api.nvim_cmd({ cmd = 'MyEcho', mods = { verbose = 3 } }, { output = true }))
eq(1, #api.nvim_list_tabpages())
2024-01-02 18:09:18 -07:00
exec_lua [[
vim.api.nvim_create_user_command('MySplit', function(opts)
vim.api.nvim_cmd({ cmd = 'split', mods = opts.smods }, {})
end, {})
]]
api.nvim_cmd({ cmd = 'MySplit' }, {})
eq(1, #api.nvim_list_tabpages())
eq(2, #api.nvim_list_wins())
api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
eq(2, #api.nvim_list_tabpages())
eq(2, fn.tabpagenr())
api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
eq(3, #api.nvim_list_tabpages())
eq(2, fn.tabpagenr())
api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 3 } }, {})
eq(4, #api.nvim_list_tabpages())
eq(4, fn.tabpagenr())
api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 0 } }, {})
eq(5, #api.nvim_list_tabpages())
eq(1, fn.tabpagenr())
end)
end)
describe('nvim_del_user_command', function()
before_each(clear)
it('can delete global commands', function()
api.nvim_create_user_command('Hello', 'echo "Hi"', {})
command('Hello')
api.nvim_del_user_command('Hello')
matches('Not an editor command: Hello', pcall_err(command, 'Hello'))
end)
it('can delete buffer-local commands', function()
api.nvim_buf_create_user_command(0, 'Hello', 'echo "Hi"', {})
command('Hello')
api.nvim_buf_del_user_command(0, 'Hello')
matches('Not an editor command: Hello', pcall_err(command, 'Hello'))
end)
end)