Skip to content

Commit 88f18a7

Browse files
committed
test(completion): add coverage for errors in LSP response
1 parent d836ea3 commit 88f18a7

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

tests/dir-completion/mock-months-lsp.lua

+10
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,14 @@ local construct_filterText = function(name)
102102
return _G.mock_filterText(name)
103103
end
104104

105+
-- Neovim<0.11 uses 'error' field in `buf_request_all` to indicate error,
106+
-- while Neovim>=0.11 uses 'err'
107+
local error_field = vim.fn.has('nvim-0.11') == 1 and 'err' or 'error'
108+
105109
Months.requests = {
106110
['textDocument/completion'] = function(params)
111+
if _G.mock_completion_error ~= nil then return { { [error_field] = _G.mock_completion_error } } end
112+
107113
-- Count actual requests for easier "force completion" tests
108114
_G.n_textdocument_completion = (_G.n_textdocument_completion or 0) + 1
109115

@@ -142,6 +148,8 @@ Months.requests = {
142148
end,
143149

144150
['completionItem/resolve'] = function(params)
151+
if _G.mock_resolve_error ~= nil then return { { [error_field] = _G.mock_resolve_error } } end
152+
145153
local doc = Months.data[params.label].documentation
146154
if doc ~= nil then params.documentation = { kind = 'markdown', value = doc } end
147155
params.detail = Months.data[params.label].detail
@@ -153,6 +161,8 @@ Months.requests = {
153161
end,
154162

155163
['textDocument/signatureHelp'] = function(params)
164+
if _G.mock_signature_error ~= nil then return { { [error_field] = _G.mock_signature_error } } end
165+
156166
params = type(params) == 'function' and params(Months.client, vim.api.nvim_get_current_buf()) or params
157167

158168
local n_line, n_col = params.position.line, params.position.character

tests/test_completion.lua

+40-6
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,12 @@ T['Manual completion']['works with two-step completion'] = function()
726726
eq(get_completion(), { 'Jackpot' })
727727
end
728728

729+
T['Manual completion']['handles request errors'] = function()
730+
child.lua('_G.mock_completion_error = "Error"')
731+
type_keys('i', 'J', '<C-Space>')
732+
eq(get_completion(), { 'Jackpot' })
733+
end
734+
729735
T['Manual completion']['uses `vim.lsp.protocol.CompletionItemKind` in LSP step'] = function()
730736
child.set_size(17, 30)
731737
child.lua([[vim.lsp.protocol.CompletionItemKind = {
@@ -1016,6 +1022,14 @@ T['Information window']['works'] = function()
10161022
child.expect_screenshot()
10171023
end
10181024

1025+
T['Information window']['handles request errors'] = function()
1026+
child.lua('_G.mock_resolve_error = "Error"')
1027+
type_keys('i', 'J', '<C-Space>')
1028+
type_keys('<C-n>')
1029+
sleep(default_info_delay + small_time)
1030+
eq(get_floating_windows(), {})
1031+
end
1032+
10191033
T['Information window']['respects `config.delay.info`'] = function()
10201034
child.lua('MiniCompletion.config.delay.info = ' .. (2 * default_info_delay))
10211035
validate_info_win(2 * default_info_delay)
@@ -1252,6 +1266,13 @@ T['Signature help']['works'] = function()
12521266
child.expect_screenshot()
12531267
end
12541268

1269+
T['Signature help']['handles request errors'] = function()
1270+
child.lua('_G.mock_signature_error = "Error"')
1271+
type_keys('i', 'abc(')
1272+
sleep(default_signature_delay + small_time)
1273+
eq(get_floating_windows(), {})
1274+
end
1275+
12551276
T['Signature help']['respects `config.delay.signature`'] = function()
12561277
child.lua('MiniCompletion.config.delay.signature = ' .. (2 * default_signature_delay))
12571278
validate_signature_win(2 * default_signature_delay)
@@ -1888,20 +1909,33 @@ T['Snippets']['prefer snippet from resolved item'] = function()
18881909
-- or `textEdti` in 'completionItem/resolve', this still can probably happen.
18891910

18901911
child.lua([[
1912+
local error_field = vim.fn.has('nvim-0.11') == 1 and 'err' or 'error'
18911913
local buf_request_all_orig = vim.lsp.buf_request_all
18921914
vim.lsp.buf_request_all = function(bufnr, method, params, callback)
18931915
if method ~= 'completionItem/resolve' then return buf_request_all_orig(bufnr, method, params, callback) end
18941916
params.textEdit = { newText = 'Snippet $1 from resolve' }
1895-
callback({ { result = params } })
1917+
callback({ { [error_field] = _G.resolve_error, result = _G.resolve_error == nil and params or nil } })
18961918
end
18971919
]])
18981920

1921+
local validate = function(ref_lines)
1922+
type_keys('i', '<C-Space>', '<C-n>')
1923+
-- - Wait for 'completionItem/resolve' request to be sent
1924+
sleep(default_info_delay + small_time)
1925+
type_keys('<C-y>')
1926+
eq(get_lines(), ref_lines)
1927+
1928+
type_keys('<C-c>')
1929+
set_lines({})
1930+
child.ensure_normal_mode()
1931+
end
1932+
18991933
mock_lsp_snippets({ 'Snippet $1 original' })
1900-
type_keys('i', '<C-Space>', '<C-n>')
1901-
-- - Wait for 'completionItem/resolve' request to be sent
1902-
sleep(default_info_delay + small_time)
1903-
type_keys('<C-y>')
1904-
eq(get_lines(), { 'Snippet from resolve' })
1934+
validate({ 'Snippet from resolve' })
1935+
1936+
-- Should handle error in 'completionItem/resolve' response
1937+
child.lua('_G.resolve_error = "Error"')
1938+
validate({ 'Snippet original' })
19051939
end
19061940

19071941
T['Snippets']['can be inserted together with additional text edits'] = function()

0 commit comments

Comments
 (0)