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

Commit 45df0f8

Browse files
committed
Updates tests of jsonrpc
The jsonrpc behave tests have been updated. The scope of the test has remained unchanged. Related to #27
1 parent d04298c commit 45df0f8

File tree

2 files changed

+93
-91
lines changed

2 files changed

+93
-91
lines changed

tests/server.features/jsonrpc.feature

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
Feature: jsonrpc module
22
jsonrpc is a module of language-server.
33

4-
Scenario: Test ReadWriter
5-
Given the string
6-
When I write it to ReadWriter
7-
Then it should read from ReadWriter
4+
Scenario: Test JsonRpcStreamWriter and JsonRpcStreamReader
5+
Given the message
6+
When I write it to JsonRpcStreamWriter
7+
Then it should read from JsonRpcStreamReader
88

9-
Scenario: Test ReadWriter
10-
Given the string
11-
When I write it to ReadWriter
12-
Then it should readline from ReadWriter
9+
Scenario: Test notification and disptacher
10+
Given a notification type rpc request
11+
When I send rpc request using JsonRpcStreamWriter
12+
Then it should invoke the notification consumer with args
1313

14-
Scenario: Test TCPReadWriter
15-
Given the string
16-
When I write it to TCPReadWriter
17-
Then it should read from TCPReadWriter
18-
19-
Scenario: Test TCPReadWriter
20-
Given the string
21-
When I write it to TCPReadWriter
22-
Then it should readline from TCPReadWriter
23-
24-
Scenario: Test send_notification and read_message
25-
Given the JSONRPC2Connection instance
26-
When I write a notification to the JSONRPC2Connection
27-
Then it should return the notification from JSONRPC2Connection
28-
29-
Scenario: Test write_response
30-
Given the JSONRPC2Connection instance
31-
When I write a response to the JSONRPC2Connection
32-
Then it should return the response from JSONRPC2Connection
14+
Scenario: Test rpc request and response
15+
Given a request type rpc request
16+
When I send rpc request using JsonRpcStreamWriter
17+
Then it should invoke consumer and return response
3318

3419
# TODO: block until we have generantee the unique request.
3520
# Scenario: Test send_request

tests/server.features/steps/jsonrpc_steps.py

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,101 +7,118 @@
77
import tempfile
88
import json
99
from behave import given, when, then
10-
from coala_langserver.jsonrpc import ReadWriter, TCPReadWriter, JSONRPC2Connection
10+
from pyls.jsonrpc import streams
11+
from pyls.jsonrpc import endpoint
12+
from pyls.jsonrpc import dispatchers
1113

14+
def issimilar(dicta, dictb):
15+
"""returns a boolean indicating if both the dicts
16+
contain equal and same items
17+
"""
1218

13-
@given('the string')
19+
# slow but safe for deeper evaluation
20+
return json.dumps(dicta) == json.dumps(dictb)
21+
22+
@given('the message')
1423
def step_impl(context):
15-
context.str = 'test-cases'
24+
context.message = {
25+
"simple": "test"
26+
}
1627

1728

18-
@when('I write it to ReadWriter')
29+
@when('I write it to JsonRpcStreamWriter')
1930
def step_impl(context):
20-
context.f = tempfile.TemporaryFile(mode='w+')
21-
context.readWriter = ReadWriter(context.f, context.f)
22-
context.readWriter.write(context.str)
31+
context.f = tempfile.TemporaryFile(mode='w+b')
32+
context.writer = streams.JsonRpcStreamWriter(context.f)
33+
context.writer.write(context.message)
2334

2435

25-
@then('it should read from ReadWriter')
36+
@then('it should read from JsonRpcStreamReader')
2637
def step_impl(context):
2738
context.f.seek(0)
28-
assert context.readWriter.read(len(context.str)) is not ''
29-
context.f.close()
39+
context._passed = False
3040

41+
def consumer(message):
42+
assert issimilar(context.message, message)
43+
context._passed = True
44+
context.writer.close()
3145

32-
@then('it should readline from ReadWriter')
33-
def step_impl(context):
34-
context.f.seek(0)
35-
assert context.readWriter.readline() is not ''
36-
context.f.close()
46+
reader = streams.JsonRpcStreamReader(context.f)
47+
reader.listen(consumer)
48+
reader.close()
3749

