[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
@@ -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"
}
}