lsp: fix apply_text_document_edit test

lsp.util.buf_versions must be set in advance.
Use helper.insert to create an anonymous buffer,
so create a named buffer for testing without using insert.
This commit is contained in:
Hirokazu Hata
2020-05-03 13:01:00 +09:00
parent 6dc8398944
commit 0107a194fa

View File

@@ -818,58 +818,83 @@ describe('LSP', function()
end) end)
describe('apply_text_document_edit', function() describe('apply_text_document_edit', function()
local target_bufnr
before_each(function() before_each(function()
insert(dedent([[ target_bufnr = exec_lua [[
First line of text local bufnr = vim.fn.bufadd("fake/uri")
Second line of text]])) local lines = {"1st line of text", "2nd line of text"}
vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines)
return bufnr
]]
end) end)
it('correctly goes ahead with the edit when all is normal', function() it('correctly goes ahead with the edit if all is normal', function()
local text_document_edit = { local text_document_edit = {
edits = { edits = {
make_edit(0, 0, 0, 0, "hi") make_edit(0, 0, 0, 3, "First")
}, },
textDocument = { textDocument = {
uri = "file://fake/uri"; uri = "file://fake/uri";
version = 5 version = 5
} }
} }
exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit, 1) exec_lua([[
local args = {...}
local target_bufnr = args[2]
vim.lsp.util.buf_versions[target_bufnr] = 4
vim.lsp.util.apply_text_document_edit(...)
]], text_document_edit, target_bufnr)
eq({ eq({
'hiline of text'; 'First line of text';
'Second line of text'; '2nd line of text';
}, buf_lines(1)) }, buf_lines(target_bufnr))
end) end)
it('correctly goes ahead with the edit whe the version is nil', function() it('correctly goes ahead with the edit if the version is vim.NIL', function()
-- we get vim.NIL when we decode json null value.
local json = exec_lua[[
return vim.fn.json_decode("{ \"a\": 1, \"b\": null }")
]]
eq(json.b, exec_lua("return vim.NIL"))
local text_document_edit = { local text_document_edit = {
edits = { edits = {
make_edit(0, 0, 0, 0, "hi") make_edit(0, 0, 0, 3, "First")
}, },
textDocument = { textDocument = {
uri = "file://fake/uri"; uri = "file://fake/uri";
version = vim.NIL version = exec_lua("return vim.NIL")
} }
} }
exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit, 1) exec_lua([[
local args = {...}
local target_bufnr = args[2]
vim.lsp.util.buf_versions[target_bufnr] = vim.NIL
vim.lsp.util.apply_text_document_edit(...)
]], text_document_edit, target_bufnr)
eq({ eq({
'hiline of text'; 'First line of text';
'Second line of text'; '2nd line of text';
}, buf_lines(1)) }, buf_lines(target_bufnr))
end) end)
it('skips the edit if the version of the edit is behind the local buffer ', function() it('skips the edit if the version of the edit is behind the local buffer ', function()
local text_document_edit = { local text_document_edit = {
edits = { edits = {
make_edit(0, 0, 0, 0, "hi") make_edit(0, 0, 0, 3, "First")
}, },
textDocument = { textDocument = {
uri = "file://fake/uri"; uri = "file://fake/uri";
version = 1 version = 1
} }
} }
exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit, 1) exec_lua([[
local args = {...}
local target_bufnr = args[2]
vim.lsp.util.buf_versions[target_bufnr] = 2
vim.lsp.util.apply_text_document_edit(...)
]], text_document_edit, target_bufnr)
eq({ eq({
'First line of text'; '1st line of text';
'Second line of text'; '2nd line of text';
}, buf_lines(1)) }, buf_lines(target_bufnr))
end) end)
end) end)