50+
if not context._passed:
51+
assert False
3852

39-
@when('I write it to TCPReadWriter')
53+
@given('a notification type rpc request')
54+
def step_impl(context):
55+
context.request = {
56+
"jsonrpc": "2.0",
57+
"method": "math/add",
58+
"params": {
59+
"a": 1,
60+
"b": 2
61+
}
62+
}
63+
64+
@when('I send rpc request using JsonRpcStreamWriter')
4065
def step_impl(context):
4166
context.f = tempfile.TemporaryFile()
42-
context.readWriter = TCPReadWriter(context.f, context.f)
43-
context.readWriter.write(context.str)
67+
context.writer = streams.JsonRpcStreamWriter(context.f)
68+
context.writer.write(context.request)
4469

4570

46-
@then('it should read from TCPReadWriter')
71+
@then('it should invoke the notification consumer with args')
4772
def step_impl(context):
4873
context.f.seek(0)
49-
assert context.readWriter.read(len(context.str)) is not ''
50-
context.f.close()
51-
74+
context._passed = False
5275

53-
@then('it should readline from TCPReadWriter')
54-
def step_impl(context):
55-
context.f.seek(0)
56-
assert context.readWriter.readline() is not ''
57-
context.f.close()
76+
class Example(dispatchers.MethodDispatcher):
5877

78+
def m_math__add(self, a, b):
79+
context.writer.close()
80+
context._passed = True
5981

60-
@given('the JSONRPC2Connection instance')
61-
def step_impl(context):
62-
context.f = tempfile.TemporaryFile()
63-
context.jsonConn = JSONRPC2Connection(conn=TCPReadWriter(context.f, context.f))
82+
epoint = endpoint.Endpoint(Example(), None)
83+
reader = streams.JsonRpcStreamReader(context.f)
84+
reader.listen(epoint.consume)
85+
reader.close()
6486

87+
if not context._passed:
88+
assert False
6589

66-
@when('I write a request to the JSONRPC2Connection with id')
90+
@given('a request type rpc request')
6791
def step_impl(context):
68-
context.jsonConn.send_request('mockMethod', {
69-
'mock': 'mock'
70-
})
71-
72-
73-
@then('it should return the request from JSONRPC2Connection with id')
92+
context.request = {
93+
"jsonrpc": "2.0",
94+
"id": 2148,
95+
"method": "math/add",
96+
"params": {
97+
"a": 1,
98+
"b": 2
99+
}
100+
}
101+
102+
@then('it should invoke consumer and return response')
74103
def step_impl(context):
75104
context.f.seek(0)
76-
assert context.jsonConn.read_message() is not None
77-
context.f.close()
105+
context._passed = False
78106

107+
class Example(dispatchers.MethodDispatcher):
79108

80-
@when('I write a notification to the JSONRPC2Connection')
81-
def step_impl(context):
82-
context.jsonConn.send_notification('mockMethod', {
83-
'mock': 'mock'
84-
})
109+
def m_math__add(self, a, b):
110+
return a + b
85111

112+
def consumer(message):
113+
assert message["result"] == sum(context.request["params"].values())
114+
context.writer.close()
115+
context._passed = True
86116

87-
@then('it should return the notification from JSONRPC2Connection')
88-
def step_impl(context):
89-
context.f.seek(0)
90-
assert context.jsonConn.read_message() is not None
91-
context.f.close()
117+
epoint = endpoint.Endpoint(Example(), consumer)
118+
reader = streams.JsonRpcStreamReader(context.f)
119+
reader.listen(epoint.consume)
120+
reader.close()
92121

122+
if not context._passed:
123+
assert False
93124

94-
@when('I write a response to the JSONRPC2Connection')
95-
def step_impl(context):
96-
# BUG: when id = 0
97-
context.ID = 1
98-
context.jsonConn.write_response(context.ID, {
99-
'mock': 'mock'
100-
})
101-
102-
103-
@then('it should return the response from JSONRPC2Connection')
104-
def step_impl(context):
105-
context.f.seek(0)
106-
assert context.jsonConn.read_message(context.ID) is not None
107-
context.f.close()

0 commit comments

Comments
 (0)