|
7 | 7 | import tempfile
|
8 | 8 | import json
|
9 | 9 | 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 |
11 | 13 |
|
| 14 | +def issimilar(dicta, dictb): |
| 15 | + """returns a boolean indicating if both the dicts |
| 16 | + contain equal and same items |
| 17 | + """ |
12 | 18 |
|
13 |
| -@given('the string') |
| 19 | + # slow but safe for deeper evaluation |
| 20 | + return json.dumps(dicta) == json.dumps(dictb) |
| 21 | + |
| 22 | +@given('the message') |
14 | 23 | def step_impl(context):
|
15 |
| - context.str = 'test-cases' |
| 24 | + context.message = { |
| 25 | + "simple": "test" |
| 26 | + } |
16 | 27 |
|
17 | 28 |
|
18 |
| -@when('I write it to ReadWriter') |
| 29 | +@when('I write it to JsonRpcStreamWriter') |
19 | 30 | 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) |
23 | 34 |
|
24 | 35 |
|
25 |
| -@then('it should read from ReadWriter') |
| 36 | +@then('it should read from JsonRpcStreamReader') |
26 | 37 | def step_impl(context):
|
27 | 38 | context.f.seek(0)
|
28 |
| - assert context.readWriter.read(len(context.str)) is not '' |
29 |
| - context.f.close() |
| 39 | + context._passed = False |
30 | 40 |
|
| 41 | + def consumer(message): |
| 42 | + assert issimilar(context.message, message) |
| 43 | + context._passed = True |
| 44 | + context.writer.close() |
31 | 45 |
|
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() |
37 | 49 |
|
| 50 | + if not context._passed: |
| 51 | + assert False |
38 | 52 |
|
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') |
40 | 65 | def step_impl(context):
|
41 | 66 | 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) |
44 | 69 |
|
45 | 70 |
|
46 |
| -@then('it should read from TCPReadWriter') |
| 71 | +@then('it should invoke the notification consumer with args') |
47 | 72 | def step_impl(context):
|
48 | 73 | context.f.seek(0)
|
49 |
| - assert context.readWriter.read(len(context.str)) is not '' |
50 |
| - context.f.close() |
51 |
| - |
| 74 | + context._passed = False |
52 | 75 |
|
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): |
58 | 77 |
|
| 78 | + def m_math__add(self, a, b): |
| 79 | + context.writer.close() |
| 80 | + context._passed = True |
59 | 81 |
|
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() |
64 | 86 |
|
| 87 | + if not context._passed: |
| 88 | + assert False |
65 | 89 |
|
66 |
| -@when('I write a request to the JSONRPC2Connection with id') |
| 90 | +@given('a request type rpc request') |
67 | 91 | 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') |
74 | 103 | def step_impl(context):
|
75 | 104 | context.f.seek(0)
|
76 |
| - assert context.jsonConn.read_message() is not None |
77 |
| - context.f.close() |
| 105 | + context._passed = False |
78 | 106 |
|
| 107 | + class Example(dispatchers.MethodDispatcher): |
79 | 108 |
|
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 |
85 | 111 |
|
| 112 | + def consumer(message): |
| 113 | + assert message["result"] == sum(context.request["params"].values()) |
| 114 | + context.writer.close() |
| 115 | + context._passed = True |
86 | 116 |
|
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() |
92 | 121 |
|
| 122 | + if not context._passed: |
| 123 | + assert False |
93 | 124 |
|
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