ale/test/lsp/test_lsp_custom_request.vader

137 lines
3.6 KiB
Plaintext
Raw Normal View History

Before:
runtime autoload/ale/linter.vim
runtime autoload/ale/lsp.vim
runtime autoload/ale/lsp_linter.vim
let g:address = 'ccls_address'
let g:callback_result = 0
let g:conn_id = -1
let g:executable = 'ccls'
2019-05-31 19:56:38 +00:00
let g:executable_or_address = ''
let g:linter_name = 'ccls'
let g:magic_number = 42
2019-05-31 19:56:38 +00:00
let g:message_list = []
let g:message_id = 1
let g:method = '$ccls/call'
let g:parameters = {}
2019-05-31 19:56:38 +00:00
let g:project_root = '/project/root'
let g:response = ''
let g:return_value = -1
let g:linter_list = [{
\ 'output_stream': 'stdout',
\ 'lint_file': 0,
\ 'language': 'cpp',
\ 'name': g:linter_name,
2019-05-31 19:56:38 +00:00
\ 'project_root': {b -> g:project_root},
\ 'aliases': [],
\ 'language_callback': {b -> 'cpp'},
\ 'read_buffer': 1,
\ 'command': '%e'
\ }]
" Encode dictionary to jsonrpc
function! Encode(obj) abort
let l:body = json_encode(a:obj)
return 'Content-Length: ' . strlen(l:body) . "\r\n\r\n" . l:body
endfunction
2019-05-31 19:56:38 +00:00
" Replace the StartLSP function to mock an LSP linter
function! ale#lsp_linter#StartLSP(buffer, linter, Callback) abort
let g:conn_id = ale#lsp#Register(g:executable_or_address, g:project_root, {})
call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer)
call ale#lsp#HandleMessage(g:conn_id, Encode({'method': 'initialize'}))
2019-05-31 19:56:38 +00:00
let l:details = {
\ 'command': g:executable,
\ 'buffer': a:buffer,
\ 'connection_id': g:conn_id,
\ 'project_root': g:project_root,
\}
call ale#lsp_linter#OnInit(a:linter, l:details, a:Callback)
endfunction
" Dummy callback
function! Callback(response) abort
let g:callback_result = a:response.result.value
endfunction
" Replace the GetAll function to mock an LSP linter
function! ale#linter#GetAll(filetype) abort
return g:linter_list
endfunction
" Replace the Send function to mock an LSP linter
function! ale#lsp#Send(conn_id, message) abort
2019-05-31 19:56:38 +00:00
call add(g:message_list, a:message)
return g:message_id
endfunction
" Code for a test case
function! TestCase() abort
" Test sending a custom request
2019-05-31 19:56:38 +00:00
let g:return_value = ale#lsp_linter#SendRequest(
\ bufnr('%'),
\ g:linter_name,
\ g:method,
\ g:parameters,
\ function('Callback'))
2019-05-31 19:56:38 +00:00
Assert index(g:message_list, [0, g:method, g:parameters]) >= 0
" Mock an incoming response to the request
2019-05-31 19:56:38 +00:00
let g:response = Encode({
\ 'id': g:message_id,
\ 'jsonrpc': '2.0',
\ 'result': {'value': g:magic_number}
\ })
call ale#lsp#HandleMessage(g:conn_id, g:response)
AssertEqual
\ g:magic_number,
\ g:callback_result
endfunction
After:
if g:conn_id isnot v:null
call ale#lsp#RemoveConnectionWithID(g:conn_id)
endif
unlet! g:callback_result
unlet! g:conn_id
unlet! g:executable
unlet! g:linter_name
unlet! g:magic_number
2019-05-31 19:56:38 +00:00
unlet! g:message_list
unlet! g:message_id
unlet! g:method
unlet! g:parameters
2019-05-31 19:56:38 +00:00
unlet! g:project_root
unlet! g:response
unlet! g:return_value
delfunction Encode
delfunction Callback
delfunction TestCase
runtime autoload/ale/linter.vim
runtime autoload/ale/lsp.vim
runtime autoload/ale/lsp_linter.vim
Given cpp(Empty cpp file):
Execute(Test custom request to server identified by executable):
2019-05-31 19:56:38 +00:00
let g:executable_or_address = g:executable
let g:linter_list[0].executable = {b -> g:executable}
let g:linter_list[0].lsp = 'stdio'
call TestCase()
Given cpp(Empty cpp file):
Execute(Test custom request to server identified by address):
2019-05-31 19:56:38 +00:00
let g:executable_or_address = g:address
let g:linter_list[0].address = {b -> g:address}
let g:linter_list[0].lsp = 'socket'
call TestCase()