diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/README.md | 4 | ||||
-rw-r--r-- | test/functional/api/command_spec.lua | 121 | ||||
-rw-r--r-- | test/functional/lua/api_spec.lua | 11 |
3 files changed, 130 insertions, 6 deletions
diff --git a/test/README.md b/test/README.md index c6e173ead2..cc630cb8bf 100644 --- a/test/README.md +++ b/test/README.md @@ -38,7 +38,7 @@ Layout - `/test/functional` : functional tests - `/test/unit` : unit tests - `/test/config` : contains `*.in` files which are transformed into `*.lua` - files using `configure_file` CMake command: this is for acessing CMake + files using `configure_file` CMake command: this is for accessing CMake variables in lua tests. - `/test/includes` : include-files for use by luajit `ffi.cdef` C definitions parser: normally used to make macros not accessible via this mechanism @@ -197,7 +197,7 @@ Guidelines (success + fail + error + pending) is the same in all environments. - *Note:* `pending()` is ignored if it is missing an argument, unless it is [contained in an `it()` block](https://github.com/neovim/neovim/blob/d21690a66e7eb5ebef18046c7a79ef898966d786/test/functional/ex_cmds/grep_spec.lua#L11). - Provide empty function argument if the `pending()` call is outside of `it()` + Provide empty function argument if the `pending()` call is outside `it()` ([example](https://github.com/neovim/neovim/commit/5c1dc0fbe7388528875aff9d7b5055ad718014de#diff-bf80b24c724b0004e8418102f68b0679R18)). - Really long `source([=[...]=])` blocks may break Vim's Lua syntax highlighting. Try `:syntax sync fromstart` to fix it. diff --git a/test/functional/api/command_spec.lua b/test/functional/api/command_spec.lua index 6f929ad1ca..6c2c136edc 100644 --- a/test/functional/api/command_spec.lua +++ b/test/functional/api/command_spec.lua @@ -6,8 +6,14 @@ local command = helpers.command local curbufmeths = helpers.curbufmeths local eq = helpers.eq local meths = helpers.meths +local bufmeths = helpers.bufmeths +local matches = helpers.matches local source = helpers.source local pcall_err = helpers.pcall_err +local exec_lua = helpers.exec_lua +local assert_alive = helpers.assert_alive +local feed = helpers.feed +local funcs = helpers.funcs describe('nvim_get_commands', function() local cmd_dict = { addr=NIL, bang=false, bar=false, complete=NIL, complete_arg=NIL, count=NIL, definition='echo "Hello World"', name='Hello', nargs='1', range=NIL, register=false, script_id=0, } @@ -78,3 +84,118 @@ describe('nvim_get_commands', function() eq({Cmd2=cmd2, Cmd3=cmd3, Cmd4=cmd4, Finger=cmd1, TestCmd=cmd0}, meths.get_commands({builtin=false})) end) end) + +describe('nvim_add_user_command', function() + before_each(clear) + + it('works with strings', function() + meths.add_user_command('SomeCommand', 'let g:command_fired = <args>', {nargs = 1}) + meths.command('SomeCommand 42') + eq(42, meths.eval('g:command_fired')) + end) + + it('works with Lua functions', function() + exec_lua [[ + result = {} + vim.api.nvim_add_user_command('CommandWithLuaCallback', function(opts) + result = opts + end, { + nargs = "*", + bang = true, + count = 2, + }) + ]] + + eq({ + args = "hello", + bang = false, + line1 = 1, + line2 = 1, + mods = "", + range = 0, + count = 2, + reg = "", + }, exec_lua [[ + vim.api.nvim_command('CommandWithLuaCallback hello') + return result + ]]) + + eq({ + args = "", + bang = true, + line1 = 10, + line2 = 10, + mods = "botright", + range = 1, + count = 10, + reg = "", + }, exec_lua [[ + vim.api.nvim_command('botright 10CommandWithLuaCallback!') + return result + ]]) + + eq({ + args = "", + bang = false, + line1 = 1, + line2 = 42, + mods = "", + range = 1, + count = 42, + reg = "", + }, exec_lua [[ + vim.api.nvim_command('CommandWithLuaCallback 42') + return result + ]]) + end) + + it('can define buffer-local commands', function() + local bufnr = meths.create_buf(false, false) + bufmeths.add_user_command(bufnr, "Hello", "", {}) + matches("Not an editor command: Hello", pcall_err(meths.command, "Hello")) + meths.set_current_buf(bufnr) + meths.command("Hello") + assert_alive() + end) + + it('can use a Lua complete function', function() + exec_lua [[ + vim.api.nvim_add_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', funcs.getcmdline()) + feed('<C-U>Test b<Tab>') + eq('Test bbb', funcs.getcmdline()) + end) +end) + +describe('nvim_del_user_command', function() + before_each(clear) + + it('can delete global commands', function() + meths.add_user_command('Hello', 'echo "Hi"', {}) + meths.command('Hello') + meths.del_user_command('Hello') + matches("Not an editor command: Hello", pcall_err(meths.command, "Hello")) + end) + + it('can delete buffer-local commands', function() + bufmeths.add_user_command(0, 'Hello', 'echo "Hi"', {}) + meths.command('Hello') + bufmeths.del_user_command(0, 'Hello') + matches("Not an editor command: Hello", pcall_err(meths.command, "Hello")) + end) +end) diff --git a/test/functional/lua/api_spec.lua b/test/functional/lua/api_spec.lua index 81e00bba6d..f6038e23fe 100644 --- a/test/functional/lua/api_spec.lua +++ b/test/functional/lua/api_spec.lua @@ -8,6 +8,7 @@ local clear = helpers.clear local eval = helpers.eval local NIL = helpers.NIL local eq = helpers.eq +local exec_lua = helpers.exec_lua before_each(clear) @@ -111,6 +112,12 @@ describe('luaeval(vim.api.…)', function() eq(7, eval([[type(luaeval('vim.api.nvim__id(nil)'))]])) eq({foo=1, bar={42, {{baz=true}, 5}}}, funcs.luaeval('vim.api.nvim__id({foo=1, bar={42, {{baz=true}, 5}}})')) + + eq(true, funcs.luaeval('vim.api.nvim__id(vim.api.nvim__id)(true)')) + eq(42, exec_lua [[ + local f = vim.api.nvim__id({42, vim.api.nvim__id}) + return f[2](f[1]) + ]]) end) it('correctly converts container objects with type_idx to API objects', function() @@ -159,12 +166,8 @@ describe('luaeval(vim.api.…)', function() it('errors out correctly when working with API', function() -- Conversion errors - eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua type', - remove_trace(exc_exec([[call luaeval("vim.api.nvim__id(vim.api.nvim__id)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua table', remove_trace(exc_exec([[call luaeval("vim.api.nvim__id({1, foo=42})")]]))) - eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua type', - remove_trace(exc_exec([[call luaeval("vim.api.nvim__id({42, vim.api.nvim__id})")]]))) -- Errors in number of arguments eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected 1 argument', remove_trace(exc_exec([[call luaeval("vim.api.nvim__id()")]]))) |