This repository has been archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 301
/
CompletionTests.ts
321 lines (245 loc) · 10.6 KB
/
CompletionTests.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* CompletionTests.ts
*/
import * as assert from "assert"
import * as types from "vscode-languageserver-types"
import * as Completion from "./../../../src/Services/Completion"
import * as SyntaxHighlighting from "./../../../src/Services/SyntaxHighlighting"
import * as Mocks from "./../../Mocks"
import * as TestHelpers from "./../../TestHelpers"
export class MockCompletionRequestor implements Completion.ICompletionsRequestor {
private _completionsRequestor: Mocks.MockRequestor<
types.CompletionItem[]
> = new Mocks.MockRequestor<types.CompletionItem[]>()
private _completionDetailsRequestor: Mocks.MockRequestor<
types.CompletionItem
> = new Mocks.MockRequestor<types.CompletionItem>()
public get completionsRequestor(): Mocks.MockRequestor<types.CompletionItem[]> {
return this._completionsRequestor
}
public get completionDetailsRequestor(): Mocks.MockRequestor<types.CompletionItem> {
return this._completionDetailsRequestor
}
public getCompletions(
context: Completion.CompletionsRequestContext,
): Promise<types.CompletionItem[]> {
return this._completionsRequestor.get(
context.language,
context.filePath,
context.line,
context.column,
)
}
public getCompletionDetails(
fileLanguage: string,
filePath: string,
completionItem: types.CompletionItem,
): Promise<types.CompletionItem> {
return this._completionDetailsRequestor.get(fileLanguage, filePath, completionItem)
}
}
const createMockCompletionItem = (label: string): types.CompletionItem => {
const ci: types.CompletionItem = {
label,
kind: types.CompletionItemKind.Variable,
}
return ci
}
describe("Completion", () => {
// Mocks
let mockConfiguration: Mocks.MockConfiguration
let mockEditor: Mocks.MockEditor
let mockLanguageManager: Mocks.MockLanguageManager
let mockCompletionRequestor: MockCompletionRequestor
// Class under test
let completion: Completion.Completion
beforeEach(() => {
mockConfiguration = new Mocks.MockConfiguration({
"editor.completions.mode": "oni",
})
mockEditor = new Mocks.MockEditor()
mockLanguageManager = new Mocks.MockLanguageManager()
mockCompletionRequestor = new MockCompletionRequestor()
completion = new Completion.Completion(
mockEditor,
mockConfiguration as any,
mockCompletionRequestor,
mockLanguageManager as any,
null /* snippet manager */,
new SyntaxHighlighting.NullSyntaxHighlighter(),
)
})
afterEach(() => {
completion.dispose()
})
it("shows completions, filtered by base", async () => {
mockEditor.simulateBufferEnter(new Mocks.MockBuffer("typescript", "test1.ts", []))
// Switch to insert mode
mockEditor.simulateModeChange("insert")
// Simulate typing
mockEditor.simulateCursorMoved(0, 1)
mockEditor.setActiveBufferLine(0, "w")
TestHelpers.runAllTimers()
let lastItems: Completion.ICompletionShowEventArgs = null
completion.onShowCompletionItems.subscribe(items => (lastItems = items))
// Validate we have a request for completions
assert.strictEqual(
mockCompletionRequestor.completionsRequestor.pendingCallCount,
1,
"There should be a request for completions queued",
)
const completionResults = [
createMockCompletionItem("win"),
createMockCompletionItem("window"),
]
mockCompletionRequestor.completionsRequestor.resolve(completionResults)
await TestHelpers.waitForAllAsyncOperations()
assert.deepEqual(
lastItems.filteredCompletions,
completionResults,
"There should now be completion results available",
)
assert.deepEqual(lastItems.base, "w", "The base should be set correctly")
})
it("doesn't fetch completions if 'editor.completions.mode' === 'hidden'", () => {
mockConfiguration.setValue("editor.completions.mode", "hidden")
mockEditor.simulateBufferEnter(new Mocks.MockBuffer("typescript", "test1.ts", []))
// Switch to insert mode
mockEditor.simulateModeChange("insert")
// Simulate typing
mockEditor.simulateCursorMoved(0, 3)
mockEditor.setActiveBufferLine(0, "win")
TestHelpers.runAllTimers()
// Validate we do not have requests for completion, because completions are turned off.
assert.strictEqual(
mockCompletionRequestor.completionsRequestor.pendingCallCount,
0,
"There should be no completion requests, because 'editor.completions.mode' is set to 'hidden'",
)
})
it("doesn't fetch completions if 'editor.completions.enabled' === 'false'", () => {
mockConfiguration.setValue("editor.completions.enabled", false)
mockEditor.simulateBufferEnter(new Mocks.MockBuffer("typescript", "test1.ts", []))
// Switch to insert mode
mockEditor.simulateModeChange("insert")
// Simulate typing
mockEditor.simulateCursorMoved(0, 3)
mockEditor.setActiveBufferLine(0, "win")
TestHelpers.runAllTimers()
// Validate we do not have requests for completion, because completions are turned off.
assert.strictEqual(
mockCompletionRequestor.completionsRequestor.pendingCallCount,
0,
"There should be no completion requests, because 'editor.completions.enabled' is set to 'false'",
)
})
it("if there is a completion matching the base, it should be the first shown", async () => {
mockEditor.simulateBufferEnter(new Mocks.MockBuffer("typescript", "test1.ts", []))
// Switch to insert mode
mockEditor.simulateModeChange("insert")
// Simulate typing
mockEditor.simulateCursorMoved(0, 3)
mockEditor.setActiveBufferLine(0, "win")
TestHelpers.runAllTimers()
let lastItems: Completion.ICompletionShowEventArgs = null
completion.onShowCompletionItems.subscribe(items => (lastItems = items))
// Validate we have a request for completions
assert.strictEqual(
mockCompletionRequestor.completionsRequestor.pendingCallCount,
1,
"There should be a request for completions queued",
)
const completionResults = [
createMockCompletionItem("window"),
createMockCompletionItem("win"),
]
mockCompletionRequestor.completionsRequestor.resolve(completionResults)
await TestHelpers.waitForAllAsyncOperations()
assert.strictEqual(
lastItems.filteredCompletions.length,
2,
"Completions were resolved successfully",
)
assert.deepEqual(
lastItems.filteredCompletions[0],
completionResults[1],
"The second completion should be the first one shown, as it matches the base",
)
})
it("if mode changed while a request was in progress, there should be no completions shown", async () => {
mockEditor.simulateBufferEnter(new Mocks.MockBuffer("typescript", "test1.ts", []))
// Switch to insert mode
mockEditor.simulateModeChange("insert")
// Simulate typing
mockEditor.simulateCursorMoved(0, 3)
mockEditor.setActiveBufferLine(0, "win")
TestHelpers.runAllTimers()
let lastItems: Completion.ICompletionShowEventArgs = null
completion.onShowCompletionItems.subscribe(items => (lastItems = items))
// Validate we have a request for completions
assert.strictEqual(
mockCompletionRequestor.completionsRequestor.pendingCallCount,
1,
"There should be a request for completions queued",
)
// While the result is pending, we'll leave insert mode -
// we shouldn't get any completions, now!
mockEditor.simulateModeChange("normal")
TestHelpers.runAllTimers()
// Resolve the slow request...
const completionResults = [
createMockCompletionItem("window"),
createMockCompletionItem("win"),
]
mockCompletionRequestor.completionsRequestor.resolve(completionResults)
await TestHelpers.waitForAllAsyncOperations()
assert.strictEqual(
lastItems,
null,
"Completions should be null, as we shouldn't have received them after the mode change",
)
})
it("if meet changed while the request was in progress, there should be no completions shown", async () => {
mockEditor.simulateBufferEnter(new Mocks.MockBuffer("typescript", "test1.ts", []))
// Switch to insert mode
mockEditor.simulateModeChange("insert")
// Simulate typing
mockEditor.simulateCursorMoved(0, 3)
mockEditor.setActiveBufferLine(0, "win")
TestHelpers.runAllTimers()
let lastItems: Completion.ICompletionShowEventArgs = null
completion.onShowCompletionItems.subscribe(items => (lastItems = items))
// Validate we have a request for completions
assert.strictEqual(
mockCompletionRequestor.completionsRequestor.pendingCallCount,
1,
"There should be a request for completions queued",
)
// While the result is pending, we'll keep typing...
// That first result should be ignored
mockEditor.simulateCursorMoved(0, 5)
mockEditor.setActiveBufferLine(0, "win.s")
TestHelpers.runAllTimers()
// Resolve the slow request...
const oldCompletionResults = [
createMockCompletionItem("window"),
createMockCompletionItem("win"),
]
mockCompletionRequestor.completionsRequestor.resolve(oldCompletionResults)
await TestHelpers.waitForAllAsyncOperations()
assert.strictEqual(
lastItems,
null,
"Completions should be null, as the only request that was completed was outdated",
)
})
it("#1076 - should not crash if buffer change comes before first cursor move", () => {
mockEditor.simulateBufferEnter(new Mocks.MockBuffer("typescript", "test1.ts", []))
// Switch to insert mode
mockEditor.simulateModeChange("insert")
// Simulate typing, but with the buffer update coming prior to the cursor move.
mockEditor.setActiveBufferLine(0, "win")
mockEditor.simulateCursorMoved(0, 3)
assert.ok(true, "Did not crash")
})
})