-
Notifications
You must be signed in to change notification settings - Fork 379
/
RsElementFeatureProviderTest.kt
128 lines (116 loc) · 4.29 KB
/
RsElementFeatureProviderTest.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
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ml
import com.intellij.codeInsight.lookup.LookupManager
import com.intellij.codeInsight.lookup.impl.LookupImpl
import com.intellij.completion.ml.util.RelevanceUtil.asRelevanceMaps
import com.intellij.testFramework.UsefulTestCase
import org.intellij.lang.annotations.Language
import org.rust.ProjectDescriptor
import org.rust.RsTestBase
import org.rust.WithStdlibRustProjectDescriptor
import org.rust.lang.core.psi.RS_KEYWORDS
@Suppress("UnstableApiUsage")
class RsElementFeatureProviderTest : RsTestBase() {
fun `test top level keyword kind features`() = doTest("ml_rust_kind", """
/*caret*/
fn main() {}
""", mapOf(
"struct" to RsKeywordMLKind.Struct.name,
"enum" to RsKeywordMLKind.Enum.name,
"fn" to RsKeywordMLKind.Fn.name,
"const" to RsKeywordMLKind.Const.name,
"pub" to RsKeywordMLKind.Pub.name,
"extern" to RsKeywordMLKind.Extern.name,
"trait" to RsKeywordMLKind.Trait.name,
"type" to RsKeywordMLKind.Type.name,
"use" to RsKeywordMLKind.Use.name,
"static" to RsKeywordMLKind.Static.name,
))
fun `test body keyword kind features`() = doTest("ml_rust_kind", """
fn main() {
/*caret*/
}
""", mapOf(
"let" to RsKeywordMLKind.Let.name,
"struct" to RsKeywordMLKind.Struct.name,
"enum" to RsKeywordMLKind.Enum.name,
"if" to RsKeywordMLKind.If.name,
"match" to RsKeywordMLKind.Match.name,
"return" to RsKeywordMLKind.Return.name,
"crate" to RsKeywordMLKind.Crate.name,
))
fun `test named elements kind features`() = doTest("ml_rust_kind", """
fn foo_func() {}
fn main() {
let foo_var = 42;
f/*caret*/
}
""", mapOf(
"foo_var" to RsPsiElementMLKind.PatBinding.name,
"foo_func" to RsPsiElementMLKind.Function.name,
))
fun `test struct field method kind feature`() = doTest("ml_rust_kind", """
struct S { field1: i32, field2: i32 }
impl S {
fn foo(&self) {}
}
fn foo(s: S) {
s.f/*caret*/
}
""", mapOf(
"field1" to RsPsiElementMLKind.NamedFieldDecl.name,
"foo" to RsPsiElementMLKind.Function.name,
))
@ProjectDescriptor(WithStdlibRustProjectDescriptor::class)
fun `test is_from_stdlib feature`() = doTest("ml_rust_is_from_stdlib", """
fn my_print() {}
fn main() {
prin/*caret*/
}
""", mapOf(
"println" to "1",
"my_print" to "0",
))
fun `test all keywords are covered`() {
val kindsKeywords = RsKeywordMLKind.values().map {
it.lookupString
}
val actualKeywords = RS_KEYWORDS.types.map {
when (val name = it.toString()) {
"async_kw" -> "async"
"auto_kw" -> "auto"
"default_kw" -> "default"
"dyn_kw" -> "dyn"
"raw_kw" -> "raw"
"union_kw" -> "union"
else -> name
}
}
UsefulTestCase.assertSameElements(kindsKeywords, actualKeywords)
}
private fun doTest(feature: String, @Language("Rust") code: String, values: Map<String, Any>) {
InlineFile(code.trimIndent()).withCaret()
myFixture.completeBasic()
val lookup = LookupManager.getInstance(project).activeLookup as LookupImpl
for ((lookupString, expectedValue) in values) {
checkFeatureValue(lookup, feature, lookupString, expectedValue)
}
}
private fun checkFeatureValue(
lookup: LookupImpl,
feature: String,
lookupString: String,
expectedValue: Any
) {
val items = lookup.items
val allRelevanceObjects = lookup.getRelevanceObjects(items, false)
val matchedItem = items.firstOrNull { it.lookupString == lookupString } ?: error("No `$lookupString` in lookup")
val relevanceObjects = allRelevanceObjects[matchedItem].orEmpty()
val featuresMap = asRelevanceMaps(relevanceObjects).second
val actualValue = featuresMap[feature]
assertEquals("Invalid value for `$feature` of `$lookupString`", expectedValue, actualValue)
}
}