-
Notifications
You must be signed in to change notification settings - Fork 380
/
RsReferenceImporterTest.kt
86 lines (75 loc) · 2.63 KB
/
RsReferenceImporterTest.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
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ide.inspections.import
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl
import org.intellij.lang.annotations.Language
import org.rust.ide.inspections.RsInspectionsTestBase
import org.rust.ide.inspections.RsUnresolvedReferenceInspection
import org.rust.ide.settings.RsCodeInsightSettings
class RsReferenceImporterTest : RsInspectionsTestBase(RsUnresolvedReferenceInspection::class) {
fun `test single item`() = doTest("""
fn main() {
func();/*caret*/
}
mod inner {
pub fn func() {}
}
""", """
use crate::inner::func;
fn main() {
func();
}
mod inner {
pub fn func() {}
}
""")
fun `test multiple items`() = doTestNotChanged("""
fn main() {
func();/*caret*/
}
mod mod1 {
pub fn func() {}
}
mod mod2 {
pub fn func() {}
}
""")
fun `test option is disabled`() = doTestNotChanged("""
fn main() {
func();/*caret*/
}
mod mod1 {
pub fn func() {}
}
mod mod2 {
pub fn func() {}
}
""", enabled = false)
private fun doTestNotChanged(@Language("Rust") code: String, enabled: Boolean = true) = doTest(code, code, enabled)
private fun doTest(@Language("Rust") before: String, @Language("Rust") after: String, enabled: Boolean = true) {
withAddUnambiguousImportsOnTheFly(enabled) {
configureByText(before)
// For some reason have to call highlighting twice
myFixture.doHighlightingAndAllowModifications()
myFixture.doHighlightingAndAllowModifications()
annotationFixture.checkByText(after)
}
}
private fun withAddUnambiguousImportsOnTheFly(value: Boolean, action: () -> Unit) {
val settings = RsCodeInsightSettings.getInstance()
val oldValue = settings.addUnambiguousImportsOnTheFly
settings.addUnambiguousImportsOnTheFly = value
try {
action()
} finally {
settings.addUnambiguousImportsOnTheFly = oldValue
}
}
}
/** Same as [CodeInsightTestFixture.doHighlighting] but allows changing PSI during highlighting */
private fun CodeInsightTestFixture.doHighlightingAndAllowModifications() {
CodeInsightTestFixtureImpl.instantiateAndRun(file, editor, intArrayOf(), /* canChangeDocument */ true)
}