Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Commit

Permalink
WIP: Add test cases for jsonrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
gaocegege committed Feb 11, 2017
1 parent b82cca6 commit 8d36176
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
File renamed without changes.
42 changes: 42 additions & 0 deletions tests/server.features/jsonrpc.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 read_message without id
Given the JSONRPC2Connection instance
When I write a message to the JSONRPC2Connection without id
Then it should return it from JSONRPC2Connection

Scenario: Test read_message with id
Given the JSONRPC2Connection instance
When I write a message to the JSONRPC2Connection with id
Then it should return it from JSONRPC2Connection with id

Scenario: Test write_response with id
Given the JSONRPC2Connection instance
When I write a message to the JSONRPC2Connection with id
Then it should get it

Scenario: Test send_notification
Given the JSONRPC2Connection instance
When I write a notification to the JSONRPC2Connection
Then it should get it
85 changes: 85 additions & 0 deletions tests/server.features/steps/jsonrpc_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- 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):
assert context.readWriter.read(len(context.str)) is not ''
context.f.close()


@then('it should readline from ReadWriter')
def step_impl(context):
assert context.readWriter.readline() is not ''


@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):
assert context.readWriter.read(len(context.str)) is not ''
context.f.close()


@then('it should readline from TCPReadWriter')
def step_impl(context):
assert context.readWriter.readline() is not ''


@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 message to the JSONRPC2Connection without id')
def step_impl(context):
context.id = 10
mockMethod = 'method'
mockParams = {
'mock': 'mock'
}
body = {
'jsonrpc': '2.0',
'id': context.id,
'method': mockMethod,
'params': mockParams,
}
body = json.dumps(body, separators=(',', ':'))
content_length = len(body)
request = (
'Content-Length: {}\r\n'
'Content-Type: application/vscode-jsonrpc; charset=utf8\r\n\r\n'
'{}'.format(content_length, body))
context.jsonConn.conn.write(request)


@then('it should return it from JSONRPC2Connection')
def step_impl(context):
assert context.jsonConn.read_message(context.id) is not None
context.f.close()

0 comments on commit 8d36176

Please sign in to comment.