forked from fwcd/kotlin-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompletionsTest.kt
359 lines (279 loc) · 16.3 KB
/
CompletionsTest.kt
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package org.javacs.kt
import org.hamcrest.Matchers.*
import org.junit.Assert.assertThat
import org.junit.Test
class InstanceMemberTest : SingleFileTestFixture("completions", "InstanceMember.kt") {
@Test fun `complete instance members`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 3, 15)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("instanceFoo")))
assertThat(labels, hasItem(startsWith("extensionFoo")))
assertThat(labels, hasItem(startsWith("fooVar")))
assertThat(labels, not(hasItem(startsWith("privateInstanceFoo"))))
assertThat(labels, not(hasItem(startsWith("getFooVar"))))
assertThat(labels, not(hasItem(startsWith("setFooVar"))))
assertThat("Reports instanceFoo only once", completions.items.filter { it.label.startsWith("instanceFoo") }, hasSize(1))
assertThat("Reports extensionFoo only once", completions.items.filter { it.label.startsWith("extensionFoo") }, hasSize(1))
}
@Test fun `find count extension function`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 8, 14)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("count"))
}
@Test fun `complete method reference`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 13, 16)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("instanceFoo")))
// assertThat(labels, not(hasItem("extensionFoo"))) Kotlin will probably implement this later
assertThat(labels, hasItem(startsWith("fooVar")))
assertThat(labels, not(hasItem(startsWith("privateInstanceFoo"))))
assertThat(labels, not(hasItem(startsWith("getFooVar"))))
assertThat(labels, not(hasItem(startsWith("setFooVar"))))
assertThat(completions.items.filter { it.label.startsWith("instanceFoo") }.firstOrNull(), hasProperty("insertText", equalTo("instanceFoo")))
}
@Test fun `complete unqualified function reference`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 17, 8)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("findFunctionReference")))
}
@Test fun `complete a function name within a call`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 22, 27)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("findFunctionReference")))
assertThat(labels, not(hasItem("instanceFoo")))
}
@Test fun `find completions on letters of method call`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 26, 26)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItems(startsWith("instanceFee"), startsWith("instanceFoo")))
}
}
class InfixMethodTest : SingleFileTestFixture("completions", "InfixFunctions.kt") {
@Test fun `complete member function`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 10, 11)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("cmpB")))
assertThat(labels, not(hasItem(startsWith("cmpA"))))
}
@Test fun `includes completion for stdlib`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 15, 10)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasSize(2))
assertThat(labels, hasItem(startsWith("and")))
assertThat(labels, hasItem("andTo"))
}
@Test fun `complete extension function`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 19, 9)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("funcA"))
}
@Test fun `complete global scope`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 26, 11)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasSize(3))
assertThat(labels, hasItem("ord"))
}
@Test fun `has global completions for binary expression`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 30, 7)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("funcA"))
assertThat(labels.filter { it.startsWith("and") }, hasSize(2))
}
}
class InstanceMembersJava : SingleFileTestFixture("completions", "InstanceMembersJava.kt") {
@Test fun `convert getFileName to fileName`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 4, 14)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("fileName"))
assertThat(labels, not(hasItem("getFileName")))
}
}
class FunctionScopeTest : SingleFileTestFixture("completions", "FunctionScope.kt") {
@Test fun `complete identifiers in function scope`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 4, 10)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("anArgument"))
assertThat(labels, hasItem("aLocal"))
assertThat(labels, hasItem("aClassVal"))
assertThat(labels, hasItem(startsWith("aClassFun")))
assertThat(labels, hasItem("aCompanionVal"))
assertThat(labels, hasItem(startsWith("aCompanionFun")))
assertThat("Reports anArgument only once", completions.items.filter { it.label == "anArgument" }, hasSize(1))
assertThat("Reports aLocal only once", completions.items.filter { it.label == "aLocal" }, hasSize(1))
assertThat("Reports aClassVal only once", completions.items.filter { it.label == "aClassVal" }, hasSize(1))
assertThat("Reports aClassFun only once", completions.items.filter { it.label.startsWith("aClassFun") }, hasSize(1))
assertThat("Reports aCompanionVal only once", completions.items.filter { it.label == "aCompanionVal" }, hasSize(1))
assertThat("Reports aCompanionFun only once", completions.items.filter { it.label.startsWith("aCompanionFun") }, hasSize(1))
}
}
class TypesTest : SingleFileTestFixture("completions", "Types.kt") {
@Test fun `complete a type name`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 2, 25)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("SomeInnerClass"))
assertThat(labels, hasItem("String"))
assertThat(labels, hasItem("SomeInnerObject"))
assertThat(labels, hasItem("SomeAlias"))
}
}
class FillEmptyBodyTest : SingleFileTestFixture("completions", "FillEmptyBody.kt") {
@Test fun `fill an empty body`() {
replace(file, 2, 16, "", """"
Callee.
""")
val completions = languageServer.textDocumentService.completion(completionParams(file, 3, 20)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("bar")))
}
}
class ConstructorTest : SingleFileTestFixture("completions", "Constructor.kt") {
@Test fun `complete a constructor`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 2, 10)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("SomeConstructor"))
}
}
class MiddleOfFunctionTest : SingleFileTestFixture("completions", "MiddleOfFunction.kt") {
@Test fun `complete in the middle of a function`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 3, 11)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("subSequence")))
}
}
class BackquotedFunctionTest : SingleFileTestFixture("completions", "BackquotedFunction.kt") {
@Test fun `complete with backquotes`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 2, 7)).get().right!!
val insertText = completions.items.map { it.insertText }
assertThat(insertText, hasItem("`fun that needs backquotes`()"))
}
}
class CompleteStaticsTest : SingleFileTestFixture("completions", "Statics.kt") {
@Test fun `java static method`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 4, 16)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("isNull")))
}
@Test fun `java static method reference`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 7, 17)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("isNull")))
}
@Test fun `object method`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 5, 17)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("objectFun")))
}
@Test fun `companion object method`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 6, 16)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("companionFun")))
}
}
class VisibilityTest : SingleFileTestFixture("completions", "Visibility.kt") {
@Test fun `find tricky visibility members`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 3, 10)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItems(startsWith("privateThisFun"), startsWith("protectedThisFun"), startsWith("publicThisFun"), startsWith("privateThisCompanionFun"), startsWith("protectedThisCompanionFun"), startsWith("publicThisCompanionFun"), startsWith("privateTopLevelFun")))
assertThat(labels, hasItems(startsWith("protectedSuperFun"), startsWith("publicSuperFun"), startsWith("protectedSuperCompanionFun"), startsWith("publicSuperCompanionFun")))
assertThat(labels, not(hasItems(startsWith("privateSuperFun"), startsWith("privateSuperCompanionFun"), startsWith("publicExtensionFun"))))
}
}
class ImportsTest : SingleFileTestFixture("completions", "Imports.kt") {
@Test fun `complete import from java-nio-path-P`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 1, 23)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItems("Path", "Paths"))
}
@Test fun `complete import from java-nio-`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 3, 25)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItems("MethodHandle"))
}
@Test fun `complete import from j`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 5, 9)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItems("java"))
}
}
class DoubleDotTest : SingleFileTestFixture("completions", "DoubleDot.kt") {
@Test fun `complete nested select`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 2, 15)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, not(hasItem("chars")))
assertThat(labels, hasItem(startsWith("anyMatch")))
}
}
class QuestionDotTest : SingleFileTestFixture("completions", "QuestionDot.kt") {
@Test fun `complete null-safe select`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 2, 8)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("chars")))
}
}
class OuterDotInnerTest : SingleFileTestFixture("completions", "OuterDotInner.kt") {
@Test fun `complete as OuterClass-InnerClass`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 2, 24)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("InnerClass"))
}
@Test fun `complete static OuterClass-InnerClass`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 6, 19)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("InnerClass"))
}
}
class EditCallTest : SingleFileTestFixture("completions", "EditCall.kt") {
@Test fun `edit existing function`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 2, 11)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("println")))
assertThat(completions.items.find { it.label.startsWith("println") }, hasProperty("insertText", equalTo("println")))
}
}
class EnumWithCompanionObjectTest : SingleFileTestFixture("completions", "Enum.kt") {
@Test fun `enum with companion object`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 9, 15)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("ILLEGAL"))
}
}
class WhenTest : SingleFileTestFixture("completions", "When.kt") {
@Test fun `nested classes completion with is`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 9, 24)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem("Test"))
}
}
class TrailingLambdaTest : SingleFileTestFixture("completions", "TrailingLambda.kt") {
@Test fun `complete function with single lambda parameter`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 6, 9)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("lambdaParameter")))
assertThat(completions.items.find { it.label.startsWith("lambdaParameter") }, hasProperty("insertText", equalTo("lambdaParameter { \${1:x} }")))
}
@Test fun `complete function with mixed parameters`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 7, 8)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("mixedParameters")))
assertThat(completions.items.find { it.label.startsWith("mixedParameters") }, hasProperty("insertText", equalTo("mixedParameters(\${1:a}) { \${2:b} }")))
}
}
class JavaGetterSetterConversionTest : SingleFileTestFixture("completions", "JavaGetterSetterConversion.kt") {
@Test fun `test java static method conversion`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 4, 44)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("getInstance")))
}
@Test fun `test java getter and setter conversion`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 5, 18)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, not(hasItem(startsWith("getFirstDayOfWeek"))))
assertThat(labels, not(hasItem(startsWith("setFirstDayOfWeek"))))
assertThat(labels, hasItem(startsWith("firstDayOfWeek")))
}
@Test fun `test java boolean getter conversion`() {
val completions = languageServer.textDocumentService.completion(completionParams(file, 6, 19)).get().right!!
val labels = completions.items.map { it.label }
assertThat(labels, hasItem(startsWith("isLenient")))
}
}