lsp_spec.lua (4000B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 local t_lsp = require('test.functional.plugin.lsp.testutil') 4 5 local clear = n.clear 6 local eq = t.eq 7 local exec_lua = n.exec_lua 8 9 local create_server_definition = t_lsp.create_server_definition 10 11 describe(':lsp', function() 12 before_each(function() 13 clear() 14 exec_lua(create_server_definition) 15 exec_lua(function() 16 local server = _G._create_server() 17 vim.lsp.config('dummy', { 18 filetypes = { 'lua' }, 19 cmd = server.cmd, 20 }) 21 vim.cmd('set ft=lua') 22 end) 23 end) 24 25 it('fails if runtime is missing/broken', function() 26 clear { 27 args_rm = { '-u' }, 28 args = { '-u', 'NONE' }, 29 env = { VIMRUNTIME = 'non-existent' }, 30 } 31 t.matches( 32 [[Vim%(lsp%):Lua: .*module 'vim%.lsp' not found:]], 33 vim.split(t.pcall_err(n.command, 'lsp enable dummy'), '\n')[1] 34 ) 35 end) 36 37 for _, test_with_arguments in ipairs({ true, false }) do 38 local test_message_suffix, lsp_command_suffix 39 if test_with_arguments then 40 test_message_suffix = ' with arguments' 41 lsp_command_suffix = ' dummy' 42 else 43 test_message_suffix = ' without arguments' 44 lsp_command_suffix = '' 45 end 46 47 it('enable' .. test_message_suffix, function() 48 local is_enabled = exec_lua(function() 49 vim.cmd('lsp enable' .. lsp_command_suffix) 50 return vim.lsp.is_enabled('dummy') 51 end) 52 eq(true, is_enabled) 53 end) 54 55 it('disable' .. test_message_suffix, function() 56 local is_enabled = exec_lua(function() 57 vim.lsp.enable('dummy') 58 vim.cmd('lsp disable' .. lsp_command_suffix) 59 return vim.lsp.is_enabled('dummy') 60 end) 61 eq(false, is_enabled) 62 end) 63 64 it('restart' .. test_message_suffix, function() 65 --- @type boolean, integer? 66 local ids_differ, attached_buffer_count = exec_lua(function() 67 vim.lsp.enable('dummy') 68 local old_id = vim.lsp.get_clients()[1].id 69 70 vim.cmd('lsp restart' .. lsp_command_suffix) 71 return vim.wait(1000, function() 72 local new_client = vim.lsp.get_clients()[1] 73 if new_client == nil then 74 return false 75 end 76 return old_id ~= new_client.id, #new_client.attached_buffers 77 end) 78 end) 79 eq(true, ids_differ) 80 eq(1, attached_buffer_count) 81 end) 82 83 it('stop' .. test_message_suffix, function() 84 local running_clients = exec_lua(function() 85 vim.lsp.enable('dummy') 86 vim.cmd('lsp stop' .. lsp_command_suffix) 87 vim.wait(1000, function() 88 return #vim.lsp.get_clients() == 0 89 end) 90 return #vim.lsp.get_clients() 91 end) 92 eq(0, running_clients) 93 end) 94 end 95 96 it('subcommand completion', function() 97 local completions = exec_lua(function() 98 return vim.fn.getcompletion('lsp ', 'cmdline') 99 end) 100 eq({ 'disable', 'enable', 'restart', 'stop' }, completions) 101 end) 102 103 it('argument completion', function() 104 local completions = exec_lua(function() 105 return vim.fn.getcompletion('lsp enable ', 'cmdline') 106 end) 107 eq({ 'dummy' }, completions) 108 end) 109 110 it('argument completion with spaces', function() 111 local cmd_length = exec_lua(function() 112 local server = _G._create_server() 113 vim.lsp.config('client name with space', { 114 cmd = server.cmd, 115 }) 116 local completion = vim.fn.getcompletion('lsp enable cl ', 'cmdline')[1] 117 return #vim.api.nvim_parse_cmd('lsp enable ' .. completion, {}).args 118 end) 119 eq(2, cmd_length) 120 end) 121 122 it('argument completion with special characters', function() 123 local cmd_length = exec_lua(function() 124 local server = _G._create_server() 125 vim.lsp.config('client"name|with\tsymbols', { 126 cmd = server.cmd, 127 }) 128 local completion = vim.fn.getcompletion('lsp enable cl ', 'cmdline')[1] 129 return #vim.api.nvim_parse_cmd('lsp enable ' .. completion, {}).args 130 end) 131 eq(2, cmd_length) 132 end) 133 end)