local protocol = require 'vim.lsp.protocol'
-- Logs to $NVIM_LOG_FILE.
--
-- TODO(justinmk): remove after https://github.com/neovim/neovim/pull/7062
local function log(loglevel, area, msg)
vim.fn.writefile({ string.format('%s %s: %s', loglevel, area, msg) }, vim.env.NVIM_LOG_FILE, 'a')
end
local function message_parts(sep, ...)
local parts = {}
for i = 1, select('#', ...) do
local arg = select(i, ...)
if arg ~= nil then
table.insert(parts, arg)
end
end
return table.concat(parts, sep)
end
-- Assert utility methods
local function assert_eq(a, b, ...)
if not vim.deep_equal(a, b) then
error(
message_parts(
': ',
...,
'assert_eq failed',
string.format(
'left == %q, right == %q',
table.concat(vim.split(vim.inspect(a), '\n'), ''),
table.concat(vim.split(vim.inspect(b), '\n'), '')
)
)
)
end
end
local function format_message_with_content_length(encoded_message)
return table.concat {
'Content-Length: ',
tostring(#encoded_message),
'\r\n\r\n',
encoded_message,
}
end
local function read_message()
local line = io.read('*l')
local length = line:lower():match('content%-length:%s*(%d+)')
return vim.json.decode(io.read(2 + length):sub(2))
end
local function send(payload)
io.stdout:write(format_message_with_content_length(vim.json.encode(payload)))
end
local function respond(id, err, result)
assert(type(id) == 'number', 'id must be a number')
send { jsonrpc = '2.0', id = id, error = err, result = result }
end
local function notify(method, params)
assert(type(method) == 'string', 'method must be a string')
send { method = method, params = params or {} }
end
local function expect_notification(method, params, ...)
local message = read_message()
assert_eq(method, message.method, ..., 'expect_notification', 'method')
if params then
assert_eq(params, message.params, ..., 'expect_notification', method, 'params')
assert_eq(
{ jsonrpc = '2.0', method = method, params = params },
message,
...,
'expect_notification',
'message'
)
end
end
local function expect_request(method, handler, ...)
local req = read_message()
assert_eq(method, req.method, ..., 'expect_request', 'method')
local err, result = handler(req.params)
respond(req.id, err, result)
end
io.stderr:setvbuf('no')
local function skeleton(config)
local on_init = assert(config.on_init)
local body = assert(config.body)
expect_request('initialize', function(params)
return nil, on_init(params)
end)
expect_notification('initialized', {})
body()
expect_request('shutdown', function()
return nil, {}
end)
expect_notification('exit', nil)
end
-- The actual tests.
local tests = {}
function tests.basic_init()
skeleton {
on_init = function(params)
assert_eq(params.workDoneToken, '1')
return {
capabilities = {
textDocumentSync = protocol.TextDocumentSyncKind.None,
},
}
end,
body = function()
notify('test')
end,
}
end
function tests.basic_init_did_change_configuration()
skeleton({
on_init = function(_)
return {
capabilities = {},
}
end,
body = function()
expect_notification('workspace/didChangeConfiguration', { settings = { dummy = 1 } })
end,
})
end
function tests.check_workspace_configuration()
skeleton {
on_init = function(_params)
return { capabilities = {} }
end,
body = function()
notify('start')
notify('workspace/configuration', {
items = {
{ section = 'testSetting1' },
{ section = 'testSetting2' },
{ section = 'test.Setting3' },
{ section = 'test.Setting4' },
},
})
expect_notification('workspace/configuration', { true, false, 'nested', vim.NIL })
notify('shutdown')
end,
}
end
function tests.prepare_rename_nil()
skeleton {
on_init = function()
return {
capabilities = {
renameProvider = {
prepareProvider = true,
},
},
}
end,
body = function()
notify('start')
expect_request('textDocument/prepareRename', function()
return nil, nil
end)
notify('shutdown')
end,
}
end
function tests.prepare_rename_placeholder()
skeleton {
on_init = function()
return {
capabilities = {
renameProvider = {
prepareProvider = true,
},
},
}
end,
body = function()
notify('start')
expect_request('textDocument/prepareRename', function()
return nil, { placeholder = 'placeholder' }
end)
expect_request('textDocument/rename', function(params)
assert_eq(params.newName, 'renameto')
return nil, nil
end)
notify('shutdown')
end,
}
end
function tests.prepare_rename_range()
skeleton {
on_init = function()
return {
capabilities = {
renameProvider = {
prepareProvider = true,
},
},
}
end,
body = function()
notify('start')
expect_request('textDocument/prepareRename', function()
return nil,
{
start = { line = 1, character = 8 },
['end'] = { line = 1, character = 12 },
}
end)
expect_request('textDocument/rename', function
|