diff options
author | w0rp <devw0rp@gmail.com> | 2017-05-08 22:18:28 +0100 |
---|---|---|
committer | w0rp <devw0rp@gmail.com> | 2017-05-08 22:18:28 +0100 |
commit | 28c6ec9cad3064966ff70c9da95c96364118eb57 (patch) | |
tree | 6d970e9455670827dced35eb59bcf62b03282775 /test | |
parent | cd79ced839fa2a5c3fc407d7cbe0cdf6734d17da (diff) | |
download | ale-28c6ec9cad3064966ff70c9da95c96364118eb57.zip |
#517 - Implement LSP chunked message parsing, sending messages to sockets, and callbacks
Diffstat (limited to 'test')
-rw-r--r-- | test/lsp/test_lsp_client_messages.vader | 78 | ||||
-rw-r--r-- | test/lsp/test_lsp_connections.vader | 111 | ||||
-rw-r--r-- | test/lsp/test_read_lsp_diagnostics.vader | 121 | ||||
-rw-r--r-- | test/test_lsp_client_messages.vader | 254 |
4 files changed, 310 insertions, 254 deletions
diff --git a/test/lsp/test_lsp_client_messages.vader b/test/lsp/test_lsp_client_messages.vader new file mode 100644 index 00000000..de18a4b8 --- /dev/null +++ b/test/lsp/test_lsp_client_messages.vader @@ -0,0 +1,78 @@ +Execute(ale#lsp#message#Initialize() should return correct messages): + AssertEqual + \ [ + \ 0, + \ 'initialize', + \ { + \ 'processId': getpid(), + \ 'rootUri': '/foo/bar', + \ 'capabilities': {}, + \ } + \ ], + \ ale#lsp#message#Initialize('/foo/bar') + +Execute(ale#lsp#message#Initialized() should return correct messages): + AssertEqual [1, 'initialized'], ale#lsp#message#Initialized() + +Execute(ale#lsp#message#Shutdown() should return correct messages): + AssertEqual [0, 'shutdown'], ale#lsp#message#Shutdown() + +Execute(ale#lsp#message#Exit() should return correct messages): + AssertEqual [1, 'exit'], ale#lsp#message#Exit(), + +Execute(ale#lsp#message#DidOpen() should return correct messages): + AssertEqual + \ [ + \ 1, + \ 'textDocument/didOpen', + \ { + \ 'textDocument': { + \ 'uri': '/foo/bar', + \ 'languageId': 'typescript', + \ 'version': 123, + \ 'text': 'foobar', + \ }, + \ } + \ ], + \ ale#lsp#message#DidOpen('/foo/bar', 'typescript', 123, 'foobar') + +Execute(ale#lsp#message#DidChange() should return correct messages): + AssertEqual + \ [ + \ 1, + \ 'textDocument/didChange', + \ { + \ 'textDocument': { + \ 'uri': '/foo/bar', + \ 'version': 123, + \ }, + \ 'contentChanges': [{'text': 'foobar'}], + \ } + \ ], + \ ale#lsp#message#DidChange('/foo/bar', 123, 'foobar') + +Execute(ale#lsp#message#DidSave() should return correct messages): + AssertEqual + \ [ + \ 1, + \ 'textDocument/didSave', + \ { + \ 'textDocument': { + \ 'uri': '/foo/bar', + \ }, + \ } + \ ], + \ ale#lsp#message#DidSave('/foo/bar') + +Execute(ale#lsp#message#DidClose() should return correct messages): + AssertEqual + \ [ + \ 1, + \ 'textDocument/didClose', + \ { + \ 'textDocument': { + \ 'uri': '/foo/bar', + \ }, + \ } + \ ], + \ ale#lsp#message#DidClose('/foo/bar') diff --git a/test/lsp/test_lsp_connections.vader b/test/lsp/test_lsp_connections.vader new file mode 100644 index 00000000..36a21bd2 --- /dev/null +++ b/test/lsp/test_lsp_connections.vader @@ -0,0 +1,111 @@ +Before: + let g:ale_lsp_next_message_id = 1 + +After: + unlet! b:data + +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#CreateMessageData() should create an appropriate message): + " 71 is the size in bytes for UTF-8, not the number of characters. + AssertEqual + \ [ + \ 1, + \ "Content-Length: 71\r\n\r\n" + \ . '{"id":1,"jsonrpc":"2.0","method":"someMethod","params":{"foo":"barÜ"}}', + \ ], + \ ale#lsp#CreateMessageData([0, 'someMethod', {'foo': 'barÜ'}]) + " Check again to ensure that we use the next ID. + AssertEqual + \ [ + \ 2, + \ "Content-Length: 71\r\n\r\n" + \ . '{"id":2,"jsonrpc":"2.0","method":"someMethod","params":{"foo":"barÜ"}}', + \ ], + \ ale#lsp#CreateMessageData([0, 'someMethod', {'foo': 'barÜ'}]) + +Execute(ale#lsp#CreateMessageData() should create messages without params): + AssertEqual + \ [ + \ 1, + \ "Content-Length: 51\r\n\r\n" + \ . '{"id":1,"jsonrpc":"2.0","method":"someOtherMethod"}', + \ ], + \ ale#lsp#CreateMessageData([0, 'someOtherMethod']) + +Execute(ale#lsp#CreateMessageData() should create notifications): + AssertEqual + \ [ + \ 0, + \ "Content-Length: 55\r\n\r\n" + \ . '{"id":null,"jsonrpc":"2.0","method":"someNotification"}', + \ ], + \ ale#lsp#CreateMessageData([1, 'someNotification']) + AssertEqual + \ [ + \ 0, + \ "Content-Length: 78\r\n\r\n" + \ . '{"id":null,"jsonrpc":"2.0","method":"someNotification","params":{"foo":"bar"}}', + \ ], + \ ale#lsp#CreateMessageData([1, 'someNotification', {'foo': 'bar'}]) + +Execute(ale#lsp#ReadMessageData() should read single whole messages): + AssertEqual + \ ['', [{'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}}]], + \ ale#lsp#ReadMessageData( + \ "Content-Length: 49\r\n\r\n" + \ . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}' + \ ) + +Execute(ale#lsp#ReadMessageData() should ignore other headers): + AssertEqual + \ ['', [{'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}}]], + \ ale#lsp#ReadMessageData( + \ "First-Header: 49\r\n" + \ . "Content-Length: 49\r\n" + \ . "Other-Header: 49\r\n" + \ . "\r\n" + \ . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}' + \ ) + +Execute(ale#lsp#ReadMessageData() should handle partial messages): + let b:data = "Content-Length: 49\r\n\r\n" . '{"id":2,"jsonrpc":"2.0","result":' + + AssertEqual [b:data, []], ale#lsp#ReadMessageData(b:data) + +Execute(ale#lsp#ReadMessageData() should handle multiple messages): + AssertEqual + \ ['', [ + \ {'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}}, + \ {'id': 2, 'jsonrpc': '2.0', 'result': {'foo123': 'barÜ'}}, + \ ]], + \ ale#lsp#ReadMessageData( + \ "Content-Length: 49\r\n\r\n" + \ . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}' + \ . "Content-Length: 52\r\n\r\n" + \ . '{"id":2,"jsonrpc":"2.0","result":{"foo123":"barÜ"}}' + \ ) + +Execute(ale#lsp#ReadMessageData() should handle a message with part of a second message): + let b:data = "Content-Length: 52\r\n\r\n" . '{"id":2,"jsonrpc":"2.' + + AssertEqual + \ [b:data, [ + \ {'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}}, + \ ]], + \ ale#lsp#ReadMessageData( + \ "Content-Length: 49\r\n\r\n" + \ . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}' + \ . b:data + \ ) diff --git a/test/lsp/test_read_lsp_diagnostics.vader b/test/lsp/test_read_lsp_diagnostics.vader new file mode 100644 index 00000000..b52da1bd --- /dev/null +++ b/test/lsp/test_read_lsp_diagnostics.vader @@ -0,0 +1,121 @@ +Before: + 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 Range + +Execute(ale#lsp#response#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#response#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [ + \ { + \ 'severity': 1, + \ 'range': Range(2, 10, 4, 15), + \ 'code': 'some-error', + \ 'message': 'Something went wrong!', + \ }, + \ ]}) + +Execute(ale#lsp#response#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#response#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [ + \ { + \ 'severity': 2, + \ 'range': Range(1, 3, 1, 3), + \ 'code': 'some-warning', + \ 'message': 'Something went wrong!', + \ }, + \ ]}) + +Execute(ale#lsp#response#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#response#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [ + \ { + \ 'range': Range(2, 10, 4, 15), + \ 'code': 'some-error', + \ 'message': 'Something went wrong!', + \ }, + \ ]}) + +Execute(ale#lsp#response#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#response#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [ + \ { + \ 'range': Range(2, 10, 4, 15), + \ 'message': 'Something went wrong!', + \ }, + \ ]}) + +Execute(ale#lsp#response#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#response#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', + \ }, + \ ]}) diff --git a/test/test_lsp_client_messages.vader b/test/test_lsp_client_messages.vader deleted file mode 100644 index d6b398c4..00000000 --- a/test/test_lsp_client_messages.vader +++ /dev/null @@ -1,254 +0,0 @@ -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', - \ }, - \ ]}) |