diff options
Diffstat (limited to 'test/functional/terminal/helpers.lua')
-rw-r--r-- | test/functional/terminal/helpers.lua | 150 |
1 files changed, 98 insertions, 52 deletions
diff --git a/test/functional/terminal/helpers.lua b/test/functional/terminal/helpers.lua index 62d3dd67a3..05db1b3c8c 100644 --- a/test/functional/terminal/helpers.lua +++ b/test/functional/terminal/helpers.lua @@ -5,11 +5,12 @@ local helpers = require('test.functional.helpers')(nil) local Screen = require('test.functional.ui.screen') local testprg = helpers.testprg local exec_lua = helpers.exec_lua -local nvim = helpers.nvim +local api = helpers.api +local nvim_prog = helpers.nvim_prog local function feed_data(data) if type(data) == 'table' then - data = table.concat(data, '\n') + data = table.concat(data, '\n') end exec_lua('vim.api.nvim_chan_send(vim.b.terminal_job_id, ...)', data) end @@ -20,7 +21,7 @@ end local function make_lua_executor(session) return function(code, ...) - local status, rv = session:request('nvim_exec_lua', code, {...}) + local status, rv = session:request('nvim_exec_lua', code, { ... }) if not status then session:stop() error(rv[2]) @@ -32,76 +33,108 @@ end -- some helpers for controlling the terminal. the codes were taken from -- infocmp xterm-256color which is less what libvterm understands -- civis/cnorm -local function hide_cursor() feed_termcode('[?25l') end -local function show_cursor() feed_termcode('[?25h') end +local function hide_cursor() + feed_termcode('[?25l') +end +local function show_cursor() + feed_termcode('[?25h') +end -- smcup/rmcup -local function enter_altscreen() feed_termcode('[?1049h') end -local function exit_altscreen() feed_termcode('[?1049l') end +local function enter_altscreen() + feed_termcode('[?1049h') +end +local function exit_altscreen() + feed_termcode('[?1049l') +end -- character attributes -local function set_fg(num) feed_termcode('[38;5;'..num..'m') end -local function set_bg(num) feed_termcode('[48;5;'..num..'m') end -local function set_bold() feed_termcode('[1m') end -local function set_italic() feed_termcode('[3m') end -local function set_underline() feed_termcode('[4m') end -local function set_underdouble() feed_termcode('[4:2m') end -local function set_undercurl() feed_termcode('[4:3m') end -local function set_strikethrough() feed_termcode('[9m') end -local function clear_attrs() feed_termcode('[0;10m') end +local function set_fg(num) + feed_termcode('[38;5;' .. num .. 'm') +end +local function set_bg(num) + feed_termcode('[48;5;' .. num .. 'm') +end +local function set_bold() + feed_termcode('[1m') +end +local function set_italic() + feed_termcode('[3m') +end +local function set_underline() + feed_termcode('[4m') +end +local function set_underdouble() + feed_termcode('[4:2m') +end +local function set_undercurl() + feed_termcode('[4:3m') +end +local function set_strikethrough() + feed_termcode('[9m') +end +local function clear_attrs() + feed_termcode('[0;10m') +end -- mouse -local function enable_mouse() feed_termcode('[?1002h') end -local function disable_mouse() feed_termcode('[?1002l') end +local function enable_mouse() + feed_termcode('[?1002h') +end +local function disable_mouse() + feed_termcode('[?1002l') +end -local default_command = '["'..testprg('tty-test')..'"]' +local default_command = { testprg('tty-test') } -local function screen_setup(extra_rows, command, cols, opts) +local function screen_setup(extra_rows, command, cols, env, screen_opts) extra_rows = extra_rows and extra_rows or 0 command = command and command or default_command cols = cols and cols or 50 - nvim('command', 'highlight TermCursor cterm=reverse') - nvim('command', 'highlight TermCursorNC ctermbg=11') + api.nvim_command('highlight TermCursor cterm=reverse') + api.nvim_command('highlight TermCursorNC ctermbg=11') local screen = Screen.new(cols, 7 + extra_rows) screen:set_default_attr_ids({ - [1] = {reverse = true}, -- focused cursor - [2] = {background = 11}, -- unfocused cursor - [3] = {bold = true}, - [4] = {foreground = 12}, - [5] = {bold = true, reverse = true}, - -- 6 was a duplicate item - [7] = {foreground = 130}, - [8] = {foreground = 15, background = 1}, -- error message - [9] = {foreground = 4}, - [10] = {foreground = 121}, -- "Press ENTER" in embedded :terminal session. - [11] = {foreground = tonumber('0x00000b')}, - [12] = {underline = true}, - [13] = {underline = true, reverse = true}, - [14] = {underline = true, reverse = true, bold = true}, - [15] = {underline = true, foreground = 12}, + [1] = { reverse = true }, -- focused cursor + [2] = { background = 11 }, -- unfocused cursor + [3] = { bold = true }, + [4] = { foreground = 12 }, -- NonText in :terminal session + [5] = { bold = true, reverse = true }, + [6] = { foreground = 81 }, -- SpecialKey in :terminal session + [7] = { foreground = 130 }, -- LineNr in host session + [8] = { foreground = 15, background = 1 }, -- ErrorMsg in :terminal session + [9] = { foreground = 4 }, + [10] = { foreground = 121 }, -- MoreMsg in :terminal session + [11] = { foreground = 11 }, -- LineNr in :terminal session + [12] = { underline = true }, + [13] = { underline = true, reverse = true }, + [14] = { underline = true, reverse = true, bold = true }, + [15] = { underline = true, foreground = 12 }, + [16] = { background = 248, foreground = 0 }, -- Visual in :terminal session }) - screen:attach(opts or {rgb=false}) + screen:attach(screen_opts or { rgb = false }) - nvim('command', 'enew | call termopen('..command..')') - nvim('input', '<CR>') - local vim_errmsg = nvim('eval', 'v:errmsg') - if vim_errmsg and "" ~= vim_errmsg then + api.nvim_command('enew') + api.nvim_call_function('termopen', { command, env and { env = env } or nil }) + api.nvim_input('<CR>') + local vim_errmsg = api.nvim_eval('v:errmsg') + if vim_errmsg and '' ~= vim_errmsg then error(vim_errmsg) end - nvim('command', 'setlocal scrollback=10') - nvim('command', 'startinsert') - nvim('input', '<Ignore>') -- Add input to separate two RPC requests + api.nvim_command('setlocal scrollback=10') + api.nvim_command('startinsert') + api.nvim_input('<Ignore>') -- Add input to separate two RPC requests -- tty-test puts the terminal into raw mode and echoes input. Tests work by -- feeding termcodes to control the display and asserting by screen:expect. - if command == default_command and opts == nil then + if command == default_command and screen_opts == nil then -- Wait for "tty ready" to be printed before each test or the terminal may -- still be in canonical mode (will echo characters for example). local empty_line = (' '):rep(cols) local expected = { - 'tty ready'..(' '):rep(cols - 9), - '{1: }' ..(' '):rep(cols - 1), + 'tty ready' .. (' '):rep(cols - 9), + '{1: }' .. (' '):rep(cols - 1), empty_line, empty_line, empty_line, @@ -112,16 +145,28 @@ local function screen_setup(extra_rows, command, cols, opts) end table.insert(expected, '{3:-- TERMINAL --}' .. ((' '):rep(cols - 14))) - screen:expect(table.concat(expected, '|\n')..'|') + screen:expect(table.concat(expected, '|\n') .. '|') else -- This eval also acts as a poke_eventloop(). - if 0 == nvim('eval', "exists('b:terminal_job_id')") then - error("terminal job failed to start") + if 0 == api.nvim_eval("exists('b:terminal_job_id')") then + error('terminal job failed to start') end end return screen end +local function setup_child_nvim(args, opts) + opts = opts or {} + local argv = { nvim_prog, unpack(args) } + + local env = opts.env or {} + if not env.VIMRUNTIME then + env.VIMRUNTIME = os.getenv('VIMRUNTIME') + end + + return screen_setup(0, argv, opts.cols, env) +end + return { feed_data = feed_data, feed_termcode = feed_termcode, @@ -141,5 +186,6 @@ return { clear_attrs = clear_attrs, enable_mouse = enable_mouse, disable_mouse = disable_mouse, - screen_setup = screen_setup + screen_setup = screen_setup, + setup_child_nvim = setup_child_nvim, } |