From 59d117ec99b6037cb9fad5bbfb6d0b18f5012927 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 7 Dec 2023 12:19:35 +0100 Subject: build: enable lintlua for test/ dir Problem: Not all Lua code is checked by stylua. Automating code-style is an important mechanism for reducing time spent on accidental (non-essential) complexity. Solution: - Enable stylua for entire `test/` directory. - Exclude these high-churn files until this issue is resolved: https://github.com/JohnnyMorganz/StyLua/issues/829 ``` test/functional/ui/decorations_spec.lua | 3560 ++++++++++++++++++++++++++++++++++++---------------- test/functional/ui/float_spec.lua | 5826 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------- test/functional/ui/multigrid_spec.lua | 1349 ++++++++++++++------ ``` - Make surgical changes to these files (or add `stylua: ignore` in some small scopes) to improve the result: ``` test/functional/vimscript/msgpack_functions_spec.lua | 1414 +++++++++++++++------ test/functional/api/buffer_spec.lua | 1389 +++++++++++---------- test/functional/api/vim_spec.lua | 2740 +++++++++++++++++++++++----------------- ``` - These "high churn" files are NOT excluded because the changes are largely an improvement: ``` test/functional/plugin/lsp_spec.lua | 2198 ++++++++++++++++++--------------- test/functional/plugin/shada_spec.lua | 4078 +++++++++++++++++++++++++++++++++++------------------------- test/functional/ui/cmdline_spec.lua | 1199 +++++++++++------- test/functional/ui/popupmenu_spec.lua | 1267 +++++++++++-------- test/functional/ui/messages_spec.lua | 1643 +++++++++++++++--------- ``` - TODO: how to check "all directories"? With `GLOB_DIRS *` and `/.deps/` (or `.deps/`) in `.styluaignore`, Lua code in `.deps/` is still checked... --- test/functional/api/vim_spec.lua | 2585 +++++++++++++++++++++----------------- 1 file changed, 1460 insertions(+), 1125 deletions(-) (limited to 'test/functional/api/vim_spec.lua') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 104ffd7d6c..ce2838879b 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -3,6 +3,7 @@ local Screen = require('test.functional.ui.screen') local luv = require('luv') local fmt = string.format +local dedent = helpers.dedent local assert_alive = helpers.assert_alive local NIL = helpers.NIL local clear, nvim, eq, neq = helpers.clear, helpers.nvim, helpers.eq, helpers.neq @@ -41,32 +42,41 @@ describe('API', function() it('validates requests', function() -- RPC - matches('Invalid method: bogus$', - pcall_err(request, 'bogus')) - matches('Invalid method: … の り 。…$', - pcall_err(request, '… の り 。…')) - matches('Invalid method: $', - pcall_err(request, '')) + matches('Invalid method: bogus$', pcall_err(request, 'bogus')) + matches('Invalid method: … の り 。…$', pcall_err(request, '… の り 。…')) + matches('Invalid method: $', pcall_err(request, '')) -- Non-RPC: rpcrequest(v:servername) uses internal channel. - matches('Invalid method: … の り 。…$', - pcall_err(request, 'nvim_eval', - [=[rpcrequest(sockconnect('pipe', v:servername, {'rpc':1}), '… の り 。…')]=])) - matches('Invalid method: bogus$', - pcall_err(request, 'nvim_eval', - [=[rpcrequest(sockconnect('pipe', v:servername, {'rpc':1}), 'bogus')]=])) + matches( + 'Invalid method: … の り 。…$', + pcall_err( + request, + 'nvim_eval', + [=[rpcrequest(sockconnect('pipe', v:servername, {'rpc':1}), '… の り 。…')]=] + ) + ) + matches( + 'Invalid method: bogus$', + pcall_err( + request, + 'nvim_eval', + [=[rpcrequest(sockconnect('pipe', v:servername, {'rpc':1}), 'bogus')]=] + ) + ) -- XXX: This must be the last one, else next one will fail: -- "Packer instance already working. Use another Packer ..." - matches("can't serialize object of type .$", - pcall_err(request, nil)) + matches("can't serialize object of type .$", pcall_err(request, nil)) end) it('handles errors in async requests', function() local error_types = meths.get_api_info()[2].error_types nvim_async('bogus') - eq({'notification', 'nvim_error_event', - {error_types.Exception.id, 'Invalid method: nvim_bogus'}}, next_msg()) + eq({ + 'notification', + 'nvim_error_event', + { error_types.Exception.id, 'Invalid method: nvim_bogus' }, + }, next_msg()) -- error didn't close channel. assert_alive() end) @@ -74,9 +84,11 @@ describe('API', function() it('failed async request emits nvim_error_event', function() local error_types = meths.get_api_info()[2].error_types nvim_async('command', 'bogus') - eq({'notification', 'nvim_error_event', - {error_types.Exception.id, 'Vim:E492: Not an editor command: bogus'}}, - next_msg()) + eq({ + 'notification', + 'nvim_error_event', + { error_types.Exception.id, 'Vim:E492: Not an editor command: bogus' }, + }, next_msg()) -- error didn't close channel. assert_alive() end) @@ -85,7 +97,7 @@ describe('API', function() nvim('command', 'split') nvim('command', 'autocmd WinEnter * startinsert') nvim('command', 'wincmd w') - eq({mode='i', blocking=false}, nvim("get_mode")) + eq({ mode = 'i', blocking = false }, nvim('get_mode')) end) describe('nvim_exec2', function() @@ -109,23 +121,31 @@ describe('API', function() it(':verbose set {option}?', function() nvim('exec2', 'set nowrap', { output = false }) - eq({ output = 'nowrap\n\tLast set from anonymous :source' }, - nvim('exec2', 'verbose set wrap?', { output = true })) + eq( + { output = 'nowrap\n\tLast set from anonymous :source' }, + nvim('exec2', 'verbose set wrap?', { output = true }) + ) -- Using script var to force creation of a script item - nvim('exec2', [[ + nvim( + 'exec2', + [[ let s:a = 1 set nowrap - ]], { output = false }) - eq({ output = 'nowrap\n\tLast set from anonymous :source (script id 1)' }, - nvim('exec2', 'verbose set wrap?', { output = true })) + ]], + { output = false } + ) + eq( + { output = 'nowrap\n\tLast set from anonymous :source (script id 1)' }, + nvim('exec2', 'verbose set wrap?', { output = true }) + ) end) it('multiline input', function() -- Heredoc + empty lines. nvim('exec2', "let x2 = 'a'\n", { output = false }) eq('a', nvim('get_var', 'x2')) - nvim('exec2','lua <avast_ye_hades('ahoy!') - ]], { output = true })) - - eq({ output = "{'output': 'ahoy! script-scoped varrrrr'}" }, nvim('exec2', [[ + ]], + { output = true } + ) + ) + + eq( + { output = "{'output': 'ahoy! script-scoped varrrrr'}" }, + nvim( + 'exec2', + [[ let s:pirate = 'script-scoped varrrrr' function! Avast_ye_hades(s) abort return a:s .. ' ' .. s:pirate endfunction echo nvim_exec2('echo Avast_ye_hades(''ahoy!'')', {'output': v:true}) - ]], { output = true })) - - matches('Vim%(echo%):E121: Undefined variable: s:pirate$', - pcall_err(request, 'nvim_exec2', [[ + ]], + { output = true } + ) + ) + + matches( + 'Vim%(echo%):E121: Undefined variable: s:pirate$', + pcall_err( + request, + 'nvim_exec2', + [[ let s:pirate = 'script-scoped varrrrr' call nvim_exec2('echo s:pirate', {'output': v:true}) - ]], { output = false })) + ]], + { output = false } + ) + ) -- Script items are created only on script var access - eq({ output = '1\n0' }, nvim('exec2', [[ + eq( + { output = '1\n0' }, + nvim( + 'exec2', + [[ echo expand("")->empty() let s:a = 123 echo expand("")->empty() - ]], { output = true })) - - eq({ output = '1\n0' }, nvim('exec2', [[ + ]], + { output = true } + ) + ) + + eq( + { output = '1\n0' }, + nvim( + 'exec2', + [[ echo expand("")->empty() function s:a() abort endfunction echo expand("")->empty() - ]], { output = true })) + ]], + { output = true } + ) + ) end) it('non-ASCII input', function() - nvim('exec2', [=[ + nvim( + 'exec2', + [=[ new exe "normal! i ax \n Ax " :%s/ax/--a1234--/g | :%s/Ax/--A1234--/g - ]=], { output = false }) + ]=], + { output = false } + ) nvim('command', '1') eq(' --a1234-- ', nvim('get_current_line')) nvim('command', '2') eq(' --A1234-- ', nvim('get_current_line')) - nvim('exec2', [[ + nvim( + 'exec2', + [[ new call setline(1,['xxx']) call feedkeys('r') call feedkeys('ñ', 'xt') - ]], { output = false }) + ]], + { output = false } + ) eq('ñxx', nvim('get_current_line')) end) it('execution error', function() - eq('nvim_exec2(): Vim:E492: Not an editor command: bogus_command', - pcall_err(request, 'nvim_exec2', 'bogus_command', {})) - eq('', nvim('eval', 'v:errmsg')) -- v:errmsg was not updated. + eq( + 'nvim_exec2(): Vim:E492: Not an editor command: bogus_command', + pcall_err(request, 'nvim_exec2', 'bogus_command', {}) + ) + eq('', nvim('eval', 'v:errmsg')) -- v:errmsg was not updated. eq('', eval('v:exception')) - eq('nvim_exec2(): Vim(buffer):E86: Buffer 23487 does not exist', - pcall_err(request, 'nvim_exec2', 'buffer 23487', {})) - eq('', eval('v:errmsg')) -- v:errmsg was not updated. + eq( + 'nvim_exec2(): Vim(buffer):E86: Buffer 23487 does not exist', + pcall_err(request, 'nvim_exec2', 'buffer 23487', {}) + ) + eq('', eval('v:errmsg')) -- v:errmsg was not updated. eq('', eval('v:exception')) end) @@ -240,7 +311,7 @@ describe('API', function() request('nvim_exec2', [[ let x2 = substitute('foo','o','X','g') let x4 = 'should be overwritten' - call nvim_exec2("source ]]..fname..[[\nlet x3 = substitute('foo','foo','set by recursive nvim_exec2','g')\nlet x5='overwritten'\nlet x4=x5\n", {'output': v:false}) + call nvim_exec2("source ]] .. fname .. [[\nlet x3 = substitute('foo','foo','set by recursive nvim_exec2','g')\nlet x5='overwritten'\nlet x4=x5\n", {'output': v:false}) ]], { output = false }) eq('set from :source file', request('nvim_get_var', 'x1')) eq('fXX', request('nvim_get_var', 'x2')) @@ -254,24 +325,38 @@ describe('API', function() local fname = tmpname() write_file(fname, 'echo "hello"\n') local sourcing_fname = tmpname() - write_file(sourcing_fname, 'call nvim_exec2("source '..fname..'", {"output": v:false})\n') + write_file(sourcing_fname, 'call nvim_exec2("source ' .. fname .. '", {"output": v:false})\n') meths.exec2('set verbose=2', { output = false }) - local traceback_output = 'line 0: sourcing "'..sourcing_fname..'"\n'.. - 'line 0: sourcing "'..fname..'"\n'.. - 'hello\n'.. - 'finished sourcing '..fname..'\n'.. - 'continuing in nvim_exec2() called at '..sourcing_fname..':1\n'.. - 'finished sourcing '..sourcing_fname..'\n'.. - 'continuing in nvim_exec2() called at nvim_exec2():0' - eq({ output = traceback_output }, - meths.exec2('call nvim_exec2("source '..sourcing_fname..'", {"output": v:false})', { output = true })) + local traceback_output = dedent([[ + line 0: sourcing "%s" + line 0: sourcing "%s" + hello + finished sourcing %s + continuing in nvim_exec2() called at %s:1 + finished sourcing %s + continuing in nvim_exec2() called at nvim_exec2():0]]):format( + sourcing_fname, + fname, + fname, + sourcing_fname, + sourcing_fname + ) + eq( + { output = traceback_output }, + meths.exec2( + 'call nvim_exec2("source ' .. sourcing_fname .. '", {"output": v:false})', + { output = true } + ) + ) os.remove(fname) os.remove(sourcing_fname) end) it('returns output', function() - eq({ output = 'this is spinal tap' }, - nvim('exec2', 'lua <]]) - screen:expect{grid=[[ + screen:expect { + grid = [[ ^ | {0:~ }|*4 15 | - ]]} + ]], + } end) end) @@ -320,7 +411,7 @@ describe('API', function() it('works', function() local fname = tmpname() nvim('command', 'new') - nvim('command', 'edit '..fname) + nvim('command', 'edit ' .. fname) nvim('command', 'normal itesting\napi') nvim('command', 'w') local f = assert(io.open(fname)) @@ -334,49 +425,49 @@ describe('API', function() end) it('Vimscript validation error: fails with specific error', function() - local status, rv = pcall(nvim, "command", "bogus_command") - eq(false, status) -- nvim_command() failed. - eq("E492:", string.match(rv, "E%d*:")) -- Vimscript error was returned. - eq('', nvim('eval', 'v:errmsg')) -- v:errmsg was not updated. + local status, rv = pcall(nvim, 'command', 'bogus_command') + eq(false, status) -- nvim_command() failed. + eq('E492:', string.match(rv, 'E%d*:')) -- Vimscript error was returned. + eq('', nvim('eval', 'v:errmsg')) -- v:errmsg was not updated. eq('', eval('v:exception')) end) it('Vimscript execution error: fails with specific error', function() - local status, rv = pcall(nvim, "command", "buffer 23487") - eq(false, status) -- nvim_command() failed. - eq("E86: Buffer 23487 does not exist", string.match(rv, "E%d*:.*")) - eq('', eval('v:errmsg')) -- v:errmsg was not updated. + local status, rv = pcall(nvim, 'command', 'buffer 23487') + eq(false, status) -- nvim_command() failed. + eq('E86: Buffer 23487 does not exist', string.match(rv, 'E%d*:.*')) + eq('', eval('v:errmsg')) -- v:errmsg was not updated. eq('', eval('v:exception')) end) it('gives E493 instead of prompting on backwards range', function() command('split') - eq('Vim(windo):E493: Backwards range given: 2,1windo echo', - pcall_err(command, '2,1windo echo')) + eq( + 'Vim(windo):E493: Backwards range given: 2,1windo echo', + pcall_err(command, '2,1windo echo') + ) end) end) describe('nvim_command_output', function() it('does not induce hit-enter prompt', function() - nvim("ui_attach", 80, 20, {}) + nvim('ui_attach', 80, 20, {}) -- Induce a hit-enter prompt use nvim_input (non-blocking). nvim('command', 'set cmdheight=1') nvim('input', [[:echo "hi\nhi2"]]) -- Verify hit-enter prompt. - eq({mode='r', blocking=true}, nvim("get_mode")) + eq({ mode = 'r', blocking = true }, nvim('get_mode')) nvim('input', [[]]) -- Verify NO hit-enter prompt. nvim('command_output', [[echo "hi\nhi2"]]) - eq({mode='n', blocking=false}, nvim("get_mode")) + eq({ mode = 'n', blocking = false }, nvim('get_mode')) end) it('captures command output', function() - eq('this is\nspinal tap', - nvim('command_output', [[echo "this is\nspinal tap"]])) - eq('no line ending!', - nvim('command_output', [[echon "no line ending!"]])) + eq('this is\nspinal tap', nvim('command_output', [[echo "this is\nspinal tap"]])) + eq('no line ending!', nvim('command_output', [[echon "no line ending!"]])) end) it('captures empty command output', function() @@ -388,49 +479,51 @@ describe('API', function() end) it('captures multiple commands', function() - eq('foo\n 1 %a "[No Name]" line 1', - nvim('command_output', 'echo "foo" | ls')) + eq( + 'foo\n 1 %a "[No Name]" line 1', + nvim('command_output', 'echo "foo" | ls') + ) end) it('captures nested execute()', function() - eq('\nnested1\nnested2\n 1 %a "[No Name]" line 1', - nvim('command_output', - [[echo execute('echo "nested1\nnested2"') | ls]])) + eq( + '\nnested1\nnested2\n 1 %a "[No Name]" line 1', + nvim('command_output', [[echo execute('echo "nested1\nnested2"') | ls]]) + ) end) it('captures nested nvim_command_output()', function() - eq('nested1\nnested2\n 1 %a "[No Name]" line 1', - nvim('command_output', - [[echo nvim_command_output('echo "nested1\nnested2"') | ls]])) + eq( + 'nested1\nnested2\n 1 %a "[No Name]" line 1', + nvim('command_output', [[echo nvim_command_output('echo "nested1\nnested2"') | ls]]) + ) end) it('returns shell |:!| output', function() local win_lf = is_os('win') and '\r' or '' - eq(':!echo foo\r\n\nfoo'..win_lf..'\n', nvim('command_output', [[!echo foo]])) + eq(':!echo foo\r\n\nfoo' .. win_lf .. '\n', nvim('command_output', [[!echo foo]])) end) it('Vimscript validation error: fails with specific error', function() - local status, rv = pcall(nvim, "command_output", "bogus commannnd") - eq(false, status) -- nvim_command_output() failed. - eq("E492: Not an editor command: bogus commannnd", - string.match(rv, "E%d*:.*")) - eq('', eval('v:errmsg')) -- v:errmsg was not updated. + local status, rv = pcall(nvim, 'command_output', 'bogus commannnd') + eq(false, status) -- nvim_command_output() failed. + eq('E492: Not an editor command: bogus commannnd', string.match(rv, 'E%d*:.*')) + eq('', eval('v:errmsg')) -- v:errmsg was not updated. -- Verify NO hit-enter prompt. - eq({mode='n', blocking=false}, nvim("get_mode")) + eq({ mode = 'n', blocking = false }, nvim('get_mode')) end) it('Vimscript execution error: fails with specific error', function() - local status, rv = pcall(nvim, "command_output", "buffer 42") - eq(false, status) -- nvim_command_output() failed. - eq("E86: Buffer 42 does not exist", string.match(rv, "E%d*:.*")) - eq('', eval('v:errmsg')) -- v:errmsg was not updated. + local status, rv = pcall(nvim, 'command_output', 'buffer 42') + eq(false, status) -- nvim_command_output() failed. + eq('E86: Buffer 42 does not exist', string.match(rv, 'E%d*:.*')) + eq('', eval('v:errmsg')) -- v:errmsg was not updated. -- Verify NO hit-enter prompt. - eq({mode='n', blocking=false}, nvim("get_mode")) + eq({ mode = 'n', blocking = false }, nvim('get_mode')) end) it('does not cause heap buffer overflow with large output', function() - eq(eval('string(range(1000000))'), - nvim('command_output', 'echo range(1000000)')) + eq(eval('string(range(1000000))'), nvim('command_output', 'echo range(1000000)')) end) end) @@ -438,22 +531,21 @@ describe('API', function() it('works', function() nvim('command', 'let g:v1 = "a"') nvim('command', 'let g:v2 = [1, 2, {"v3": 3}]') - eq({v1 = 'a', v2 = { 1, 2, { v3 = 3 } } }, nvim('eval', 'g:')) + eq({ v1 = 'a', v2 = { 1, 2, { v3 = 3 } } }, nvim('eval', 'g:')) end) it('handles NULL-initialized strings correctly', function() - eq(1, nvim('eval',"matcharg(1) == ['', '']")) - eq({'', ''}, nvim('eval','matcharg(1)')) + eq(1, nvim('eval', "matcharg(1) == ['', '']")) + eq({ '', '' }, nvim('eval', 'matcharg(1)')) end) it('works under deprecated name', function() - eq(2, request("vim_eval", "1+1")) + eq(2, request('vim_eval', '1+1')) end) - it("Vimscript error: returns error details, does NOT update v:errmsg", function() - eq('Vim:E121: Undefined variable: bogus', - pcall_err(request, 'nvim_eval', 'bogus expression')) - eq('', eval('v:errmsg')) -- v:errmsg was not updated. + it('Vimscript error: returns error details, does NOT update v:errmsg', function() + eq('Vim:E121: Undefined variable: bogus', pcall_err(request, 'nvim_eval', 'bogus expression')) + eq('', eval('v:errmsg')) -- v:errmsg was not updated. end) end) @@ -461,31 +553,41 @@ describe('API', function() it('works', function() nvim('call_function', 'setqflist', { { { filename = 'something', lnum = 17 } }, 'r' }) eq(17, nvim('call_function', 'getqflist', {})[1].lnum) - eq(17, nvim('call_function', 'eval', {17})) - eq('foo', nvim('call_function', 'simplify', {'this/./is//redundant/../../../foo'})) - end) - - it("Vimscript validation error: returns specific error, does NOT update v:errmsg", function() - eq('Vim:E117: Unknown function: bogus function', - pcall_err(request, 'nvim_call_function', 'bogus function', {'arg1'})) - eq('Vim:E119: Not enough arguments for function: atan', - pcall_err(request, 'nvim_call_function', 'atan', {})) + eq(17, nvim('call_function', 'eval', { 17 })) + eq('foo', nvim('call_function', 'simplify', { 'this/./is//redundant/../../../foo' })) + end) + + it('Vimscript validation error: returns specific error, does NOT update v:errmsg', function() + eq( + 'Vim:E117: Unknown function: bogus function', + pcall_err(request, 'nvim_call_function', 'bogus function', { 'arg1' }) + ) + eq( + 'Vim:E119: Not enough arguments for function: atan', + pcall_err(request, 'nvim_call_function', 'atan', {}) + ) eq('', eval('v:exception')) - eq('', eval('v:errmsg')) -- v:errmsg was not updated. - end) - - it("Vimscript error: returns error details, does NOT update v:errmsg", function() - eq('Vim:E808: Number or Float required', - pcall_err(request, 'nvim_call_function', 'atan', {'foo'})) - eq('Vim:Invalid channel stream "xxx"', - pcall_err(request, 'nvim_call_function', 'chanclose', {999, 'xxx'})) - eq('Vim:E900: Invalid channel id', - pcall_err(request, 'nvim_call_function', 'chansend', {999, 'foo'})) + eq('', eval('v:errmsg')) -- v:errmsg was not updated. + end) + + it('Vimscript error: returns error details, does NOT update v:errmsg', function() + eq( + 'Vim:E808: Number or Float required', + pcall_err(request, 'nvim_call_function', 'atan', { 'foo' }) + ) + eq( + 'Vim:Invalid channel stream "xxx"', + pcall_err(request, 'nvim_call_function', 'chanclose', { 999, 'xxx' }) + ) + eq( + 'Vim:E900: Invalid channel id', + pcall_err(request, 'nvim_call_function', 'chansend', { 999, 'foo' }) + ) eq('', eval('v:exception')) - eq('', eval('v:errmsg')) -- v:errmsg was not updated. + eq('', eval('v:errmsg')) -- v:errmsg was not updated. end) - it("Vimscript exception: returns exception details, does NOT update v:errmsg", function() + it('Vimscript exception: returns exception details, does NOT update v:errmsg', function() source([[ function! Foo() abort throw 'wtf' @@ -493,10 +595,11 @@ describe('API', function() ]]) eq('function Foo, line 1: wtf', pcall_err(request, 'nvim_call_function', 'Foo', {})) eq('', eval('v:exception')) - eq('', eval('v:errmsg')) -- v:errmsg was not updated. + eq('', eval('v:errmsg')) -- v:errmsg was not updated. end) it('validation', function() + -- stylua: ignore local too_many_args = { 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x' } source([[ function! Foo(...) abort @@ -504,8 +607,10 @@ describe('API', function() endfunction ]]) -- E740 - eq('Function called with too many arguments', - pcall_err(request, 'nvim_call_function', 'Foo', too_many_args)) + eq( + 'Function called with too many arguments', + pcall_err(request, 'nvim_call_function', 'Foo', too_many_args) + ) end) end) @@ -524,40 +629,55 @@ describe('API', function() ]]) -- :help Dictionary-function - eq('Hello, World!', nvim('call_dict_function', 'g:test_dict_fn', 'F', {'World'})) + eq('Hello, World!', nvim('call_dict_function', 'g:test_dict_fn', 'F', { 'World' })) -- Funcref is sent as NIL over RPC. eq({ greeting = 'Hello', F = NIL }, nvim('get_var', 'test_dict_fn')) -- :help numbered-function - eq('Hi, Moon ...', nvim('call_dict_function', 'g:test_dict_fn2', 'F2', {'Moon'})) + eq('Hi, Moon ...', nvim('call_dict_function', 'g:test_dict_fn2', 'F2', { 'Moon' })) -- Funcref is sent as NIL over RPC. eq({ greeting = 'Hi', F2 = NIL }, nvim('get_var', 'test_dict_fn2')) -- Function specified via RPC dict. source('function! G() dict\n return "@".(self.result)."@"\nendfunction') - eq('@it works@', nvim('call_dict_function', { result = 'it works', G = 'G'}, 'G', {})) + eq('@it works@', nvim('call_dict_function', { result = 'it works', G = 'G' }, 'G', {})) end) it('validation', function() command('let g:d={"baz":"zub","meep":[]}') - eq('Not found: bogus', - pcall_err(request, 'nvim_call_dict_function', 'g:d', 'bogus', {1,2})) - eq('Not a function: baz', - pcall_err(request, 'nvim_call_dict_function', 'g:d', 'baz', {1,2})) - eq('Not a function: meep', - pcall_err(request, 'nvim_call_dict_function', 'g:d', 'meep', {1,2})) - eq('Vim:E117: Unknown function: f', - pcall_err(request, 'nvim_call_dict_function', { f = '' }, 'f', {1,2})) - eq('Not a function: f', - pcall_err(request, 'nvim_call_dict_function', "{ 'f': '' }", 'f', {1,2})) - eq('dict argument type must be String or Dictionary', - pcall_err(request, 'nvim_call_dict_function', 42, 'f', {1,2})) - eq('Failed to evaluate dict expression', - pcall_err(request, 'nvim_call_dict_function', 'foo', 'f', {1,2})) - eq('dict not found', - pcall_err(request, 'nvim_call_dict_function', '42', 'f', {1,2})) - eq('Invalid (empty) function name', - pcall_err(request, 'nvim_call_dict_function', "{ 'f': '' }", '', {1,2})) + eq( + 'Not found: bogus', + pcall_err(request, 'nvim_call_dict_function', 'g:d', 'bogus', { 1, 2 }) + ) + eq( + 'Not a function: baz', + pcall_err(request, 'nvim_call_dict_function', 'g:d', 'baz', { 1, 2 }) + ) + eq( + 'Not a function: meep', + pcall_err(request, 'nvim_call_dict_function', 'g:d', 'meep', { 1, 2 }) + ) + eq( + 'Vim:E117: Unknown function: f', + pcall_err(request, 'nvim_call_dict_function', { f = '' }, 'f', { 1, 2 }) + ) + eq( + 'Not a function: f', + pcall_err(request, 'nvim_call_dict_function', "{ 'f': '' }", 'f', { 1, 2 }) + ) + eq( + 'dict argument type must be String or Dictionary', + pcall_err(request, 'nvim_call_dict_function', 42, 'f', { 1, 2 }) + ) + eq( + 'Failed to evaluate dict expression', + pcall_err(request, 'nvim_call_dict_function', 'foo', 'f', { 1, 2 }) + ) + eq('dict not found', pcall_err(request, 'nvim_call_dict_function', '42', 'f', { 1, 2 })) + eq( + 'Invalid (empty) function name', + pcall_err(request, 'nvim_call_dict_function', "{ 'f': '' }", '', { 1, 2 }) + ) end) end) @@ -565,21 +685,21 @@ describe('API', function() local start_dir before_each(function() - funcs.mkdir("Xtestdir") + funcs.mkdir('Xtestdir') start_dir = funcs.getcwd() end) after_each(function() - helpers.rmdir("Xtestdir") + helpers.rmdir('Xtestdir') end) it('works', function() - meths.set_current_dir("Xtestdir") - eq(funcs.getcwd(), start_dir .. helpers.get_pathsep() .. "Xtestdir") + meths.set_current_dir('Xtestdir') + eq(funcs.getcwd(), start_dir .. helpers.get_pathsep() .. 'Xtestdir') end) it('sets previous directory', function() - meths.set_current_dir("Xtestdir") + meths.set_current_dir('Xtestdir') command('cd -') eq(funcs.getcwd(), start_dir) end) @@ -590,60 +710,67 @@ describe('API', function() meths.exec_lua('vim.api.nvim_set_var("test", 3)', {}) eq(3, meths.get_var('test')) - eq(17, meths.exec_lua('a, b = ...\nreturn a + b', {10,7})) + eq(17, meths.exec_lua('a, b = ...\nreturn a + b', { 10, 7 })) - eq(NIL, meths.exec_lua('function xx(a,b)\nreturn a..b\nend',{})) - eq("xy", meths.exec_lua('return xx(...)', {'x','y'})) + eq(NIL, meths.exec_lua('function xx(a,b)\nreturn a..b\nend', {})) + eq('xy', meths.exec_lua('return xx(...)', { 'x', 'y' })) -- Deprecated name: nvim_execute_lua. - eq("xy", meths.execute_lua('return xx(...)', {'x','y'})) + eq('xy', meths.execute_lua('return xx(...)', { 'x', 'y' })) end) it('reports errors', function() - eq([[Error loading lua: [string ""]:0: '=' expected near '+']], - pcall_err(meths.exec_lua, 'a+*b', {})) + eq( + [[Error loading lua: [string ""]:0: '=' expected near '+']], + pcall_err(meths.exec_lua, 'a+*b', {}) + ) - eq([[Error loading lua: [string ""]:0: unexpected symbol near '1']], - pcall_err(meths.exec_lua, '1+2', {})) + eq( + [[Error loading lua: [string ""]:0: unexpected symbol near '1']], + pcall_err(meths.exec_lua, '1+2', {}) + ) - eq([[Error loading lua: [string ""]:0: unexpected symbol]], - pcall_err(meths.exec_lua, 'aa=bb\0', {})) + eq( + [[Error loading lua: [string ""]:0: unexpected symbol]], + pcall_err(meths.exec_lua, 'aa=bb\0', {}) + ) - eq([[attempt to call global 'bork' (a nil value)]], - pcall_err(meths.exec_lua, 'bork()', {})) + eq([[attempt to call global 'bork' (a nil value)]], pcall_err(meths.exec_lua, 'bork()', {})) - eq('did\nthe\nfail', - pcall_err(meths.exec_lua, 'error("did\\nthe\\nfail")', {})) + eq('did\nthe\nfail', pcall_err(meths.exec_lua, 'error("did\\nthe\\nfail")', {})) end) it('uses native float values', function() - eq(2.5, meths.exec_lua("return select(1, ...)", {2.5})) - eq("2.5", meths.exec_lua("return vim.inspect(...)", {2.5})) + eq(2.5, meths.exec_lua('return select(1, ...)', { 2.5 })) + eq('2.5', meths.exec_lua('return vim.inspect(...)', { 2.5 })) -- "special" float values are still accepted as return values. eq(2.5, meths.exec_lua("return vim.api.nvim_eval('2.5')", {})) - eq("{\n [false] = 2.5,\n [true] = 3\n}", meths.exec_lua("return vim.inspect(vim.api.nvim_eval('2.5'))", {})) + eq( + '{\n [false] = 2.5,\n [true] = 3\n}', + meths.exec_lua("return vim.inspect(vim.api.nvim_eval('2.5'))", {}) + ) end) end) describe('nvim_notify', function() it('can notify a info message', function() - nvim("notify", "hello world", 2, {}) + nvim('notify', 'hello world', 2, {}) end) it('can be overridden', function() - command("lua vim.notify = function(...) return 42 end") + command('lua vim.notify = function(...) return 42 end') eq(42, meths.exec_lua("return vim.notify('Hello world')", {})) - nvim("notify", "hello world", 4, {}) + nvim('notify', 'hello world', 4, {}) end) end) describe('nvim_input', function() - it("Vimscript error: does NOT fail, updates v:errmsg", function() - local status, _ = pcall(nvim, "input", ":call bogus_fn()") - local v_errnum = string.match(nvim("eval", "v:errmsg"), "E%d*:") - eq(true, status) -- nvim_input() did not fail. - eq("E117:", v_errnum) -- v:errmsg was updated. + it('Vimscript error: does NOT fail, updates v:errmsg', function() + local status, _ = pcall(nvim, 'input', ':call bogus_fn()') + local v_errnum = string.match(nvim('eval', 'v:errmsg'), 'E%d*:') + eq(true, status) -- nvim_input() did not fail. + eq('E117:', v_errnum) -- v:errmsg was updated. end) it('does not crash even if trans_special result is largest #11788, #12287', function() @@ -654,10 +781,8 @@ describe('API', function() describe('nvim_paste', function() it('validation', function() - eq("Invalid 'phase': -2", - pcall_err(request, 'nvim_paste', 'foo', true, -2)) - eq("Invalid 'phase': 4", - pcall_err(request, 'nvim_paste', 'foo', true, 4)) + eq("Invalid 'phase': -2", pcall_err(request, 'nvim_paste', 'foo', true, -2)) + eq("Invalid 'phase': 4", pcall_err(request, 'nvim_paste', 'foo', true, 4)) end) local function run_streamed_paste_tests() it('stream: multiple chunks form one undo-block', function() @@ -686,7 +811,7 @@ describe('API', function() 2/chunk 3 2/chunk 4 (end) ]]) - feed('u') -- Undo. + feed('u') -- Undo. expect(expected1) end) it('stream: Insert mode', function() @@ -1036,7 +1161,7 @@ describe('API', function() line 2 line 3 ]]) - eq({0,4,1,0}, funcs.getpos('.')) -- Cursor follows the paste. + eq({ 0, 4, 1, 0 }, funcs.getpos('.')) -- Cursor follows the paste. eq(false, nvim('get_option_value', 'paste', {})) command('%delete _') -- Without final "\n". @@ -1045,7 +1170,7 @@ describe('API', function() line 1 line 2 line 3]]) - eq({0,3,6,0}, funcs.getpos('.')) + eq({ 0, 3, 6, 0 }, funcs.getpos('.')) command('%delete _') -- CRLF #10872 nvim('paste', 'line 1\r\nline 2\r\nline 3\r\n', true, -1) @@ -1054,7 +1179,7 @@ describe('API', function() line 2 line 3 ]]) - eq({0,4,1,0}, funcs.getpos('.')) + eq({ 0, 4, 1, 0 }, funcs.getpos('.')) command('%delete _') -- CRLF without final "\n". nvim('paste', 'line 1\r\nline 2\r\nline 3\r', true, -1) @@ -1063,7 +1188,7 @@ describe('API', function() line 2 line 3 ]]) - eq({0,4,1,0}, funcs.getpos('.')) + eq({ 0, 4, 1, 0 }, funcs.getpos('.')) command('%delete _') -- CRLF without final "\r\n". nvim('paste', 'line 1\r\nline 2\r\nline 3', true, -1) @@ -1071,17 +1196,17 @@ describe('API', function() line 1 line 2 line 3]]) - eq({0,3,6,0}, funcs.getpos('.')) + eq({ 0, 3, 6, 0 }, funcs.getpos('.')) command('%delete _') -- Various other junk. nvim('paste', 'line 1\r\n\r\rline 2\nline 3\rline 4\r', true, -1) expect('line 1\n\n\nline 2\nline 3\nline 4\n') - eq({0,7,1,0}, funcs.getpos('.')) + eq({ 0, 7, 1, 0 }, funcs.getpos('.')) eq(false, nvim('get_option_value', 'paste', {})) end) it('Replace-mode', function() -- Within single line - nvim('put', {'aabbccdd', 'eeffgghh', 'iijjkkll'}, "c", true, false) + nvim('put', { 'aabbccdd', 'eeffgghh', 'iijjkkll' }, 'c', true, false) command('normal l') command('startreplace') nvim('paste', '123456', true, -1) @@ -1091,7 +1216,7 @@ describe('API', function() iijjkkll]]) command('%delete _') -- Across lines - nvim('put', {'aabbccdd', 'eeffgghh', 'iijjkkll'}, "c", true, false) + nvim('put', { 'aabbccdd', 'eeffgghh', 'iijjkkll' }, 'c', true, false) command('normal l') command('startreplace') nvim('paste', '123\n456', true, -1) @@ -1147,101 +1272,103 @@ describe('API', function() it('crlf=false does not break lines at CR, CRLF', function() nvim('paste', 'line 1\r\n\r\rline 2\nline 3\rline 4\r', false, -1) expect('line 1\r\n\r\rline 2\nline 3\rline 4\r') - eq({0,3,14,0}, funcs.getpos('.')) + eq({ 0, 3, 14, 0 }, funcs.getpos('.')) end) it('vim.paste() failure', function() nvim('exec_lua', 'vim.paste = (function(lines, phase) error("fake fail") end)', {}) - eq('fake fail', - pcall_err(request, 'nvim_paste', 'line 1\nline 2\nline 3', false, 1)) + eq('fake fail', pcall_err(request, 'nvim_paste', 'line 1\nline 2\nline 3', false, 1)) end) end) describe('nvim_put', function() it('validation', function() - eq("Invalid 'line': expected String, got Integer", - pcall_err(request, 'nvim_put', {42}, 'l', false, false)) - eq("Invalid 'type': 'x'", - pcall_err(request, 'nvim_put', {'foo'}, 'x', false, false)) + eq( + "Invalid 'line': expected String, got Integer", + pcall_err(request, 'nvim_put', { 42 }, 'l', false, false) + ) + eq("Invalid 'type': 'x'", pcall_err(request, 'nvim_put', { 'foo' }, 'x', false, false)) end) it("fails if 'nomodifiable'", function() command('set nomodifiable') - eq([[Vim:E21: Cannot make changes, 'modifiable' is off]], - pcall_err(request, 'nvim_put', {'a','b'}, 'l', true, true)) + eq( + [[Vim:E21: Cannot make changes, 'modifiable' is off]], + pcall_err(request, 'nvim_put', { 'a', 'b' }, 'l', true, true) + ) end) it('inserts text', function() -- linewise - nvim('put', {'line 1','line 2','line 3'}, 'l', true, true) + nvim('put', { 'line 1', 'line 2', 'line 3' }, 'l', true, true) expect([[ line 1 line 2 line 3]]) - eq({0,4,1,0}, funcs.getpos('.')) + eq({ 0, 4, 1, 0 }, funcs.getpos('.')) command('%delete _') -- charwise - nvim('put', {'line 1','line 2','line 3'}, 'c', true, false) + nvim('put', { 'line 1', 'line 2', 'line 3' }, 'c', true, false) expect([[ line 1 line 2 line 3]]) - eq({0,1,1,0}, funcs.getpos('.')) -- follow=false + eq({ 0, 1, 1, 0 }, funcs.getpos('.')) -- follow=false -- blockwise - nvim('put', {'AA','BB'}, 'b', true, true) + nvim('put', { 'AA', 'BB' }, 'b', true, true) expect([[ lAAine 1 lBBine 2 line 3]]) - eq({0,2,4,0}, funcs.getpos('.')) + eq({ 0, 2, 4, 0 }, funcs.getpos('.')) command('%delete _') -- Empty lines list. nvim('put', {}, 'c', true, true) - eq({0,1,1,0}, funcs.getpos('.')) + eq({ 0, 1, 1, 0 }, funcs.getpos('.')) expect([[]]) -- Single empty line. - nvim('put', {''}, 'c', true, true) - eq({0,1,1,0}, funcs.getpos('.')) + nvim('put', { '' }, 'c', true, true) + eq({ 0, 1, 1, 0 }, funcs.getpos('.')) expect([[ ]]) - nvim('put', {'AB'}, 'c', true, true) + nvim('put', { 'AB' }, 'c', true, true) -- after=false, follow=true - nvim('put', {'line 1','line 2'}, 'c', false, true) + nvim('put', { 'line 1', 'line 2' }, 'c', false, true) expect([[ Aline 1 line 2B]]) - eq({0,2,7,0}, funcs.getpos('.')) + eq({ 0, 2, 7, 0 }, funcs.getpos('.')) command('%delete _') - nvim('put', {'AB'}, 'c', true, true) + nvim('put', { 'AB' }, 'c', true, true) -- after=false, follow=false - nvim('put', {'line 1','line 2'}, 'c', false, false) + nvim('put', { 'line 1', 'line 2' }, 'c', false, false) expect([[ Aline 1 line 2B]]) - eq({0,1,2,0}, funcs.getpos('.')) + eq({ 0, 1, 2, 0 }, funcs.getpos('.')) eq('', nvim('eval', 'v:errmsg')) end) it('detects charwise/linewise text (empty {type})', function() -- linewise (final item is empty string) - nvim('put', {'line 1','line 2','line 3',''}, '', true, true) + nvim('put', { 'line 1', 'line 2', 'line 3', '' }, '', true, true) expect([[ line 1 line 2 line 3]]) - eq({0,4,1,0}, funcs.getpos('.')) + eq({ 0, 4, 1, 0 }, funcs.getpos('.')) command('%delete _') -- charwise (final item is non-empty) - nvim('put', {'line 1','line 2','line 3'}, '', true, true) + nvim('put', { 'line 1', 'line 2', 'line 3' }, '', true, true) expect([[ line 1 line 2 line 3]]) - eq({0,3,6,0}, funcs.getpos('.')) + eq({ 0, 3, 6, 0 }, funcs.getpos('.')) end) it('allows block width', function() -- behave consistently with setreg(); support "\022{NUM}" return by getregtype() - meths.put({'line 1','line 2','line 3'}, 'l', false, false) + meths.put({ 'line 1', 'line 2', 'line 3' }, 'l', false, false) expect([[ line 1 line 2 @@ -1249,23 +1376,21 @@ describe('API', function() ]]) -- larger width create spaces - meths.put({'a', 'bc'}, 'b3', false, false) + meths.put({ 'a', 'bc' }, 'b3', false, false) expect([[ a line 1 bc line 2 line 3 ]]) -- smaller width is ignored - meths.put({'xxx', 'yyy'}, '\0221', false, true) + meths.put({ 'xxx', 'yyy' }, '\0221', false, true) expect([[ xxxa line 1 yyybc line 2 line 3 ]]) - eq("Invalid 'type': 'bx'", - pcall_err(meths.put, {'xxx', 'yyy'}, 'bx', false, true)) - eq("Invalid 'type': 'b3x'", - pcall_err(meths.put, {'xxx', 'yyy'}, 'b3x', false, true)) + eq("Invalid 'type': 'bx'", pcall_err(meths.put, { 'xxx', 'yyy' }, 'bx', false, true)) + eq("Invalid 'type': 'b3x'", pcall_err(meths.put, { 'xxx', 'yyy' }, 'b3x', false, true)) end) end) @@ -1297,18 +1422,18 @@ describe('API', function() end) it('nvim_get_var, nvim_set_var, nvim_del_var', function() - nvim('set_var', 'lua', {1, 2, {['3'] = 1}}) - eq({1, 2, {['3'] = 1}}, nvim('get_var', 'lua')) - eq({1, 2, {['3'] = 1}}, nvim('eval', 'g:lua')) + nvim('set_var', 'lua', { 1, 2, { ['3'] = 1 } }) + eq({ 1, 2, { ['3'] = 1 } }, nvim('get_var', 'lua')) + eq({ 1, 2, { ['3'] = 1 } }, nvim('eval', 'g:lua')) eq(1, funcs.exists('g:lua')) meths.del_var('lua') eq(0, funcs.exists('g:lua')) - eq("Key not found: lua", pcall_err(meths.del_var, 'lua')) + eq('Key not found: lua', pcall_err(meths.del_var, 'lua')) meths.set_var('lua', 1) -- Empty keys are allowed in Vim dicts (and msgpack). - nvim('set_var', 'dict_empty_key', {[''] = 'empty key'}) - eq({[''] = 'empty key'}, nvim('get_var', 'dict_empty_key')) + nvim('set_var', 'dict_empty_key', { [''] = 'empty key' }) + eq({ [''] = 'empty key' }, nvim('get_var', 'dict_empty_key')) -- Set locked g: var. command('lockvar lua') @@ -1330,12 +1455,12 @@ describe('API', function() local pathsep = helpers.get_pathsep() local xconfig = 'Xhome' .. pathsep .. 'Xconfig' local xdata = 'Xhome' .. pathsep .. 'Xdata' - local autoload_folder = table.concat({xconfig, 'nvim', 'autoload'}, pathsep) - local autoload_file = table.concat({autoload_folder , 'testload.vim'}, pathsep) + local autoload_folder = table.concat({ xconfig, 'nvim', 'autoload' }, pathsep) + local autoload_file = table.concat({ autoload_folder, 'testload.vim' }, pathsep) mkdir_p(autoload_folder) - write_file(autoload_file , [[let testload#value = 2]]) + write_file(autoload_file, [[let testload#value = 2]]) - clear{ args_rm={'-u'}, env={ XDG_CONFIG_HOME=xconfig, XDG_DATA_HOME=xdata } } + clear { args_rm = { '-u' }, env = { XDG_CONFIG_HOME = xconfig, XDG_DATA_HOME = xdata } } eq(2, meths.get_var('testload#value')) rmdir('Xhome') end) @@ -1355,58 +1480,64 @@ describe('API', function() eq({}, eval('v:oldfiles')) feed('i foo foo foo0/foo') - eq({1, 1}, meths.win_get_cursor(0)) + eq({ 1, 1 }, meths.win_get_cursor(0)) eq(1, eval('v:searchforward')) feed('n') - eq({1, 5}, meths.win_get_cursor(0)) + eq({ 1, 5 }, meths.win_get_cursor(0)) meths.set_vvar('searchforward', 0) eq(0, eval('v:searchforward')) feed('n') - eq({1, 1}, meths.win_get_cursor(0)) + eq({ 1, 1 }, meths.win_get_cursor(0)) meths.set_vvar('searchforward', 1) eq(1, eval('v:searchforward')) feed('n') - eq({1, 5}, meths.win_get_cursor(0)) + eq({ 1, 5 }, meths.win_get_cursor(0)) local screen = Screen.new(60, 3) screen:set_default_attr_ids({ - [0] = {bold = true, foreground = Screen.colors.Blue}, - [1] = {background = Screen.colors.Yellow}, + [0] = { bold = true, foreground = Screen.colors.Blue }, + [1] = { background = Screen.colors.Yellow }, }) screen:attach() eq(1, eval('v:hlsearch')) - screen:expect{grid=[[ + screen:expect { + grid = [[ {1:foo} {1:^foo} {1:foo} | {0:~ }| | - ]]} + ]], + } meths.set_vvar('hlsearch', 0) eq(0, eval('v:hlsearch')) - screen:expect{grid=[[ + screen:expect { + grid = [[ foo ^foo foo | {0:~ }| | - ]]} + ]], + } meths.set_vvar('hlsearch', 1) eq(1, eval('v:hlsearch')) - screen:expect{grid=[[ + screen:expect { + grid = [[ {1:foo} {1:^foo} {1:foo} | {0:~ }| | - ]]} + ]], + } end) it('vim_set_var returns the old value', function() - local val1 = {1, 2, {['3'] = 1}} - local val2 = {4, 7} + local val1 = { 1, 2, { ['3'] = 1 } } + local val2 = { 4, 7 } eq(NIL, request('vim_set_var', 'lua', val1)) eq(val1, request('vim_set_var', 'lua', val2)) end) it('vim_del_var returns the old value', function() - local val1 = {1, 2, {['3'] = 1}} - local val2 = {4, 7} - eq(NIL, request('vim_set_var', 'lua', val1)) + local val1 = { 1, 2, { ['3'] = 1 } } + local val2 = { 4, 7 } + eq(NIL, request('vim_set_var', 'lua', val1)) eq(val1, request('vim_set_var', 'lua', val2)) eq(val2, request('vim_del_var', 'lua')) end) @@ -1430,27 +1561,26 @@ describe('API', function() end) it('works to set global value of local options', function() - nvim('set_option_value', 'lisp', true, {scope='global'}) - eq(true, nvim('get_option_value', 'lisp', {scope='global'})) + nvim('set_option_value', 'lisp', true, { scope = 'global' }) + eq(true, nvim('get_option_value', 'lisp', { scope = 'global' })) eq(false, nvim('get_option_value', 'lisp', {})) eq(nil, nvim('command_output', 'setglobal lisp?'):match('nolisp')) eq('nolisp', nvim('command_output', 'setlocal lisp?'):match('nolisp')) - nvim('set_option_value', 'shiftwidth', 20, {scope='global'}) + nvim('set_option_value', 'shiftwidth', 20, { scope = 'global' }) eq('20', nvim('command_output', 'setglobal shiftwidth?'):match('%d+')) eq('8', nvim('command_output', 'setlocal shiftwidth?'):match('%d+')) end) it('updates where the option was last set from', function() nvim('set_option_value', 'equalalways', false, {}) - local status, rv = pcall(nvim, 'command_output', - 'verbose set equalalways?') + local status, rv = pcall(nvim, 'command_output', 'verbose set equalalways?') eq(true, status) - ok(nil ~= string.find(rv, 'noequalalways\n'.. - '\tLast set from API client %(channel id %d+%)')) + ok( + nil ~= string.find(rv, 'noequalalways\n' .. '\tLast set from API client %(channel id %d+%)') + ) nvim('exec_lua', 'vim.api.nvim_set_option_value("equalalways", true, {})', {}) - status, rv = pcall(nvim, 'command_output', - 'verbose set equalalways?') + status, rv = pcall(nvim, 'command_output', 'verbose set equalalways?') eq(true, status) eq(' equalalways\n\tLast set from Lua', rv) end) @@ -1470,37 +1600,49 @@ describe('API', function() end) it('validation', function() - eq("Invalid 'scope': expected 'local' or 'global'", - pcall_err(nvim, 'get_option_value', 'scrolloff', {scope = 'bogus'})) - eq("Invalid 'scope': expected 'local' or 'global'", - pcall_err(nvim, 'set_option_value', 'scrolloff', 1, {scope = 'bogus'})) - eq("Invalid 'scope': expected String, got Integer", - pcall_err(nvim, 'get_option_value', 'scrolloff', {scope = 42})) - eq("Invalid 'value': expected valid option type, got Array", - pcall_err(nvim, 'set_option_value', 'scrolloff', {}, {})) - eq("Invalid value for option 'scrolloff': expected number, got boolean true", - pcall_err(nvim, 'set_option_value', 'scrolloff', true, {})) - eq("Invalid value for option 'scrolloff': expected number, got string \"wrong\"", - pcall_err(nvim, 'set_option_value', 'scrolloff', 'wrong', {})) + eq( + "Invalid 'scope': expected 'local' or 'global'", + pcall_err(nvim, 'get_option_value', 'scrolloff', { scope = 'bogus' }) + ) + eq( + "Invalid 'scope': expected 'local' or 'global'", + pcall_err(nvim, 'set_option_value', 'scrolloff', 1, { scope = 'bogus' }) + ) + eq( + "Invalid 'scope': expected String, got Integer", + pcall_err(nvim, 'get_option_value', 'scrolloff', { scope = 42 }) + ) + eq( + "Invalid 'value': expected valid option type, got Array", + pcall_err(nvim, 'set_option_value', 'scrolloff', {}, {}) + ) + eq( + "Invalid value for option 'scrolloff': expected number, got boolean true", + pcall_err(nvim, 'set_option_value', 'scrolloff', true, {}) + ) + eq( + 'Invalid value for option \'scrolloff\': expected number, got string "wrong"', + pcall_err(nvim, 'set_option_value', 'scrolloff', 'wrong', {}) + ) end) it('can get local values when global value is set', function() eq(0, nvim('get_option_value', 'scrolloff', {})) - eq(-1, nvim('get_option_value', 'scrolloff', {scope = 'local'})) + eq(-1, nvim('get_option_value', 'scrolloff', { scope = 'local' })) end) it('can set global and local values', function() nvim('set_option_value', 'makeprg', 'hello', {}) eq('hello', nvim('get_option_value', 'makeprg', {})) - eq('', nvim('get_option_value', 'makeprg', {scope = 'local'})) - nvim('set_option_value', 'makeprg', 'world', {scope = 'local'}) - eq('world', nvim('get_option_value', 'makeprg', {scope = 'local'})) - nvim('set_option_value', 'makeprg', 'goodbye', {scope = 'global'}) - eq('goodbye', nvim('get_option_value', 'makeprg', {scope = 'global'})) + eq('', nvim('get_option_value', 'makeprg', { scope = 'local' })) + nvim('set_option_value', 'makeprg', 'world', { scope = 'local' }) + eq('world', nvim('get_option_value', 'makeprg', { scope = 'local' })) + nvim('set_option_value', 'makeprg', 'goodbye', { scope = 'global' }) + eq('goodbye', nvim('get_option_value', 'makeprg', { scope = 'global' })) nvim('set_option_value', 'makeprg', 'hello', {}) - eq('hello', nvim('get_option_value', 'makeprg', {scope = 'global'})) + eq('hello', nvim('get_option_value', 'makeprg', { scope = 'global' })) eq('hello', nvim('get_option_value', 'makeprg', {})) - eq('', nvim('get_option_value', 'makeprg', {scope = 'local'})) + eq('', nvim('get_option_value', 'makeprg', { scope = 'local' })) end) it('clears the local value of an option with nil', function() @@ -1509,63 +1651,63 @@ describe('API', function() eq(42, nvim('get_option_value', 'shiftwidth', {})) -- Set local value - nvim('set_option_value', 'shiftwidth', 8, {scope = 'local'}) + nvim('set_option_value', 'shiftwidth', 8, { scope = 'local' }) eq(8, nvim('get_option_value', 'shiftwidth', {})) - eq(8, nvim('get_option_value', 'shiftwidth', {scope = 'local'})) - eq(42, nvim('get_option_value', 'shiftwidth', {scope = 'global'})) + eq(8, nvim('get_option_value', 'shiftwidth', { scope = 'local' })) + eq(42, nvim('get_option_value', 'shiftwidth', { scope = 'global' })) -- Clear value without scope nvim('set_option_value', 'shiftwidth', NIL, {}) eq(42, nvim('get_option_value', 'shiftwidth', {})) - eq(42, nvim('get_option_value', 'shiftwidth', {scope = 'local'})) + eq(42, nvim('get_option_value', 'shiftwidth', { scope = 'local' })) -- Clear value with explicit scope - nvim('set_option_value', 'shiftwidth', 8, {scope = 'local'}) - nvim('set_option_value', 'shiftwidth', NIL, {scope = 'local'}) + nvim('set_option_value', 'shiftwidth', 8, { scope = 'local' }) + nvim('set_option_value', 'shiftwidth', NIL, { scope = 'local' }) eq(42, nvim('get_option_value', 'shiftwidth', {})) - eq(42, nvim('get_option_value', 'shiftwidth', {scope = 'local'})) + eq(42, nvim('get_option_value', 'shiftwidth', { scope = 'local' })) -- Now try with options with a special "local is unset" value (e.g. 'undolevels') nvim('set_option_value', 'undolevels', 1000, {}) - nvim('set_option_value', 'undolevels', 1200, {scope = 'local'}) - eq(1200, nvim('get_option_value', 'undolevels', {scope = 'local'})) - nvim('set_option_value', 'undolevels', NIL, {scope = 'local'}) - eq(-123456, nvim('get_option_value', 'undolevels', {scope = 'local'})) + nvim('set_option_value', 'undolevels', 1200, { scope = 'local' }) + eq(1200, nvim('get_option_value', 'undolevels', { scope = 'local' })) + nvim('set_option_value', 'undolevels', NIL, { scope = 'local' }) + eq(-123456, nvim('get_option_value', 'undolevels', { scope = 'local' })) eq(1000, nvim('get_option_value', 'undolevels', {})) nvim('set_option_value', 'autoread', true, {}) - nvim('set_option_value', 'autoread', false, {scope = 'local'}) - eq(false, nvim('get_option_value', 'autoread', {scope = 'local'})) - nvim('set_option_value', 'autoread', NIL, {scope = 'local'}) - eq(NIL, nvim('get_option_value', 'autoread', {scope = 'local'})) + nvim('set_option_value', 'autoread', false, { scope = 'local' }) + eq(false, nvim('get_option_value', 'autoread', { scope = 'local' })) + nvim('set_option_value', 'autoread', NIL, { scope = 'local' }) + eq(NIL, nvim('get_option_value', 'autoread', { scope = 'local' })) eq(true, nvim('get_option_value', 'autoread', {})) end) it('set window options', function() nvim('set_option_value', 'colorcolumn', '4,3', {}) - eq('4,3', nvim('get_option_value', 'colorcolumn', {scope = 'local'})) - command("set modified hidden") - command("enew") -- edit new buffer, window option is preserved - eq('4,3', nvim('get_option_value', 'colorcolumn', {scope = 'local'})) + eq('4,3', nvim('get_option_value', 'colorcolumn', { scope = 'local' })) + command('set modified hidden') + command('enew') -- edit new buffer, window option is preserved + eq('4,3', nvim('get_option_value', 'colorcolumn', { scope = 'local' })) end) it('set local window options', function() - nvim('set_option_value', 'colorcolumn', '4,3', {win=0, scope='local'}) - eq('4,3', nvim('get_option_value', 'colorcolumn', {win = 0, scope = 'local'})) - command("set modified hidden") - command("enew") -- edit new buffer, window option is reset - eq('', nvim('get_option_value', 'colorcolumn', {win = 0, scope = 'local'})) + nvim('set_option_value', 'colorcolumn', '4,3', { win = 0, scope = 'local' }) + eq('4,3', nvim('get_option_value', 'colorcolumn', { win = 0, scope = 'local' })) + command('set modified hidden') + command('enew') -- edit new buffer, window option is reset + eq('', nvim('get_option_value', 'colorcolumn', { win = 0, scope = 'local' })) end) it('get buffer or window-local options', function() nvim('command', 'new') local buf = nvim('get_current_buf').id - nvim('set_option_value', 'tagfunc', 'foobar', {buf=buf}) - eq('foobar', nvim('get_option_value', 'tagfunc', {buf = buf})) + nvim('set_option_value', 'tagfunc', 'foobar', { buf = buf }) + eq('foobar', nvim('get_option_value', 'tagfunc', { buf = buf })) local win = nvim('get_current_win').id - nvim('set_option_value', 'number', true, {win=win}) - eq(true, nvim('get_option_value', 'number', {win = win})) + nvim('set_option_value', 'number', true, { win = win }) + eq(true, nvim('get_option_value', 'number', { win = win })) end) it('getting current buffer option does not adjust cursor #19381', function() @@ -1574,9 +1716,9 @@ describe('API', function() local win = nvim('get_current_win').id insert('some text') feed('0v$') - eq({1, 9}, nvim('win_get_cursor', win)) - nvim('get_option_value', 'filetype', {buf = buf}) - eq({1, 9}, nvim('win_get_cursor', win)) + eq({ 1, 9 }, nvim('win_get_cursor', win)) + nvim('get_option_value', 'filetype', { buf = buf }) + eq({ 1, 9 }, nvim('win_get_cursor', win)) end) it('can get default option values for filetypes', function() @@ -1584,23 +1726,25 @@ describe('API', function() for ft, opts in pairs { lua = { commentstring = '-- %s' }, vim = { commentstring = '"%s' }, - man = { tagfunc = 'v:lua.require\'man\'.goto_tag' }, - xml = { formatexpr = 'xmlformat#Format()' } + man = { tagfunc = "v:lua.require'man'.goto_tag" }, + xml = { formatexpr = 'xmlformat#Format()' }, } do for option, value in pairs(opts) do eq(value, nvim('get_option_value', option, { filetype = ft })) end end - command'au FileType lua setlocal commentstring=NEW\\ %s' + command 'au FileType lua setlocal commentstring=NEW\\ %s' eq('NEW %s', nvim('get_option_value', 'commentstring', { filetype = 'lua' })) end) it('errors for bad FileType autocmds', function() - command'au FileType lua setlocal commentstring=BAD' - eq([[FileType Autocommands for "lua": Vim(setlocal):E537: 'commentstring' must be empty or contain %s: commentstring=BAD]], - pcall_err(nvim, 'get_option_value', 'commentstring', { filetype = 'lua' })) + command 'au FileType lua setlocal commentstring=BAD' + eq( + [[FileType Autocommands for "lua": Vim(setlocal):E537: 'commentstring' must be empty or contain %s: commentstring=BAD]], + pcall_err(nvim, 'get_option_value', 'commentstring', { filetype = 'lua' }) + ) end) it("value of 'modified' is always false for scratch buffers", function() @@ -1659,83 +1803,83 @@ describe('API', function() end) describe('nvim_get_mode', function() - it("during normal-mode `g` returns blocking=true", function() - nvim("input", "o") -- add a line - eq({mode='i', blocking=false}, nvim("get_mode")) - nvim("input", [[]]) - eq(2, nvim("eval", "line('.')")) - eq({mode='n', blocking=false}, nvim("get_mode")) + it('during normal-mode `g` returns blocking=true', function() + nvim('input', 'o') -- add a line + eq({ mode = 'i', blocking = false }, nvim('get_mode')) + nvim('input', [[]]) + eq(2, nvim('eval', "line('.')")) + eq({ mode = 'n', blocking = false }, nvim('get_mode')) - nvim("input", "g") - eq({mode='n', blocking=true}, nvim("get_mode")) + nvim('input', 'g') + eq({ mode = 'n', blocking = true }, nvim('get_mode')) - nvim("input", "k") -- complete the operator - eq(1, nvim("eval", "line('.')")) -- verify the completed operator - eq({mode='n', blocking=false}, nvim("get_mode")) + nvim('input', 'k') -- complete the operator + eq(1, nvim('eval', "line('.')")) -- verify the completed operator + eq({ mode = 'n', blocking = false }, nvim('get_mode')) end) - it("returns the correct result multiple consecutive times", function() - for _ = 1,5 do - eq({mode='n', b