-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathconnection.test.ts
186 lines (163 loc) · 6.25 KB
/
connection.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as assert from 'assert';
import { Duplex } from 'stream';
import {
InitializeParams, InitializeRequest, InitializeResult, createProtocolConnection, StreamMessageReader, StreamMessageWriter, Logger, ProtocolConnection,
WorkDoneProgressBegin, WorkDoneProgressReport, WorkDoneProgressEnd
} from '../main';
import { DocumentSymbolRequest, DocumentSymbolParams } from '../../common/protocol';
import { ProgressType } from 'vscode-jsonrpc';
import { SymbolInformation, SymbolKind } from 'vscode-languageserver-types';
class NullLogger implements Logger {
error(_message: string): void {
}
warn(_message: string): void {
}
info(_message: string): void {
}
log(_message: string): void {
}
}
class TestStream extends Duplex {
_write(chunk: string, _encoding: string, done: () => void) {
this.emit('data', chunk);
done();
}
_read(_size: number) {
}
}
suite('Connection Tests', () => {
test('Ensure proper param passing', async() => {
const up = new TestStream();
const down = new TestStream();
const logger = new NullLogger();
const serverConnection = createProtocolConnection(new StreamMessageReader(up), new StreamMessageWriter(down), logger);
const clientConnection = createProtocolConnection(new StreamMessageReader(down), new StreamMessageWriter(up), logger);
serverConnection.listen();
clientConnection.listen();
let paramsCorrect: boolean = false;
serverConnection.onRequest(InitializeRequest.type, (params) => {
paramsCorrect = !Array.isArray(params);
const result: InitializeResult = {
capabilities: {
}
};
return result;
});
const init: InitializeParams = {
rootUri: 'file:///home/dirkb',
processId: 1,
capabilities: {},
workspaceFolders: null,
};
await clientConnection.sendRequest(InitializeRequest.type, init);
assert.ok(paramsCorrect, 'Parameters are transferred correctly');
});
});
suite('Partial result tests', () => {
let serverConnection: ProtocolConnection;
let clientConnection: ProtocolConnection;
const progressType: ProgressType<any> = new ProgressType();
setup(() => {
const up = new TestStream();
const down = new TestStream();
const logger = new NullLogger();
serverConnection = createProtocolConnection(new StreamMessageReader(up), new StreamMessageWriter(down), logger);
clientConnection = createProtocolConnection(new StreamMessageReader(down), new StreamMessageWriter(up), logger);
serverConnection.listen();
clientConnection.listen();
});
test('Token provided', async () => {
serverConnection.onRequest(DocumentSymbolRequest.type, (params) => {
assert.ok(params.partialResultToken === '3b1db4c9-e011-489e-a9d1-0653e64707c2');
return [];
});
const params: DocumentSymbolParams = {
textDocument: { uri: 'file:///abc.txt' },
partialResultToken: '3b1db4c9-e011-489e-a9d1-0653e64707c2'
};
await clientConnection.sendRequest(DocumentSymbolRequest.type, params);
});
test('Result reported', async () => {
const result: SymbolInformation = {
name: 'abc',
kind: SymbolKind.Class,
location: {
uri: 'file:///abc.txt',
range: { start: { line: 0, character: 1 }, end: { line: 2, character: 3} }
}
};
serverConnection.onRequest(DocumentSymbolRequest.type, async (params) => {
assert.ok(params.partialResultToken === '3b1db4c9-e011-489e-a9d1-0653e64707c2');
await serverConnection.sendProgress(progressType, params.partialResultToken!, [ result ]);
return [];
});
const params: DocumentSymbolParams = {
textDocument: { uri: 'file:///abc.txt' },
partialResultToken: '3b1db4c9-e011-489e-a9d1-0653e64707c2'
};
let progressOK: boolean = false;
clientConnection.onProgress(progressType, '3b1db4c9-e011-489e-a9d1-0653e64707c2', (values) => {
progressOK = (values !== undefined && values.length === 1);
});
await clientConnection.sendRequest(DocumentSymbolRequest.type, params);
assert.ok(progressOK);
});
});
suite('Work done tests', () => {
let serverConnection: ProtocolConnection;
let clientConnection: ProtocolConnection;
const progressType: ProgressType<WorkDoneProgressBegin | WorkDoneProgressReport | WorkDoneProgressEnd> = new ProgressType();
setup(() => {
const up = new TestStream();
const down = new TestStream();
const logger = new NullLogger();
serverConnection = createProtocolConnection(new StreamMessageReader(up), new StreamMessageWriter(down), logger);
clientConnection = createProtocolConnection(new StreamMessageReader(down), new StreamMessageWriter(up), logger);
serverConnection.listen();
clientConnection.listen();
});
test('Token provided', async () => {
serverConnection.onRequest(DocumentSymbolRequest.type, (params) => {
assert.ok(params.workDoneToken === '3b1db4c9-e011-489e-a9d1-0653e64707c2');
return [];
});
const params: DocumentSymbolParams = {
textDocument: { uri: 'file:///abc.txt' },
workDoneToken: '3b1db4c9-e011-489e-a9d1-0653e64707c2'
};
await clientConnection.sendRequest(DocumentSymbolRequest.type, params);
});
test('Result reported', async () => {
serverConnection.onRequest(DocumentSymbolRequest.type, async (params) => {
assert.ok(params.workDoneToken === '3b1db4c9-e011-489e-a9d1-0653e64707c2');
await serverConnection.sendProgress(progressType, params.workDoneToken!, {
kind: 'begin',
title: 'progress'
});
await serverConnection.sendProgress(progressType, params.workDoneToken!, {
kind: 'report',
message: 'message'
});
await serverConnection.sendProgress(progressType, params.workDoneToken!, {
kind: 'end',
message: 'message'
});
return [];
});
const params: DocumentSymbolParams = {
textDocument: { uri: 'file:///abc.txt' },
workDoneToken: '3b1db4c9-e011-489e-a9d1-0653e64707c2'
};
let result: string = '';
clientConnection.onProgress(progressType, '3b1db4c9-e011-489e-a9d1-0653e64707c2', (value) => {
result += value.kind;
});
await clientConnection.sendRequest(DocumentSymbolRequest.type, params);
assert.ok(result === 'beginreportend');
});
});