2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
local Screen = require('test.functional.ui.screen')
|
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
2024-04-20 08:44:13 -07:00
|
|
|
local eval = n.eval
|
|
|
|
local clear = n.clear
|
|
|
|
local source = n.source
|
|
|
|
local exc_exec = n.exc_exec
|
2024-04-08 02:03:20 -07:00
|
|
|
local pcall_err = t.pcall_err
|
2024-04-20 08:44:13 -07:00
|
|
|
local fn = n.fn
|
|
|
|
local command = n.command
|
|
|
|
local feed = n.feed
|
2024-04-08 02:03:20 -07:00
|
|
|
local is_os = t.is_os
|
2016-05-02 21:35:04 -07:00
|
|
|
|
2016-07-31 10:23:29 -07:00
|
|
|
describe('execute()', function()
|
2016-05-02 21:35:04 -07:00
|
|
|
before_each(clear)
|
|
|
|
|
2016-09-29 17:33:50 -07:00
|
|
|
it('captures the same result as :redir', function()
|
2021-09-19 02:29:37 -07:00
|
|
|
command([[
|
|
|
|
echomsg 'foo 1'
|
|
|
|
echomsg 'foo 2'
|
|
|
|
redir => g:__redir_output
|
|
|
|
silent! messages
|
|
|
|
redir END
|
|
|
|
]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(eval('g:__redir_output'), fn.execute('messages'))
|
2016-05-02 21:35:04 -07:00
|
|
|
end)
|
|
|
|
|
2016-09-29 17:33:50 -07:00
|
|
|
it('captures the concatenated outputs of a List of commands', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('foobar', fn.execute({ 'echon "foo"', 'echon "bar"' }))
|
|
|
|
eq('\nfoo\nbar', fn.execute({ 'echo "foo"', 'echo "bar"' }))
|
2016-05-02 21:35:04 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-30 19:40:02 -07:00
|
|
|
it('supports nested execute("execute(...)")', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('42', fn.execute([[echon execute("echon execute('echon 42')")]]))
|
2017-01-30 19:40:02 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('supports nested :redir to a variable', function()
|
2016-05-02 21:35:04 -07:00
|
|
|
source([[
|
|
|
|
function! g:Foo()
|
|
|
|
let a = ''
|
|
|
|
redir => a
|
|
|
|
silent echon "foo"
|
|
|
|
redir END
|
|
|
|
return a
|
|
|
|
endfunction
|
|
|
|
function! g:Bar()
|
|
|
|
let a = ''
|
|
|
|
redir => a
|
2017-01-30 19:40:02 -07:00
|
|
|
silent echon "bar1"
|
2016-05-02 21:35:04 -07:00
|
|
|
call g:Foo()
|
2017-01-30 19:40:02 -07:00
|
|
|
silent echon "bar2"
|
2016-05-02 21:35:04 -07:00
|
|
|
redir END
|
2017-01-30 19:40:02 -07:00
|
|
|
silent echon "bar3"
|
2016-05-02 21:35:04 -07:00
|
|
|
return a
|
|
|
|
endfunction
|
|
|
|
]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('top1bar1foobar2bar3', fn.execute('echon "top1"|call g:Bar()'))
|
2017-01-30 19:40:02 -07:00
|
|
|
end)
|
2016-05-02 21:35:04 -07:00
|
|
|
|
2017-01-30 19:40:02 -07:00
|
|
|
it('supports nested :redir to a register', function()
|
|
|
|
source([[
|
|
|
|
let @a = ''
|
|
|
|
function! g:Foo()
|
|
|
|
redir @a>>
|
|
|
|
silent echon "foo"
|
|
|
|
redir END
|
|
|
|
return @a
|
|
|
|
endfunction
|
|
|
|
function! g:Bar()
|
|
|
|
redir @a>>
|
|
|
|
silent echon "bar1"
|
|
|
|
call g:Foo()
|
|
|
|
silent echon "bar2"
|
|
|
|
redir END
|
|
|
|
silent echon "bar3"
|
|
|
|
return @a
|
|
|
|
endfunction
|
|
|
|
]])
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('top1bar1foobar2bar3', fn.execute('echon "top1"|call g:Bar()'))
|
2017-01-30 19:40:02 -07:00
|
|
|
-- :redir itself doesn't nest, so the redirection ends in g:Foo
|
|
|
|
eq('bar1foo', eval('@a'))
|
2016-05-02 21:35:04 -07:00
|
|
|
end)
|
|
|
|
|
2016-09-29 17:33:50 -07:00
|
|
|
it('captures a transformed string', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('^A', fn.execute('echon "\\<C-a>"'))
|
2016-05-02 21:35:04 -07:00
|
|
|
end)
|
|
|
|
|
2016-09-29 17:33:50 -07:00
|
|
|
it('returns empty string if the argument list is empty', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('', fn.execute({}))
|
2016-07-31 10:23:29 -07:00
|
|
|
eq(0, exc_exec('let g:ret = execute(v:_null_list)'))
|
2016-05-02 21:35:04 -07:00
|
|
|
eq('', eval('g:ret'))
|
|
|
|
end)
|
|
|
|
|
2016-09-29 17:33:50 -07:00
|
|
|
it('captures errors', function()
|
2016-05-02 21:35:04 -07:00
|
|
|
local ret
|
2016-07-31 10:23:29 -07:00
|
|
|
ret = exc_exec('call execute(v:_null_dict)')
|
2023-05-04 16:02:43 -07:00
|
|
|
eq('Vim(call):E731: Using a Dictionary as a String', ret)
|
2016-07-31 10:23:29 -07:00
|
|
|
ret = exc_exec('call execute(function("tr"))')
|
2023-05-04 16:02:43 -07:00
|
|
|
eq('Vim(call):E729: Using a Funcref as a String', ret)
|
2016-07-31 10:23:29 -07:00
|
|
|
ret = exc_exec('call execute(["echo 42", v:_null_dict, "echo 44"])')
|
2023-05-04 16:02:43 -07:00
|
|
|
eq('Vim:E731: Using a Dictionary as a String', ret)
|
2016-07-31 10:23:29 -07:00
|
|
|
ret = exc_exec('call execute(["echo 42", function("tr"), "echo 44"])')
|
2023-05-04 16:02:43 -07:00
|
|
|
eq('Vim:E729: Using a Funcref as a String', ret)
|
2016-05-02 21:35:04 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-18 13:13:59 -07:00
|
|
|
it('captures output with highlights', function()
|
|
|
|
eq(
|
|
|
|
'\nErrorMsg xxx ctermfg=15 ctermbg=1 guifg=White guibg=Red',
|
|
|
|
eval('execute("hi ErrorMsg")')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2017-01-18 13:13:59 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('does not corrupt the command display #5422', function()
|
2018-03-31 02:12:27 -07:00
|
|
|
local screen = Screen.new(70, 7)
|
2017-01-18 13:13:59 -07:00
|
|
|
feed(':echo execute("hi ErrorMsg")<CR>')
|
|
|
|
screen:expect(
|
|
|
|
[[
|
2018-03-31 02:12:27 -07:00
|
|
|
|
|
2023-12-09 05:42:00 -07:00
|
|
|
{1:~ }|*2
|
2018-03-31 02:12:27 -07:00
|
|
|
{2: }|
|
2018-12-11 19:29:10 -07:00
|
|
|
|
|
2017-01-18 13:13:59 -07:00
|
|
|
ErrorMsg xxx ctermfg=15 ctermbg=1 guifg=White guibg=Red |
|
2018-03-31 02:12:27 -07:00
|
|
|
{3:Press ENTER or type command to continue}^ |
|
|
|
|
]],
|
|
|
|
{
|
|
|
|
[1] = { bold = true, foreground = Screen.colors.Blue1 },
|
|
|
|
[2] = { bold = true, reverse = true },
|
|
|
|
[3] = { bold = true, foreground = Screen.colors.SeaGreen4 },
|
|
|
|
}
|
|
|
|
)
|
2017-01-18 13:13:59 -07:00
|
|
|
feed('<CR>')
|
|
|
|
end)
|
|
|
|
|
2018-12-06 07:27:25 -07:00
|
|
|
it('places cursor correctly #6035', function()
|
2018-12-08 02:14:43 -07:00
|
|
|
local screen = Screen.new(40, 6)
|
2018-12-06 07:27:25 -07:00
|
|
|
source([=[
|
2018-12-08 02:14:43 -07:00
|
|
|
" test 1: non-silenced output goes as usual
|
|
|
|
function! Test1()
|
|
|
|
echo 1234
|
|
|
|
let x = execute('echon "abcdef"', '')
|
|
|
|
echon 'ABCD'
|
2018-12-06 07:27:25 -07:00
|
|
|
endfunction
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
" test 2: silenced output does not affect ui
|
|
|
|
function! Test2()
|
|
|
|
echo 1234
|
|
|
|
let x = execute('echon "abcdef"', 'silent')
|
|
|
|
echon 'ABCD'
|
2018-12-06 07:27:25 -07:00
|
|
|
endfunction
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
" test 3: silenced! error does not affect ui
|
|
|
|
function! Test3()
|
|
|
|
echo 1234
|
|
|
|
let x = execute('echoerr "abcdef"', 'silent!')
|
2022-05-21 14:07:34 -07:00
|
|
|
echon 'ABCDXZYZ'
|
2018-12-06 07:27:25 -07:00
|
|
|
endfunction
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
" test 4: silenced echoerr goes as usual
|
|
|
|
" bug here
|
|
|
|
function! Test4()
|
|
|
|
echo 1234
|
|
|
|
let x = execute('echoerr "abcdef"', 'silent')
|
|
|
|
echon 'ABCD'
|
2018-12-06 07:27:25 -07:00
|
|
|
endfunction
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
" test 5: silenced! echoerr does not affect ui
|
|
|
|
function! Test5()
|
|
|
|
echo 1234
|
|
|
|
let x = execute('echoerr "abcdef"', 'silent!')
|
|
|
|
echon 'ABCD'
|
2018-12-06 07:27:25 -07:00
|
|
|
endfunction
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
" test 6: silenced error goes as usual
|
|
|
|
function! Test6()
|
|
|
|
echo 1234
|
|
|
|
let x = execute('echo undefined', 'silent')
|
|
|
|
echon 'ABCD'
|
2018-12-06 07:27:25 -07:00
|
|
|
endfunction
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
" test 7: existing error does not mess the result
|
|
|
|
function! Test7()
|
|
|
|
" display from Test6() is still visible
|
|
|
|
" why does the "abcdef" goes into a newline
|
|
|
|
let x = execute('echon "abcdef"', '')
|
|
|
|
echon 'ABCD'
|
2018-12-06 07:27:25 -07:00
|
|
|
endfunction
|
|
|
|
]=])
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
feed([[:call Test1()<cr>]])
|
2018-12-06 07:27:25 -07:00
|
|
|
screen:expect([[
|
2018-12-08 02:14:43 -07:00
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*4
|
2018-12-11 19:29:10 -07:00
|
|
|
ABCD |
|
2018-12-06 07:27:25 -07:00
|
|
|
]])
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
feed([[:call Test2()<cr>]])
|
2018-12-06 07:27:25 -07:00
|
|
|
screen:expect([[
|
2018-12-08 02:14:43 -07:00
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*4
|
2018-12-08 02:14:43 -07:00
|
|
|
1234ABCD |
|
2018-12-06 07:27:25 -07:00
|
|
|
]])
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
feed([[:call Test3()<cr>]])
|
2018-12-06 07:27:25 -07:00
|
|
|
screen:expect([[
|
2018-12-08 02:14:43 -07:00
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*4
|
2022-05-21 14:07:34 -07:00
|
|
|
1234ABCDXZYZ |
|
2018-12-06 07:27:25 -07:00
|
|
|
]])
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
feed([[:call Test4()<cr>]])
|
|
|
|
-- unexpected: need to fix
|
|
|
|
-- echoerr does not set did_emsg
|
|
|
|
-- "ef" was overwritten since msg_col was recovered wrongly
|
2018-12-06 07:27:25 -07:00
|
|
|
screen:expect([[
|
2018-12-08 02:14:43 -07:00
|
|
|
1234 |
|
2024-03-22 03:02:52 -07:00
|
|
|
{9:Error detected while processing function}|
|
|
|
|
{9: Test4:} |
|
|
|
|
{8:line 2:} |
|
|
|
|
{9:abcd}ABCD |
|
|
|
|
{6:Press ENTER or type command to continue}^ |
|
2018-12-06 07:27:25 -07:00
|
|
|
]])
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
feed([[<cr>]]) -- to clear screen
|
|
|
|
feed([[:call Test5()<cr>]])
|
2018-12-06 07:27:25 -07:00
|
|
|
screen:expect([[
|
2018-12-08 02:14:43 -07:00
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*4
|
2018-12-08 02:14:43 -07:00
|
|
|
1234ABCD |
|
2018-12-06 07:27:25 -07:00
|
|
|
]])
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
feed([[:call Test6()<cr>]])
|
2018-12-06 07:27:25 -07:00
|
|
|
screen:expect([[
|
2018-12-11 19:29:10 -07:00
|
|
|
|
|
2024-03-22 03:02:52 -07:00
|
|
|
{9:Error detected while processing function}|
|
|
|
|
{9: Test6:} |
|
|
|
|
{8:line 2:} |
|
|
|
|
{9:E121}ABCD |
|
|
|
|
{6:Press ENTER or type command to continue}^ |
|
2018-12-06 07:27:25 -07:00
|
|
|
]])
|
|
|
|
|
2018-12-08 02:14:43 -07:00
|
|
|
feed([[:call Test7()<cr>]])
|
2018-12-06 07:27:25 -07:00
|
|
|
screen:expect([[
|
2024-03-22 03:02:52 -07:00
|
|
|
{9:Error detected while processing function}|
|
|
|
|
{9: Test6:} |
|
|
|
|
{8:line 2:} |
|
|
|
|
{9:E121}ABCD |
|
2018-12-11 19:29:10 -07:00
|
|
|
ABCD |
|
2024-03-22 03:02:52 -07:00
|
|
|
{6:Press ENTER or type command to continue}^ |
|
2018-12-06 07:27:25 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2018-01-21 11:58:19 -07:00
|
|
|
-- This deviates from vim behavior, but is consistent
|
|
|
|
-- with how nvim currently displays the output.
|
2018-12-06 07:27:25 -07:00
|
|
|
it('captures shell-command output', function()
|
2022-11-21 17:13:30 -07:00
|
|
|
local win_lf = is_os('win') and '\13' or ''
|
2024-01-12 10:59:57 -07:00
|
|
|
eq('\n:!echo foo\r\n\nfoo' .. win_lf .. '\n', fn.execute('!echo foo'))
|
2016-09-29 17:33:50 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-08 21:13:51 -07:00
|
|
|
describe('{silent} argument', function()
|
|
|
|
it('captures & displays output for ""', function()
|
|
|
|
local screen = Screen.new(40, 5)
|
|
|
|
command('let g:mes = execute("echon 42", "")')
|
|
|
|
screen:expect([[
|
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*3
|
2017-01-08 21:13:51 -07:00
|
|
|
42 |
|
|
|
|
]])
|
|
|
|
eq('42', eval('g:mes'))
|
|
|
|
end)
|
|
|
|
|
2023-05-05 04:03:08 -07:00
|
|
|
it('gives E493 instead of prompting on backwards range for ""', function()
|
|
|
|
command('split')
|
|
|
|
eq(
|
|
|
|
'Vim(windo):E493: Backwards range given: 2,1windo echo',
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(fn.execute, '2,1windo echo', '')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-05-05 04:03:08 -07:00
|
|
|
eq(
|
|
|
|
'Vim(windo):E493: Backwards range given: 2,1windo echo',
|
2024-01-12 10:59:57 -07:00
|
|
|
pcall_err(fn.execute, { '2,1windo echo' }, '')
|
2024-01-02 18:09:18 -07:00
|
|
|
)
|
2023-05-05 04:03:08 -07:00
|
|
|
end)
|
|
|
|
|
2017-01-08 21:13:51 -07:00
|
|
|
it('captures but does not display output for "silent"', function()
|
|
|
|
local screen = Screen.new(40, 5)
|
|
|
|
command('let g:mes = execute("echon 42")')
|
|
|
|
screen:expect([[
|
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*3
|
2017-01-08 21:13:51 -07:00
|
|
|
|
|
|
|
|
]])
|
|
|
|
eq('42', eval('g:mes'))
|
|
|
|
|
|
|
|
command('let g:mes = execute("echon 13", "silent")')
|
2017-06-26 05:49:15 -07:00
|
|
|
screen:expect {
|
|
|
|
grid = [[
|
2017-01-08 21:13:51 -07:00
|
|
|
^ |
|
2024-03-22 03:02:52 -07:00
|
|
|
{1:~ }|*3
|
2017-01-08 21:13:51 -07:00
|
|
|
|
|
2017-06-26 05:49:15 -07:00
|
|
|
]],
|
|
|
|
unchanged = true,
|
|
|
|
}
|
2017-01-08 21:13:51 -07:00
|
|
|
eq('13', eval('g:mes'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('suppresses errors for "silent!"', function()
|
|
|
|
eq(0, exc_exec('let g:mes = execute(0.0, "silent!")'))
|
|
|
|
eq('', eval('g:mes'))
|
|
|
|
|
|
|
|
eq(0, exc_exec('let g:mes = execute("echon add(1, 1)", "silent!")'))
|
|
|
|
eq('1', eval('g:mes'))
|
|
|
|
|
|
|
|
eq(0, exc_exec('let g:mes = execute(["echon 42", "echon add(1, 1)"], "silent!")'))
|
|
|
|
eq('421', eval('g:mes'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('propagates errors for "" and "silent"', function()
|
|
|
|
local ret
|
|
|
|
ret = exc_exec('call execute(v:_null_dict, "silent")')
|
2023-05-04 16:02:43 -07:00
|
|
|
eq('Vim(call):E731: Using a Dictionary as a String', ret)
|
2017-01-08 21:13:51 -07:00
|
|
|
|
|
|
|
ret = exc_exec('call execute("echo add(1, 1)", "")')
|
2020-12-02 11:37:18 -07:00
|
|
|
eq('Vim(echo):E897: List or Blob required', ret)
|
2017-01-08 21:13:51 -07:00
|
|
|
|
|
|
|
ret = exc_exec('call execute(["echon 42", "echo add(1, 1)"], "")')
|
2020-12-02 11:37:18 -07:00
|
|
|
eq('Vim(echo):E897: List or Blob required', ret)
|
2017-01-08 21:13:51 -07:00
|
|
|
|
|
|
|
ret = exc_exec('call execute("echo add(1, 1)", "silent")')
|
2020-12-02 11:37:18 -07:00
|
|
|
eq('Vim(echo):E897: List or Blob required', ret)
|
2017-01-08 21:13:51 -07:00
|
|
|
|
|
|
|
ret = exc_exec('call execute(["echon 42", "echo add(1, 1)"], "silent")')
|
2020-12-02 11:37:18 -07:00
|
|
|
eq('Vim(echo):E897: List or Blob required', ret)
|
2017-01-08 21:13:51 -07:00
|
|
|
end)
|
2016-05-02 21:35:04 -07:00
|
|
|
end)
|
|
|
|
end)
|