From 9248c90551ea29db80d5bf47f3327c19ac921481 Mon Sep 17 00:00:00 2001 From: Kilari Teja Date: Wed, 4 Apr 2018 08:34:45 +0530 Subject: [PATCH] Updates tests for langserver The behave tests have been updated to reflect the new jsonrpc handling mechanism. The scope of the tests remains unchanged. Related to https://github.com/coala/coala-vs-code/issues/27 --- tests/server.features/langserver.feature | 8 ++- .../server.features/steps/langserver_steps.py | 66 +++++++++++++++---- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/tests/server.features/langserver.feature b/tests/server.features/langserver.feature index d8b2c5e..5b3c5fc 100644 --- a/tests/server.features/langserver.feature +++ b/tests/server.features/langserver.feature @@ -6,8 +6,12 @@ Feature: langserver module When I send a initialize request to the server Then it should return the response with textDocumentSync - # TODO: Add positive test case. - Scenario: Test serve_did_save + Scenario: Test negative serve_did_save Given the LangServer instance When I send a did_save request about a non-existed file to the server Then it should send a publishDiagnostics request + + Scenario: Test positive serve_did_save + Given the LangServer instance + When I send a did_save request about a existing file to the server + Then it should send a publishDiagnostics request diff --git a/tests/server.features/steps/langserver_steps.py b/tests/server.features/steps/langserver_steps.py index 565b913..1894161 100644 --- a/tests/server.features/steps/langserver_steps.py +++ b/tests/server.features/steps/langserver_steps.py @@ -4,16 +4,17 @@ # ---------------------------------------------------------------------------- # STEPS: # ---------------------------------------------------------------------------- +import os import tempfile from behave import given, when, then -from coala_langserver.jsonrpc import TCPReadWriter +from pyls.jsonrpc import streams from coala_langserver.langserver import LangServer @given('the LangServer instance') def step_impl(context): context.f = tempfile.TemporaryFile() - context.langServer = LangServer(conn=TCPReadWriter(context.f, context.f)) + context.langServer = LangServer(context.f, context.f) @when('I send a initialize request to the server') @@ -27,16 +28,26 @@ def step_impl(context): 'id': 1, 'jsonrpc': '2.0' } - context.langServer.handle(1, request) + context.langServer._endpoint.consume(request) @then('it should return the response with textDocumentSync') def step_impl(context): context.f.seek(0) - response = context.langServer.read_message(1) - assert response is not None - assert response['result']['capabilities']['textDocumentSync'] is 1 - context.f.close() + context._passed = False + + def consumer(response): + assert response is not None + assert response['result']['capabilities']['textDocumentSync'] == 1 + context.f.close() + context._passed = True + + reader = streams.JsonRpcStreamReader(context.f) + reader.listen(consumer) + reader.close() + + if not context._passed: + assert False @when('I send a did_save request about a non-existed file to the server') @@ -50,14 +61,43 @@ def step_impl(context): }, 'jsonrpc': '2.0' } - context.langServer.handle(None, request) + context.langServer._endpoint.consume(request) + context._diagnosticsCount = 0 +@when('I send a did_save request about a existing file to the server') +def step_impl(context): + thisfile = os.path.realpath(__file__) + thisdir = os.path.dirname(thisfile) + parturi = os.path.join(thisdir, "../../resources", "unqualified.py") + + request = { + 'method': 'textDocument/didSave', + 'params': { + 'textDocument': { + 'uri': 'file://{}'.format(parturi) + } + }, + 'jsonrpc': '2.0' + } + context.langServer._endpoint.consume(request) + context._diagnosticsCount = 4 @then('it should send a publishDiagnostics request') def step_impl(context): context.f.seek(0) - response = context.langServer.read_message() - assert response is not None - assert response['method'] == 'textDocument/publishDiagnostics' - assert len(response['params']['diagnostics']) is 0 - context.f.close() + context._passed = False + + def consumer(response): + assert response is not None + assert response['method'] == 'textDocument/publishDiagnostics' + assert len(response['params']['diagnostics']) is context._diagnosticsCount + + context.f.close() + context._passed = True + + reader = streams.JsonRpcStreamReader(context.f) + reader.listen(consumer) + reader.close() + + if not context._passed: + assert False