diff options
Diffstat (limited to 'test/functional/helpers.lua')
-rw-r--r-- | test/functional/helpers.lua | 94 |
1 files changed, 57 insertions, 37 deletions
diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index 6c3f5190c9..b758817b41 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -6,23 +6,33 @@ local Session = require('nvim.session') local nvim_prog = os.getenv('NVIM_PROG') or 'build/bin/nvim' local nvim_argv = {nvim_prog, '-u', 'NONE', '-i', 'NONE', '-N', '--embed'} +local prepend_argv if os.getenv('VALGRIND') then local log_file = os.getenv('VALGRIND_LOG') or 'valgrind-%p.log' - local valgrind_argv = {'valgrind', '-q', '--tool=memcheck', - '--leak-check=yes', '--track-origins=yes', - '--show-possibly-lost=no', - '--suppressions=.valgrind.supp', - '--log-file='..log_file} - if os.getenv('VALGRIND_GDB') then - table.insert(valgrind_argv, '--vgdb=yes') - table.insert(valgrind_argv, '--vgdb-error=0') + prepend_argv = {'valgrind', '-q', '--tool=memcheck', + '--leak-check=yes', '--track-origins=yes', + '--show-possibly-lost=no', + '--suppressions=.valgrind.supp', + '--log-file='..log_file} + if os.getenv('GDB') then + table.insert(prepend_argv, '--vgdb=yes') + table.insert(prepend_argv, '--vgdb-error=0') end - local len = #valgrind_argv +elseif os.getenv('GDB') then + local gdbserver_port = '7777' + if os.getenv('GDBSERVER_PORT') then + gdbserver_port = os.getenv('GDBSERVER_PORT') + end + prepend_argv = {'gdbserver', 'localhost:'..gdbserver_port} +end + +if prepend_argv then + local len = #prepend_argv for i = 1, #nvim_argv do - valgrind_argv[i + len] = nvim_argv[i] + prepend_argv[i + len] = nvim_argv[i] end - nvim_argv = valgrind_argv + nvim_argv = prepend_argv end local session, loop_running, loop_stopped, last_error @@ -42,7 +52,6 @@ local function request(method, ...) if not loop_stopped then -- Except when the loop has been stopped by a notification triggered -- by the initial request, for example. - session:request('vim_eval', '1') end return rv end @@ -99,25 +108,20 @@ local function nvim_eval(expr) return request('vim_eval', expr) end -local function nvim_feed(input, mode) - mode = mode or '' - request('vim_feedkeys', input, mode) -end - -local function buffer_slice(start, stop, buffer_idx) - local include_end = false - if not stop then - stop = -1 - include_end = true +local function nvim_feed(input) + while #input > 0 do + local written = request('vim_input', input) + input = input:sub(written + 1) end - local buffer = request('vim_get_buffers')[buffer_idx or 1] - local slice = request('buffer_get_line_slice', buffer, start or 0, stop, - true, include_end) - return table.concat(slice, '\n') end local function nvim_replace_termcodes(input) - return request('vim_replace_termcodes', input, false, true, true) + -- small hack to stop <C-@> from being replaced by the internal + -- representation(which is different and won't work for vim_input) + local temp_replacement = 'CCCCCCCCC@@@@@@@@@@' + input = input:gsub('<[Cc][-]@>', temp_replacement) + local rv = request('vim_replace_termcodes', input, false, true, true) + return rv:gsub(temp_replacement, '\000') end local function dedent(str) @@ -150,13 +154,14 @@ end local function rawfeed(...) for _, v in ipairs({...}) do - nvim_feed(dedent(v), 'nt') + nvim_feed(dedent(v)) end end local function clear() if session then session:request('vim_command', 'qa!') + session._async_session._msgpack_stream._loop:exit() end local loop = Loop.new() local msgpack_stream = MsgpackStream.new(loop) @@ -166,22 +171,32 @@ local function clear() end local function insert(...) - nvim_feed('i', 'nt') + nvim_feed('i') rawfeed(...) - nvim_feed(nvim_replace_termcodes('<ESC>'), 'nt') + nvim_feed(nvim_replace_termcodes('<ESC>')) end local function execute(...) for _, v in ipairs({...}) do if v:sub(1, 1) ~= '/' then -- not a search command, prefix with colon - nvim_feed(':', 'nt') + nvim_feed(':') end - nvim_feed(v, 'nt') - nvim_feed(nvim_replace_termcodes('<CR>'), 'nt') + nvim_feed(v) + nvim_feed(nvim_replace_termcodes('<CR>')) end end +local function source(code) + local tmpname = os.tmpname() + local tmpfile = io.open(tmpname, "w") + tmpfile:write(code) + tmpfile:flush() + tmpfile:close() + nvim_command('source '..tmpname) + os.remove(tmpname) +end + local function eq(expected, actual) return assert.are.same(expected, actual) end @@ -190,10 +205,6 @@ local function neq(expected, actual) return assert.are_not.same(expected, actual) end -local function expect(contents, first, last, buffer_index) - return eq(dedent(contents), buffer_slice(first, last, buffer_index)) -end - local function ok(expr) assert.is_true(expr) end @@ -223,6 +234,10 @@ local function curbuf(method, ...) end local function curbuf_contents() + -- Before inspecting the buffer, execute 'vim_eval' to wait until all + -- previously sent keys are processed(vim_eval is a deferred function, and + -- only processed after all input) + session:request('vim_eval', '1') return table.concat(curbuf('get_line_slice', 0, -1, true, true), '\n') end @@ -242,11 +257,16 @@ local function curtab(method, ...) return tabpage(method, tab, ...) end +local function expect(contents) + return eq(dedent(contents), curbuf_contents()) +end + clear() return { clear = clear, dedent = dedent, + source = source, rawfeed = rawfeed, insert = insert, feed = feed, |