test(terminal): remove unnecessary string operations (#26434)

This commit is contained in:
zeertzjq
2023-12-07 07:15:37 +08:00
committed by GitHub
parent 8bb5089974
commit 6b00b8a369
5 changed files with 15 additions and 28 deletions

View File

@@ -487,7 +487,7 @@ if is_os('win') then
clear() clear()
feed_command('set modifiable swapfile undolevels=20') feed_command('set modifiable swapfile undolevels=20')
poke_eventloop() poke_eventloop()
local cmd = '["cmd.exe","/K","PROMPT=$g$s"]' local cmd = { "cmd.exe", "/K", "PROMPT=$g$s" }
screen = thelpers.screen_setup(nil, cmd) screen = thelpers.screen_setup(nil, cmd)
end) end)

View File

@@ -52,9 +52,9 @@ local function clear_attrs() feed_termcode('[0;10m') end
local function enable_mouse() feed_termcode('[?1002h') end local function enable_mouse() feed_termcode('[?1002h') end
local function disable_mouse() feed_termcode('[?1002l') 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 extra_rows = extra_rows and extra_rows or 0
command = command and command or default_command command = command and command or default_command
cols = cols and cols or 50 cols = cols and cols or 50
@@ -81,9 +81,10 @@ local function screen_setup(extra_rows, command, cols, opts)
[15] = {underline = true, foreground = 12}, [15] = {underline = true, foreground = 12},
}) })
screen:attach(opts or {rgb=false}) screen:attach(screen_opts or {rgb=false})
nvim('command', 'enew | call termopen('..command..')') nvim('command', 'enew')
nvim('call_function', 'termopen', {command, env and {env = env} or nil})
nvim('input', '<CR>') nvim('input', '<CR>')
local vim_errmsg = nvim('eval', 'v:errmsg') local vim_errmsg = nvim('eval', 'v:errmsg')
if vim_errmsg and "" ~= vim_errmsg then if vim_errmsg and "" ~= vim_errmsg then
@@ -96,7 +97,7 @@ local function screen_setup(extra_rows, command, cols, opts)
-- tty-test puts the terminal into raw mode and echoes input. Tests work by -- 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. -- 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 -- Wait for "tty ready" to be printed before each test or the terminal may
-- still be in canonical mode (will echo characters for example). -- still be in canonical mode (will echo characters for example).
local empty_line = (' '):rep(cols) local empty_line = (' '):rep(cols)
@@ -125,22 +126,8 @@ end
local function setup_child_nvim(args, opts) local function setup_child_nvim(args, opts)
opts = opts or {} opts = opts or {}
local argv = { nvim_prog, unpack(args) } local argv = { nvim_prog, unpack(args) }
local cmd = string.format('[%s]', vim.iter(argv):map(function(s) return screen_setup(0, argv, opts.cols, opts.env)
return string.format('\'%s\'', s)
end):join(', '))
if opts.env then
local s = {}
for k, v in pairs(opts.env) do
table.insert(s, string.format('%s: \'%s\'', k, v))
end
cmd = string.format('%s, #{env: #{%s}}', cmd, table.concat(s, ', '))
end
return screen_setup(0, cmd, opts.cols)
end end
return { return {

View File

@@ -392,9 +392,9 @@ describe("'scrollback' option", function()
it('set to 0 behaves as 1', function() it('set to 0 behaves as 1', function()
local screen local screen
if is_os('win') then if is_os('win') then
screen = thelpers.screen_setup(nil, "['cmd.exe']", 30) screen = thelpers.screen_setup(nil, { 'cmd.exe' }, 30)
else else
screen = thelpers.screen_setup(nil, "['sh']", 30) screen = thelpers.screen_setup(nil, { 'sh' }, 30)
end end
meths.set_option_value('scrollback', 0, {}) meths.set_option_value('scrollback', 0, {})
@@ -407,10 +407,10 @@ describe("'scrollback' option", function()
local screen local screen
if is_os('win') then if is_os('win') then
command([[let $PROMPT='$$']]) command([[let $PROMPT='$$']])
screen = thelpers.screen_setup(nil, "['cmd.exe']", 30) screen = thelpers.screen_setup(nil, { 'cmd.exe' }, 30)
else else
command('let $PS1 = "$"') command('let $PS1 = "$"')
screen = thelpers.screen_setup(nil, "['sh']", 30) screen = thelpers.screen_setup(nil, { 'sh' }, 30)
end end
meths.set_option_value('scrollback', 200, {}) meths.set_option_value('scrollback', 200, {})

View File

@@ -1887,8 +1887,8 @@ describe('TUI', function()
finally(function() finally(function()
os.remove('testF') os.remove('testF')
end) end)
local screen = thelpers.screen_setup(0, '"'..nvim_prog local screen = thelpers.screen_setup(0, nvim_prog
..' -u NONE -i NONE --cmd \'set noswapfile noshowcmd noruler\' --cmd \'normal iabc\' > /dev/null 2>&1 && cat testF && rm testF"') ..' -u NONE -i NONE --cmd \'set noswapfile noshowcmd noruler\' --cmd \'normal iabc\' > /dev/null 2>&1 && cat testF && rm testF')
feed_data(':w testF\n:q\n') feed_data(':w testF\n:q\n')
screen:expect([[ screen:expect([[
:w testF | :w testF |

View File

@@ -179,7 +179,7 @@ describe(':terminal with multigrid', function()
before_each(function() before_each(function()
clear() clear()
screen = thelpers.screen_setup(0,nil,50,{ext_multigrid=true}) screen = thelpers.screen_setup(0, nil, 50, nil, {ext_multigrid=true})
end) end)
it('resizes to requested size', function() it('resizes to requested size', function()