forked from fwcd/kotlin-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInlayHintTest.kt
163 lines (133 loc) · 4.78 KB
/
InlayHintTest.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
package org.javacs.kt
import org.eclipse.lsp4j.InlayHint
import org.eclipse.lsp4j.Position
import org.hamcrest.Matchers.isIn
import org.hamcrest.Matchers.hasSize
import org.hamcrest.Matchers.everyItem
import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.equalTo
import org.junit.Assert.assertThat
import org.junit.Assert.assertTrue
import org.junit.Assert.assertEquals
import org.junit.Test
private fun predicate(pos: Position, label: String) = {
hint: InlayHint -> hint.position == pos && hint.label.left.contains(label)
}
private fun nPredicateFilter(
hints: List<InlayHint>,
predicates: List<(InlayHint) -> Boolean>
): List<InlayHint> =
hints.filter {
predicates.any { p -> p(it) }
}
class InlayHintDeclarationTest : SingleFileTestFixture("inlayhints", "Declarations.kt") {
private val hints = languageServer.textDocumentService.inlayHint(inlayHintParams(file, range(0, 0, 0, 0))).get()
@Test
fun `lambda declaration hints`() {
val result = hints.filter {
it.position == Position(2, 10)
}
assertThat(result, hasSize(1))
val label = result.single().label.left.replaceBefore("(", "")
val regex = Regex("\\(([^)]+)\\) -> .*")
assertTrue(label.matches(regex))
}
@Test
fun `destrucuted declaration hints`() {
val predicates = listOf(
predicate(Position(17, 10), "Float"),
predicate(Position(17, 13), "Double"),
)
val result = nPredicateFilter(hints, predicates)
assertThat(result, hasSize(2))
assertThat(result, everyItem(isIn(hints)))
}
@Test
fun `should not render hint with explicit type`() {
val result = hints.filter {
it.label.left.contains("Type")
}
assertTrue(result.isEmpty())
}
@Test
fun `generic type hints`() {
val expected = listOf(Position(5, 13), Position(20, 7))
val result = hints.filter {
it.label.left.matches(Regex("Box<([^)]+)>"))
}.map { it.position }
assertEquals(result.size, expected.size)
assertEquals(result.sortedBy { it.line }, expected.sortedBy { it.line })
}
@Test
fun `inferred hint for single-expression function`() {
val hint = hints.filter {
it.position == Position(22, 24)
}
assertThat(hint, hasSize(1))
assertThat(hint.single().label.left, containsString("String"))
}
}
class InlayHintCallableParameterTest : SingleFileTestFixture("inlayhints", "Parameters.kt") {
private val hints = languageServer.textDocumentService.inlayHint(inlayHintParams(file, range(0, 0, 0, 0))).get()
@Test
fun `class parameter hints`() {
val predicates = listOf(
predicate(Position(13, 4), "x"),
predicate(Position(14, 4), "y"),
predicate(Position(15, 4), "z"),
)
val result = nPredicateFilter(hints, predicates)
assertThat(result, hasSize(3))
assertThat(result, everyItem(isIn(hints)))
}
@Test
fun `has one vararg parameter hint`() {
val varargHintCount = hints.filter {
it.label.left.contains("ints")
}.size
assertThat(varargHintCount, equalTo(1))
}
@Test
fun `mixed parameter types`(){
val predicates = listOf(
predicate(Position(17, 14), "d"),
predicate(Position(17, 19), "p1"),
predicate(Position(17, 25), "ints"),
)
val result = nPredicateFilter(hints, predicates)
assertThat(result, hasSize(3))
assertThat(result, everyItem(isIn(hints)))
}
@Test
fun `inferred lambda parameter type`() {
val hint = hints.filter {
it.label.left.contains("Int")
}
assertThat(hint, hasSize(1))
assertThat(hint.single().label.left, containsString("Int"))
}
}
class InlayHintChainedTest : SingleFileTestFixture("inlayhints", "ChainedMethods.kt") {
private val hints = languageServer.textDocumentService.inlayHint(inlayHintParams(file, range(0, 0, 0, 0))).get()
@Test
fun `chained hints`() {
val predicates = listOf(
predicate(Position(17, 34), "List<String>"),
predicate(Position(18, 26), "List<Int>"),
predicate(Position(19, 19), "Array<Int>"),
)
val result = nPredicateFilter(hints, predicates)
assertThat(result, hasSize(3))
assertThat(result, everyItem(isIn(hints)))
}
@Test
fun `generic chained hints`() {
val predicates = listOf(
predicate(Position(22, 16), "A<Int>"),
predicate(Position(23, 8), "B<Int>"),
)
val result = nPredicateFilter(hints, predicates)
assertThat(result, hasSize(2))
assertThat(result, everyItem(isIn(hints)))
}
}