[Analysis API] tests: use tagged caret to find declaration to analyze in the air against

We cannot use a KtFile as analysis context.

^KT-55527
This commit is contained in:
Ilya Kirillov
2023-05-02 17:51:08 +02:00
committed by Space Team
parent 66de893963
commit ba3a3915c7
9 changed files with 28 additions and 9 deletions
@@ -16,14 +16,13 @@ import org.jetbrains.kotlin.test.directives.model.SimpleDirectivesContainer
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.assertions
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
abstract class AbstractFileImportingScopeContextTest : AbstractAnalysisApiBasedSingleModuleTest() {
override fun doTestByFileStructure(ktFiles: List<KtFile>, module: TestModule, testServices: TestServices) {
val ktFile = ktFiles.first()
val renderDefaultImportingScope = Directives.RENDER_DEFAULT_IMPORTING_SCOPE in module.directives
analyseForTest(ktFile.children.firstIsInstance()) {
analyseForTest(ktFile) {
val ktScopeContext = ktFile.getImportingScopeContext()
val scopeContextStringRepresentation = render(ktScopeContext, renderDefaultImportingScope)
@@ -18,7 +18,7 @@ abstract class AbstractReferenceShortenerForWholeFileTest : AbstractAnalysisApiB
val file = ktFiles.first()
val shortening = executeOnPooledThreadInReadAction {
analyseForTest(file.declarations.first()) {
analyseForTest(file) {
collectPossibleReferenceShortenings(file)
}
}
@@ -1,4 +1,4 @@
class A {
class A<caret_onAirContext> {
abstract class B {
abstract val b: A.B
}
@@ -1,4 +1,4 @@
import c.b
val a = 5 // some KtElement to analyze in the air against
val a<caret_onAirContext> = 5
@@ -1,7 +1,7 @@
// FILE: main.kt
import other.A as AAA
val a = 5 // some KtElement to analyze in the air against
val a<caret_onAirContext> = 5
// FILE: other/other.kt
package other
@@ -1,3 +1,3 @@
// RENDER_DEFAULT_IMPORTING_SCOPE
val a = 5 // some KtElement to analyze in the air against
val a<caret_onAirContext> = 5
@@ -1,7 +1,7 @@
// FILE: main.kt
import other.A
val a = 5 // some KtElement to analyze in the air against
val a<caret_onAirContext> = 5
// FILE: other/other.kt
package other
@@ -1,7 +1,7 @@
// FILE: main.kt
import other.*
val a = 5 // some KtElement to analyze in the air against
val a<caret_onAirContext> = 5
// FILE: other/other.kt
package other
@@ -17,11 +17,13 @@ import org.jetbrains.kotlin.analysis.test.framework.TestWithDisposable
import org.jetbrains.kotlin.analysis.test.framework.project.structure.ktModuleProvider
import org.jetbrains.kotlin.analysis.test.framework.services.ExpressionMarkerProvider
import org.jetbrains.kotlin.analysis.test.framework.services.ExpressionMarkersSourceFilePreprocessor
import org.jetbrains.kotlin.analysis.test.framework.services.expressionMarkerProvider
import org.jetbrains.kotlin.analysis.test.framework.services.libraries.CompilerExecutor
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.FrontendKind
import org.jetbrains.kotlin.analysis.test.framework.utils.SkipTestException
import org.jetbrains.kotlin.platform.jvm.JvmPlatforms
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.TestConfiguration
@@ -192,6 +194,20 @@ abstract class AbstractAnalysisApiBasedTest : TestWithDisposable() {
}
}
/**
* Invoke the analysis in the context of given [file]
*
* To perform the test for in-air analysis, it will look for the declaration marked with the caret `<caret_onAirContext>`
*/
protected fun <R> analyseForTest(file: KtFile, action: KtAnalysisSession.(KtElement) -> R): R {
return if (configurator.analyseInDependentSession) {
val declaration = testServices.expressionMarkerProvider.getElementOfTypeAtCaret<KtDeclaration>(file, ON_AIR_CONTEXT_CARET_TAG)
analyseForTest(declaration, action)
} else {
analyze(file, action = { action(file) })
}
}
@BeforeEach
fun initTestInfo(testInfo: TestInfo) {
this.testInfo = KotlinTestInfo(
@@ -200,4 +216,8 @@ abstract class AbstractAnalysisApiBasedTest : TestWithDisposable() {
tags = testInfo.tags
)
}
companion object {
private const val ON_AIR_CONTEXT_CARET_TAG = "onAirContext"
}
}