test(tui_spec): use child_exec_lua() more (#30757)

It is more readable.
This commit is contained in:
zeertzjq
2024-10-11 16:23:14 +08:00
committed by GitHub
parent c3cb702ac7
commit c4762b3097

View File

@@ -1225,16 +1225,12 @@ describe('TUI', function()
end) end)
it('paste: recovers from vim.paste() failure', function() it('paste: recovers from vim.paste() failure', function()
child_session:request( child_exec_lua([[
'nvim_exec_lua',
[[
_G.save_paste_fn = vim.paste _G.save_paste_fn = vim.paste
-- Stack traces for this test are non-deterministic, so disable them -- Stack traces for this test are non-deterministic, so disable them
_G.debug.traceback = function(msg) return msg end _G.debug.traceback = function(msg) return msg end
vim.paste = function(lines, phase) error("fake fail") end vim.paste = function(lines, phase) error("fake fail") end
]], ]])
{}
)
-- Prepare something for dot-repeat/redo. -- Prepare something for dot-repeat/redo.
feed_data('ifoo\n\027[27u') feed_data('ifoo\n\027[27u')
wait_for_mode('n') wait_for_mode('n')
@@ -1287,7 +1283,7 @@ describe('TUI', function()
{3:-- TERMINAL --} | {3:-- TERMINAL --} |
]]) ]])
-- Paste works if vim.paste() succeeds. -- Paste works if vim.paste() succeeds.
child_session:request('nvim_exec_lua', [[vim.paste = _G.save_paste_fn]], {}) child_exec_lua([[vim.paste = _G.save_paste_fn]])
feed_data('\027[200~line A\nline B\n\027[201~') feed_data('\027[200~line A\nline B\n\027[201~')
screen:expect([[ screen:expect([[
foo | foo |
@@ -1303,13 +1299,9 @@ describe('TUI', function()
it('paste: vim.paste() cancel (retval=false) #10865', function() it('paste: vim.paste() cancel (retval=false) #10865', function()
-- This test only exercises the "cancel" case. Use-case would be "dangling -- This test only exercises the "cancel" case. Use-case would be "dangling
-- paste", but that is not implemented yet. #10865 -- paste", but that is not implemented yet. #10865
child_session:request( child_exec_lua([[
'nvim_exec_lua',
[[
vim.paste = function(lines, phase) return false end vim.paste = function(lines, phase) return false end
]], ]])
{}
)
feed_data('\027[200~line A\nline B\n\027[201~') feed_data('\027[200~line A\nline B\n\027[201~')
expect_child_buf_lines({ '' }) expect_child_buf_lines({ '' })
feed_data('ifoo\n\027[27u') feed_data('ifoo\n\027[27u')
@@ -1317,22 +1309,18 @@ describe('TUI', function()
end) end)
it('paste: vim.paste() cancel (retval=false) with streaming #30462', function() it('paste: vim.paste() cancel (retval=false) with streaming #30462', function()
child_session:request( child_exec_lua([[
'nvim_exec_lua', vim.paste = (function(overridden)
[[ return function(lines, phase)
vim.paste = (function(overridden) for i, line in ipairs(lines) do
return function(lines, phase) if line:find('!') then
for i, line in ipairs(lines) do return false
if line:find('!') then
return false
end
end end
return overridden(lines, phase)
end end
end)(vim.paste) return overridden(lines, phase)
]], end
{} end)(vim.paste)
) ]])
feed_data('A') feed_data('A')
wait_for_mode('i') wait_for_mode('i')
feed_data('\027[200~aaa') feed_data('\027[200~aaa')
@@ -1353,15 +1341,11 @@ describe('TUI', function()
end) end)
it("paste: 'nomodifiable' buffer", function() it("paste: 'nomodifiable' buffer", function()
child_session:request('nvim_command', 'set nomodifiable') child_exec_lua([[
child_session:request( vim.bo.modifiable = false
'nvim_exec_lua',
[[
-- Truncate the error message to hide the line number -- Truncate the error message to hide the line number
_G.debug.traceback = function(msg) return msg:sub(-49) end _G.debug.traceback = function(msg) return msg:sub(-49) end
]], ]])
{}
)
feed_data('\027[200~fail 1\nfail 2\n\027[201~') feed_data('\027[200~fail 1\nfail 2\n\027[201~')
screen:expect([[ screen:expect([[
| |
@@ -1373,7 +1357,7 @@ describe('TUI', function()
{3:-- TERMINAL --} | {3:-- TERMINAL --} |
]]) ]])
feed_data('\n') -- <Enter> to dismiss hit-enter prompt feed_data('\n') -- <Enter> to dismiss hit-enter prompt
child_session:request('nvim_command', 'set modifiable') child_exec_lua('vim.bo.modifiable = true')
feed_data('\027[200~success 1\nsuccess 2\n\027[201~') feed_data('\027[200~success 1\nsuccess 2\n\027[201~')
screen:expect([[ screen:expect([[
success 1 | success 1 |
@@ -1528,9 +1512,7 @@ describe('TUI', function()
end) end)
it('paste: streamed paste with isolated "stop paste" code', function() it('paste: streamed paste with isolated "stop paste" code', function()
child_session:request( child_exec_lua([[
'nvim_exec_lua',
[[
_G.paste_phases = {} _G.paste_phases = {}
vim.paste = (function(overridden) vim.paste = (function(overridden)
return function(lines, phase) return function(lines, phase)
@@ -1538,9 +1520,7 @@ describe('TUI', function()
overridden(lines, phase) overridden(lines, phase)
end end
end)(vim.paste) end)(vim.paste)
]], ]])
{}
)
feed_data('i') feed_data('i')
wait_for_mode('i') wait_for_mode('i')
feed_data('\027[200~pasted') -- phase 1 feed_data('\027[200~pasted') -- phase 1
@@ -1563,7 +1543,7 @@ describe('TUI', function()
feed_data('\027[201~') -- phase 3 feed_data('\027[201~') -- phase 3
poke_both_eventloop() poke_both_eventloop()
screen:expect_unchanged() screen:expect_unchanged()
local _, rv = child_session:request('nvim_exec_lua', [[return _G.paste_phases]], {}) local rv = child_exec_lua('return _G.paste_phases')
-- In rare cases there may be multiple chunks of phase 2 because of timing. -- In rare cases there may be multiple chunks of phase 2 because of timing.
eq({ 1, 2, 3 }, { rv[1], rv[2], rv[#rv] }) eq({ 1, 2, 3 }, { rv[1], rv[2], rv[#rv] })
end) end)
@@ -1749,9 +1729,7 @@ describe('TUI', function()
eq(expected, rv) eq(expected, rv)
---@type table ---@type table
local expected_version = ({ local expected_version = child_exec_lua('return vim.version()')
child_session:request('nvim_exec_lua', 'return vim.version()', {}),
})[2]
-- vim.version() returns `prerelease` string. Coerce it to boolean. -- vim.version() returns `prerelease` string. Coerce it to boolean.
expected_version.prerelease = not not expected_version.prerelease expected_version.prerelease = not not expected_version.prerelease