255 lines
6.8 KiB
Plaintext
255 lines
6.8 KiB
Plaintext
Before:
|
|
let g:ale_lsp_next_message_id = 1
|
|
|
|
function CheckMessage(message, expected_method_name, ...) abort
|
|
if a:0 > 1
|
|
throw 'Too many arguments!'
|
|
endif
|
|
|
|
let l:match = matchlist(a:message, '\v^Content-Length: (\d+)' . "\r\n\r\n" . '(.+)$')
|
|
|
|
if empty(l:match)
|
|
Assert 0, 'Invalid message format: ' . a:message
|
|
endif
|
|
|
|
if strlen(l:match[2]) < str2nr(l:match[1])
|
|
Assert 0, 'Invalid Content-Length (' . l:match[1] . ') :' . a:message
|
|
endif
|
|
|
|
let l:expected_json = {
|
|
\ 'id': g:ale_lsp_next_message_id - 1,
|
|
\ 'jsonrpc': '2.0',
|
|
\ 'method': a:expected_method_name,
|
|
\}
|
|
|
|
if a:0 > 0
|
|
let l:expected_json.params = a:1
|
|
endif
|
|
|
|
AssertEqual l:expected_json, json_decode(l:match[2])
|
|
endfunction
|
|
|
|
function Range(start_line, start_char, end_line, end_char) abort
|
|
return {
|
|
\ 'start': {'line': a:start_line, 'character': a:start_char},
|
|
\ 'end': {'line': a:end_line, 'character': a:end_char},
|
|
\}
|
|
endfunction
|
|
|
|
After:
|
|
delfunction CheckMessage
|
|
delfunction Range
|
|
|
|
Execute(GetNextMessageID() should increment appropriately):
|
|
" We should get the initial ID, and increment a bit.
|
|
AssertEqual 1, ale#lsp#GetNextMessageID()
|
|
AssertEqual 2, ale#lsp#GetNextMessageID()
|
|
AssertEqual 3, ale#lsp#GetNextMessageID()
|
|
|
|
" Set the maximum ID.
|
|
let g:ale_lsp_next_message_id = 9223372036854775807
|
|
|
|
" When we hit the maximum ID, the next ID afterwards should be 1.
|
|
AssertEqual 9223372036854775807, ale#lsp#GetNextMessageID()
|
|
AssertEqual 1, ale#lsp#GetNextMessageID()
|
|
|
|
Execute(ale#lsp#CreateMessage() should create an appropriate message):
|
|
" 71 is the size in bytes for UTF-8, not the number of characters.
|
|
AssertEqual
|
|
\ "Content-Length: 71\r\n\r\n"
|
|
\ . '{"id":1,"jsonrpc":"2.0","method":"someMethod","params":{"foo":"barÜ"}}',
|
|
\ ale#lsp#CreateMessage('someMethod', {'foo': 'barÜ'})
|
|
" Check again to ensure that we use the next ID.
|
|
AssertEqual
|
|
\ "Content-Length: 71\r\n\r\n"
|
|
\ . '{"id":2,"jsonrpc":"2.0","method":"someMethod","params":{"foo":"barÜ"}}',
|
|
\ ale#lsp#CreateMessage('someMethod', {'foo': 'barÜ'})
|
|
|
|
Execute(ale#lsp#ReadMessage() should read messages correctly):
|
|
AssertEqual
|
|
\ {'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}},
|
|
\ ale#lsp#ReadMessage(
|
|
\ "Content-Length: 49\r\n\r\n"
|
|
\ . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}'
|
|
\ )
|
|
|
|
Execute(ale#lsp#message#Initialize() should return correct messages):
|
|
call CheckMessage(
|
|
\ ale#lsp#message#Initialize(123, '/foo/bar'),
|
|
\ 'initialize',
|
|
\ {
|
|
\ 'processId': 123,
|
|
\ 'rootUri': '/foo/bar',
|
|
\ 'capabilities': {},
|
|
\ }
|
|
\)
|
|
|
|
Execute(ale#lsp#message#Initialized() should return correct messages):
|
|
call CheckMessage(ale#lsp#message#Initialized(), 'initialized')
|
|
|
|
Execute(ale#lsp#message#Shutdown() should return correct messages):
|
|
call CheckMessage(ale#lsp#message#Shutdown(), 'shutdown')
|
|
|
|
Execute(ale#lsp#message#Exit() should return correct messages):
|
|
call CheckMessage(ale#lsp#message#Exit(), 'exit')
|
|
|
|
Execute(ale#lsp#message#DidOpen() should return correct messages):
|
|
call CheckMessage(
|
|
\ ale#lsp#message#DidOpen('/foo/bar', 'typescript', 123, 'foobar'),
|
|
\ 'textDocument/didOpen',
|
|
\ {
|
|
\ 'textDocument': {
|
|
\ 'uri': '/foo/bar',
|
|
\ 'languageId': 'typescript',
|
|
\ 'version': 123,
|
|
\ 'text': 'foobar',
|
|
\ },
|
|
\ }
|
|
\)
|
|
|
|
Execute(ale#lsp#message#DidChange() should return correct messages):
|
|
call CheckMessage(
|
|
\ ale#lsp#message#DidChange('/foo/bar', 123, 'foobar'),
|
|
\ 'textDocument/didChange',
|
|
\ {
|
|
\ 'textDocument': {
|
|
\ 'uri': '/foo/bar',
|
|
\ 'version': 123,
|
|
\ },
|
|
\ 'contentChanges': [{'text': 'foobar'}]
|
|
\ }
|
|
\)
|
|
|
|
Execute(ale#lsp#message#DidSave() should return correct messages):
|
|
call CheckMessage(
|
|
\ ale#lsp#message#DidSave('/foo/bar'),
|
|
\ 'textDocument/didSave',
|
|
\ {
|
|
\ 'textDocument': {
|
|
\ 'uri': '/foo/bar',
|
|
\ },
|
|
\ }
|
|
\)
|
|
|
|
Execute(ale#lsp#message#DidClose() should return correct messages):
|
|
call CheckMessage(
|
|
\ ale#lsp#message#DidClose('/foo/bar'),
|
|
\ 'textDocument/didClose',
|
|
\ {
|
|
\ 'textDocument': {
|
|
\ 'uri': '/foo/bar',
|
|
\ },
|
|
\ }
|
|
\)
|
|
|
|
Execute(ale#lsp#ReadDiagnostics() should handle errors):
|
|
AssertEqual ['filename.ts', [
|
|
\ {
|
|
\ 'type': 'E',
|
|
\ 'message': 'Something went wrong!',
|
|
\ 'lnum': 3,
|
|
\ 'col': 11,
|
|
\ 'end_lnum': 5,
|
|
\ 'end_col': 16,
|
|
\ 'nr': 'some-error',
|
|
\ }
|
|
\ ]],
|
|
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
|
|
\ {
|
|
\ 'severity': 1,
|
|
\ 'range': Range(2, 10, 4, 15),
|
|
\ 'code': 'some-error',
|
|
\ 'message': 'Something went wrong!',
|
|
\ },
|
|
\ ]})
|
|
|
|
Execute(ale#lsp#ReadDiagnostics() should handle warnings):
|
|
AssertEqual ['filename.ts', [
|
|
\ {
|
|
\ 'type': 'W',
|
|
\ 'message': 'Something went wrong!',
|
|
\ 'lnum': 2,
|
|
\ 'col': 4,
|
|
\ 'end_lnum': 2,
|
|
\ 'end_col': 4,
|
|
\ 'nr': 'some-warning',
|
|
\ }
|
|
\ ]],
|
|
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
|
|
\ {
|
|
\ 'severity': 2,
|
|
\ 'range': Range(1, 3, 1, 3),
|
|
\ 'code': 'some-warning',
|
|
\ 'message': 'Something went wrong!',
|
|
\ },
|
|
\ ]})
|
|
|
|
Execute(ale#lsp#ReadDiagnostics() should treat messages with missing severity as errors):
|
|
AssertEqual ['filename.ts', [
|
|
\ {
|
|
\ 'type': 'E',
|
|
\ 'message': 'Something went wrong!',
|
|
\ 'lnum': 3,
|
|
\ 'col': 11,
|
|
\ 'end_lnum': 5,
|
|
\ 'end_col': 16,
|
|
\ 'nr': 'some-error',
|
|
\ }
|
|
\ ]],
|
|
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
|
|
\ {
|
|
\ 'range': Range(2, 10, 4, 15),
|
|
\ 'code': 'some-error',
|
|
\ 'message': 'Something went wrong!',
|
|
\ },
|
|
\ ]})
|
|
|
|
Execute(ale#lsp#ReadDiagnostics() should handle messages without codes):
|
|
AssertEqual ['filename.ts', [
|
|
\ {
|
|
\ 'type': 'E',
|
|
\ 'message': 'Something went wrong!',
|
|
\ 'lnum': 3,
|
|
\ 'col': 11,
|
|
\ 'end_lnum': 5,
|
|
\ 'end_col': 16,
|
|
\ }
|
|
\ ]],
|
|
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
|
|
\ {
|
|
\ 'range': Range(2, 10, 4, 15),
|
|
\ 'message': 'Something went wrong!',
|
|
\ },
|
|
\ ]})
|
|
|
|
Execute(ale#lsp#ReadDiagnostics() should handle multiple messages):
|
|
AssertEqual ['filename.ts', [
|
|
\ {
|
|
\ 'type': 'E',
|
|
\ 'message': 'Something went wrong!',
|
|
\ 'lnum': 1,
|
|
\ 'col': 3,
|
|
\ 'end_lnum': 1,
|
|
\ 'end_col': 3,
|
|
\ },
|
|
\ {
|
|
\ 'type': 'W',
|
|
\ 'message': 'A warning',
|
|
\ 'lnum': 2,
|
|
\ 'col': 5,
|
|
\ 'end_lnum': 2,
|
|
\ 'end_col': 5,
|
|
\ },
|
|
\ ]],
|
|
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
|
|
\ {
|
|
\ 'range': Range(0, 2, 0, 2),
|
|
\ 'message': 'Something went wrong!',
|
|
\ },
|
|
\ {
|
|
\ 'severity': 2,
|
|
\ 'range': Range(1, 4, 1, 4),
|
|
\ 'message': 'A warning',
|
|
\ },
|
|
\ ]})
|