Skip to content

Commit 3fe2529

Browse files
committed
chore: add simple REST completion retrieval extension function
1 parent 14e1a1a commit 3fe2529

File tree

7 files changed

+55
-43
lines changed

7 files changed

+55
-43
lines changed

completions/src/test/kotlin/CompletionTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface CompletionTest : BaseCompletionTest {
99
code = "fun main() {\n val alex = 1\n val alex1 = 1 + a\n}",
1010
line = 2,
1111
character = 21,
12-
completions = listOf(
12+
expected = listOf(
1313
"alex"
1414
)
1515
)
@@ -21,7 +21,7 @@ interface CompletionTest : BaseCompletionTest {
2121
code = "fun main() {\n val alex = 1\n val alex1 = 1 + a\n}",
2222
line = 2,
2323
character = 21,
24-
completions = listOf(
24+
expected = listOf(
2525
"alex"
2626
),
2727
isJs = true
@@ -34,7 +34,7 @@ interface CompletionTest : BaseCompletionTest {
3434
code = "fun main() {\n 3.0.toIn\n}",
3535
line = 1,
3636
character = 12,
37-
completions = listOf(
37+
expected = listOf(
3838
"toInt()"
3939
)
4040
)
@@ -46,7 +46,7 @@ interface CompletionTest : BaseCompletionTest {
4646
code = "fun main() {\n 3.0.toIn\n}",
4747
line = 1,
4848
character = 12,
49-
completions = listOf(
49+
expected = listOf(
5050
"toInt()"
5151
),
5252
isJs = true
@@ -60,7 +60,7 @@ interface CompletionTest : BaseCompletionTest {
6060
code = "fun main() {\n list\n}",
6161
line = 1,
6262
character = 8,
63-
completions = listOf(
63+
expected = listOf(
6464
"listOf()",
6565
"listOf(element: T)",
6666
"listOfNotNull(element: T?)",
@@ -76,7 +76,7 @@ interface CompletionTest : BaseCompletionTest {
7676
code = "fun main() {\n list\n}",
7777
line = 1,
7878
character = 8,
79-
completions = listOf(
79+
expected = listOf(
8080
"listOf()",
8181
"listOf(element: T)",
8282
"listOfNotNull(element: T?)",

completions/src/test/kotlin/ConcurrencyCompletionRunnerTest.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import base.BaseCompletionTest
22
import kotlinx.coroutines.Dispatchers
3+
import kotlinx.coroutines.delay
34
import kotlinx.coroutines.launch
45
import kotlinx.coroutines.runBlocking
56
import org.junit.jupiter.api.Test
7+
import org.junit.platform.commons.logging.LoggerFactory
68
import kotlin.test.Ignore
9+
import kotlin.time.Duration.Companion.milliseconds
710

811
abstract class ConcurrencyCompletionRunnerTest : BaseCompletionTest {
912
// TODO(Dmitrii Krasnov): this test is disabled until KTL-2807 is fixed
@@ -15,7 +18,7 @@ abstract class ConcurrencyCompletionRunnerTest : BaseCompletionTest {
1518
code = "fun main() {\n val alex = 1\n val alex1 = 1 + a\n}",
1619
line = 2,
1720
character = 21,
18-
completions = listOf(
21+
expected = listOf(
1922
"alex"
2023
)
2124
)
@@ -31,7 +34,7 @@ abstract class ConcurrencyCompletionRunnerTest : BaseCompletionTest {
3134
code = "fun main() {\n val alex = 1\n val alex1 = 1 + a\n}",
3235
line = 2,
3336
character = 21,
34-
completions = listOf(
37+
expected = listOf(
3538
"alex"
3639
),
3740
isJs = true

completions/src/test/kotlin/ImportTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface ImportTest : BaseCompletionTest {
1414
code = "fun main() {\n val rand = Random\n}",
1515
line = 1,
1616
character = 21,
17-
completions = listOf(
17+
expected = listOf(
1818
"Random (kotlin.random.Random)"
1919
)
2020
)
@@ -69,7 +69,7 @@ interface ImportTest : BaseCompletionTest {
6969
""".trimMargin(),
7070
line = 1,
7171
character = 20,
72-
completions = listOf(
72+
expected = listOf(
7373
"Random (kotlin.random.Random)"
7474
)
7575
)
@@ -83,7 +83,7 @@ interface ImportTest : BaseCompletionTest {
8383
"}",
8484
line = 1,
8585
character = 15,
86-
completions = listOf(
86+
expected = listOf(
8787
"sin(x: Double) (kotlin.math.sin)",
8888
"sin(x: Float) (kotlin.math.sin)"
8989
)
@@ -134,7 +134,7 @@ interface ImportTest : BaseCompletionTest {
134134
code = "fun main() {\n val rand = Random\n}",
135135
line = 1,
136136
character = 21,
137-
completions = listOf(
137+
expected = listOf(
138138
"Random (kotlin.random.Random)"
139139
),
140140
isJs = true
@@ -174,7 +174,7 @@ interface ImportTest : BaseCompletionTest {
174174
""".trimMargin(),
175175
line = 1,
176176
character = 20,
177-
completions = listOf(
177+
expected = listOf(
178178
"Random (kotlin.random.Random)"
179179
),
180180
isJs = true
@@ -189,7 +189,7 @@ interface ImportTest : BaseCompletionTest {
189189
"}",
190190
line = 1,
191191
character = 15,
192-
completions = listOf(
192+
expected = listOf(
193193
"sin(x: Double) (kotlin.math.sin)",
194194
"sin(x: Float) (kotlin.math.sin)"
195195
),
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
package base
22

3+
import completions.model.Project
4+
import completions.model.ProjectFile
5+
import model.Completion
6+
import org.springframework.test.web.reactive.server.WebTestClient
7+
import java.time.Duration
8+
import kotlin.time.Duration.Companion.minutes
9+
import kotlin.time.toJavaDuration
10+
311
interface BaseCompletionTest {
412
fun performCompletionChecks(
513
code: String,
614
line: Int,
715
character: Int,
8-
completions: List<String>,
16+
expected: List<String>,
917
isJs: Boolean = false
1018
)
19+
20+
companion object {
21+
fun WebTestClient.retrieveCompletions(url: String, code: String): List<Completion> {
22+
val project = Project(files = listOf(ProjectFile(text = code, name = "file.kt")))
23+
return withTimeout {
24+
post()
25+
.uri(url)
26+
.bodyValue(project)
27+
.exchange()
28+
.expectStatus().isOk
29+
.expectBodyList(Completion::class.java)
30+
.returnResult()
31+
.responseBody
32+
} ?: emptyList()
33+
}
34+
35+
private fun <T> WebTestClient.withTimeout(
36+
duration: Duration = 2.minutes.toJavaDuration(),
37+
body: WebTestClient.() -> T?
38+
): T? = with(mutate().responseTimeout(duration).build()) { body() }
39+
}
1140
}

completions/src/test/kotlin/lsp/LspClientTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class LspClientTest : CompletionTest {
5959
code: String,
6060
line: Int,
6161
character: Int,
62-
completions: List<String>,
62+
expected: List<String>,
6363
isJs: Boolean
6464
) = runBlocking {
6565
if (isJs) return@runBlocking
@@ -69,7 +69,7 @@ class LspClientTest : CompletionTest {
6969
val received = client.getCompletion(uri, caret).await()
7070

7171
val labels = received.mapNotNull { it.toCompletion()?.displayText }
72-
assertAll(completions.map { exp ->
72+
assertAll(expected.map { exp ->
7373
{ assertTrue(labels.any { it.contains(exp) }, "Expected completion $exp but got $labels") }
7474
})
7575
}

completions/src/test/kotlin/lsp/LspCompletionProviderTest.kt

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package lsp
22

33
import CompletionTest
4+
import base.BaseCompletionTest.Companion.retrieveCompletions
45
import lsp.utils.CARET_MARKER
56
import lsp.utils.KotlinLspComposeExtension
67
import lsp.utils.extractCaret
7-
import completions.model.Project
8-
import completions.model.ProjectFile
98
import model.Completion
109
import model.Icon
1110
import org.eclipse.lsp4j.Position
@@ -17,11 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired
1716
import org.springframework.boot.test.context.SpringBootTest
1817
import org.springframework.boot.test.web.server.LocalServerPort
1918
import org.springframework.test.web.reactive.server.WebTestClient
20-
import java.time.Duration
2119
import kotlin.test.assertEquals
2220
import kotlin.test.assertTrue
23-
import kotlin.time.Duration.Companion.minutes
24-
import kotlin.time.toJavaDuration
2521

2622
@SpringBootTest(
2723
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
@@ -94,34 +90,18 @@ class LspCompletionProviderTest : CompletionTest {
9490
code: String,
9591
line: Int,
9692
character: Int,
97-
completions: List<String>,
93+
expected: List<String>,
9894
isJs: Boolean
9995
) {
10096
val caret = Position(line, character)
10197
val completions = retrieveCompletionsFromEndpoint(code, caret).map { it.displayText }
102-
assertAll(executables = completions.map { exp ->
98+
assertAll(expected.map { exp ->
10399
{ assertTrue(completions.any { it.contains(exp) }, "Expected completion $exp but got $completions") }
104100
})
105101
}
106102

107103
private fun retrieveCompletionsFromEndpoint(code: String, position: Position): List<Completion> {
108-
val project = Project(files = listOf(ProjectFile(text = code, name = "file.kt")))
109104
val url = "http://localhost:$port/api/complete/lsp?line=${position.line}&ch=${position.character}"
110-
return withTimeout {
111-
post()
112-
.uri(url)
113-
.bodyValue(project)
114-
.exchange()
115-
.expectStatus().isOk
116-
.expectBodyList(Completion::class.java)
117-
.returnResult()
118-
.responseBody
119-
} ?: emptyList()
120-
105+
return webTestClient.retrieveCompletions(url, code)
121106
}
122-
123-
private fun <T> withTimeout(
124-
duration: Duration = 2.minutes.toJavaDuration(),
125-
body: WebTestClient.() -> T?
126-
): T? = with(webTestClient.mutate().responseTimeout(duration).build()) { body() }
127107
}

completions/src/test/kotlin/lsp/ws/KotlinLspProxyWSTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class KotlinLspProxyWSTest : CompletionTest {
5757
code: String,
5858
line: Int,
5959
character: Int,
60-
completions: List<String>,
60+
expected: List<String>,
6161
isJs: Boolean
6262
) {
6363
if (isJs) return // silently ignore JS completions for now
@@ -74,7 +74,7 @@ class KotlinLspProxyWSTest : CompletionTest {
7474
StepVerifier.create(completionsMono)
7575
.assertNext { received ->
7676
val labels = received.map { it.displayText }
77-
assertAll(completions.map { exp ->
77+
assertAll(expected.map { exp ->
7878
{ assertTrue(labels.any { it.contains(exp) }, "Expected completion $exp but got $labels") }
7979
})
8080
}

0 commit comments

Comments
 (0)