diff options
author | Jakob Schnitzer <mail@jakobschnitzer.de> | 2017-06-28 16:52:04 +0200 |
---|---|---|
committer | Jakob Schnitzer <mail@jakobschnitzer.de> | 2017-06-28 16:52:04 +0200 |
commit | e8829710bc5f38208499e0ad38402eac24a67ac2 (patch) | |
tree | 4e1ae954c2e301adadbfa7038b823ea9ea2fb08e /test/functional | |
parent | ff8b2eb435c518f0eafd0e509afe1f5ee4a81fd1 (diff) | |
parent | f0dafa89c2b7602cfedf0bd3409858e4c212b0a2 (diff) | |
download | rneovim-e8829710bc5f38208499e0ad38402eac24a67ac2.tar.gz rneovim-e8829710bc5f38208499e0ad38402eac24a67ac2.tar.bz2 rneovim-e8829710bc5f38208499e0ad38402eac24a67ac2.zip |
Merge branch 'master' into option-fixes
Diffstat (limited to 'test/functional')
43 files changed, 3742 insertions, 177 deletions
diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index 9699ea8f85..823c134ebd 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -10,6 +10,7 @@ local insert = helpers.insert local NIL = helpers.NIL local meth_pcall = helpers.meth_pcall local command = helpers.command +local bufmeths = helpers.bufmeths describe('api/buf', function() before_each(clear) @@ -121,6 +122,15 @@ describe('api/buf', function() local get_lines, set_lines = curbufmeths.get_lines, curbufmeths.set_lines local line_count = curbufmeths.line_count + it('fails correctly when input is not valid', function() + eq(1, curbufmeths.get_number()) + local err, emsg = pcall(bufmeths.set_lines, 1, 1, 2, false, {'b\na'}) + eq(false, err) + local exp_emsg = 'String cannot contain newlines' + -- Expected {filename}:{lnum}: {exp_emsg} + eq(': ' .. exp_emsg, emsg:sub(-#exp_emsg - 2)) + end) + it('has correct line_count when inserting and deleting', function() eq(1, line_count()) set_lines(-1, -1, true, {'line'}) diff --git a/test/functional/api/keymap_spec.lua b/test/functional/api/keymap_spec.lua new file mode 100644 index 0000000000..833e0d2f3c --- /dev/null +++ b/test/functional/api/keymap_spec.lua @@ -0,0 +1,246 @@ + +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local command = helpers.command +local curbufmeths = helpers.curbufmeths +local eq = helpers.eq +local funcs = helpers.funcs +local meths = helpers.meths +local source = helpers.source + +local function local_copy(t) + local copy = {} + for k,v in pairs(t) do + copy[k] = v + end + return copy +end + +describe('get_keymap', function() + before_each(clear) + + -- Basic mapping and table to be used to describe results + local foo_bar_string = 'nnoremap foo bar' + local foo_bar_map_table = { + lhs='foo', + silent=0, + rhs='bar', + expr=0, + sid=0, + buffer=0, + nowait=0, + mode='n', + noremap=1, + } + + it('returns empty list when no map', function() + eq({}, meths.get_keymap('n')) + end) + + it('returns list of all applicable mappings', function() + command(foo_bar_string) + -- Only one mapping available + -- Should be the same as the dictionary we supplied earlier + -- and the dictionary you would get from maparg + -- since this is a global map, and not script local + eq({foo_bar_map_table}, meths.get_keymap('n')) + eq({funcs.maparg('foo', 'n', false, true)}, + meths.get_keymap('n') + ) + + -- Add another mapping + command('nnoremap foo_longer bar_longer') + local foolong_bar_map_table = local_copy(foo_bar_map_table) + foolong_bar_map_table['lhs'] = 'foo_longer' + foolong_bar_map_table['rhs'] = 'bar_longer' + + eq({foolong_bar_map_table, foo_bar_map_table}, + meths.get_keymap('n') + ) + + -- Remove a mapping + command('unmap foo_longer') + eq({foo_bar_map_table}, + meths.get_keymap('n') + ) + end) + + it('works for other modes', function() + -- Add two mappings, one in insert and one normal + -- We'll only check the insert mode one + command('nnoremap not_going to_check') + + command('inoremap foo bar') + -- The table will be the same except for the mode + local insert_table = local_copy(foo_bar_map_table) + insert_table['mode'] = 'i' + + eq({insert_table}, meths.get_keymap('i')) + end) + + it('considers scope', function() + -- change the map slightly + command('nnoremap foo_longer bar_longer') + local foolong_bar_map_table = local_copy(foo_bar_map_table) + foolong_bar_map_table['lhs'] = 'foo_longer' + foolong_bar_map_table['rhs'] = 'bar_longer' + + local buffer_table = local_copy(foo_bar_map_table) + buffer_table['buffer'] = 1 + + command('nnoremap <buffer> foo bar') + + -- The buffer mapping should not show up + eq({foolong_bar_map_table}, meths.get_keymap('n')) + eq({buffer_table}, curbufmeths.get_keymap('n')) + end) + + it('considers scope for overlapping maps', function() + command('nnoremap foo bar') + + local buffer_table = local_copy(foo_bar_map_table) + buffer_table['buffer'] = 1 + + command('nnoremap <buffer> foo bar') + + eq({foo_bar_map_table}, meths.get_keymap('n')) + eq({buffer_table}, curbufmeths.get_keymap('n')) + end) + + it('can retrieve mapping for different buffers', function() + local original_buffer = curbufmeths.get_number() + -- Place something in each of the buffers to make sure they stick around + -- and set hidden so we can leave them + command('set hidden') + command('new') + command('normal! ihello 2') + command('new') + command('normal! ihello 3') + + local final_buffer = curbufmeths.get_number() + + command('nnoremap <buffer> foo bar') + -- Final buffer will have buffer mappings + local buffer_table = local_copy(foo_bar_map_table) + buffer_table['buffer'] = final_buffer + eq({buffer_table}, meths.buf_get_keymap(final_buffer, 'n')) + eq({buffer_table}, meths.buf_get_keymap(0, 'n')) + + command('buffer ' .. original_buffer) + eq(original_buffer, curbufmeths.get_number()) + -- Original buffer won't have any mappings + eq({}, meths.get_keymap('n')) + eq({}, curbufmeths.get_keymap('n')) + eq({buffer_table}, meths.buf_get_keymap(final_buffer, 'n')) + end) + + -- Test toggle switches for basic options + -- @param option The key represented in the `maparg()` result dict + local function global_and_buffer_test(map, + option, + option_token, + global_on_result, + buffer_on_result, + global_off_result, + buffer_off_result, + new_windows) + + local function make_new_windows(number_of_windows) + if new_windows == nil then + return nil + end + + for _=1,number_of_windows do + command('new') + end + end + + local mode = string.sub(map, 1,1) + -- Don't run this for the <buffer> mapping, since it doesn't make sense + if option_token ~= '<buffer>' then + it(string.format( 'returns %d for the key "%s" when %s is used globally with %s (%s)', + global_on_result, option, option_token, map, mode), function() + make_new_windows(new_windows) + command(map .. ' ' .. option_token .. ' foo bar') + local result = meths.get_keymap(mode)[1][option] + eq(global_on_result, result) + end) + end + + it(string.format('returns %d for the key "%s" when %s is used for buffers with %s (%s)', + buffer_on_result, option, option_token, map, mode), function() + make_new_windows(new_windows) + command(map .. ' <buffer> ' .. option_token .. ' foo bar') + local result = curbufmeths.get_keymap(mode)[1][option] + eq(buffer_on_result, result) + end) + + -- Don't run these for the <buffer> mapping, since it doesn't make sense + if option_token ~= '<buffer>' then + it(string.format('returns %d for the key "%s" when %s is not used globally with %s (%s)', + global_off_result, option, option_token, map, mode), function() + make_new_windows(new_windows) + command(map .. ' baz bat') + local result = meths.get_keymap(mode)[1][option] + eq(global_off_result, result) + end) + + it(string.format('returns %d for the key "%s" when %s is not used for buffers with %s (%s)', + buffer_off_result, option, option_token, map, mode), function() + make_new_windows(new_windows) + command(map .. ' <buffer> foo bar') + + local result = curbufmeths.get_keymap(mode)[1][option] + eq(buffer_off_result, result) + end) + end + end + + -- Standard modes and returns the same values in the dictionary as maparg() + local mode_list = {'nnoremap', 'nmap', 'imap', 'inoremap', 'cnoremap'} + for mode in pairs(mode_list) do + global_and_buffer_test(mode_list[mode], 'silent', '<silent>', 1, 1, 0, 0) + global_and_buffer_test(mode_list[mode], 'nowait', '<nowait>', 1, 1, 0, 0) + global_and_buffer_test(mode_list[mode], 'expr', '<expr>', 1, 1, 0, 0) + end + + -- noremap will now be 2 if script was used, which is not the same as maparg() + global_and_buffer_test('nmap', 'noremap', '<script>', 2, 2, 0, 0) + global_and_buffer_test('nnoremap', 'noremap', '<script>', 2, 2, 1, 1) + + -- buffer will return the buffer ID, which is not the same as maparg() + -- Three of these tests won't run + global_and_buffer_test('nnoremap', 'buffer', '<buffer>', nil, 3, nil, nil, 2) + + it('returns script numbers for global maps', function() + source([[ + function! s:maparg_test_function() abort + return 'testing' + endfunction + + nnoremap fizz :call <SID>maparg_test_function()<CR> + ]]) + local sid_result = meths.get_keymap('n')[1]['sid'] + eq(1, sid_result) + eq('testing', meths.call_function('<SNR>' .. sid_result .. '_maparg_test_function', {})) + end) + + it('returns script numbers for buffer maps', function() + source([[ + function! s:maparg_test_function() abort + return 'testing' + endfunction + + nnoremap <buffer> fizz :call <SID>maparg_test_function()<CR> + ]]) + local sid_result = curbufmeths.get_keymap('n')[1]['sid'] + eq(1, sid_result) + eq('testing', meths.call_function('<SNR>' .. sid_result .. '_maparg_test_function', {})) + end) + + it('works with <F12> and others', function() + command('nnoremap <F12> :let g:maparg_test_var = 1<CR>') + eq('<F12>', meths.get_keymap('n')[1]['lhs']) + eq(':let g:maparg_test_var = 1<CR>', meths.get_keymap('n')[1]['rhs']) + end) +end) diff --git a/test/functional/api/server_requests_spec.lua b/test/functional/api/server_requests_spec.lua index 658077b112..cf15062325 100644 --- a/test/functional/api/server_requests_spec.lua +++ b/test/functional/api/server_requests_spec.lua @@ -9,6 +9,8 @@ local nvim_prog, command, funcs = helpers.nvim_prog, helpers.command, helpers.fu local source, next_message = helpers.source, helpers.next_message local ok = helpers.ok local meths = helpers.meths +local spawn, nvim_argv = helpers.spawn, helpers.nvim_argv +local set_session = helpers.set_session describe('server -> client', function() local cid @@ -225,4 +227,75 @@ describe('server -> client', function() end) end) + describe('when connecting to another nvim instance', function() + local function connect_test(server, mode, address) + local serverpid = funcs.getpid() + local client = spawn(nvim_argv) + set_session(client, true) + local clientpid = funcs.getpid() + neq(serverpid, clientpid) + local id = funcs.sockconnect(mode, address, {rpc=true}) + ok(id > 0) + + funcs.rpcrequest(id, 'nvim_set_current_line', 'hello') + local client_id = funcs.rpcrequest(id, 'nvim_get_api_info')[1] + + set_session(server, true) + eq(serverpid, funcs.getpid()) + eq('hello', meths.get_current_line()) + + -- method calls work both ways + funcs.rpcrequest(client_id, 'nvim_set_current_line', 'howdy!') + eq(id, funcs.rpcrequest(client_id, 'nvim_get_api_info')[1]) + + set_session(client, true) + eq(clientpid, funcs.getpid()) + eq('howdy!', meths.get_current_line()) + + server:close() + client:close() + end + + it('over a named pipe', function() + local server = spawn(nvim_argv) + set_session(server) + local address = funcs.serverlist()[1] + local first = string.sub(address,1,1) + ok(first == '/' or first == '\\') + connect_test(server, 'pipe', address) + end) + + it('to an ip adress', function() + local server = spawn(nvim_argv) + set_session(server) + local address = funcs.serverstart("127.0.0.1:") + eq('127.0.0.1:', string.sub(address,1,10)) + connect_test(server, 'tcp', address) + end) + + it('to a hostname', function() + local server = spawn(nvim_argv) + set_session(server) + local address = funcs.serverstart("localhost:") + eq('localhost:', string.sub(address,1,10)) + connect_test(server, 'tcp', address) + end) + end) + + describe('when connecting to its own pipe adress', function() + it('it does not deadlock', function() + local address = funcs.serverlist()[1] + local first = string.sub(address,1,1) + ok(first == '/' or first == '\\') + local serverpid = funcs.getpid() + + local id = funcs.sockconnect('pipe', address, {rpc=true}) + + funcs.rpcrequest(id, 'nvim_set_current_line', 'hello') + eq('hello', meths.get_current_line()) + eq(serverpid, funcs.rpcrequest(id, "nvim_eval", "getpid()")) + + eq(id, funcs.rpcrequest(id, 'nvim_get_api_info')[1]) + end) + end) end) diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 5b173f3196..c531d4af46 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -81,6 +81,36 @@ describe('api', function() end) end) + describe('nvim_execute_lua', function() + it('works', function() + meths.execute_lua('vim.api.nvim_set_var("test", 3)', {}) + eq(3, meths.get_var('test')) + + eq(17, meths.execute_lua('a, b = ...\nreturn a + b', {10,7})) + + eq(NIL, meths.execute_lua('function xx(a,b)\nreturn a..b\nend',{})) + eq("xy", meths.execute_lua('return xx(...)', {'x','y'})) + end) + + it('reports errors', function() + eq({false, 'Error loading lua: [string "<nvim>"]:1: '.. + "'=' expected near '+'"}, + meth_pcall(meths.execute_lua, 'a+*b', {})) + + eq({false, 'Error loading lua: [string "<nvim>"]:1: '.. + "unexpected symbol near '1'"}, + meth_pcall(meths.execute_lua, '1+2', {})) + + eq({false, 'Error loading lua: [string "<nvim>"]:1: '.. + "unexpected symbol"}, + meth_pcall(meths.execute_lua, 'aa=bb\0', {})) + + eq({false, 'Error executing lua: [string "<nvim>"]:1: '.. + "attempt to call global 'bork' (a nil value)"}, + meth_pcall(meths.execute_lua, 'bork()', {})) + end) + end) + describe('nvim_input', function() it("VimL error: does NOT fail, updates v:errmsg", function() local status, _ = pcall(nvim, "input", ":call bogus_fn()<CR>") @@ -221,6 +251,102 @@ describe('api', function() end) 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", [[<C-\><C-N>]]) + 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", "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', blocking=false}, nvim("get_mode")) + end + nvim("input", "g") + for _ = 1,4 do + eq({mode='n', blocking=true}, nvim("get_mode")) + end + nvim("input", "g") + for _ = 1,7 do + eq({mode='n', blocking=false}, nvim("get_mode")) + end + end) + + it("during normal-mode CTRL-W, returns blocking=true", function() + nvim("input", "<C-W>") + eq({mode='n', blocking=true}, nvim("get_mode")) + + nvim("input", "s") -- complete the operator + eq(2, nvim("eval", "winnr('$')")) -- verify the completed operator + eq({mode='n', blocking=false}, nvim("get_mode")) + end) + + it("during press-enter prompt returns blocking=true", function() + eq({mode='n', blocking=false}, nvim("get_mode")) + command("echom 'msg1'") + command("echom 'msg2'") + command("echom 'msg3'") + command("echom 'msg4'") + command("echom 'msg5'") + eq({mode='n', blocking=false}, nvim("get_mode")) + nvim("input", ":messages<CR>") + eq({mode='r', blocking=true}, nvim("get_mode")) + end) + + it("during getchar() returns blocking=false", function() + nvim("input", ":let g:test_input = nr2char(getchar())<CR>") + -- Events are enabled during getchar(), RPC calls are *not* blocked. #5384 + eq({mode='n', blocking=false}, nvim("get_mode")) + eq(0, nvim("eval", "exists('g:test_input')")) + nvim("input", "J") + eq("J", nvim("eval", "g:test_input")) + eq({mode='n', blocking=false}, nvim("get_mode")) + end) + + -- TODO: bug #6247#issuecomment-286403810 + it("batched with input", function() + eq({mode='n', blocking=false}, nvim("get_mode")) + command("echom 'msg1'") + command("echom 'msg2'") + command("echom 'msg3'") + command("echom 'msg4'") + command("echom 'msg5'") + + local req = { + {'nvim_get_mode', {}}, + {'nvim_input', {':messages<CR>'}}, + {'nvim_get_mode', {}}, + {'nvim_eval', {'1'}}, + } + eq({ { {mode='n', blocking=false}, + 13, + {mode='n', blocking=false}, -- TODO: should be blocked=true + 1 }, + NIL}, meths.call_atomic(req)) + eq({mode='r', blocking=true}, nvim("get_mode")) + end) + -- TODO: bug #6166 + it("during insert-mode map-pending, returns blocking=true #6166", function() + command("inoremap xx foo") + nvim("input", "ix") + eq({mode='i', blocking=true}, nvim("get_mode")) + end) + -- TODO: bug #6166 + it("during normal-mode gU, returns blocking=false #6166", function() + nvim("input", "gu") + eq({mode='no', blocking=false}, nvim("get_mode")) + end) + end) + describe('nvim_replace_termcodes', function() it('escapes K_SPECIAL as K_SPECIAL KS_SPECIAL KE_FILLER', function() eq('\128\254X', helpers.nvim('replace_termcodes', '\128', true, true, true)) @@ -241,6 +367,22 @@ describe('api', function() eq('\128\253\44', helpers.nvim('replace_termcodes', '<LeftMouse>', true, true, true)) end) + + it('converts keycodes', function() + eq('\nx\27x\rx<x', helpers.nvim('replace_termcodes', + '<NL>x<Esc>x<CR>x<lt>x', true, true, true)) + end) + + it('does not crash when transforming an empty string', function() + -- Actually does not test anything, because current code will use NULL for + -- an empty string. + -- + -- Problem here is that if String argument has .data in allocated memory + -- then `return str` in vim_replace_termcodes body will make Neovim free + -- `str.data` twice: once when freeing arguments, then when freeing return + -- value. + eq('', meths.replace_termcodes('', true, true, true)) + end) end) describe('nvim_feedkeys', function() @@ -446,6 +588,36 @@ describe('api', function() end) end) + describe('list_runtime_paths', function() + it('returns nothing with empty &runtimepath', function() + meths.set_option('runtimepath', '') + eq({}, meths.list_runtime_paths()) + end) + it('returns single runtimepath', function() + meths.set_option('runtimepath', 'a') + eq({'a'}, meths.list_runtime_paths()) + end) + it('returns two runtimepaths', function() + meths.set_option('runtimepath', 'a,b') + eq({'a', 'b'}, meths.list_runtime_paths()) + end) + it('returns empty strings when appropriate', function() + meths.set_option('runtimepath', 'a,,b') + eq({'a', '', 'b'}, meths.list_runtime_paths()) + meths.set_option('runtimepath', ',a,b') + eq({'', 'a', 'b'}, meths.list_runtime_paths()) + meths.set_option('runtimepath', 'a,b,') + eq({'a', 'b', ''}, meths.list_runtime_paths()) + end) + it('truncates too long paths', function() + local long_path = ('/a'):rep(8192) + meths.set_option('runtimepath', long_path) + local paths_list = meths.list_runtime_paths() + neq({long_path}, paths_list) + eq({long_path:sub(1, #(paths_list[1]))}, paths_list) + end) + end) + it('can throw exceptions', function() local status, err = pcall(nvim, 'get_option', 'invalid-option') eq(false, status) @@ -459,7 +631,7 @@ describe('api', function() eq(very_long_name, err:match('Ax+Z?')) end) - it("doesn't leak memory on incorrect argument types", function() + it("does not leak memory on incorrect argument types", function() local status, err = pcall(nvim, 'set_current_dir',{'not', 'a', 'dir'}) eq(false, status) ok(err:match(': Wrong type for argument 1, expecting String') ~= nil) diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua index 6882f50a3e..8a65d3f71e 100644 --- a/test/functional/api/window_spec.lua +++ b/test/functional/api/window_spec.lua @@ -9,6 +9,7 @@ local funcs = helpers.funcs local request = helpers.request local NIL = helpers.NIL local meth_pcall = helpers.meth_pcall +local meths = helpers.meths local command = helpers.command -- check if str is visible at the beginning of some line @@ -55,6 +56,12 @@ describe('api/win', function() eq('typing\n some dumb text', curbuf_contents()) end) + it('does not leak memory when using invalid window ID with invalid pos', + function() + eq({false, 'Invalid window id'}, + meth_pcall(meths.win_set_cursor, 1, {"b\na"})) + end) + it('updates the screen, and also when the window is unfocused', function() insert("prologue") feed('100o<esc>') diff --git a/test/functional/eval/api_functions_spec.lua b/test/functional/eval/api_functions_spec.lua index 7f6f53d226..fea4a87a26 100644 --- a/test/functional/eval/api_functions_spec.lua +++ b/test/functional/eval/api_functions_spec.lua @@ -106,7 +106,7 @@ describe('api functions', function() it('have metadata accessible with api_info()', function() local api_keys = eval("sort(keys(api_info()))") - eq({'error_types', 'functions', 'types', 'version'}, api_keys) + eq({'error_types', 'functions', 'types', 'ui_events', 'version'}, api_keys) end) it('are highlighted by vim.vim syntax file', function() diff --git a/test/functional/eval/function_spec.lua b/test/functional/eval/function_spec.lua new file mode 100644 index 0000000000..776e760aaf --- /dev/null +++ b/test/functional/eval/function_spec.lua @@ -0,0 +1,29 @@ +local helpers = require('test.functional.helpers')(after_each) + +local clear = helpers.clear +local eq = helpers.eq +local exc_exec = helpers.exc_exec + +describe('Up to MAX_FUNC_ARGS arguments are handled by', function() + local max_func_args = 20 -- from eval.h + local range = helpers.funcs.range + + before_each(clear) + + it('printf()', function() + local printf = helpers.funcs.printf + local rep = helpers.funcs['repeat'] + local expected = '2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,' + eq(expected, printf(rep('%d,', max_func_args-1), unpack(range(2, max_func_args)))) + local ret = exc_exec('call printf("", 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)') + eq('Vim(call):E740: Too many arguments for function printf', ret) + end) + + it('rpcnotify()', function() + local rpcnotify = helpers.funcs.rpcnotify + local ret = rpcnotify(0, 'foo', unpack(range(3, max_func_args))) + eq(1, ret) + ret = exc_exec('call rpcnotify(0, "foo", 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)') + eq('Vim(call):E740: Too many arguments for function rpcnotify', ret) + end) +end) diff --git a/test/functional/eval/input_spec.lua b/test/functional/eval/input_spec.lua index 393fc10175..74ad32bc6c 100644 --- a/test/functional/eval/input_spec.lua +++ b/test/functional/eval/input_spec.lua @@ -1,9 +1,13 @@ local helpers = require('test.functional.helpers')(after_each) local Screen = require('test.functional.ui.screen') +local eq = helpers.eq local feed = helpers.feed +local meths = helpers.meths local clear = helpers.clear +local source = helpers.source local command = helpers.command +local exc_exec = helpers.exc_exec local screen @@ -11,28 +15,352 @@ before_each(function() clear() screen = Screen.new(25, 5) screen:attach() + source([[ + hi Test ctermfg=Red guifg=Red term=bold + function CustomCompl(...) + return 'TEST' + endfunction + function CustomListCompl(...) + return ['FOO'] + endfunction + ]]) + screen:set_default_attr_ids({ + EOB={bold = true, foreground = Screen.colors.Blue1}, + T={foreground=Screen.colors.Red}, + }) end) describe('input()', function() - it('works correctly with multiline prompts', function() + it('works with multiline prompts', function() feed([[:call input("Test\nFoo")<CR>]]) screen:expect([[ - {1:~ }| - {1:~ }| - {1:~ }| + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| Test | Foo^ | - ]], {{bold=true, foreground=Screen.colors.Blue}}) + ]]) end) - it('works correctly with multiline prompts and :echohl', function() - command('hi Test ctermfg=Red guifg=Red term=bold') + it('works with multiline prompts and :echohl', function() feed([[:echohl Test | call input("Test\nFoo")<CR>]]) screen:expect([[ - {1:~ }| - {1:~ }| - {1:~ }| - {2:Test} | - {2:Foo}^ | - ]], {{bold=true, foreground=Screen.colors.Blue}, {foreground=Screen.colors.Red}}) + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Test} | + {T:Foo}^ | + ]]) + command('redraw!') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo}^ | + ]]) + end) + it('allows unequal numeric arguments when using multiple args', function() + command('echohl Test') + feed([[:call input(1, 2)<CR>]]) + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:1}2^ | + ]]) + feed('<BS>') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:1}^ | + ]]) + end) + it('allows unequal numeric values when using {opts} dictionary', function() + command('echohl Test') + meths.set_var('opts', {prompt=1, default=2, cancelreturn=3}) + feed([[:echo input(opts)<CR>]]) + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:1}2^ | + ]]) + feed('<BS>') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:1}^ | + ]]) + feed('<Esc>') + screen:expect([[ + ^ | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:3} | + ]]) + end) + it('works with redraw', function() + command('echohl Test') + meths.set_var('opts', {prompt='Foo>', default='Bar'}) + feed([[:echo inputdialog(opts)<CR>]]) + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo>}Bar^ | + ]]) + command('redraw!') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo>}Bar^ | + ]]) + feed('<BS>') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo>}Ba^ | + ]]) + command('redraw!') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo>}Ba^ | + ]]) + end) + it('allows omitting everything with dictionary argument', function() + command('echohl Test') + feed([[:call input({})<CR>]]) + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + ^ | + ]]) + end) + it('supports completion', function() + feed(':let var = input("", "", "custom,CustomCompl")<CR>') + feed('<Tab><CR>') + eq('TEST', meths.get_var('var')) + + feed(':let var = input({"completion": "customlist,CustomListCompl"})<CR>') + feed('<Tab><CR>') + eq('FOO', meths.get_var('var')) + end) + it('supports cancelreturn', function() + feed(':let var = input({"cancelreturn": "BAR"})<CR>') + feed('<Esc>') + eq('BAR', meths.get_var('var')) + end) + it('supports default string', function() + feed(':let var = input("", "DEF1")<CR>') + feed('<CR>') + eq('DEF1', meths.get_var('var')) + + feed(':let var = input({"default": "DEF2"})<CR>') + feed('<CR>') + eq('DEF2', meths.get_var('var')) + end) + it('errors out on invalid inputs', function() + eq('Vim(call):E730: using List as a String', + exc_exec('call input([])')) + eq('Vim(call):E730: using List as a String', + exc_exec('call input("", [])')) + eq('Vim(call):E730: using List as a String', + exc_exec('call input("", "", [])')) + eq('Vim(call):E730: using List as a String', + exc_exec('call input({"prompt": []})')) + eq('Vim(call):E730: using List as a String', + exc_exec('call input({"cancelreturn": []})')) + eq('Vim(call):E730: using List as a String', + exc_exec('call input({"default": []})')) + eq('Vim(call):E730: using List as a String', + exc_exec('call input({"completion": []})')) + eq('Vim(call):E5050: {opts} must be the only argument', + exc_exec('call input({}, "default")')) + eq('Vim(call):E118: Too many arguments for function: input', + exc_exec('call input("prompt> ", "default", "file", "extra")')) + end) +end) +describe('inputdialog()', function() + it('works with multiline prompts', function() + feed([[:call inputdialog("Test\nFoo")<CR>]]) + screen:expect([[ + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + Test | + Foo^ | + ]]) + end) + it('works with multiline prompts and :echohl', function() + feed([[:echohl Test | call inputdialog("Test\nFoo")<CR>]]) + screen:expect([[ + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Test} | + {T:Foo}^ | + ]]) + command('redraw!') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo}^ | + ]]) + end) + it('allows unequal numeric arguments when using multiple args', function() + command('echohl Test') + feed([[:call inputdialog(1, 2)<CR>]]) + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:1}2^ | + ]]) + feed('<BS>') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:1}^ | + ]]) + end) + it('allows unequal numeric values when using {opts} dictionary', function() + command('echohl Test') + meths.set_var('opts', {prompt=1, default=2, cancelreturn=3}) + feed([[:echo input(opts)<CR>]]) + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:1}2^ | + ]]) + feed('<BS>') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:1}^ | + ]]) + feed('<Esc>') + screen:expect([[ + ^ | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:3} | + ]]) + end) + it('works with redraw', function() + command('echohl Test') + meths.set_var('opts', {prompt='Foo>', default='Bar'}) + feed([[:echo input(opts)<CR>]]) + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo>}Bar^ | + ]]) + command('redraw!') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo>}Bar^ | + ]]) + feed('<BS>') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo>}Ba^ | + ]]) + command('redraw!') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + {T:Foo>}Ba^ | + ]]) + end) + it('allows omitting everything with dictionary argument', function() + command('echohl Test') + feed(':echo inputdialog({})<CR>') + screen:expect([[ + | + {EOB:~ }| + {EOB:~ }| + {EOB:~ }| + ^ | + ]]) + end) + it('supports completion', function() + feed(':let var = inputdialog({"completion": "customlist,CustomListCompl"})<CR>') + feed('<Tab><CR>') + eq('FOO', meths.get_var('var')) + end) + it('supports cancelreturn', function() + feed(':let var = inputdialog("", "", "CR1")<CR>') + feed('<Esc>') + eq('CR1', meths.get_var('var')) + + feed(':let var = inputdialog({"cancelreturn": "BAR"})<CR>') + feed('<Esc>') + eq('BAR', meths.get_var('var')) + end) + it('supports default string', function() + feed(':let var = inputdialog("", "DEF1")<CR>') + feed('<CR>') + eq('DEF1', meths.get_var('var')) + + feed(':let var = inputdialog({"default": "DEF2"})<CR>') + feed('<CR>') + eq('DEF2', meths.get_var('var')) + end) + it('errors out on invalid inputs', function() + eq('Vim(call):E730: using List as a String', + exc_exec('call inputdialog([])')) + eq('Vim(call):E730: using List as a String', + exc_exec('call inputdialog("", [])')) + eq('Vim(call):E730: using List as a String', + exc_exec('call inputdialog("", "", [])')) + eq('Vim(call):E730: using List as a String', + exc_exec('call inputdialog({"prompt": []})')) + eq('Vim(call):E730: using List as a String', + exc_exec('call inputdialog({"cancelreturn": []})')) + eq('Vim(call):E730: using List as a String', + exc_exec('call inputdialog({"default": []})')) + eq('Vim(call):E730: using List as a String', + exc_exec('call inputdialog({"completion": []})')) + eq('Vim(call):E5050: {opts} must be the only argument', + exc_exec('call inputdialog({}, "default")')) + eq('Vim(call):E118: Too many arguments for function: inputdialog', + exc_exec('call inputdialog("prompt> ", "default", "file", "extra")')) end) end) diff --git a/test/functional/eval/map_functions_spec.lua b/test/functional/eval/map_functions_spec.lua new file mode 100644 index 0000000000..a260522aa3 --- /dev/null +++ b/test/functional/eval/map_functions_spec.lua @@ -0,0 +1,120 @@ + +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local eq = helpers.eq +local eval = helpers.eval +local funcs = helpers.funcs +local nvim = helpers.nvim +local source = helpers.source + +describe('maparg()', function() + before_each(clear) + + local foo_bar_map_table = { + lhs='foo', + silent=0, + rhs='bar', + expr=0, + sid=0, + buffer=0, + nowait=0, + mode='n', + noremap=1, + } + + it('returns a dictionary', function() + nvim('command', 'nnoremap foo bar') + eq('bar', funcs.maparg('foo')) + eq(foo_bar_map_table, funcs.maparg('foo', 'n', false, true)) + end) + + it('returns 1 for silent when <silent> is used', function() + nvim('command', 'nnoremap <silent> foo bar') + eq(1, funcs.maparg('foo', 'n', false, true)['silent']) + + nvim('command', 'nnoremap baz bat') + eq(0, funcs.maparg('baz', 'n', false, true)['silent']) + end) + + it('returns an empty string when no map is present', function() + eq('', funcs.maparg('not a mapping')) + end) + + it('returns an empty dictionary when no map is present and dict is requested', function() + eq({}, funcs.maparg('not a mapping', 'n', false, true)) + end) + + it('returns the same value for noremap and <script>', function() + nvim('command', 'inoremap <script> hello world') + nvim('command', 'inoremap this that') + eq( + funcs.maparg('hello', 'i', false, true)['noremap'], + funcs.maparg('this', 'i', false, true)['noremap'] + ) + end) + + it('returns a boolean for buffer', function() + -- Open enough windows to know we aren't on buffer number 1 + nvim('command', 'new') + nvim('command', 'new') + nvim('command', 'new') + nvim('command', 'cnoremap <buffer> this that') + eq(1, funcs.maparg('this', 'c', false, true)['buffer']) + + -- Global will return 0 always + nvim('command', 'nnoremap other another') + eq(0, funcs.maparg('other', 'n', false, true)['buffer']) + end) + + it('returns script numbers', function() + source([[ + function! s:maparg_test_function() abort + return 'testing' + endfunction + + nnoremap fizz :call <SID>maparg_test_function()<CR> + ]]) + eq(1, funcs.maparg('fizz', 'n', false, true)['sid']) + eq('testing', nvim('call_function', '<SNR>1_maparg_test_function', {})) + end) + + it('works with <F12> and others', function() + source([[ + let g:maparg_test_var = 0 + + nnoremap <F12> :let g:maparg_test_var = 1<CR> + ]]) + eq(0, eval('g:maparg_test_var')) + source([[ + call feedkeys("\<F12>") + ]]) + eq(1, eval('g:maparg_test_var')) + + eq(':let g:maparg_test_var = 1<CR>', funcs.maparg('<F12>', 'n', false, true)['rhs']) + end) + + it('works with <expr>', function() + source([[ + let counter = 0 + inoremap <expr> <C-L> ListItem() + inoremap <expr> <C-R> ListReset() + + func ListItem() + let g:counter += 1 + return g:counter . '. ' + endfunc + + func ListReset() + let g:counter = 0 + return '' + endfunc + + call feedkeys("i\<C-L>") + ]]) + eq(1, eval('g:counter')) + + local map_dict = funcs.maparg('<C-L>', 'i', false, true) + eq(1, map_dict['expr']) + eq('i', map_dict['mode']) + end) +end) diff --git a/test/functional/eval/msgpack_functions_spec.lua b/test/functional/eval/msgpack_functions_spec.lua index c5520deb73..b241635dfe 100644 --- a/test/functional/eval/msgpack_functions_spec.lua +++ b/test/functional/eval/msgpack_functions_spec.lua @@ -337,7 +337,8 @@ describe('msgpack*() functions', function() eq(1, eval('type(parsed[0]) == type(0) ' .. '|| parsed[0]._TYPE is v:msgpack_types.integer')) if eval('type(parsed[0]) == type(0)') == 1 then - eq(1, eval('0xFFFFFFFFFFFFFFFF == parsed[0]')) + command('call assert_equal(0xFFFFFFFFFFFFFFFF, parsed[0])') + eq({}, eval('v:errors')) else eq({_TYPE={}, _VAL={1, 3, 0x7FFFFFFF, 0x7FFFFFFF}}, eval('parsed[0]')) end @@ -351,7 +352,8 @@ describe('msgpack*() functions', function() eq(1, eval('type(parsed[0]) == type(0) ' .. '|| parsed[0]._TYPE is v:msgpack_types.integer')) if eval('type(parsed[0]) == type(0)') == 1 then - eq(1, eval('-0x8000000000000000 == parsed[0]')) + command('call assert_equal(-0x7fffffffffffffff - 1, parsed[0])') + eq({}, eval('v:errors')) else eq({_TYPE={}, _VAL={-1, 2, 0, 0}}, eval('parsed[0]')) end @@ -461,7 +463,7 @@ describe('msgpackparse() function', function() eval(cmd) eval(cmd) -- do it again (try to force segfault) local api_info = eval(cmd) -- do it again - eq({'error_types', 'functions', 'types', 'version'}, api_info) + eq({'error_types', 'functions', 'types', 'ui_events', 'version'}, api_info) end) it('fails when called with no arguments', function() diff --git a/test/functional/eval/server_spec.lua b/test/functional/eval/server_spec.lua index 420aea04aa..115114c3c3 100644 --- a/test/functional/eval/server_spec.lua +++ b/test/functional/eval/server_spec.lua @@ -1,20 +1,27 @@ local helpers = require('test.functional.helpers')(after_each) -local nvim, eq, neq, eval = helpers.nvim, helpers.eq, helpers.neq, helpers.eval +local eq, neq, eval = helpers.eq, helpers.neq, helpers.eval +local command = helpers.command local clear, funcs, meths = helpers.clear, helpers.funcs, helpers.meths local os_name = helpers.os_name +local function clear_serverlist() + for _, server in pairs(funcs.serverlist()) do + funcs.serverstop(server) + end +end + describe('serverstart(), serverstop()', function() before_each(clear) it('sets $NVIM_LISTEN_ADDRESS on first invocation', function() -- Unset $NVIM_LISTEN_ADDRESS - nvim('command', 'let $NVIM_LISTEN_ADDRESS = ""') + command('let $NVIM_LISTEN_ADDRESS = ""') local s = eval('serverstart()') assert(s ~= nil and s:len() > 0, "serverstart() returned empty") eq(s, eval('$NVIM_LISTEN_ADDRESS')) - nvim('command', "call serverstop('"..s.."')") + command("call serverstop('"..s.."')") eq('', eval('$NVIM_LISTEN_ADDRESS')) end) @@ -47,10 +54,38 @@ describe('serverstart(), serverstop()', function() end) it('serverstop() ignores invalid input', function() - nvim('command', "call serverstop('')") - nvim('command', "call serverstop('bogus-socket-name')") + command("call serverstop('')") + command("call serverstop('bogus-socket-name')") end) + it('parses endpoints correctly', function() + clear_serverlist() + eq({}, funcs.serverlist()) + + local s = funcs.serverstart('127.0.0.1:0') -- assign random port + assert(string.match(s, '127.0.0.1:%d+')) + eq(s, funcs.serverlist()[1]) + clear_serverlist() + + s = funcs.serverstart('127.0.0.1:') -- assign random port + assert(string.match(s, '127.0.0.1:%d+')) + eq(s, funcs.serverlist()[1]) + clear_serverlist() + + funcs.serverstart('127.0.0.1:12345') + funcs.serverstart('127.0.0.1:12345') -- exists already; ignore + funcs.serverstart('::1:12345') + funcs.serverstart('::1:12345') -- exists already; ignore + local expected = { + '127.0.0.1:12345', + '::1:12345', + } + eq(expected, funcs.serverlist()) + clear_serverlist() + + funcs.serverstart('127.0.0.1:65536') -- invalid port + eq({}, funcs.serverlist()) + end) end) describe('serverlist()', function() @@ -75,7 +110,7 @@ describe('serverlist()', function() -- The new servers should be at the end of the list. for i = 1, #servs do eq(servs[i], new_servs[i + n]) - nvim('command', "call serverstop('"..servs[i].."')") + command("call serverstop('"..servs[i].."')") end -- After serverstop() the servers should NOT be in the list. eq(n, eval('len(serverlist())')) diff --git a/test/functional/ex_cmds/encoding_spec.lua b/test/functional/ex_cmds/encoding_spec.lua index 0769259be4..7f2bd78a47 100644 --- a/test/functional/ex_cmds/encoding_spec.lua +++ b/test/functional/ex_cmds/encoding_spec.lua @@ -15,7 +15,7 @@ describe('&encoding', function() feed_command('set encoding=latin1') -- error message expected feed('<cr>') - neq(nil, string.find(eval('v:errmsg'), '^E474:')) + neq(nil, string.find(eval('v:errmsg'), '^E519:')) eq('utf-8', eval('&encoding')) -- check nvim is still in utf-8 mode eq(3, eval('strwidth("Bär")')) @@ -25,7 +25,7 @@ describe('&encoding', function() clear('--cmd', 'set enc=latin1') -- error message expected feed('<cr>') - neq(nil, string.find(eval('v:errmsg'), '^E474:')) + neq(nil, string.find(eval('v:errmsg'), '^E519:')) eq('utf-8', eval('&encoding')) eq(3, eval('strwidth("Bär")')) end) diff --git a/test/functional/ex_cmds/mksession_spec.lua b/test/functional/ex_cmds/mksession_spec.lua new file mode 100644 index 0000000000..5d658f10bb --- /dev/null +++ b/test/functional/ex_cmds/mksession_spec.lua @@ -0,0 +1,49 @@ +local lfs = require('lfs') +local helpers = require('test.functional.helpers')(after_each) + +local clear = helpers.clear +local command = helpers.command +local get_pathsep = helpers.get_pathsep +local eq = helpers.eq +local funcs = helpers.funcs + +local file_prefix = 'Xtest-functional-ex_cmds-mksession_spec' + +describe(':mksession', function() + local session_file = file_prefix .. '.vim' + local tab_dir = file_prefix .. '.d' + + before_each(function() + clear() + lfs.mkdir(tab_dir) + end) + + after_each(function() + os.remove(session_file) + lfs.rmdir(tab_dir) + end) + + it('restores tab-local working directories', function() + local tmpfile_base = file_prefix .. '-tmpfile' + local cwd_dir = funcs.getcwd() + + -- :mksession does not save empty tabs, so create some buffers. + command('edit ' .. tmpfile_base .. '1') + command('tabnew') + command('edit ' .. tmpfile_base .. '2') + command('tcd ' .. tab_dir) + command('tabfirst') + command('mksession ' .. session_file) + + -- Create a new test instance of Nvim. + clear() + + command('source ' .. session_file) + -- First tab should have the original working directory. + command('tabnext 1') + eq(cwd_dir, funcs.getcwd()) + -- Second tab should have the tab-local working directory. + command('tabnext 2') + eq(cwd_dir .. get_pathsep() .. tab_dir, funcs.getcwd()) + end) +end) diff --git a/test/functional/ex_cmds/oldfiles_spec.lua b/test/functional/ex_cmds/oldfiles_spec.lua index 656b3f9bae..4002855c24 100644 --- a/test/functional/ex_cmds/oldfiles_spec.lua +++ b/test/functional/ex_cmds/oldfiles_spec.lua @@ -4,13 +4,15 @@ local helpers = require('test.functional.helpers')(after_each) local buf, eq, feed_command = helpers.curbufmeths, helpers.eq, helpers.feed_command local feed, nvim_prog, wait = helpers.feed, helpers.nvim_prog, helpers.wait local ok, set_session, spawn = helpers.ok, helpers.set_session, helpers.spawn +local eval = helpers.eval -local shada_file = 'test.shada' +local shada_file = 'Xtest.shada' local function _clear() - set_session(spawn({nvim_prog, '--embed', '-u', 'NONE', '--cmd', + set_session(spawn({nvim_prog, '--embed', '-u', 'NONE', -- Need shada for these tests. - 'set noswapfile undodir=. directory=. viewdir=. backupdir=. belloff= noshowcmd noruler'})) + '-i', shada_file, + '--cmd', 'set noswapfile undodir=. directory=. viewdir=. backupdir=. belloff= noshowcmd noruler'})) end describe(':oldfiles', function() @@ -29,8 +31,8 @@ describe(':oldfiles', function() screen:attach() feed_command('edit testfile1') feed_command('edit testfile2') - feed_command('wshada ' .. shada_file) - feed_command('rshada! ' .. shada_file) + feed_command('wshada') + feed_command('rshada!') local oldfiles = helpers.meths.get_vvar('oldfiles') feed_command('oldfiles') screen:expect([[ @@ -41,6 +43,38 @@ describe(':oldfiles', function() Press ENTER or type command to continue^ | ]]) end) + + it('can be filtered with :filter', function() + feed_command('edit file_one.txt') + local file1 = buf.get_name() + feed_command('edit file_two.txt') + local file2 = buf.get_name() + feed_command('edit another.txt') + local another = buf.get_name() + feed_command('wshada') + feed_command('rshada!') + + local function get_oldfiles(cmd) + local t = eval([[split(execute(']]..cmd..[['), "\n")]]) + for i, _ in ipairs(t) do + t[i] = t[i]:gsub('^%d+:%s+', '') + end + table.sort(t) + return t + end + + local oldfiles = get_oldfiles('oldfiles') + eq({another, file1, file2}, oldfiles) + + oldfiles = get_oldfiles('filter file_ oldfiles') + eq({file1, file2}, oldfiles) + + oldfiles = get_oldfiles('filter /another/ oldfiles') + eq({another}, oldfiles) + + oldfiles = get_oldfiles('filter! file_ oldfiles') + eq({another}, oldfiles) + end) end) describe(':browse oldfiles', function() @@ -54,10 +88,9 @@ describe(':browse oldfiles', function() filename = buf.get_name() feed_command('edit testfile2') filename2 = buf.get_name() - feed_command('wshada ' .. shada_file) + feed_command('wshada') wait() _clear() - feed_command('rshada! ' .. shada_file) -- Ensure nvim is out of "Press ENTER..." prompt. feed('<cr>') diff --git a/test/functional/ex_cmds/script_spec.lua b/test/functional/ex_cmds/script_spec.lua new file mode 100644 index 0000000000..4e57d2755d --- /dev/null +++ b/test/functional/ex_cmds/script_spec.lua @@ -0,0 +1,75 @@ +local helpers = require('test.functional.helpers')(after_each) + +local eq = helpers.eq +local neq = helpers.neq +local meths = helpers.meths +local clear = helpers.clear +local dedent = helpers.dedent +local source = helpers.source +local exc_exec = helpers.exc_exec +local missing_provider = helpers.missing_provider + +before_each(clear) + +describe('script_get-based command', function() + local garbage = ')}{+*({}]*[;(+}{&[]}{*])(' + + local function test_garbage_exec(cmd, check_neq) + describe(cmd, function() + it('works correctly when skipping oneline variant', function() + eq(true, pcall(source, (dedent([[ + if 0 + %s %s + endif + ]])):format(cmd, garbage))) + eq('', meths.command_output('messages')) + if check_neq then + neq(0, exc_exec(dedent([[ + %s %s + ]])):format(cmd, garbage)) + end + end) + it('works correctly when skipping HEREdoc variant', function() + eq(true, pcall(source, (dedent([[ + if 0 + %s << EOF + %s + EOF + endif + ]])):format(cmd, garbage))) + eq('', meths.command_output('messages')) + if check_neq then + eq(true, pcall(source, (dedent([[ + let g:exc = 0 + try + %s << EOF + %s + EOF + catch + let g:exc = v:exception + endtry + ]])):format(cmd, garbage))) + neq(0, meths.get_var('exc')) + end + end) + end) + end + + clear() + + -- Built-in scripts + test_garbage_exec('lua', true) + + -- Provider-based scripts + test_garbage_exec('ruby', not missing_provider('ruby')) + test_garbage_exec('python', not missing_provider('python')) + test_garbage_exec('python3', not missing_provider('python3')) + + -- Missing scripts + test_garbage_exec('tcl', false) + test_garbage_exec('mzscheme', false) + test_garbage_exec('perl', false) + + -- Not really a script + test_garbage_exec('xxxinvalidlanguagexxx', true) +end) diff --git a/test/functional/fixtures/api_level_2.mpack b/test/functional/fixtures/api_level_2.mpack Binary files differnew file mode 100644 index 0000000000..0ca2ba8866 --- /dev/null +++ b/test/functional/fixtures/api_level_2.mpack diff --git a/test/functional/fixtures/tty-test.c b/test/functional/fixtures/tty-test.c index 3406b3a202..7ba21d652a 100644 --- a/test/functional/fixtures/tty-test.c +++ b/test/functional/fixtures/tty-test.c @@ -6,6 +6,9 @@ #include <stdlib.h> #include <uv.h> +// -V:STRUCT_CAST:641 +#define STRUCT_CAST(Type, obj) ((Type *)(obj)) + uv_tty_t tty; #ifdef _WIN32 @@ -88,9 +91,9 @@ static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf) uv_tty_init(&write_loop, &out, 1, 0); uv_write_t req; uv_buf_t b = {.base = buf->base, .len = (size_t)cnt}; - uv_write(&req, (uv_stream_t *)&out, &b, 1, NULL); + uv_write(&req, STRUCT_CAST(uv_stream_t, &out), &b, 1, NULL); uv_run(&write_loop, UV_RUN_DEFAULT); - uv_close((uv_handle_t *)&out, NULL); + uv_close(STRUCT_CAST(uv_handle_t, &out), NULL); uv_run(&write_loop, UV_RUN_DEFAULT); if (uv_loop_close(&write_loop)) { abort(); @@ -149,7 +152,7 @@ int main(int argc, char **argv) uv_tty_init(uv_default_loop(), &tty, fileno(stderr), 1); uv_tty_set_mode(&tty, UV_TTY_MODE_RAW); tty.data = &interrupted; - uv_read_start((uv_stream_t *)&tty, alloc_cb, read_cb); + uv_read_start(STRUCT_CAST(uv_stream_t, &tty), alloc_cb, read_cb); #ifndef WIN32 struct sigaction sa; sigemptyset(&sa.sa_mask); diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index 0f30910450..4a170d993b 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -76,8 +76,8 @@ end local session, loop_running, last_error -local function set_session(s) - if session then +local function set_session(s, keep) + if session and not keep then session:close() end session = s @@ -174,7 +174,7 @@ local os_name = (function() end)() local function iswin() - return os_name() == 'windows' + return package.config:sub(1,1) == '\\' end -- Executes a VimL function. @@ -385,9 +385,9 @@ local function curbuf(method, ...) end local function wait() - -- Execute 'vim_eval' (a deferred function) to block + -- Execute 'nvim_eval' (a deferred function) to block -- until all pending input is processed. - session:request('vim_eval', '1') + session:request('nvim_eval', '1') end -- sleeps the test runner (_not_ the nvim instance) @@ -492,17 +492,6 @@ local exc_exec = function(cmd) return ret end -local function redir_exec(cmd) - nvim_command(([[ - redir => g:__output - silent! execute "%s" - redir END - ]]):format(cmd:gsub('\n', '\\n'):gsub('[\\"]', '\\%0'))) - local ret = nvim_eval('get(g:, "__output", 0)') - nvim_command('unlet! g:__output') - return ret -end - local function create_callindex(func) local table = {} setmetatable(table, { @@ -562,11 +551,55 @@ local curbufmeths = create_callindex(curbuf) local curwinmeths = create_callindex(curwin) local curtabmeths = create_callindex(curtab) +local function redir_exec(cmd) + meths.set_var('__redir_exec_cmd', cmd) + nvim_command([[ + redir => g:__redir_exec_output + silent! execute g:__redir_exec_cmd + redir END + ]]) + local ret = meths.get_var('__redir_exec_output') + meths.del_var('__redir_exec_output') + meths.del_var('__redir_exec_cmd') + return ret +end + local function get_pathsep() return funcs.fnamemodify('.', ':p'):sub(-1) end -local M = { +local function missing_provider(provider) + if provider == 'ruby' then + local prog = funcs['provider#' .. provider .. '#Detect']() + return prog == '' and (provider .. ' not detected') or false + elseif provider == 'python' or provider == 'python3' then + local py_major_version = (provider == 'python3' and 3 or 2) + local errors = funcs['provider#pythonx#Detect'](py_major_version)[2] + return errors ~= '' and errors or false + else + assert(false, 'Unknown provider: ' .. provider) + end +end + +local function alter_slashes(obj) + if not iswin() then + return obj + end + if type(obj) == 'string' then + local ret = obj:gsub('/', '\\') + return ret + elseif type(obj) == 'table' then + local ret = {} + for k, v in pairs(obj) do + ret[k] = alter_slashes(v) + end + return ret + else + assert(false, 'Could only alter slashes for tables of strings and strings') + end +end + +local module = { prepend_argv = prepend_argv, clear = clear, connect = connect, @@ -596,6 +629,7 @@ local M = { nvim = nvim, nvim_async = nvim_async, nvim_prog = nvim_prog, + nvim_argv = nvim_argv, nvim_set = nvim_set, nvim_dir = nvim_dir, buffer = buffer, @@ -632,6 +666,8 @@ local M = { meth_pcall = meth_pcall, NIL = mpack.NIL, get_pathsep = get_pathsep, + missing_provider = missing_provider, + alter_slashes = alter_slashes, } return function(after_each) @@ -641,5 +677,5 @@ return function(after_each) check_cores('build/bin/nvim') end) end - return M + return module end diff --git a/test/functional/legacy/036_regexp_character_classes_spec.lua b/test/functional/legacy/036_regexp_character_classes_spec.lua index 110f7dd852..38e8145d1c 100644 --- a/test/functional/legacy/036_regexp_character_classes_spec.lua +++ b/test/functional/legacy/036_regexp_character_classes_spec.lua @@ -275,4 +275,16 @@ describe('character classes in regexp', function() diff(sixlines(string.sub(punct1, 1)..digits..punct2..upper..punct3.. lower..punct4..ctrl2..iso_text)) end) + it('does not convert character class ranges to an incorrect class', function() + source([[ + 1 s/\%#=0[0-z]//g + 2 s/\%#=1[0-z]//g + 3 s/\%#=2[0-z]//g + 4 s/\%#=0[^0-z]//g + 5 s/\%#=1[^0-z]//g + 6 s/\%#=2[^0-z]//g + ]]) + diff(string.rep(ctrl1..punct1..punct4..ctrl2..iso_text..'\n', 3) + ..string.rep(digits..punct2..upper..punct3..lower..'\n', 3)) + end) end) diff --git a/test/functional/legacy/assert_spec.lua b/test/functional/legacy/assert_spec.lua index d6255d42e7..2f342ec9a3 100644 --- a/test/functional/legacy/assert_spec.lua +++ b/test/functional/legacy/assert_spec.lua @@ -255,6 +255,16 @@ describe('assert function:', function() end) end) + -- assert_report({msg}) + describe('assert_report()', function() + it('should add a message to v:errors', function() + command("call assert_report('something is wrong')") + command("call assert_match('something is wrong', v:errors[0])") + command('call remove(v:errors, 0)') + expected_empty() + end) + end) + -- assert_exception({cmd}, [, {error}]) describe('assert_exception()', function() it('should assert thrown exceptions properly', function() diff --git a/test/functional/legacy/eval_spec.lua b/test/functional/legacy/eval_spec.lua index d7ef508194..c5d38d6d05 100644 --- a/test/functional/legacy/eval_spec.lua +++ b/test/functional/legacy/eval_spec.lua @@ -516,6 +516,15 @@ describe('eval', function() eq({'item'}, eval("y")) end) + it('sets the unnamed register when the "u" option is passed to setreg', function() + command("call setreg('a','a reg', 'cu')") + eq("a reg", eval('@"')) + command("call setreg('b','b reg', 'cu')") + eq("b reg", eval('@"')) + command("call setreg('c','c reg', 'c')") + eq("b reg", eval('@"')) + end) + it('search and expressions', function() command('so test_eval_setup.vim') command([=[call SetReg('/', ['abc/'])]=]) diff --git a/test/functional/legacy/search_spec.lua b/test/functional/legacy/search_spec.lua new file mode 100644 index 0000000000..5f71861821 --- /dev/null +++ b/test/functional/legacy/search_spec.lua @@ -0,0 +1,474 @@ +local helpers = require('test.functional.helpers')(after_each) +local Screen = require('test.functional.ui.screen') +local clear = helpers.clear +local command = helpers.command +local eq = helpers.eq +local eval = helpers.eval +local feed = helpers.feed +local funcs = helpers.funcs + +describe('search cmdline', function() + local screen + + before_each(function() + clear() + command('set nohlsearch') + screen = Screen.new(20, 3) + screen:attach() + screen:set_default_attr_ids({ + inc = {reverse = true} + }) + end) + + local function tenlines() + funcs.setline(1, { + ' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', + ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar' + }) + command('1') + end + + it('history can be navigated with <C-N>/<C-P>', function() + tenlines() + command('set noincsearch') + feed('/foobar<CR>') + feed('/the<CR>') + eq('the', eval('@/')) + feed('/thes<C-P><C-P><CR>') + eq('foobar', eval('@/')) + end) + + describe('can traverse matches', function() + before_each(tenlines) + local function forwarditer(wrapscan) + command('set incsearch '..wrapscan) + feed('/the') + screen:expect([[ + 1 | + 2 {inc:the}se | + /the^ | + ]]) + feed('<C-G>') + screen:expect([[ + 2 these | + 3 {inc:the} | + /the^ | + ]]) + eq({0, 0, 0, 0}, funcs.getpos('"')) + feed('<C-G>') + screen:expect([[ + 3 the | + 4 {inc:the}ir | + /the^ | + ]]) + feed('<C-G>') + screen:expect([[ + 4 their | + 5 {inc:the}re | + /the^ | + ]]) + feed('<C-G>') + screen:expect([[ + 5 there | + 6 {inc:the}ir | + /the^ | + ]]) + feed('<C-G>') + screen:expect([[ + 6 their | + 7 {inc:the} | + /the^ | + ]]) + feed('<C-G>') + screen:expect([[ + 7 the | + 8 {inc:the}m | + /the^ | + ]]) + feed('<C-G>') + screen:expect([[ + 8 them | + 9 {inc:the}se | + /the^ | + ]]) + feed('<C-G>') + if wrapscan == 'wrapscan' then + screen:expect([[ + 2 {inc:the}se | + 3 the | + /the^ | + ]]) + else + screen:expect([[ + 8 them | + 9 {inc:the}se | + /the^ | + ]]) + feed('<CR>') + eq({0, 0, 0, 0}, funcs.getpos('"')) + end + end + + local function backiter(wrapscan) + command('set incsearch '..wrapscan) + command('$') + + feed('?the') + screen:expect([[ + 9 {inc:the}se | + 10 foobar | + ?the^ | + ]]) + if wrapscan == 'wrapscan' then + feed('<C-G>') + screen:expect([[ + 2 {inc:the}se | + 3 the | + ?the^ | + ]]) + feed('<CR>') + screen:expect([[ + 2 ^these | + 3 the | + ?the | + ]]) + else + feed('<C-G>') + screen:expect([[ + 9 {inc:the}se | + 10 foobar | + ?the^ | + ]]) + feed('<CR>') + screen:expect([[ + 9 ^these | + 10 foobar | + ?the | + ]]) + end + command('$') + feed('?the') + screen:expect([[ + 9 {inc:the}se | + 10 foobar | + ?the^ | + ]]) + feed('<C-T>') + screen:expect([[ + 8 {inc:the}m | + 9 these | + ?the^ | + ]]) + for i = 1, 6 do + feed('<C-T>') + -- Avoid sleep just before expect, otherwise expect will take the full + -- timeout + if i ~= 6 then + screen:sleep(1) + end + end + screen:expect([[ + 2 {inc:the}se | + 3 the | + ?the^ | + ]]) + feed('<C-T>') + if wrapscan == 'wrapscan' then + screen:expect([[ + 9 {inc:the}se | + 10 foobar | + ?the^ | + ]]) + else + screen:expect([[ + 2 {inc:the}se | + 3 the | + ?the^ | + ]]) + end + end + + it("using <C-G> and 'nowrapscan'", function() + forwarditer('nowrapscan') + end) + + it("using <C-G> and 'wrapscan'", function() + forwarditer('wrapscan') + end) + + it("using <C-T> and 'nowrapscan'", function() + backiter('nowrapscan') + end) + + it("using <C-T> and 'wrapscan'", function() + backiter('wrapscan') + end) + end) + + it('expands pattern with <C-L>', function() + tenlines() + command('set incsearch wrapscan') + + feed('/the') + screen:expect([[ + 1 | + 2 {inc:the}se | + /the^ | + ]]) + feed('<C-L>') + screen:expect([[ + 1 | + 2 {inc:thes}e | + /thes^ | + ]]) + feed('<C-G>') + screen:expect([[ + 9 {inc:thes}e | + 10 foobar | + /thes^ | + ]]) + feed('<C-G>') + screen:expect([[ + 2 {inc:thes}e | + 3 the | + /thes^ | + ]]) + feed('<CR>') + screen:expect([[ + 2 ^these | + 3 the | + /thes | + ]]) + + command('1') + command('set nowrapscan') + feed('/the') + screen:expect([[ + 1 | + 2 {inc:the}se | + /the^ | + ]]) + feed('<C-L>') + screen:expect([[ + 1 | + 2 {inc:thes}e | + /thes^ | + ]]) + feed('<C-G>') + screen:expect([[ + 9 {inc:thes}e | + 10 foobar | + /thes^ | + ]]) + feed('<C-G><CR>') + screen:expect([[ + 9 ^these | + 10 foobar | + /thes | + ]]) + end) + + it('reduces pattern with <BS> and keeps cursor position', function() + tenlines() + command('set incsearch wrapscan') + + -- First match + feed('/thei') + screen:expect([[ + 4 {inc:thei}r | + 5 there | + /thei^ | + ]]) + -- Match from initial cursor position when modifying search + feed('<BS>') + screen:expect([[ + 1 | + 2 {inc:the}se | + /the^ | + ]]) + -- New text advances to next match + feed('s') + screen:expect([[ + 1 | + 2 {inc:thes}e | + /thes^ | + ]]) + -- Stay on this match when deleting a character + feed('<BS>') + screen:expect([[ + 1 | + 2 {inc:the}se | + /the^ | + ]]) + -- Advance to previous match + feed('<C-T>') + screen:expect([[ + 9 {inc:the}se | + 10 foobar | + /the^ | + ]]) + -- Extend search to include next character + feed('<C-L>') + screen:expect([[ + 9 {inc:thes}e | + 10 foobar | + /thes^ | + ]]) + -- Deleting all characters resets the cursor position + feed('<BS><BS><BS><BS>') + screen:expect([[ + 1 | + 2 these | + /^ | + ]]) + feed('the') + screen:expect([[ + 1 | + 2 {inc:the}se | + /the^ | + ]]) + feed('\\>') + screen:expect([[ + 2 these | + 3 {inc:the} | + /the\>^ | + ]]) + end) + + it('can traverse matches in the same line with <C-G>/<C-T>', function() + funcs.setline(1, { ' 1', ' 2 these', ' 3 the theother' }) + command('1') + command('set incsearch') + + -- First match + feed('/the') + screen:expect([[ + 1 | + 2 {inc:the}se | + /the^ | + ]]) + + -- Next match, different line + feed('<C-G>') + screen:expect([[ + 2 these | + 3 {inc:the} theother | + /the^ | + ]]) + + -- Next match, same line + feed('<C-G>') + screen:expect([[ + 2 these | + 3 the {inc:the}other | + /the^ | + ]]) + feed('<C-G>') + screen:expect([[ + 2 these | + 3 the theo{inc:the}r | + /the^ | + ]]) + + -- Previous match, same line + feed('<C-T>') + screen:expect([[ + 2 these | + 3 the {inc:the}other | + /the^ | + ]]) + feed('<C-T>') + screen:expect([[ + 2 these | + 3 {inc:the} theother | + /the^ | + ]]) + + -- Previous match, different line + feed('<C-T>') + screen:expect([[ + 2 {inc:the}se | + 3 the theother | + /the^ | + ]]) + end) + + it('keeps the view after deleting a char from the search', function() + screen:detach() + screen = Screen.new(20, 6) + screen:attach() + screen:set_default_attr_ids({ + inc = {reverse = true} + }) + screen:set_default_attr_ignore({ + {bold=true, reverse=true}, {bold=true, foreground=Screen.colors.Blue1} + }) + tenlines() + + feed('/foo') + screen:expect([[ + 6 their | + 7 the | + 8 them | + 9 these | + 10 {inc:foo}bar | + /foo^ | + ]]) + feed('<BS>') + screen:expect([[ + 6 their | + 7 the | + 8 them | + 9 these | + 10 {inc:fo}obar | + /fo^ | + ]]) + feed('<CR>') + screen:expect([[ + 6 their | + 7 the | + 8 them | + 9 these | + 10 ^foobar | + /fo | + ]]) + eq({lnum = 10, leftcol = 0, col = 4, topfill = 0, topline = 6, + coladd = 0, skipcol = 0, curswant = 4}, + funcs.winsaveview()) + end) + + it('restores original view after failed search', function() + screen:detach() + screen = Screen.new(40, 3) + screen:attach() + screen:set_default_attr_ids({ + inc = {reverse = true}, + err = { foreground = Screen.colors.Grey100, background = Screen.colors.Red }, + more = { bold = true, foreground = Screen.colors.SeaGreen4 }, + }) + tenlines() + feed('0') + feed('/foo') + screen:expect([[ + 9 these | + 10 {inc:foo}bar | + /foo^ | + ]]) + feed('<C-W>') + screen:expect([[ + 1 | + 2 these | + /^ | + ]]) + feed('<CR>') + screen:expect([[ + / | + {err:E35: No previous regular expression} | + {more:Press ENTER or type command to continue}^ | + ]]) + feed('<CR>') + eq({lnum = 1, leftcol = 0, col = 0, topfill = 0, topline = 1, + coladd = 0, skipcol = 0, curswant = 0}, + funcs.winsaveview()) + end) +end) diff --git a/test/functional/lua/api_spec.lua b/test/functional/lua/api_spec.lua new file mode 100644 index 0000000000..b1dc5c07fd --- /dev/null +++ b/test/functional/lua/api_spec.lua @@ -0,0 +1,204 @@ +-- Test suite for testing interactions with API bindings +local helpers = require('test.functional.helpers')(after_each) + +local exc_exec = helpers.exc_exec +local funcs = helpers.funcs +local clear = helpers.clear +local eval = helpers.eval +local NIL = helpers.NIL +local eq = helpers.eq + +before_each(clear) + +describe('luaeval(vim.api.…)', function() + describe('with channel_id and buffer handle', function() + describe('nvim_buf_get_lines', function() + it('works', function() + funcs.setline(1, {"abc", "def", "a\nb", "ttt"}) + eq({{_TYPE={}, _VAL={'a\nb'}}}, + funcs.luaeval('vim.api.nvim_buf_get_lines(1, 2, 3, false)')) + end) + end) + describe('nvim_buf_set_lines', function() + it('works', function() + funcs.setline(1, {"abc", "def", "a\nb", "ttt"}) + eq(NIL, funcs.luaeval('vim.api.nvim_buf_set_lines(1, 1, 2, false, {"b\\0a"})')) + eq({'abc', {_TYPE={}, _VAL={'b\na'}}, {_TYPE={}, _VAL={'a\nb'}}, 'ttt'}, + funcs.luaeval('vim.api.nvim_buf_get_lines(1, 0, 4, false)')) + end) + end) + end) + describe('with errors', function() + it('transforms API error from nvim_buf_set_lines into lua error', function() + funcs.setline(1, {"abc", "def", "a\nb", "ttt"}) + eq({false, 'String cannot contain newlines'}, + funcs.luaeval('{pcall(vim.api.nvim_buf_set_lines, 1, 1, 2, false, {"b\\na"})}')) + end) + + it('transforms API error from nvim_win_set_cursor into lua error', function() + eq({false, 'Argument "pos" must be a [row, col] array'}, + funcs.luaeval('{pcall(vim.api.nvim_win_set_cursor, 0, {1, 2, 3})}')) + -- Used to produce a memory leak due to a bug in nvim_win_set_cursor + eq({false, 'Invalid window id'}, + funcs.luaeval('{pcall(vim.api.nvim_win_set_cursor, -1, {1, 2, 3})}')) + end) + + it('transforms API error from nvim_win_set_cursor + same array as in first test into lua error', + function() + eq({false, 'Argument "pos" must be a [row, col] array'}, + funcs.luaeval('{pcall(vim.api.nvim_win_set_cursor, 0, {"b\\na"})}')) + end) + end) + + it('correctly evaluates API code which calls luaeval', function() + local str = (([===[vim.api.nvim_eval([==[ + luaeval('vim.api.nvim_eval([=[ + luaeval("vim.api.nvim_eval([[ + luaeval(1) + ]])") + ]=])') + ]==])]===]):gsub('\n', ' ')) + eq(1, funcs.luaeval(str)) + end) + + it('correctly converts from API objects', function() + eq(1, funcs.luaeval('vim.api.nvim_eval("1")')) + eq('1', funcs.luaeval([[vim.api.nvim_eval('"1"')]])) + eq({}, funcs.luaeval('vim.api.nvim_eval("[]")')) + eq({}, funcs.luaeval('vim.api.nvim_eval("{}")')) + eq(1, funcs.luaeval('vim.api.nvim_eval("1.0")')) + eq(true, funcs.luaeval('vim.api.nvim_eval("v:true")')) + eq(false, funcs.luaeval('vim.api.nvim_eval("v:false")')) + eq(NIL, funcs.luaeval('vim.api.nvim_eval("v:null")')) + + eq(0, eval([[type(luaeval('vim.api.nvim_eval("1")'))]])) + eq(1, eval([[type(luaeval('vim.api.nvim_eval("''1''")'))]])) + eq(3, eval([[type(luaeval('vim.api.nvim_eval("[]")'))]])) + eq(4, eval([[type(luaeval('vim.api.nvim_eval("{}")'))]])) + eq(5, eval([[type(luaeval('vim.api.nvim_eval("1.0")'))]])) + eq(6, eval([[type(luaeval('vim.api.nvim_eval("v:true")'))]])) + eq(6, eval([[type(luaeval('vim.api.nvim_eval("v:false")'))]])) + eq(7, eval([[type(luaeval('vim.api.nvim_eval("v:null")'))]])) + + eq({foo=42}, funcs.luaeval([[vim.api.nvim_eval('{"foo": 42}')]])) + eq({42}, funcs.luaeval([[vim.api.nvim_eval('[42]')]])) + + eq({foo={bar=42}, baz=50}, funcs.luaeval([[vim.api.nvim_eval('{"foo": {"bar": 42}, "baz": 50}')]])) + eq({{42}, {}}, funcs.luaeval([=[vim.api.nvim_eval('[[42], []]')]=])) + end) + + it('correctly converts to API objects', function() + eq(1, funcs.luaeval('vim.api.nvim__id(1)')) + eq('1', funcs.luaeval('vim.api.nvim__id("1")')) + eq({1}, funcs.luaeval('vim.api.nvim__id({1})')) + eq({foo=1}, funcs.luaeval('vim.api.nvim__id({foo=1})')) + eq(1.5, funcs.luaeval('vim.api.nvim__id(1.5)')) + eq(true, funcs.luaeval('vim.api.nvim__id(true)')) + eq(false, funcs.luaeval('vim.api.nvim__id(false)')) + eq(NIL, funcs.luaeval('vim.api.nvim__id(nil)')) + + eq(0, eval([[type(luaeval('vim.api.nvim__id(1)'))]])) + eq(1, eval([[type(luaeval('vim.api.nvim__id("1")'))]])) + eq(3, eval([[type(luaeval('vim.api.nvim__id({1})'))]])) + eq(4, eval([[type(luaeval('vim.api.nvim__id({foo=1})'))]])) + eq(5, eval([[type(luaeval('vim.api.nvim__id(1.5)'))]])) + eq(6, eval([[type(luaeval('vim.api.nvim__id(true)'))]])) + eq(6, eval([[type(luaeval('vim.api.nvim__id(false)'))]])) + 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}}})')) + end) + + it('correctly converts container objects with type_idx to API objects', function() + eq(5, eval('type(luaeval("vim.api.nvim__id({[vim.type_idx]=vim.types.float, [vim.val_idx]=0})"))')) + eq(4, eval([[type(luaeval('vim.api.nvim__id({[vim.type_idx]=vim.types.dictionary})'))]])) + eq(3, eval([[type(luaeval('vim.api.nvim__id({[vim.type_idx]=vim.types.array})'))]])) + + eq({}, funcs.luaeval('vim.api.nvim__id({[vim.type_idx]=vim.types.array})')) + + -- Presence of type_idx makes Vim ignore some keys + eq({42}, funcs.luaeval('vim.api.nvim__id({[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})')) + eq({foo=2}, funcs.luaeval('vim.api.nvim__id({[vim.type_idx]=vim.types.dictionary, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})')) + eq(10, funcs.luaeval('vim.api.nvim__id({[vim.type_idx]=vim.types.float, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})')) + eq({}, funcs.luaeval('vim.api.nvim__id({[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2})')) + end) + + it('correctly converts arrays with type_idx to API objects', function() + eq(3, eval([[type(luaeval('vim.api.nvim__id_array({[vim.type_idx]=vim.types.array})'))]])) + + eq({}, funcs.luaeval('vim.api.nvim__id_array({[vim.type_idx]=vim.types.array})')) + + eq({42}, funcs.luaeval('vim.api.nvim__id_array({[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})')) + eq({{foo=2}}, funcs.luaeval('vim.api.nvim__id_array({{[vim.type_idx]=vim.types.dictionary, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}})')) + eq({10}, funcs.luaeval('vim.api.nvim__id_array({{[vim.type_idx]=vim.types.float, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}})')) + eq({}, funcs.luaeval('vim.api.nvim__id_array({[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2})')) + + eq({}, funcs.luaeval('vim.api.nvim__id_array({})')) + eq(3, eval([[type(luaeval('vim.api.nvim__id_array({})'))]])) + end) + + it('correctly converts dictionaries with type_idx to API objects', function() + eq(4, eval([[type(luaeval('vim.api.nvim__id_dictionary({[vim.type_idx]=vim.types.dictionary})'))]])) + + eq({}, funcs.luaeval('vim.api.nvim__id_dictionary({[vim.type_idx]=vim.types.dictionary})')) + + eq({v={42}}, funcs.luaeval('vim.api.nvim__id_dictionary({v={[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}})')) + eq({foo=2}, funcs.luaeval('vim.api.nvim__id_dictionary({[vim.type_idx]=vim.types.dictionary, [vim.val_idx]=10, [5]=1, foo=2, [1]=42})')) + eq({v=10}, funcs.luaeval('vim.api.nvim__id_dictionary({v={[vim.type_idx]=vim.types.float, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}})')) + eq({v={}}, funcs.luaeval('vim.api.nvim__id_dictionary({v={[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2}})')) + + -- If API requests dictionary, then empty table will be the one. This is not + -- the case normally because empty table is an empty arrray. + eq({}, funcs.luaeval('vim.api.nvim__id_dictionary({})')) + eq(4, eval([[type(luaeval('vim.api.nvim__id_dictionary({})'))]])) + end) + + it('errors out correctly when working with API', function() + -- Conversion errors + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Cannot convert given lua type', + exc_exec([[call luaeval("vim.api.nvim__id(vim.api.nvim__id)")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Cannot convert given lua table', + exc_exec([[call luaeval("vim.api.nvim__id({1, foo=42})")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Cannot convert given lua type', + exc_exec([[call luaeval("vim.api.nvim__id({42, vim.api.nvim__id})")]])) + -- Errors in number of arguments + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected 1 argument', + exc_exec([[call luaeval("vim.api.nvim__id()")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected 1 argument', + exc_exec([[call luaeval("vim.api.nvim__id(1, 2)")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected 2 arguments', + exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2, 3)")]])) + -- Error in argument types + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua string', + exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2)")]])) + + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua number', + exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 'test', 1, false)")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Number is not integral', + exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 1.5, 1, false)")]])) + + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua table', + exc_exec([[call luaeval("vim.api.nvim__id_float('test')")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Unexpected type', + exc_exec([[call luaeval("vim.api.nvim__id_float({[vim.type_idx]=vim.types.dictionary})")]])) + + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua table', + exc_exec([[call luaeval("vim.api.nvim__id_array(1)")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Unexpected type', + exc_exec([[call luaeval("vim.api.nvim__id_array({[vim.type_idx]=vim.types.dictionary})")]])) + + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Expected lua table', + exc_exec([[call luaeval("vim.api.nvim__id_dictionary(1)")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: Unexpected type', + exc_exec([[call luaeval("vim.api.nvim__id_dictionary({[vim.type_idx]=vim.types.array})")]])) + -- TODO: check for errors with Tabpage argument + -- TODO: check for errors with Window argument + -- TODO: check for errors with Buffer argument + end) + + it('accepts any value as API Boolean', function() + eq('', funcs.luaeval('vim.api.nvim_replace_termcodes("", vim, false, nil)')) + eq('', funcs.luaeval('vim.api.nvim_replace_termcodes("", 0, 1.5, "test")')) + eq('', funcs.luaeval('vim.api.nvim_replace_termcodes("", true, {}, {[vim.type_idx]=vim.types.array})')) + end) +end) diff --git a/test/functional/lua/commands_spec.lua b/test/functional/lua/commands_spec.lua new file mode 100644 index 0000000000..017ee55729 --- /dev/null +++ b/test/functional/lua/commands_spec.lua @@ -0,0 +1,164 @@ +-- Test suite for checking :lua* commands +local helpers = require('test.functional.helpers')(after_each) + +local eq = helpers.eq +local NIL = helpers.NIL +local clear = helpers.clear +local meths = helpers.meths +local funcs = helpers.funcs +local source = helpers.source +local dedent = helpers.dedent +local exc_exec = helpers.exc_exec +local write_file = helpers.write_file +local redir_exec = helpers.redir_exec +local curbufmeths = helpers.curbufmeths + +before_each(clear) + +describe(':lua command', function() + it('works', function() + eq('', redir_exec( + 'lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TEST"})')) + eq({'', 'TEST'}, curbufmeths.get_lines(0, 100, false)) + source(dedent([[ + lua << EOF + vim.api.nvim_buf_set_lines(1, 1, 2, false, {"TSET"}) + EOF]])) + eq({'', 'TSET'}, curbufmeths.get_lines(0, 100, false)) + source(dedent([[ + lua << EOF + vim.api.nvim_buf_set_lines(1, 1, 2, false, {"SETT"})]])) + eq({'', 'SETT'}, curbufmeths.get_lines(0, 100, false)) + source(dedent([[ + 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"}) + EOF]])) + eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false)) + end) + it('throws catchable errors', function() + eq([[Vim(lua):E5104: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unexpected symbol near ')']], + exc_exec('lua ()')) + eq([[Vim(lua):E5105: Error while calling lua chunk: [string "<VimL compiled string>"]:1: TEST]], + exc_exec('lua error("TEST")')) + eq([[Vim(lua):E5105: Error while calling lua chunk: [string "<VimL compiled string>"]:1: Invalid buffer id]], + exc_exec('lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})')) + eq({''}, curbufmeths.get_lines(0, 100, false)) + end) + it('works with NULL errors', function() + eq([=[Vim(lua):E5105: Error while calling lua chunk: [NULL]]=], + exc_exec('lua error(nil)')) + end) + it('accepts embedded NLs without heredoc', function() + -- Such code is usually used for `:execute 'lua' {generated_string}`: + -- heredocs do not work in this case. + meths.command([[ + 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"}) + ]]) + eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false)) + end) + it('preserves global and not preserves local variables', function() + eq('', redir_exec('lua gvar = 42')) + eq('', redir_exec('lua local lvar = 100500')) + eq(NIL, funcs.luaeval('lvar')) + eq(42, funcs.luaeval('gvar')) + end) + it('works with long strings', function() + local s = ('x'):rep(100500) + + eq('\nE5104: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unfinished string near \'<eof>\'', redir_exec(('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s})'):format(s))) + eq({''}, curbufmeths.get_lines(0, -1, false)) + + eq('', redir_exec(('lua vim.api.nvim_buf_set_lines(1, 1, 2, false, {"%s"})'):format(s))) + eq({'', s}, curbufmeths.get_lines(0, -1, false)) + end) +end) + +describe(':luado command', function() + it('works', function() + curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"}) + eq('', redir_exec('luado lines = (lines or {}) lines[#lines + 1] = {linenr, line}')) + eq({'ABC', 'def', 'gHi'}, curbufmeths.get_lines(0, -1, false)) + eq({{1, 'ABC'}, {2, 'def'}, {3, 'gHi'}}, funcs.luaeval('lines')) + + -- Automatic transformation of numbers + eq('', redir_exec('luado return linenr')) + eq({'1', '2', '3'}, curbufmeths.get_lines(0, -1, false)) + + eq('', redir_exec('luado return ("<%02x>"):format(line:byte())')) + eq({'<31>', '<32>', '<33>'}, curbufmeths.get_lines(0, -1, false)) + end) + it('stops processing lines when suddenly out of lines', function() + curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"}) + eq('', redir_exec('2,$luado runs = ((runs or 0) + 1) vim.api.nvim_command("%d")')) + eq({''}, curbufmeths.get_lines(0, -1, false)) + eq(1, funcs.luaeval('runs')) + end) + it('works correctly when changing lines out of range', function() + curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"}) + eq('\nE322: line number out of range: 1 past the end\nE320: Cannot find line 2', + redir_exec('2,$luado vim.api.nvim_command("%d") return linenr')) + eq({''}, curbufmeths.get_lines(0, -1, false)) + end) + it('fails on errors', function() + eq([[Vim(luado):E5109: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unexpected symbol near ')']], + exc_exec('luado ()')) + eq([[Vim(luado):E5111: Error while calling lua function: [string "<VimL compiled string>"]:1: attempt to perform arithmetic on global 'liness' (a nil value)]], + exc_exec('luado return liness + 1')) + end) + it('works with NULL errors', function() + eq([=[Vim(luado):E5111: Error while calling lua function: [NULL]]=], + exc_exec('luado error(nil)')) + end) + it('fails in sandbox when needed', function() + curbufmeths.set_lines(0, 1, false, {"ABC", "def", "gHi"}) + eq('\nE48: Not allowed in sandbox: sandbox luado runs = (runs or 0) + 1', + redir_exec('sandbox luado runs = (runs or 0) + 1')) + eq(NIL, funcs.luaeval('runs')) + end) + it('works with long strings', function() + local s = ('x'):rep(100500) + + eq('\nE5109: Error while creating lua chunk: [string "<VimL compiled string>"]:1: unfinished string near \'<eof>\'', redir_exec(('luado return "%s'):format(s))) + eq({''}, curbufmeths.get_lines(0, -1, false)) + + eq('', redir_exec(('luado return "%s"'):format(s))) + eq({s}, curbufmeths.get_lines(0, -1, false)) + end) +end) + +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"}) + ]]) + eq('', redir_exec('luafile ' .. fname)) + eq({'', 'ETTS', 'TTSE', 'STTE'}, curbufmeths.get_lines(0, 100, false)) + 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)) + 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), + exc_exec('luafile ' .. fname)) + end) + 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)) + end) +end) diff --git a/test/functional/lua/luaeval_spec.lua b/test/functional/lua/luaeval_spec.lua new file mode 100644 index 0000000000..6da0001cea --- /dev/null +++ b/test/functional/lua/luaeval_spec.lua @@ -0,0 +1,255 @@ +-- Test suite for testing luaeval() function +local helpers = require('test.functional.helpers')(after_each) + +local redir_exec = helpers.redir_exec +local exc_exec = helpers.exc_exec +local command = helpers.command +local meths = helpers.meths +local funcs = helpers.funcs +local clear = helpers.clear +local eval = helpers.eval +local NIL = helpers.NIL +local eq = helpers.eq + +before_each(clear) + +local function startswith(expected, actual) + eq(expected, actual:sub(1, #expected)) +end + +describe('luaeval()', function() + local nested_by_level = {} + local nested = {} + local nested_s = '{}' + for i=1,100 do + if i % 2 == 0 then + nested = {nested} + nested_s = '{' .. nested_s .. '}' + else + nested = {nested=nested} + nested_s = '{nested=' .. nested_s .. '}' + end + nested_by_level[i] = {o=nested, s=nested_s} + end + + describe('second argument', function() + it('is successfully received', function() + local t = {t=true, f=false, --[[n=NIL,]] d={l={'string', 42, 0.42}}} + eq(t, funcs.luaeval("_A", t)) + -- Not tested: nil, funcrefs, returned object identity: behaviour will + -- most likely change. + end) + end) + describe('lua values', function() + it('are successfully transformed', function() + eq({n=1, f=1.5, s='string', l={4, 2}}, + funcs.luaeval('{n=1, f=1.5, s="string", l={4, 2}}')) + -- Not tested: nil inside containers: behaviour will most likely change. + eq(NIL, funcs.luaeval('nil')) + eq({['']=1}, funcs.luaeval('{[""]=1}')) + end) + end) + describe('recursive lua values', function() + it('are successfully transformed', function() + funcs.luaeval('rawset(_G, "d", {})') + funcs.luaeval('rawset(d, "d", d)') + eq('\n{\'d\': {...@0}}', funcs.execute('echo luaeval("d")')) + + funcs.luaeval('rawset(_G, "l", {})') + funcs.luaeval('table.insert(l, l)') + eq('\n[[...@0]]', funcs.execute('echo luaeval("l")')) + end) + end) + describe('strings', function() + it('are successfully converted to special dictionaries', function() + command([[let s = luaeval('"\0"')]]) + eq({_TYPE={}, _VAL={'\n'}}, meths.get_var('s')) + eq(1, funcs.eval('s._TYPE is v:msgpack_types.binary')) + end) + it('are successfully converted to special dictionaries in table keys', + function() + command([[let d = luaeval('{["\0"]=1}')]]) + eq({_TYPE={}, _VAL={{{_TYPE={}, _VAL={'\n'}}, 1}}}, meths.get_var('d')) + eq(1, funcs.eval('d._TYPE is v:msgpack_types.map')) + eq(1, funcs.eval('d._VAL[0][0]._TYPE is v:msgpack_types.string')) + end) + it('are successfully converted to special dictionaries from a list', + function() + command([[let l = luaeval('{"abc", "a\0b", "c\0d", "def"}')]]) + eq({'abc', {_TYPE={}, _VAL={'a\nb'}}, {_TYPE={}, _VAL={'c\nd'}}, 'def'}, + meths.get_var('l')) + eq(1, funcs.eval('l[1]._TYPE is v:msgpack_types.binary')) + eq(1, funcs.eval('l[2]._TYPE is v:msgpack_types.binary')) + end) + end) + + -- Not checked: funcrefs converted to NIL. To be altered to something more + -- meaningful later. + + it('correctly evaluates scalars', function() + eq(1, funcs.luaeval('1')) + eq(0, eval('type(luaeval("1"))')) + + eq(1.5, funcs.luaeval('1.5')) + eq(5, eval('type(luaeval("1.5"))')) + + eq("test", funcs.luaeval('"test"')) + eq(1, eval('type(luaeval("\'test\'"))')) + + eq('', funcs.luaeval('""')) + eq({_TYPE={}, _VAL={'\n'}}, funcs.luaeval([['\0']])) + eq({_TYPE={}, _VAL={'\n', '\n'}}, funcs.luaeval([['\0\n\0']])) + eq(1, eval([[luaeval('"\0\n\0"')._TYPE is v:msgpack_types.binary]])) + + eq(true, funcs.luaeval('true')) + eq(false, funcs.luaeval('false')) + eq(NIL, funcs.luaeval('nil')) + end) + + it('correctly evaluates containers', function() + eq({}, funcs.luaeval('{}')) + eq(3, eval('type(luaeval("{}"))')) + + eq({test=1, foo=2}, funcs.luaeval('{test=1, foo=2}')) + eq(4, eval('type(luaeval("{test=1, foo=2}"))')) + + eq({4, 2}, funcs.luaeval('{4, 2}')) + eq(3, eval('type(luaeval("{4, 2}"))')) + + local level = 30 + eq(nested_by_level[level].o, funcs.luaeval(nested_by_level[level].s)) + + eq({_TYPE={}, _VAL={{{_TYPE={}, _VAL={'\n', '\n'}}, {_TYPE={}, _VAL={'\n', '\n\n'}}}}}, + funcs.luaeval([[{['\0\n\0']='\0\n\0\0'}]])) + eq(1, eval([[luaeval('{["\0\n\0"]="\0\n\0\0"}')._TYPE is v:msgpack_types.map]])) + eq(1, eval([[luaeval('{["\0\n\0"]="\0\n\0\0"}')._VAL[0][0]._TYPE is v:msgpack_types.string]])) + eq(1, eval([[luaeval('{["\0\n\0"]="\0\n\0\0"}')._VAL[0][1]._TYPE is v:msgpack_types.binary]])) + eq({nested={{_TYPE={}, _VAL={{{_TYPE={}, _VAL={'\n', '\n'}}, {_TYPE={}, _VAL={'\n', '\n\n'}}}}}}}, + funcs.luaeval([[{nested={{['\0\n\0']='\0\n\0\0'}}}]])) + end) + + it('correctly passes scalars as argument', function() + eq(1, funcs.luaeval('_A', 1)) + eq(1.5, funcs.luaeval('_A', 1.5)) + eq('', funcs.luaeval('_A', '')) + eq('test', funcs.luaeval('_A', 'test')) + eq(NIL, funcs.luaeval('_A', NIL)) + eq(true, funcs.luaeval('_A', true)) + eq(false, funcs.luaeval('_A', false)) + end) + + it('correctly passes containers as argument', function() + eq({}, funcs.luaeval('_A', {})) + eq({test=1}, funcs.luaeval('_A', {test=1})) + eq({4, 2}, funcs.luaeval('_A', {4, 2})) + local level = 28 + eq(nested_by_level[level].o, funcs.luaeval('_A', nested_by_level[level].o)) + end) + + local function sp(typ, val) + return ('{"_TYPE": v:msgpack_types.%s, "_VAL": %s}'):format(typ, val) + end + local function mapsp(...) + local val = '' + for i=1,(select('#', ...)/2) do + val = ('%s[%s,%s],'):format(val, select(i * 2 - 1, ...), + select(i * 2, ...)) + end + return sp('map', '[' .. val .. ']') + end + local function luaevalarg(argexpr, expr) + return eval(([=[ + [ + extend(g:, {'_ret': luaeval(%s, %s)})._ret, + type(g:_ret)==type({})&&has_key(g:_ret, '_TYPE') + ? [ + get(keys(filter(copy(v:msgpack_types), 'v:val is g:_ret._TYPE')), 0, + g:_ret._TYPE), + get(g:_ret, '_VAL', g:_ret) + ] + : [0, g:_ret]][1] + ]=]):format(expr or '"_A"', argexpr):gsub('\n', '')) + end + + it('correctly passes special dictionaries', function() + eq({'binary', {'\n', '\n'}}, luaevalarg(sp('binary', '["\\n", "\\n"]'))) + eq({'binary', {'\n', '\n'}}, luaevalarg(sp('string', '["\\n", "\\n"]'))) + eq({0, true}, luaevalarg(sp('boolean', 1))) + eq({0, false}, luaevalarg(sp('boolean', 0))) + eq({0, NIL}, luaevalarg(sp('nil', 0))) + eq({0, {[""]=""}}, luaevalarg(mapsp(sp('binary', '[""]'), '""'))) + eq({0, {[""]=""}}, luaevalarg(mapsp(sp('string', '[""]'), '""'))) + end) + + it('issues an error in some cases', function() + eq("Vim(call):E5100: Cannot convert given lua table: table should either have a sequence of positive integer keys or contain only string keys", + exc_exec('call luaeval("{1, foo=2}")')) + eq("Vim(call):E5101: Cannot convert given lua type", + exc_exec('call luaeval("vim.api.nvim_buf_get_lines")')) + startswith("Vim(call):E5107: Error while creating lua chunk for luaeval(): ", + exc_exec('call luaeval("1, 2, 3")')) + startswith("Vim(call):E5108: Error while calling lua chunk for luaeval(): ", + exc_exec('call luaeval("(nil)()")')) + eq("Vim(call):E5101: Cannot convert given lua type", + exc_exec('call luaeval("{42, vim.api}")')) + eq("Vim(call):E5101: Cannot convert given lua type", + exc_exec('call luaeval("{foo=42, baz=vim.api}")')) + + -- The following should not crash: conversion error happens inside + eq("Vim(call):E5101: Cannot convert given lua type", + exc_exec('call luaeval("vim.api")')) + -- The following should not show internal error + eq("\nE5101: Cannot convert given lua type\n0", + redir_exec('echo luaeval("vim.api")')) + end) + + it('correctly converts containers with type_idx', function() + eq(5, eval('type(luaeval("{[vim.type_idx]=vim.types.float, [vim.val_idx]=0}"))')) + eq(4, eval([[type(luaeval('{[vim.type_idx]=vim.types.dictionary}'))]])) + eq(3, eval([[type(luaeval('{[vim.type_idx]=vim.types.array}'))]])) + + eq({}, funcs.luaeval('{[vim.type_idx]=vim.types.array}')) + + -- Presence of type_idx makes Vim ignore some keys + eq({42}, funcs.luaeval('{[vim.type_idx]=vim.types.array, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}')) + eq({foo=2}, funcs.luaeval('{[vim.type_idx]=vim.types.dictionary, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}')) + eq(10, funcs.luaeval('{[vim.type_idx]=vim.types.float, [vim.val_idx]=10, [5]=1, foo=2, [1]=42}')) + + -- The following should not crash + eq({}, funcs.luaeval('{[vim.type_idx]=vim.types.dictionary}')) + end) + + it('correctly converts self-containing containers', function() + meths.set_var('l', {}) + eval('add(l, l)') + eq(true, eval('luaeval("_A == _A[1]", l)')) + eq(true, eval('luaeval("_A[1] == _A[1][1]", [l])')) + eq(true, eval('luaeval("_A.d == _A.d[1]", {"d": l})')) + eq(true, eval('luaeval("_A ~= _A[1]", [l])')) + + meths.set_var('d', {foo=42}) + eval('extend(d, {"d": d})') + eq(true, eval('luaeval("_A == _A.d", d)')) + eq(true, eval('luaeval("_A[1] == _A[1].d", [d])')) + eq(true, eval('luaeval("_A.d == _A.d.d", {"d": d})')) + eq(true, eval('luaeval("_A ~= _A.d", {"d": d})')) + end) + + it('errors out correctly when doing incorrect things in lua', function() + -- Conversion errors + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: attempt to call field \'xxx_nonexistent_key_xxx\' (a nil value)', + exc_exec([[call luaeval("vim.xxx_nonexistent_key_xxx()")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [string "<VimL compiled string>"]:1: ERROR', + exc_exec([[call luaeval("error('ERROR')")]])) + eq('Vim(call):E5108: Error while calling lua chunk for luaeval(): [NULL]', + exc_exec([[call luaeval("error(nil)")]])) + end) + + it('does not leak memory when called with too long line', + function() + local s = ('x'):rep(65536) + eq('Vim(call):E5107: Error while creating lua chunk for luaeval(): [string "<VimL compiled string>"]:1: unexpected symbol near \')\'', + exc_exec([[call luaeval("(']] .. s ..[[' + )")]])) + eq(s, funcs.luaeval('"' .. s .. '"')) + end) +end) diff --git a/test/functional/lua/overrides_spec.lua b/test/functional/lua/overrides_spec.lua new file mode 100644 index 0000000000..6e1d50071d --- /dev/null +++ b/test/functional/lua/overrides_spec.lua @@ -0,0 +1,294 @@ +-- Test for Vim overrides of lua built-ins +local helpers = require('test.functional.helpers')(after_each) +local Screen = require('test.functional.ui.screen') + +local eq = helpers.eq +local neq = helpers.neq +local NIL = helpers.NIL +local feed = helpers.feed +local clear = helpers.clear +local funcs = helpers.funcs +local meths = helpers.meths +local iswin = helpers.iswin +local command = helpers.command +local write_file = helpers.write_file +local redir_exec = helpers.redir_exec +local alter_slashes = helpers.alter_slashes + +local screen + +local fname = 'Xtest-functional-lua-overrides-luafile' + +before_each(clear) + +after_each(function() + os.remove(fname) +end) + +describe('print', function() + it('returns nothing', function() + eq(NIL, funcs.luaeval('print("abc")')) + eq(0, funcs.luaeval('select("#", print("abc"))')) + end) + it('allows catching printed text with :execute', function() + eq('\nabc', funcs.execute('lua print("abc")')) + eq('\nabc', funcs.execute('luado print("abc")')) + eq('\nabc', funcs.execute('call luaeval("print(\'abc\')")')) + write_file(fname, 'print("abc")') + eq('\nabc', funcs.execute('luafile ' .. fname)) + + eq('\nabc', redir_exec('lua print("abc")')) + eq('\nabc', redir_exec('luado print("abc")')) + eq('\nabc', redir_exec('call luaeval("print(\'abc\')")')) + write_file(fname, 'print("abc")') + eq('\nabc', redir_exec('luafile ' .. fname)) + end) + it('handles errors in __tostring', function() + write_file(fname, [[ + local meta_nilerr = { __tostring = function() error(nil) end } + local meta_abcerr = { __tostring = function() error("abc") end } + local meta_tblout = { __tostring = function() return {"TEST"} end } + v_nilerr = setmetatable({}, meta_nilerr) + v_abcerr = setmetatable({}, meta_abcerr) + v_tblout = setmetatable({}, meta_tblout) + ]]) + eq('', redir_exec('luafile ' .. fname)) + eq('\nE5114: Error while converting print argument #2: [NULL]', + redir_exec('lua print("foo", v_nilerr, "bar")')) + eq('\nE5114: Error while converting print argument #2: Xtest-functional-lua-overrides-luafile:2: abc', + redir_exec('lua print("foo", v_abcerr, "bar")')) + eq('\nE5114: Error while converting print argument #2: <Unknown error: lua_tolstring returned NULL for tostring result>', + redir_exec('lua print("foo", v_tblout, "bar")')) + end) + it('prints strings with NULs and NLs correctly', function() + meths.set_option('more', true) + eq('\nabc ^@ def\nghi^@^@^@jkl\nTEST\n\n\nT\n', + redir_exec([[lua print("abc \0 def\nghi\0\0\0jkl\nTEST\n\n\nT\n")]])) + eq('\nabc ^@ def\nghi^@^@^@jkl\nTEST\n\n\nT^@', + redir_exec([[lua print("abc \0 def\nghi\0\0\0jkl\nTEST\n\n\nT\0")]])) + eq('\nT^@', redir_exec([[lua print("T\0")]])) + eq('\nT\n', redir_exec([[lua print("T\n")]])) + end) +end) + +describe('debug.debug', function() + before_each(function() + screen = Screen.new() + screen:attach() + screen:set_default_attr_ids({ + [0] = {bold=true, foreground=255}, + E = {foreground = Screen.colors.Grey100, background = Screen.colors.Red}, + cr = {bold = true, foreground = Screen.colors.SeaGreen4}, + }) + end) + it('works', function() + command([[lua + function Test(a) + print(a) + debug.debug() + print(a * 100) + end + ]]) + feed(':lua Test()\n') + screen:expect([[ + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + nil | + lua_debug> ^ | + ]]) + feed('print("TEST")\n') + screen:expect([[ + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + nil | + lua_debug> print("TEST") | + TEST | + lua_debug> ^ | + ]]) + feed('<C-c>') + screen:expect([[ + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + nil | + lua_debug> print("TEST") | + TEST | + | + {E:E5105: Error while calling lua chunk: [string "<VimL }| + {E:compiled string>"]:5: attempt to perform arithmetic o}| + {E:n local 'a' (a nil value)} | + Interrupt: {cr:Press ENTER or type command to continue}^ | + ]]) + feed('<C-l>:lua Test()\n') + screen:expect([[ + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + nil | + lua_debug> ^ | + ]]) + feed('\n') + screen:expect([[ + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + nil | + lua_debug> | + {E:E5105: Error while calling lua chunk: [string "<VimL }| + {E:compiled string>"]:5: attempt to perform arithmetic o}| + {E:n local 'a' (a nil value)} | + {cr:Press ENTER or type command to continue}^ | + ]]) + end) +end) + +describe('package.path/package.cpath', function() + local sl = alter_slashes + + local function get_new_paths(sufs, runtimepaths) + runtimepaths = runtimepaths or meths.list_runtime_paths() + local new_paths = {} + local sep = package.config:sub(1, 1) + for _, v in ipairs(runtimepaths) do + for _, suf in ipairs(sufs) do + new_paths[#new_paths + 1] = v .. sep .. 'lua' .. suf + end + end + return new_paths + end + local function execute_lua(cmd, ...) + return meths.execute_lua(cmd, {...}) + end + local function eval_lua(expr, ...) + return meths.execute_lua('return ' .. expr, {...}) + end + local function set_path(which, value) + return execute_lua('package[select(1, ...)] = select(2, ...)', which, value) + end + + it('contains directories from &runtimepath on first invocation', function() + local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}) + local new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str)) + + local new_cpaths = get_new_paths(iswin() and {'\\?.dll'} or {'/?.so'}) + local new_cpaths_str = table.concat(new_cpaths, ';') + eq(new_cpaths_str, eval_lua('package.cpath'):sub(1, #new_cpaths_str)) + end) + it('puts directories from &runtimepath always at the start', function() + meths.set_option('runtimepath', 'a,b') + local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {'a', 'b'}) + local new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str)) + + set_path('path', sl'foo/?.lua;foo/?/init.lua;' .. new_paths_str) + + neq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str)) + + command('set runtimepath+=c') + new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {'a', 'b', 'c'}) + new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str)) + end) + it('understands uncommon suffixes', function() + set_path('cpath', './?/foo/bar/baz/x.nlua') + meths.set_option('runtimepath', 'a') + local new_paths = get_new_paths({'/?/foo/bar/baz/x.nlua'}, {'a'}) + local new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str, eval_lua('package.cpath'):sub(1, #new_paths_str)) + + set_path('cpath', './yyy?zzz/x') + meths.set_option('runtimepath', 'b') + new_paths = get_new_paths({'/yyy?zzz/x'}, {'b'}) + new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str, eval_lua('package.cpath'):sub(1, #new_paths_str)) + + set_path('cpath', './yyy?zzz/123?ghi/x') + meths.set_option('runtimepath', 'b') + new_paths = get_new_paths({'/yyy?zzz/123?ghi/x'}, {'b'}) + new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str, eval_lua('package.cpath'):sub(1, #new_paths_str)) + end) + it('preserves empty items', function() + local many_empty_path = ';;;;;;' + local many_empty_cpath = ';;;;;;./?.luaso' + set_path('path', many_empty_path) + set_path('cpath', many_empty_cpath) + meths.set_option('runtimepath', 'a') + local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {'a'}) + local new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str .. ';' .. many_empty_path, eval_lua('package.path')) + local new_cpaths = get_new_paths({'/?.luaso'}, {'a'}) + local new_cpaths_str = table.concat(new_cpaths, ';') + eq(new_cpaths_str .. ';' .. many_empty_cpath, eval_lua('package.cpath')) + end) + it('preserves empty value', function() + set_path('path', '') + meths.set_option('runtimepath', 'a') + local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {'a'}) + local new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str .. ';', eval_lua('package.path')) + end) + it('purges out all additions if runtimepath is set to empty', function() + local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}) + local new_paths_str = table.concat(new_paths, ';') + local path = eval_lua('package.path') + eq(new_paths_str, path:sub(1, #new_paths_str)) + + local new_cpaths = get_new_paths(iswin() and {'\\?.dll'} or {'/?.so'}) + local new_cpaths_str = table.concat(new_cpaths, ';') + local cpath = eval_lua('package.cpath') + eq(new_cpaths_str, cpath:sub(1, #new_cpaths_str)) + + meths.set_option('runtimepath', '') + eq(path:sub(#new_paths_str + 2, -1), eval_lua('package.path')) + eq(cpath:sub(#new_cpaths_str + 2, -1), eval_lua('package.cpath')) + end) + it('works with paths with escaped commas', function() + meths.set_option('runtimepath', '\\,') + local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {','}) + local new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str)) + end) + it('ignores paths with semicolons', function() + meths.set_option('runtimepath', 'foo;bar,\\,') + local new_paths = get_new_paths(sl{'/?.lua', '/?/init.lua'}, {','}) + local new_paths_str = table.concat(new_paths, ';') + eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str)) + end) +end) diff --git a/test/functional/options/defaults_spec.lua b/test/functional/options/defaults_spec.lua index f43d8eeafa..b83b7b8eee 100644 --- a/test/functional/options/defaults_spec.lua +++ b/test/functional/options/defaults_spec.lua @@ -8,6 +8,8 @@ local clear = helpers.clear local eval = helpers.eval local eq = helpers.eq local neq = helpers.neq +local mkdir = helpers.mkdir +local rmdir = helpers.rmdir local function init_session(...) local args = { helpers.nvim_prog, '-i', 'NONE', '--embed', @@ -23,13 +25,13 @@ describe('startup defaults', function() if helpers.pending_win32(pending) then return end local function expect_filetype(expected) - local screen = Screen.new(48, 4) + local screen = Screen.new(50, 4) screen:attach() command('filetype') screen:expect([[ - ^ | - ~ | - ~ | + ^ | + ~ | + ~ | ]]..expected ) end @@ -37,31 +39,49 @@ describe('startup defaults', function() it('enabled by `-u NORC`', function() init_session('-u', 'NORC') expect_filetype( - 'filetype detection:ON plugin:ON indent:ON |') + 'filetype detection:ON plugin:ON indent:ON |') end) it('disabled by `-u NONE`', function() init_session('-u', 'NONE') expect_filetype( - 'filetype detection:OFF plugin:OFF indent:OFF |') + 'filetype detection:OFF plugin:OFF indent:OFF |') end) it('overridden by early `filetype on`', function() init_session('-u', 'NORC', '--cmd', 'filetype on') expect_filetype( - 'filetype detection:ON plugin:OFF indent:OFF |') + 'filetype detection:ON plugin:OFF indent:OFF |') end) it('overridden by early `filetype plugin on`', function() init_session('-u', 'NORC', '--cmd', 'filetype plugin on') expect_filetype( - 'filetype detection:ON plugin:ON indent:OFF |') + 'filetype detection:ON plugin:ON indent:OFF |') end) it('overridden by early `filetype indent on`', function() init_session('-u', 'NORC', '--cmd', 'filetype indent on') expect_filetype( - 'filetype detection:ON plugin:OFF indent:ON |') + 'filetype detection:ON plugin:OFF indent:ON |') + end) + + it('adjusted by late `filetype off`', function() + init_session('-u', 'NORC', '-c', 'filetype off') + expect_filetype( + 'filetype detection:OFF plugin:(on) indent:(on) |') + end) + + it('adjusted by late `filetype plugin off`', function() + init_session('-u', 'NORC', '-c', 'filetype plugin off') + expect_filetype( + 'filetype detection:ON plugin:OFF indent:ON |') + end) + + it('adjusted by late `filetype indent off`', function() + init_session('-u', 'NORC', '-c', 'filetype indent off') + expect_filetype( + 'filetype detection:ON plugin:ON indent:OFF |') end) end) @@ -80,6 +100,11 @@ describe('startup defaults', function() init_session('-u', 'NORC', '--cmd', 'syntax off') eq(0, eval('exists("g:syntax_on")')) end) + + it('adjusted by late `syntax off`', function() + init_session('-u', 'NORC', '-c', 'syntax off') + eq(0, eval('exists("g:syntax_on")')) + end) end) describe('packpath', function() @@ -98,6 +123,56 @@ describe('startup defaults', function() it('v:progpath is set to the absolute path', function() eq(eval("fnamemodify(v:progpath, ':p')"), eval('v:progpath')) end) + + describe('$NVIM_LOG_FILE', function() + -- TODO(jkeyes): use stdpath('data') instead. + local datasubdir = helpers.iswin() and 'nvim-data' or 'nvim' + local xdgdir = 'Xtest-startup-xdg-logpath' + local xdgdatadir = xdgdir..'/'..datasubdir + after_each(function() + os.remove('Xtest-logpath') + rmdir(xdgdir) + end) + + it('is used if expansion succeeds', function() + clear({env={ + NVIM_LOG_FILE='Xtest-logpath', + }}) + eq('Xtest-logpath', eval('$NVIM_LOG_FILE')) + end) + it('defaults to stdpath("data")/log if empty', function() + eq(true, mkdir(xdgdir) and mkdir(xdgdatadir)) + clear({env={ + XDG_DATA_HOME=xdgdir, + NVIM_LOG_FILE='', -- Empty is invalid. + }}) + -- server_start() calls ELOG, which tickles log_path_init(). + pcall(command, 'call serverstart(serverlist()[0])') + + eq(xdgdir..'/'..datasubdir..'/log', string.gsub(eval('$NVIM_LOG_FILE'), '\\', '/')) + end) + it('defaults to stdpath("data")/log if invalid', function() + eq(true, mkdir(xdgdir) and mkdir(xdgdatadir)) + clear({env={ + XDG_DATA_HOME=xdgdir, + NVIM_LOG_FILE='.', -- Any directory is invalid. + }}) + -- server_start() calls ELOG, which tickles log_path_init(). + pcall(command, 'call serverstart(serverlist()[0])') + + eq(xdgdir..'/'..datasubdir..'/log', string.gsub(eval('$NVIM_LOG_FILE'), '\\', '/')) + end) + it('defaults to .nvimlog if stdpath("data") is invalid', function() + clear({env={ + XDG_DATA_HOME='Xtest-missing-xdg-dir', + NVIM_LOG_FILE='.', -- Any directory is invalid. + }}) + -- server_start() calls ELOG, which tickles log_path_init(). + pcall(command, 'call serverstart(serverlist()[0])') + + eq('.nvimlog', eval('$NVIM_LOG_FILE')) + end) + end) end) describe('XDG-based defaults', function() diff --git a/test/functional/options/pastetoggle_spec.lua b/test/functional/options/pastetoggle_spec.lua index ec3c60fe37..a1f86f73ff 100644 --- a/test/functional/options/pastetoggle_spec.lua +++ b/test/functional/options/pastetoggle_spec.lua @@ -6,32 +6,35 @@ local command = helpers.command local eq = helpers.eq local eval = helpers.eval local sleep = helpers.sleep +local expect = helpers.expect describe("'pastetoggle' option", function() before_each(function() clear() command('set nopaste') - command('set pastetoggle=a') end) + it("toggles 'paste'", function() - eq(eval('&paste'), 0) + command('set pastetoggle=a') + eq(0, eval('&paste')) feed('a') -- Need another key so that the vgetorpeek() function returns. feed('j') - eq(eval('&paste'), 1) + eq(1, eval('&paste')) end) - it("multiple key 'pastetoggle' is waited for", function() - eq(eval('&paste'), 0) - local pastetoggle = 'lllll' - command('set pastetoggle=' .. pastetoggle) - command('set timeoutlen=1 ttimeoutlen=10000') - feed(pastetoggle:sub(0, 2)) - -- sleep() for long enough that vgetorpeek() is gotten into, but short - -- enough that ttimeoutlen is not reached. - sleep(200) - feed(pastetoggle:sub(3, -1)) - -- Need another key so that the vgetorpeek() function returns. - feed('j') - eq(eval('&paste'), 1) + + + it('does not wait for timeout', function() + command('set pastetoggle=abc') + command('set ttimeoutlen=9999999') + eq(0, eval('&paste')) + -- n.b. need <esc> to return from vgetorpeek() + feed('abc<esc>') + eq(1, eval('&paste')) + feed('ab') + sleep(10) + feed('c<esc>') + expect('bc') + eq(1, eval('&paste')) end) end) diff --git a/test/functional/plugin/shada_spec.lua b/test/functional/plugin/shada_spec.lua index b543037ae2..639833071b 100644 --- a/test/functional/plugin/shada_spec.lua +++ b/test/functional/plugin/shada_spec.lua @@ -179,6 +179,7 @@ describe('In autoload/shada.vim', function() ' + n name \'@\'', ' + rc contents ["abc", "def"]', ' + rt type CHARACTERWISE', + ' + ru is_unnamed FALSE', ' + rw block width 10', ' + sb search backward TRUE', ' + sc smartcase value FALSE', @@ -204,6 +205,7 @@ describe('In autoload/shada.vim', function() 'rt': 0, 'rw': 10, 'rc': ['abc', 'def'], + 'ru': {'_TYPE': v:msgpack_types.boolean, '_VAL': 0}, 'n': 0x40, 'l': 10, 'c': 0, @@ -226,6 +228,8 @@ describe('In autoload/shada.vim', function() .. '0 (CHARACTERWISE), 1 (LINEWISE), 2 (BLOCKWISE)', ' + rt type 10', ' # Expected boolean', + ' + ru is_unnamed 10', + ' # Expected boolean', ' + sc smartcase value NIL', ' # Expected boolean', ' + sm magic value "TRUE"', @@ -240,6 +244,7 @@ describe('In autoload/shada.vim', function() 'sp': {'_TYPE': v:msgpack_types.string, '_VAL': ["abc"]}, 'rt': 10, 'rc': '10', + 'ru': 10, 'n': -0x40, 'l': -10, 'c': 'abc', @@ -636,6 +641,7 @@ describe('In autoload/shada.vim', function() ' # Required key missing: rc', ' + rw block width 0', ' + rt type CHARACTERWISE', + ' + ru is_unnamed FALSE', }, ([[ [{'type': 5, 'timestamp': 0, 'data': { }}] ]]):gsub('\n', '')) sd2strings_eq({ @@ -645,6 +651,7 @@ describe('In autoload/shada.vim', function() ' # Required key missing: rc', ' + rw block width 0', ' + rt type CHARACTERWISE', + ' + ru is_unnamed FALSE', }, ([[ [{'type': 5, 'timestamp': 0, 'data': { 'n': 0x20, }}] ]]):gsub('\n', '')) @@ -655,9 +662,11 @@ describe('In autoload/shada.vim', function() ' + rc contents ["abc", "def"]', ' + rw block width 0', ' + rt type CHARACTERWISE', + ' + ru is_unnamed FALSE', }, ([[ [{'type': 5, 'timestamp': 0, 'data': { 'n': 0x20, 'rc': ["abc", "def"], + 'ru': {'_TYPE': v:msgpack_types.boolean, '_VAL': 0}, }}] ]]):gsub('\n', '')) sd2strings_eq({ 'Register with timestamp ' .. epoch .. ':', @@ -668,9 +677,11 @@ describe('In autoload/shada.vim', function() ' | - "abcdefghijklmnopqrstuvwxyz"', ' + rw block width 0', ' + rt type CHARACTERWISE', + ' + ru is_unnamed TRUE', }, ([[ [{'type': 5, 'timestamp': 0, 'data': { 'n': 0x20, 'rc': ['abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'], + 'ru': {'_TYPE': v:msgpack_types.boolean, '_VAL': 1}, }}] ]]):gsub('\n', '')) sd2strings_eq({ 'Register with timestamp ' .. epoch .. ':', @@ -681,6 +692,7 @@ describe('In autoload/shada.vim', function() ' | - "abcdefghijklmnopqrstuvwxyz"', ' + rw block width 0', ' + rt type CHARACTERWISE', + ' + ru is_unnamed FALSE', }, ([[ [{'type': 5, 'timestamp': 0, 'data': { 'n': 0x20, 'rc': ['abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'], @@ -696,6 +708,7 @@ describe('In autoload/shada.vim', function() ' | - "abcdefghijklmnopqrstuvwxyz"', ' + rw block width 5', ' + rt type LINEWISE', + ' + ru is_unnamed FALSE', }, ([[ [{'type': 5, 'timestamp': 0, 'data': { 'n': 0x20, 'rc': ['abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'], @@ -712,11 +725,14 @@ describe('In autoload/shada.vim', function() ' # Expected integer', ' + rw block width ""', ' + rt type BLOCKWISE', + ' # Expected boolean', + ' + ru is_unnamed ""', }, ([[ [{'type': 5, 'timestamp': 0, 'data': { 'n': 0x20, 'rc': ['abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'], 'rw': "", 'rt': 2, + 'ru': "" }}] ]]):gsub('\n', '')) sd2strings_eq({ 'Register with timestamp ' .. epoch .. ':', @@ -729,11 +745,32 @@ describe('In autoload/shada.vim', function() ' # Unexpected enum value: expected one of 0 (CHARACTERWISE), ' .. '1 (LINEWISE), 2 (BLOCKWISE)', ' + rt type 10', + ' # Expected boolean', + ' + ru is_unnamed ["abc", "def"]', }, ([[ [{'type': 5, 'timestamp': 0, 'data': { 'n': 0x20, 'rc': 0, 'rw': -1, 'rt': 10, + 'ru': ['abc', 'def'], + }}] ]]):gsub('\n', '')) + sd2strings_eq({ + 'Register with timestamp ' .. epoch .. ':', + ' % Key Description Value', + ' + n name \' \'', + ' + rc contents @', + ' | - "abcdefghijklmnopqrstuvwxyz"', + ' | - "abcdefghijklmnopqrstuvwxyz"', + ' + rw block width 5', + ' + rt type LINEWISE', + ' # Expected boolean', + ' + ru is_unnamed 0', + }, ([[ [{'type': 5, 'timestamp': 0, 'data': { + 'n': 0x20, + 'rc': ['abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'], + 'rw': 5, + 'rt': 1, + 'ru': 0, }}] ]]):gsub('\n', '')) end) diff --git a/test/functional/provider/python3_spec.lua b/test/functional/provider/python3_spec.lua index 89a546675f..aa50f53451 100644 --- a/test/functional/provider/python3_spec.lua +++ b/test/functional/provider/python3_spec.lua @@ -3,14 +3,14 @@ local eval, command, feed = helpers.eval, helpers.command, helpers.feed local eq, clear, insert = helpers.eq, helpers.clear, helpers.insert local expect, write_file = helpers.expect, helpers.write_file local feed_command = helpers.feed_command +local missing_provider = helpers.missing_provider do clear() - command('let [g:interp, g:errors] = provider#pythonx#Detect(3)') - local errors = eval('g:errors') - if errors ~= '' then + local err = missing_provider('python3') + if err then pending( - 'Python 3 (or the Python 3 neovim module) is broken or missing:\n' .. errors, + 'Python 3 (or the Python 3 neovim module) is broken or missing:\n' .. err, function() end) return end diff --git a/test/functional/provider/python_spec.lua b/test/functional/provider/python_spec.lua index 94dfa90ea8..25f5e0a6d0 100644 --- a/test/functional/provider/python_spec.lua +++ b/test/functional/provider/python_spec.lua @@ -12,14 +12,14 @@ local command = helpers.command local exc_exec = helpers.exc_exec local write_file = helpers.write_file local curbufmeths = helpers.curbufmeths +local missing_provider = helpers.missing_provider do clear() - command('let [g:interp, g:errors] = provider#pythonx#Detect(2)') - local errors = meths.get_var('errors') - if errors ~= '' then + local err = missing_provider('python') + if err then pending( - 'Python 2 (or the Python 2 neovim module) is broken or missing:\n' .. errors, + 'Python 2 (or the Python 2 neovim module) is broken or missing:\n' .. err, function() end) return end diff --git a/test/functional/provider/ruby_spec.lua b/test/functional/provider/ruby_spec.lua index 7b0e17688d..9f5ef3b3fc 100644 --- a/test/functional/provider/ruby_spec.lua +++ b/test/functional/provider/ruby_spec.lua @@ -10,13 +10,11 @@ local expect = helpers.expect local command = helpers.command local write_file = helpers.write_file local curbufmeths = helpers.curbufmeths +local missing_provider = helpers.missing_provider do clear() - command('let g:prog = provider#ruby#Detect()') - local prog = meths.get_var('prog') - - if prog == '' then + if missing_provider('ruby') then pending( "Cannot find the neovim RubyGem. Try :CheckHealth", function() end) diff --git a/test/functional/shada/registers_spec.lua b/test/functional/shada/registers_spec.lua index fc812f799c..71af14aba8 100644 --- a/test/functional/shada/registers_spec.lua +++ b/test/functional/shada/registers_spec.lua @@ -148,4 +148,40 @@ describe('ShaDa support code', function() eq({{'\171«'}, 'v'}, getreg('e')) end) + it('has a blank unnamed register if it wasn\'t set and register 0 is empty', + function() + setreg('1', {'one'}, 'c') + setreg('2', {'two'}, 'c') + setreg('a', {'a'}, 'c') + nvim_command('qall') + reset() + eq({{}, ''}, getreg('0')) + eq({{'one'}, 'v'}, getreg('1')) + eq({{}, ''}, getreg('"')) + eq({{'a'}, 'v'}, getreg('a')) + end) + + it('defaults the unnamed register to register 0 if it wasn\'t set', + function() + setreg('0', {'zero'}, 'c') + setreg('1', {'one'}, 'c') + setreg('2', {'two'}, 'c') + nvim_command('qall') + reset() + eq({{'zero'}, 'v'}, getreg('0')) + eq({{'one'}, 'v'}, getreg('1')) + eq({{'zero'}, 'v'}, getreg('"')) + end) + + it('remembers which register was the unnamed register when loading', + function() + setreg('0', {'zero'}, 'c') + setreg('1', {'one'}, 'cu') + setreg('2', {'two'}, 'c') + nvim_command('qall') + reset() + eq({{'zero'}, 'v'}, getreg('0')) + eq({{'one'}, 'v'}, getreg('1')) + eq({{'one'}, 'v'}, getreg('"')) + end) end) diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index b14bceecdd..3ed63f68e9 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -356,9 +356,17 @@ describe("tui 't_Co' (terminal colors)", function() assert_term_colors("yet-another-term", "screen-256color", 256) end) - it("TERM=linux uses 8 colors", function() + it("TERM=linux uses 256 colors", function() if is_linux then - assert_term_colors("linux", nil, 8) + assert_term_colors("linux", nil, 256) + else + pending() + end + end) + + it("TERM=linux-16color uses 256 colors", function() + if is_linux then + assert_term_colors("linux-16color", nil, 256) else pending() end diff --git a/test/functional/ui/cursor_spec.lua b/test/functional/ui/cursor_spec.lua index abe0e0b1fd..b47210a777 100644 --- a/test/functional/ui/cursor_spec.lua +++ b/test/functional/ui/cursor_spec.lua @@ -20,102 +20,102 @@ describe('ui/cursor', function() it("'guicursor' is published as a UI event", function() local expected_mode_info = { [1] = { - blinkoff = 250, - blinkon = 400, - blinkwait = 700, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, cell_percentage = 0, cursor_shape = 'block', name = 'normal', - hl_id = 46, - id_lm = 47, + hl_id = 0, + id_lm = 0, mouse_shape = 0, short_name = 'n' }, [2] = { - blinkoff = 250, - blinkon = 400, - blinkwait = 700, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, cell_percentage = 0, cursor_shape = 'block', name = 'visual', - hl_id = 46, - id_lm = 47, + hl_id = 0, + id_lm = 0, mouse_shape = 0, short_name = 'v' }, [3] = { - blinkoff = 250, - blinkon = 400, - blinkwait = 700, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, cell_percentage = 25, cursor_shape = 'vertical', name = 'insert', - hl_id = 46, - id_lm = 47, + hl_id = 0, + id_lm = 0, mouse_shape = 0, short_name = 'i' }, [4] = { - blinkoff = 250, - blinkon = 400, - blinkwait = 700, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, cell_percentage = 20, cursor_shape = 'horizontal', name = 'replace', - hl_id = 46, - id_lm = 47, + hl_id = 0, + id_lm = 0, mouse_shape = 0, short_name = 'r' }, [5] = { - blinkoff = 250, - blinkon = 400, - blinkwait = 700, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, cell_percentage = 0, cursor_shape = 'block', name = 'cmdline_normal', - hl_id = 46, - id_lm = 47, + hl_id = 0, + id_lm = 0, mouse_shape = 0, short_name = 'c' }, [6] = { - blinkoff = 250, - blinkon = 400, - blinkwait = 700, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, cell_percentage = 25, cursor_shape = 'vertical', name = 'cmdline_insert', - hl_id = 46, - id_lm = 47, + hl_id = 0, + id_lm = 0, mouse_shape = 0, short_name = 'ci' }, [7] = { - blinkoff = 250, - blinkon = 400, - blinkwait = 700, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, cell_percentage = 20, cursor_shape = 'horizontal', name = 'cmdline_replace', - hl_id = 46, - id_lm = 47, + hl_id = 0, + id_lm = 0, mouse_shape = 0, short_name = 'cr' }, [8] = { - blinkoff = 250, - blinkon = 400, - blinkwait = 700, - cell_percentage = 50, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, + cell_percentage = 20, cursor_shape = 'horizontal', name = 'operator', - hl_id = 46, - id_lm = 46, + hl_id = 0, + id_lm = 0, mouse_shape = 0, short_name = 'o' }, [9] = { - blinkoff = 250, - blinkon = 400, - blinkwait = 700, - cell_percentage = 35, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, + cell_percentage = 25, cursor_shape = 'vertical', name = 'visual_select', - hl_id = 46, - id_lm = 46, + hl_id = 0, + id_lm = 0, mouse_shape = 0, short_name = 've' }, [10] = { @@ -147,19 +147,19 @@ describe('ui/cursor', function() mouse_shape = 0, short_name = 'ml' }, [17] = { - blinkoff = 150, - blinkon = 175, - blinkwait = 175, + blinkoff = 0, + blinkon = 0, + blinkwait = 0, cell_percentage = 0, cursor_shape = 'block', name = 'showmatch', - hl_id = 46, - id_lm = 46, + hl_id = 0, + id_lm = 0, short_name = 'sm' }, } screen:expect(function() - -- Default 'guicursor' published on startup. + -- Default 'guicursor', published on startup. eq(expected_mode_info, screen._mode_info) eq(true, screen._cursor_style_enabled) eq('normal', screen.mode) @@ -179,20 +179,53 @@ describe('ui/cursor', function() end) -- Change the cursor style. - meths.set_option('guicursor', 'n-v-c:ver35-blinkwait171-blinkoff172-blinkon173,ve:hor35,o:ver50,i-ci:block,r-cr:hor90,sm:ver42') + helpers.command('set guicursor=n-v-c:block,i-ci-ve:ver25,r-cr-o:hor20' + ..',a:blinkwait700-blinkoff400-blinkon250-Cursor/lCursor' + ..',sm:block-blinkwait175-blinkoff150-blinkon175') + + -- Update the expected values. + for _, m in ipairs(expected_mode_info) do + if m.name == 'showmatch' then + if m.blinkon then m.blinkon = 175 end + if m.blinkoff then m.blinkoff = 150 end + if m.blinkwait then m.blinkwait = 175 end + else + if m.blinkon then m.blinkon = 250 end + if m.blinkoff then m.blinkoff = 400 end + if m.blinkwait then m.blinkwait = 700 end + end + if m.hl_id then m.hl_id = 48 end + if m.id_lm then m.id_lm = 49 end + end + + -- Assert the new expectation. + screen:expect(function() + eq(expected_mode_info, screen._mode_info) + eq(true, screen._cursor_style_enabled) + eq('normal', screen.mode) + end) + + -- Another cursor style. + meths.set_option('guicursor', 'n-v-c:ver35-blinkwait171-blinkoff172-blinkon173' + ..',ve:hor35,o:ver50,i-ci:block,r-cr:hor90,sm:ver42') screen:expect(function() local named = {} for _, m in ipairs(screen._mode_info) do named[m.name] = m end eq('vertical', named.normal.cursor_shape) + eq(35, named.normal.cell_percentage) eq('horizontal', named.visual_select.cursor_shape) + eq(35, named.visual_select.cell_percentage) eq('vertical', named.operator.cursor_shape) + eq(50, named.operator.cell_percentage) eq('block', named.insert.cursor_shape) eq('vertical', named.showmatch.cursor_shape) + eq(90, named.cmdline_replace.cell_percentage) eq(171, named.normal.blinkwait) eq(172, named.normal.blinkoff) eq(173, named.normal.blinkon) + eq(42, named.showmatch.cell_percentage) end) end) diff --git a/test/functional/ui/highlight_spec.lua b/test/functional/ui/highlight_spec.lua index 2bda907c33..d1357ea525 100644 --- a/test/functional/ui/highlight_spec.lua +++ b/test/functional/ui/highlight_spec.lua @@ -3,8 +3,9 @@ local Screen = require('test.functional.ui.screen') local os = require('os') local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert local command = helpers.command -local eval = helpers.eval +local eval, exc_exec = helpers.eval, helpers.exc_exec local feed_command, request, eq = helpers.feed_command, helpers.request, helpers.eq +local curbufmeths = helpers.curbufmeths describe('colorscheme compatibility', function() before_each(function() @@ -650,3 +651,415 @@ describe("'listchars' highlight", function() ]]) end) end) + +describe("'winhighlight' highlight", function() + local screen + + before_each(function() + clear() + screen = Screen.new(20,8) + screen:attach() + screen:set_default_attr_ids({ + [0] = {bold=true, foreground=Screen.colors.Blue}, + [1] = {background = Screen.colors.DarkBlue}, + [2] = {background = Screen.colors.DarkBlue, bold = true, foreground = Screen.colors.Blue1}, + [3] = {bold = true, reverse = true}, + [4] = {reverse = true}, + [5] = {background = Screen.colors.DarkGreen}, + [6] = {background = Screen.colors.DarkGreen, bold = true, foreground = Screen.colors.Blue1}, + [7] = {background = Screen.colors.DarkMagenta}, + [8] = {background = Screen.colors.DarkMagenta, bold = true, foreground = Screen.colors.Blue1}, + [9] = {foreground = Screen.colors.Brown}, + [10] = {foreground = Screen.colors.Brown, background = Screen.colors.DarkBlue}, + [11] = {background = Screen.colors.DarkBlue, bold = true, reverse = true}, + [12] = {background = Screen.colors.DarkGreen, reverse = true}, + [13] = {background = Screen.colors.Magenta4, reverse = true}, + [14] = {background = Screen.colors.DarkBlue, reverse = true}, + [15] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red}, + [16] = {foreground = Screen.colors.Blue1}, + [17] = {background = Screen.colors.LightRed}, + [18] = {background = Screen.colors.Gray90}, + [19] = {foreground = Screen.colors.LightGrey, background = Screen.colors.DarkGray}, + [20] = {background = Screen.colors.LightGrey, underline = true}, + [21] = {bold = true}, + [22] = {bold = true, foreground = Screen.colors.SeaGreen4}, + [23] = {background = Screen.colors.LightMagenta}, + [24] = {background = Screen.colors.WebGray}, + }) + command("hi Background1 guibg=DarkBlue") + command("hi Background2 guibg=DarkGreen") + end) + + it('works for background color', function() + insert("aa") + command("split") + command("set winhl=Normal:Background1") + screen:expect([[ + {1:a^a }| + {2:~ }| + {2:~ }| + {11:[No Name] [+] }| + aa | + {0:~ }| + {4:[No Name] [+] }| + | + ]]) + + command("enew") + screen:expect([[ + {1:^ }| + {2:~ }| + {2:~ }| + {11:[No Name] }| + aa | + {0:~ }| + {4:[No Name] [+] }| + | + ]]) + end) + + it('handles invalid values', function() + command("set winhl=Normal:Background1") + screen:expect([[ + {1:^ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + | + ]]) + + eq('Vim(set):E474: Invalid argument: winhl=xxx:yyy', + exc_exec("set winhl=xxx:yyy")) + eq('Normal:Background1', eval('&winhl')) + screen:expect([[ + {1:^ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + | + ]]) + end) + + + it('works local to the buffer', function() + insert("aa") + command("split") + command("setlocal winhl=Normal:Background1") + screen:expect([[ + {1:a^a }| + {2:~ }| + {2:~ }| + {11:[No Name] [+] }| + aa | + {0:~ }| + {4:[No Name] [+] }| + | + ]]) + + command("enew") + screen:expect([[ + ^ | + {0:~ }| + {0:~ }| + {3:[No Name] }| + aa | + {0:~ }| + {4:[No Name] [+] }| + | + ]]) + + command("bnext") + screen:expect([[ + {1:^aa }| + {2:~ }| + {2:~ }| + {11:[No Name] [+] }| + aa | + {0:~ }| + {4:[No Name] [+] }| + <f 1 --100%-- col 1 | + ]]) + end) + + it('for inactive window background works', function() + command("set winhl=Normal:Background1,NormalNC:Background2") + -- tests global value is copied across split + command("split") + screen:expect([[ + {1:^ }| + {2:~ }| + {2:~ }| + {11:[No Name] }| + {5: }| + {6:~ }| + {12:[No Name] }| + | + ]]) + + feed("<c-w><c-w>") + screen:expect([[ + {5: }| + {6:~ }| + {6:~ }| + {12:[No Name] }| + {1:^ }| + {2:~ }| + {11:[No Name] }| + | + ]]) + + feed("<c-w><c-w>") + screen:expect([[ + {1:^ }| + {2:~ }| + {2:~ }| + {11:[No Name] }| + {5: }| + {6:~ }| + {12:[No Name] }| + | + ]]) + end) + + it('works with NormalNC', function() + command("hi NormalNC guibg=DarkMagenta") + -- tests global value is copied across split + command("split") + screen:expect([[ + ^ | + {0:~ }| + {0:~ }| + {3:[No Name] }| + {7: }| + {8:~ }| + {13:[No Name] }| + | + ]]) + + command("wincmd w") + screen:expect([[ + {7: }| + {8:~ }| + {8:~ }| + {13:[No Name] }| + ^ | + {0:~ }| + {3:[No Name] }| + | + ]]) + + + -- winbg=Normal:... overrides global NormalNC + command("set winhl=Normal:Background1") + screen:expect([[ + {7: }| + {8:~ }| + {8:~ }| + {13:[No Name] }| + {1:^ }| + {2:~ }| + {11:[No Name] }| + | + ]]) + + command("wincmd w") + screen:expect([[ + ^ | + {0:~ }| + {0:~ }| + {3:[No Name] }| + {1: }| + {2:~ }| + {14:[No Name] }| + | + ]]) + + command("wincmd w") + command("set winhl=Normal:Background1,NormalNC:Background2") + screen:expect([[ + {7: }| + {8:~ }| + {8:~ }| + {13:[No Name] }| + {1:^ }| + {2:~ }| + {11:[No Name] }| + | + ]]) + + command("wincmd w") + screen:expect([[ + ^ | + {0:~ }| + {0:~ }| + {3:[No Name] }| + {5: }| + {6:~ }| + {12:[No Name] }| + | + ]]) + end) + + it('background applies also to non-text', function() + insert('Lorem ipsum dolor sit amet ') + command('set shiftwidth=2') + feed('>>') + command('set number') + command('set breakindent') + command('set briopt=shift:5,min:0') + command('set list') + command('set showbreak=↪') + screen:expect([[ + {9: 1 } ^Lorem ipsum do| + {9: } {0:↪}lor sit | + {9: } {0:↪}amet{0:-} | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]) + + command('set winhl=Normal:Background1') + screen:expect([[ + {10: 1 }{1: ^Lorem ipsum do}| + {10: }{1: }{2:↪}{1:lor sit }| + {10: }{1: }{2:↪}{1:amet}{2:-}{1: }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + | + ]]) + + command('set nowrap') + command('set listchars+=extends:❯,precedes:❮') + feed('3w') + screen:expect([[ + {10: 1 }{2:❮}{1: dolor ^sit ame}{2:❯}| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + {2:~ }| + | + ]]) + end) + + it('can override NonText, Conceal and EndOfBuffer', function() + curbufmeths.set_lines(0,-1,true, {"raa\000"}) + command('call matchaddpos("Conceal", [[1,2]], 0, -1, {"conceal": "#"})') + command('set cole=2 cocu=nvic') + command('split') + command('call matchaddpos("Conceal", [[1,2]], 0, -1, {"conceal": "#"})') + command('set winhl=SpecialKey:ErrorMsg,EndOfBuffer:Background1,' + ..'Conceal:Background2') + + screen:expect([[ + ^r{5:#}a{15:^@} | + {1:~ }| + {1:~ }| + {3:[No Name] [+] }| + r{19:#}a{16:^@} | + {0:~ }| + {4:[No Name] [+] }| + | + ]]) + end) + + it('can override LineNr, CursorColumn and ColorColumn', function() + insert('very text\nmore text') + command('set number') + command('set colorcolumn=2') + command('set cursorcolumn') + + command('split') + command('set winhl=LineNr:Background1,CursorColumn:Background2,' + ..'ColorColumn:ErrorMsg') + screen:expect([[ + {1: 1 }v{15:e}ry tex{5:t} | + {1: 2 }m{15:o}re tex^t | + {0:~ }| + {3:[No Name] [+] }| + {9: 1 }v{17:e}ry tex{18:t} | + {9: 2 }m{17:o}re text | + {4:[No Name] [+] }| + | + ]]) + end) + + it('can override Tabline', function() + command('tabnew') + command('set winhl=TabLine:Background1,TabLineSel:ErrorMsg') + + screen:expect([[ + {20: No Name] }{15: No Name]}{20:X}| + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]) + command("tabnext") + screen:expect([[ + {21: No Name] }{1: No Name]}{20:X}| + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]) + end) + + it('can override popupmenu', function() + insert('word wording wordy') + command('split') + command('set winhl=Pmenu:Background1,PmenuSel:Background2,' + ..'PmenuSbar:ErrorMsg,PmenuThumb:Normal') + screen:expect([[ + word wording word^y | + {0:~ }| + {0:~ }| + {3:[No Name] [+] }| + word wording wordy | + {0:~ }| + {4:[No Name] [+] }| + | + ]]) + + feed('oword<c-x><c-p>') + screen:expect([[ + word wording wordy | + wordy^ | + {1:word }{0: }| + {1:wording }{3: }| + {5:wordy }rdy | + wordy | + {4:[No Name] [+] }| + {21:-- }{22:match 1 of 3} | + ]]) + + feed('<esc>u<c-w><c-w>oword<c-x><c-p>') + screen:expect([[ + word wording wordy | + wordy | + {23:word }{0: }| + {23:wording }{4: }| + {24:wordy }rdy | + wordy^ | + {3:[No Name] [+] }| + {21:-- }{22:match 1 of 3} | + ]]) + end) +end) diff --git a/test/functional/ui/inccommand_spec.lua b/test/functional/ui/inccommand_spec.lua index a7be1a9dc8..8bdc4601c0 100644 --- a/test/functional/ui/inccommand_spec.lua +++ b/test/functional/ui/inccommand_spec.lua @@ -805,6 +805,7 @@ describe(":substitute, inccommand=split", function() it('does not show split window for :s/', function() feed("2gg") feed(":s/tw") + screen:sleep(1) screen:expect([[ Inc substitution on | two lines | diff --git a/test/functional/ui/screen.lua b/test/functional/ui/screen.lua index 7d9cd6c026..5408e1e195 100644 --- a/test/functional/ui/screen.lua +++ b/test/functional/ui/screen.lua @@ -198,8 +198,9 @@ function Screen:expect(expected, attr_ids, attr_ignore, condition, any) condition = expected expected = nil else - -- Remove the last line and dedent. - expected = dedent(expected:gsub('\n[ ]+$', '')) + -- Remove the last line and dedent. Note that gsub returns more then one + -- value. + expected = dedent(expected:gsub('\n[ ]+$', ''), 0) for row in expected:gmatch('[^\n]+') do row = row:sub(1, #row - 1) -- Last char must be the screen delimiter. table.insert(expected_rows, row) diff --git a/test/functional/ui/screen_basic_spec.lua b/test/functional/ui/screen_basic_spec.lua index d9cb3d7b6f..bfcdc7f652 100644 --- a/test/functional/ui/screen_basic_spec.lua +++ b/test/functional/ui/screen_basic_spec.lua @@ -6,7 +6,7 @@ local insert = helpers.insert local eq = helpers.eq local eval = helpers.eval -describe('Initial screen', function() +describe('screen', function() local screen local nvim_argv = {helpers.nvim_prog, '-u', 'NONE', '-i', 'NONE', '-N', '--cmd', 'set shortmess+=I background=light noswapfile belloff= noshowcmd noruler', @@ -27,7 +27,7 @@ describe('Initial screen', function() screen:detach() end) - it('is the default initial screen', function() + it('default initial screen', function() screen:expect([[ ^ | {0:~ }| @@ -566,11 +566,61 @@ describe('Screen', function() end) end) - it('nvim_ui_attach() handles very large width/height #2180', function() - screen:detach() - screen = Screen.new(999, 999) + describe('press enter', function() + it('does not crash on <F1> at “Press ENTER”', function() + command('nnoremap <F1> :echo "TEST"<CR>') + feed(':ls<CR>') + screen:expect([[ + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + :ls | + 1 %a "[No Name]" line 1 | + {7:Press ENTER or type command to continue}^ | + ]]) + feed('<F1>') + screen:expect([[ + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + TEST | + ]]) + end) + end) +end) + +describe('nvim_ui_attach()', function() + before_each(function() + clear() + end) + it('handles very large width/height #2180', function() + local screen = Screen.new(999, 999) screen:attach() eq(999, eval('&lines')) eq(999, eval('&columns')) end) + it('invalid option returns error', function() + local screen = Screen.new() + local status, rv = pcall(function() screen:attach({foo={'foo'}}) end) + eq(false, status) + eq('No such ui option', rv:match("No such .*")) + end) end) diff --git a/test/functional/ui/tabline_spec.lua b/test/functional/ui/tabline_spec.lua new file mode 100644 index 0000000000..56331a33b5 --- /dev/null +++ b/test/functional/ui/tabline_spec.lua @@ -0,0 +1,57 @@ +local helpers = require('test.functional.helpers')(after_each) +local Screen = require('test.functional.ui.screen') +local clear, command, eq = helpers.clear, helpers.command, helpers.eq + +describe('ui/tabline', function() + local screen + local event_tabs, event_curtab + + before_each(function() + clear() + screen = Screen.new(25, 5) + screen:attach({rgb=true, ext_tabline=true}) + screen:set_on_event_handler(function(name, data) + if name == "tabline_update" then + event_curtab, event_tabs = unpack(data) + end + end) + end) + + after_each(function() + screen:detach() + end) + + describe('externalized', function() + it('publishes UI events', function() + command("tabedit another-tab") + + local expected_tabs = { + {tab = { id = 1 }, name = '[No Name]'}, + {tab = { id = 2 }, name = 'another-tab'}, + } + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + | + ]], nil, nil, function() + eq({ id = 2 }, event_curtab) + eq(expected_tabs, event_tabs) + end) + + command("tabNext") + screen:expect([[ + ^ | + ~ | + ~ | + ~ | + | + ]], nil, nil, function() + eq({ id = 1 }, event_curtab) + eq(expected_tabs, event_tabs) + end) + + end) + end) +end) diff --git a/test/functional/ui/wildmode_spec.lua b/test/functional/ui/wildmode_spec.lua index 6639bf272d..052cdd55a1 100644 --- a/test/functional/ui/wildmode_spec.lua +++ b/test/functional/ui/wildmode_spec.lua @@ -31,6 +31,27 @@ describe("'wildmode'", function() :sign define^ | ]]) end) + + it('does not crash after cycling back to original text', function() + command('set wildmode=full') + feed(':j<Tab><Tab><Tab>') + screen:expect([[ + | + ~ | + ~ | + join jumps | + :j^ | + ]]) + -- This would cause nvim to crash before #6650 + feed('<BS><Tab>') + screen:expect([[ + | + ~ | + ~ | + ! # & < = > @ > | + :!^ | + ]]) + end) end) end) diff --git a/test/functional/viml/completion_spec.lua b/test/functional/viml/completion_spec.lua index b35e8d4f94..0e5278345c 100644 --- a/test/functional/viml/completion_spec.lua +++ b/test/functional/viml/completion_spec.lua @@ -868,13 +868,13 @@ describe('completion', function() end) end) -describe('External completion popupmenu', function() +describe('ui/externalized/popupmenu', function() local screen local items, selected, anchor before_each(function() clear() screen = Screen.new(60, 8) - screen:attach({rgb=true, popupmenu_external=true}) + screen:attach({rgb=true, ext_popupmenu=true}) screen:set_default_attr_ids({ [1] = {bold=true, foreground=Screen.colors.Blue}, [2] = {bold = true}, diff --git a/test/functional/viml/function_spec.lua b/test/functional/viml/function_spec.lua index 776e760aaf..0cf92f7d40 100644 --- a/test/functional/viml/function_spec.lua +++ b/test/functional/viml/function_spec.lua @@ -1,29 +1,221 @@ local helpers = require('test.functional.helpers')(after_each) -local clear = helpers.clear local eq = helpers.eq -local exc_exec = helpers.exc_exec +local clear = helpers.clear +local funcs = helpers.funcs +local dedent = helpers.dedent +local redir_exec = helpers.redir_exec -describe('Up to MAX_FUNC_ARGS arguments are handled by', function() - local max_func_args = 20 -- from eval.h - local range = helpers.funcs.range +before_each(clear) - before_each(clear) +local function check_nofunc(fname) + eq(0, funcs.exists('*' .. fname)) +end - it('printf()', function() - local printf = helpers.funcs.printf - local rep = helpers.funcs['repeat'] - local expected = '2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,' - eq(expected, printf(rep('%d,', max_func_args-1), unpack(range(2, max_func_args)))) - local ret = exc_exec('call printf("", 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)') - eq('Vim(call):E740: Too many arguments for function printf', ret) - end) +local function check_func(fname, body, indent) + if type(body) == 'number' then + body = ('return %i'):format(body) + end + eq(dedent(([[ - it('rpcnotify()', function() - local rpcnotify = helpers.funcs.rpcnotify - local ret = rpcnotify(0, 'foo', unpack(range(3, max_func_args))) - eq(1, ret) - ret = exc_exec('call rpcnotify(0, "foo", 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)') - eq('Vim(call):E740: Too many arguments for function rpcnotify', ret) + function %s()%s + endfunction]] + ), 3):format( + fname, + body and ('\n1' .. (' '):rep(2 + (indent or 8)) .. body) or ''), + redir_exec('function ' .. fname)) +end + +describe(':endfunction', function() + it('accepts bang', function() + eq('', redir_exec([[ + function F() + endfunction! + ]])) + check_func('F') + eq('', redir_exec([[ + function! F() + return 1 + endfunction! + ]])) + check_func('F', 1) + end) + it('accepts comments', function() + eq('', redir_exec([[ + function F1() + endfunction " Comment + ]])) + check_func('F1') + eq('', redir_exec([[ + function F2() + endfunction " }}} + ]])) + check_func('F2') + eq('', redir_exec([[ + function F3() + endfunction " F3 + ]])) + check_func('F3') + eq('', redir_exec([[ + function F4() + endfunction! " F4 + ]])) + check_func('F4') + eq('', redir_exec([[ + function! F4() + return 2 + endfunction! " F4 + ]])) + check_func('F4', 2) + end) + it('accepts function name', function() + eq('', redir_exec([[ + function F0() + endfunction F0 + ]])) + check_func('F0') + eq('', redir_exec([[ + function F1() + endfunction! F1 + ]])) + check_func('F1') + eq('', redir_exec([[ + function! F2() + endfunction! F2 + ]])) + check_func('F2') + eq('', redir_exec([[ + function! F2() + return 3 + endfunction! F2 + ]])) + check_func('F2', 3) + end) + it('accepts weird characters', function() + eq('', redir_exec([[ + function F1() + endfunction: }}} + ]])) + check_func('F1') + -- From accurev + eq('', redir_exec([[ + function F2() + endfunction :}}} + ]])) + check_func('F2') + -- From cream-vimabbrev + eq('', redir_exec([[ + function F3() + endfunction 1}}} + ]])) + check_func('F3') + -- From pyunit + eq('', redir_exec([[ + function F4() + endfunction # }}} + ]])) + check_func('F4') + -- From vim-lldb + eq('', redir_exec([[ + function F5() + endfunction() + ]])) + check_func('F5') + -- From vim-mail + eq('', redir_exec([[ + function F6() + endfunction; + ]])) + check_func('F6') + end) + it('accepts commented bar', function() + eq('', redir_exec([[ + function F1() + endfunction " F1 | echo 42 + ]])) + check_func('F1') + eq('', redir_exec([[ + function! F1() + return 42 + endfunction! " F1 | echo 42 + ]])) + check_func('F1', 42) + end) + it('errors out on an uncommented bar', function() + eq('\nE488: Trailing characters: | echo 42', redir_exec([[ + function F1() + endfunction | echo 42 + ]])) + check_nofunc('F1') + end) + it('allows running multiple commands', function() + eq('\n2', redir_exec([[ + function F1() + echo 2 + endfunction + call F1() + ]])) + check_func('F1', 'echo 2') + eq('\n2\n3\n4', redir_exec([[ + function F2() + echo 2 + endfunction F2 + function F3() + echo 3 + endfunction " F3 + function! F4() + echo 4 + endfunction! + call F2() + call F3() + call F4() + ]])) + check_func('F2', 'echo 2') + check_func('F3', 'echo 3') + check_func('F4', 'echo 4') + end) + it('allows running multiple commands with only one character in between', + function() + eq('\n3', redir_exec(dedent([[ + function! F1() + echo 3 + endfunction! + call F1()]]))) + check_func('F1', 'echo 3', 2) + eq('\n4', redir_exec(dedent([[ + function F5() + echo 4 + endfunction + call F5()]]))) + check_func('F5', 'echo 4', 2) + eq('\n5', redir_exec(dedent([[ + function F6() + echo 5 + endfunction " TEST + call F6()]]))) + check_func('F6', 'echo 5', 2) + eq('\n6', redir_exec(dedent([[ + function F7() + echo 6 + endfunction F7 + call F7()]]))) + check_func('F7', 'echo 6', 2) + eq('\n2\n3\n4', redir_exec(dedent([[ + function F2() + echo 2 + endfunction F2 + function F3() + echo 3 + endfunction " F3 + function! F4() + echo 4 + endfunction! + call F2() + call F3() + call F4()]]))) + check_func('F2', 'echo 2', 2) + check_func('F3', 'echo 3', 2) + check_func('F4', 'echo 4', 2) end) end) +-- vim: foldmarker=▶,▲ |