From 5e9b8e1f29fbed13cf3b43627c5223c30ed68b4a Mon Sep 17 00:00:00 2001 From: Ce Gao Date: Sun, 12 Feb 2017 13:42:48 +0800 Subject: [PATCH] tests: Add test cases for jsonrpc --- .coveragerc | 1 + coala_langserver/fs.py | 53 --------- coala_langserver/langserver.py | 3 - tests/resources/{qulified.py => qualified.py} | 0 tests/server.features/jsonrpc.feature | 38 +++++++ tests/server.features/steps/jsonrpc_steps.py | 106 ++++++++++++++++++ 6 files changed, 145 insertions(+), 56 deletions(-) delete mode 100644 coala_langserver/fs.py rename tests/resources/{qulified.py => qualified.py} (100%) create mode 100644 tests/server.features/jsonrpc.feature create mode 100644 tests/server.features/steps/jsonrpc_steps.py diff --git a/.coveragerc b/.coveragerc index 4ae72a5..0534080 100644 --- a/.coveragerc +++ b/.coveragerc @@ -5,6 +5,7 @@ source = . [report] exclude_lines = if self.debug: + pass pragma: no cover raise NotImplementedError if __name__ == .__main__.: diff --git a/coala_langserver/fs.py b/coala_langserver/fs.py deleted file mode 100644 index 8a4d791..0000000 --- a/coala_langserver/fs.py +++ /dev/null @@ -1,53 +0,0 @@ -import os -from abc import ABC, abstractmethod -from typing import List - - -class FileException(Exception): - pass - - -class Entry: - '""Generic representation of a directory entry.""' - - def __init__(self, name, is_dir, size): - self.name = name - self.is_dir = is_dir - self.size = size - - -class FileSystem(ABC): - - @abstractmethod - def open(self, path: str) -> str: - pass - - @abstractmethod - def listdir(self, path: str) -> List[Entry]: - pass - - def walk(self, top: str): - dir = self.listdir(top) - files, dirs = [], [] - for e in dir: - if e.is_dir: - dirs.append(os.path.join(top, e.name)) - else: - files.append(os.path.join(top, e.name)) - yield top, dirs, files - for d in dirs: - yield from self.walk(d) - - -class LocalFileSystem(FileSystem): - - def open(self, path): - return open(path).read() - - def listdir(self, path): - entries = [] - names = os.listdir(path) - for n in names: - p = os.path.join(path, n) - entries.append(Entry(n, os.path.isdir(p), os.path.getsize(p))) - return entries diff --git a/coala_langserver/langserver.py b/coala_langserver/langserver.py index 582a084..b9448af 100644 --- a/coala_langserver/langserver.py +++ b/coala_langserver/langserver.py @@ -3,7 +3,6 @@ import socketserver import traceback -from .fs import LocalFileSystem from .jsonrpc import JSONRPC2Connection, ReadWriter, TCPReadWriter from .log import log from .coalashim import run_coala_with_specific_file @@ -32,8 +31,6 @@ class LangServer(JSONRPC2Connection): def __init__(self, conn=None): super().__init__(conn=conn) self.root_path = None - self.symbol_cache = None - self.fs = LocalFileSystem() def handle(self, _id, request): '""Handle the request from language client.""' diff --git a/tests/resources/qulified.py b/tests/resources/qualified.py similarity index 100% rename from tests/resources/qulified.py rename to tests/resources/qualified.py diff --git a/tests/server.features/jsonrpc.feature b/tests/server.features/jsonrpc.feature new file mode 100644 index 0000000..047f604 --- /dev/null +++ b/tests/server.features/jsonrpc.feature @@ -0,0 +1,38 @@ +Feature: jsonrpc module + jsonrpc is a module of language-server. + + Scenario: Test ReadWriter + Given the string + When I write it to ReadWriter + Then it should read from ReadWriter + + Scenario: Test ReadWriter + Given the string + When I write it to ReadWriter + Then it should readline from ReadWriter + + Scenario: Test TCPReadWriter + Given the string + When I write it to TCPReadWriter + Then it should read from TCPReadWriter + + Scenario: Test TCPReadWriter + Given the string + When I write it to TCPReadWriter + Then it should readline from TCPReadWriter + + Scenario: Test send_notification and read_message + Given the JSONRPC2Connection instance + When I write a notification to the JSONRPC2Connection + Then it should return the notification from JSONRPC2Connection + + Scenario: Test write_response + Given the JSONRPC2Connection instance + When I write a response to the JSONRPC2Connection + Then it should return the response from JSONRPC2Connection + + # TODO: block until we have generantee the unique request. +# Scenario: Test send_request +# Given the JSONRPC2Connection instance +# When I write a request to the JSONRPC2Connection with id +# Then it should return the request from JSONRPC2Connection with id diff --git a/tests/server.features/steps/jsonrpc_steps.py b/tests/server.features/steps/jsonrpc_steps.py new file mode 100644 index 0000000..0b7b443 --- /dev/null +++ b/tests/server.features/steps/jsonrpc_steps.py @@ -0,0 +1,106 @@ +# -*- coding: UTF-8 -*- + +# @mark.steps +# ---------------------------------------------------------------------------- +# STEPS: +# ---------------------------------------------------------------------------- +import tempfile +import json +from behave import given, when, then +from coala_langserver.jsonrpc import ReadWriter, TCPReadWriter, JSONRPC2Connection + + +@given('the string') +def step_impl(context): + context.str = 'test-cases' + + +@when('I write it to ReadWriter') +def step_impl(context): + context.f = tempfile.TemporaryFile(mode='w+') + context.readWriter = ReadWriter(context.f, context.f) + context.readWriter.write(context.str) + + +@then('it should read from ReadWriter') +def step_impl(context): + context.f.seek(0) + assert context.readWriter.read(len(context.str)) is not '' + context.f.close() + + +@then('it should readline from ReadWriter') +def step_impl(context): + context.f.seek(0) + assert context.readWriter.readline() is not '' + context.f.close() + + +@when('I write it to TCPReadWriter') +def step_impl(context): + context.f = tempfile.TemporaryFile() + context.readWriter = TCPReadWriter(context.f, context.f) + context.readWriter.write(context.str) + + +@then('it should read from TCPReadWriter') +def step_impl(context): + context.f.seek(0) + assert context.readWriter.read(len(context.str)) is not '' + context.f.close() + + +@then('it should readline from TCPReadWriter') +def step_impl(context): + context.f.seek(0) + assert context.readWriter.readline() is not '' + context.f.close() + + +@given('the JSONRPC2Connection instance') +def step_impl(context): + context.f = tempfile.TemporaryFile() + context.jsonConn = JSONRPC2Connection(conn=TCPReadWriter(context.f, context.f)) + + +@when('I write a request to the JSONRPC2Connection with id') +def step_impl(context): + context.jsonConn.send_request('mockMethod', { + 'mock': 'mock' + }) + + +@then('it should return the request from JSONRPC2Connection with id') +def step_impl(context): + context.f.seek(0) + assert context.jsonConn.read_message() is not None + context.f.close() + + +@when('I write a notification to the JSONRPC2Connection') +def step_impl(context): + context.jsonConn.send_notification('mockMethod', { + 'mock': 'mock' + }) + + +@then('it should return the notification from JSONRPC2Connection') +def step_impl(context): + context.f.seek(0) + assert context.jsonConn.read_message() is not None + context.f.close() + + +@when('I write a response to the JSONRPC2Connection') +def step_impl(context): + context.ID = 1 + context.jsonConn.write_response(context.ID, { + 'mock': 'mock' + }) + + +@then('it should return the response from JSONRPC2Connection') +def step_impl(context): + context.f.seek(0) + assert context.jsonConn.read_message(context.ID) is not None + context.f.close()