[LL FIR] implement tests on dependent copy session analyze

^KT-60987
This commit is contained in:
Dmitrii Gridin
2023-08-08 20:42:16 +02:00
committed by Space Team
parent 682cb991b8
commit d19a23aae5
22 changed files with 482 additions and 7 deletions
@@ -17,8 +17,12 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.elementsInRange
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.test.directives.model.RegisteredDirectives
import org.jetbrains.kotlin.test.directives.model.SimpleDirectivesContainer
import org.jetbrains.kotlin.test.directives.model.singleOrZeroValue
import org.jetbrains.kotlin.test.model.TestFile
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.SourceFilePreprocessor
@@ -101,6 +105,18 @@ class ExpressionMarkerProvider : TestService {
?: error("No expression found at caret")
}
inline fun <reified P : KtElement> getElementOfTypeAtCaretByDirective(
file: KtFile,
registeredDirectives: RegisteredDirectives,
caretTag: String? = null,
): P {
val elementAtCaret = getElementOfTypeAtCaret<P>(file, caretTag)
val expectedType = expectedTypeClass(registeredDirectives) ?: return elementAtCaret
return (elementAtCaret as PsiElement).parentsWithSelf.firstNotNullOfOrNull { currentElement ->
currentElement.takeIf(expectedType::isInstance) as? P
} ?: error("Element of ${P::class.simpleName} & ${expectedType.simpleName} is not found")
}
@OptIn(PrivateForInline::class)
inline fun <reified P : KtElement> getElementsOfTypeAtCarets(
files: Collection<KtFile>,
@@ -136,16 +152,22 @@ class ExpressionMarkerProvider : TestService {
return elements.single()
}
fun getSelectedElementOfTypeByDirective(ktFile: KtFile, module: TestModule): PsiElement {
val selectedElement = getSelectedElement(ktFile)
val expectedType = module.directives[Directives.LOOK_UP_FOR_ELEMENT_OF_TYPE].firstOrNull() ?: return selectedElement
fun expectedTypeClass(registeredDirectives: RegisteredDirectives): Class<PsiElement>? {
val expectedType = registeredDirectives.singleOrZeroValue(Directives.LOOK_UP_FOR_ELEMENT_OF_TYPE) ?: return null
val ktPsiPackage = "org.jetbrains.kotlin.psi."
val expectedTypeFqName = ktPsiPackage + expectedType.removePrefix(ktPsiPackage)
@Suppress("UNCHECKED_CAST") val expectedClass = Class.forName(expectedTypeFqName) as Class<PsiElement>
if (expectedClass.isInstance(selectedElement)) return selectedElement
@Suppress("UNCHECKED_CAST")
return Class.forName(expectedTypeFqName) as Class<PsiElement>
}
fun getSelectedElementOfTypeByDirective(ktFile: KtFile, module: TestModule): PsiElement {
val selectedElement = getSelectedElement(ktFile)
val expectedType = expectedTypeClass(module.directives) ?: return selectedElement
if (expectedType.isInstance(selectedElement)) return selectedElement
return selectedElement.collectDescendantsOfType<PsiElement> {
expectedClass.isInstance(it)
expectedType.isInstance(it)
}.single { it.textRange == selectedElement.textRange }
}
@@ -0,0 +1,19 @@
Tower Data Context:
Element 0
Scope: FirDefaultStarImportingScope
Element 1
Scope: FirDefaultSimpleImportingScope
Element 2
Scope: FirExplicitStarImportingScope
Element 3
Scope: FirDefaultSimpleImportingScope
Element 4
Scope: FirDefaultSimpleImportingScope
Element 5
Scope: FirPackageMemberScope
Element 6
Scope: FirExplicitSimpleImportingScope
Element 7
Implicit receiver:
FirRegularClassSymbol public final class A : R|kotlin/Any|
Type: A
@@ -0,0 +1,15 @@
KT element: KtClass
FIR element: FirRegularClassImpl
FIR source kind: KtRealSourceElementKind
FIR element rendered:
public final [ResolvedTo(BODY_RESOLVE)] class A : R|kotlin/Any| {
public [ResolvedTo(BODY_RESOLVE)] [ContainingClassKey=A] constructor(): R|A| {
super<R|kotlin/Any|>()
}
public final [ResolvedTo(BODY_RESOLVE)] fun foo(): R|kotlin/Int| {
^foo Int(42)
}
}
@@ -0,0 +1,3 @@
class<caret> A() {
fun foo() = 42
}
@@ -0,0 +1,19 @@
Tower Data Context:
Element 0
Scope: FirDefaultStarImportingScope
Element 1
Scope: FirDefaultSimpleImportingScope
Element 2
Scope: FirExplicitStarImportingScope
Element 3
Scope: FirDefaultSimpleImportingScope
Element 4
Scope: FirDefaultSimpleImportingScope
Element 5
Scope: FirPackageMemberScope
Element 6
Scope: FirExplicitSimpleImportingScope
Element 7
Implicit receiver:
FirRegularClassSymbol public final class A : R|kotlin/Any|
Type: A
@@ -0,0 +1,6 @@
KT element: KtClassBody
FIR element: null
FIR source kind: null
FIR element rendered:
null
@@ -0,0 +1,3 @@
class A() {<caret>
fun foo() = 42
}
@@ -0,0 +1,38 @@
Tower Data Context:
Element 0
Scope: FirDefaultStarImportingScope
Element 1
Scope: FirDefaultSimpleImportingScope
Element 2
Scope: FirExplicitStarImportingScope
Element 3
Scope: FirDefaultSimpleImportingScope
Element 4
Scope: FirDefaultSimpleImportingScope
Element 5
Scope: FirPackageMemberScope
Element 6
Scope: FirExplicitSimpleImportingScope
Element 7
Scope: FirScriptDeclarationsScope
Classifiers:
FirRegularClassSymbol public? final? class A : R|kotlin/Any|
Element 8
Scope: FirLocalScope
Classifiers:
FirRegularClassSymbol public final class A : R|kotlin/Any|
Properties:
FirPropertySymbol lval args: R|kotlin/Array<kotlin/String>|
Element 9
Context receivers:
FirScriptSymbol context(<script>@R|kotlin/script/templates/standard/ScriptTemplateWithArgs|)
SCRIPT:
lval args: R|kotlin/Array<kotlin/String>|
public? final? class A : R|kotlin/Any|
Type: kotlin/script/templates/standard/ScriptTemplateWithArgs
Label: <script>
Element 10
Implicit receiver:
FirRegularClassSymbol public final class A : R|kotlin/Any|
Type: A
@@ -0,0 +1,6 @@
KT element: KtClassBody
FIR element: null
FIR source kind: null
FIR element rendered:
null
@@ -0,0 +1,3 @@
class A() {<caret>
fun foo() = 42
}
@@ -0,0 +1,38 @@
Tower Data Context:
Element 0
Scope: FirDefaultStarImportingScope
Element 1
Scope: FirDefaultSimpleImportingScope
Element 2
Scope: FirExplicitStarImportingScope
Element 3
Scope: FirDefaultSimpleImportingScope
Element 4
Scope: FirDefaultSimpleImportingScope
Element 5
Scope: FirPackageMemberScope
Element 6
Scope: FirExplicitSimpleImportingScope
Element 7
Scope: FirScriptDeclarationsScope
Classifiers:
FirRegularClassSymbol public? final? class A : R|kotlin/Any|
Element 8
Scope: FirLocalScope
Classifiers:
FirRegularClassSymbol public final class A : R|kotlin/Any|
Properties:
FirPropertySymbol lval args: R|kotlin/Array<kotlin/String>|
Element 9
Context receivers:
FirScriptSymbol context(<script>@R|kotlin/script/templates/standard/ScriptTemplateWithArgs|)
SCRIPT:
lval args: R|kotlin/Array<kotlin/String>|
public? final? class A : R|kotlin/Any|
Type: kotlin/script/templates/standard/ScriptTemplateWithArgs
Label: <script>
Element 10
Implicit receiver:
FirRegularClassSymbol public final class A : R|kotlin/Any|
Type: A
@@ -0,0 +1,15 @@
KT element: KtClass
FIR element: FirRegularClassImpl
FIR source kind: KtRealSourceElementKind
FIR element rendered:
public final [ResolvedTo(BODY_RESOLVE)] class A : R|kotlin/Any| {
public [ResolvedTo(BODY_RESOLVE)] [ContainingClassKey=A] constructor(): R|A| {
super<R|kotlin/Any|>()
}
public final [ResolvedTo(BODY_RESOLVE)] fun foo(): R|kotlin/Int| {
^foo Int(42)
}
}
@@ -0,0 +1,3 @@
class<caret> A() {
fun foo() = 42
}
@@ -63,7 +63,7 @@ abstract class AbstractContextCollectorTest : AbstractAnalysisApiBasedSingleModu
}
}
private object ElementContextRenderer {
internal object ElementContextRenderer {
fun render(context: ContextCollector.Context, builder: StringBuilder) = with(builder) {
renderTowerDataContext(context.towerDataContext)
renderSmartCasts(context.smartCasts)
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.low.level.api.fir
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.configurators.AnalysisApiFirScriptTestConfigurator
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.configurators.AnalysisApiFirSourceTestConfigurator
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.ContextCollector
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.services.TestModuleStructure
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.assertions
abstract class AbstractDependentCopyContextTest : AbstractDependentCopyTest() {
context(LLFirResolveSessionDepended)
override fun doDependentCopyTest(file: KtFile, element: KtElement, moduleStructure: TestModuleStructure, testServices: TestServices) {
val context = getTowerContextProvider(file).getClosestAvailableParentContext(element)
testServices.assertions.assertNotNull(context)
val actual = buildString {
ElementContextRenderer.render(ContextCollector.Context(context!!, emptyMap()), this)
}
testServices.assertions.assertEqualsToTestDataFileSibling(actual, extension = ".context.txt")
}
}
abstract class AbstractSourceDependentCopyContextTest : AbstractDependentCopyContextTest() {
override val configurator: AnalysisApiTestConfigurator = AnalysisApiFirSourceTestConfigurator(analyseInDependentSession = false)
}
abstract class AbstractScriptDependentCopyContextTest : AbstractDependentCopyContextTest() {
override val configurator: AnalysisApiTestConfigurator get() = AnalysisApiFirScriptTestConfigurator
}
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.low.level.api.fir
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.configurators.AnalysisApiFirScriptTestConfigurator
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.configurators.AnalysisApiFirSourceTestConfigurator
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestConfigurator
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.services.TestModuleStructure
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.assertions
abstract class AbstractDependentCopyFirTest : AbstractDependentCopyTest() {
context(LLFirResolveSessionDepended)
override fun doDependentCopyTest(file: KtFile, element: KtElement, moduleStructure: TestModuleStructure, testServices: TestServices) {
val fir = getOrBuildFirFor(element)
val actual = renderActualFir(fir, element)
testServices.assertions.assertEqualsToTestDataFileSibling(actual, extension = ".fir.txt")
}
}
abstract class AbstractSourceDependentCopyFirTest : AbstractDependentCopyFirTest() {
override val configurator: AnalysisApiTestConfigurator = AnalysisApiFirSourceTestConfigurator(analyseInDependentSession = false)
}
abstract class AbstractScriptDependentCopyFirTest : AbstractDependentCopyFirTest() {
override val configurator: AnalysisApiTestConfigurator get() = AnalysisApiFirScriptTestConfigurator
}
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.low.level.api.fir
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.analysis.low.level.api.fir.api.LowLevelFirApiFacadeForResolveOnAir.getFirResolveSessionForDependentCopy
import org.jetbrains.kotlin.analysis.low.level.api.fir.test.base.AbstractLowLevelApiSingleFileTest
import org.jetbrains.kotlin.analysis.test.framework.services.expressionMarkerProvider
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.services.TestModuleStructure
import org.jetbrains.kotlin.test.services.TestServices
/**
* This test class is supposed to specify the behavior of [getFirResolveSessionForDependentCopy].
* E.g., end test can check how a copied declaration is resolved or which content has.
*
* @see AbstractDependentCopyContextTest
* @see AbstractDependentCopyFirTest
*/
abstract class AbstractDependentCopyTest : AbstractLowLevelApiSingleFileTest() {
override fun doTestByFileStructure(ktFile: KtFile, moduleStructure: TestModuleStructure, testServices: TestServices) {
val element = testServices.expressionMarkerProvider.getElementOfTypeAtCaretByDirective<KtElement>(
file = ktFile,
registeredDirectives = moduleStructure.allDirectives,
)
val fileCopy = ktFile.copy() as KtFile
val sameElementInCopy = PsiTreeUtil.findSameElementInCopy(element, fileCopy)
resolveWithClearCaches(ktFile) { originalSession ->
val dependentSession = getFirResolveSessionForDependentCopy(
originalFirResolveSession = originalSession,
originalKtFile = ktFile,
elementToAnalyze = sameElementInCopy,
) as LLFirResolveSessionDepended
with(dependentSession) {
doDependentCopyTest(fileCopy, sameElementInCopy, moduleStructure, testServices)
}
}
}
context(LLFirResolveSessionDepended)
abstract fun doDependentCopyTest(file: KtFile, element: KtElement, moduleStructure: TestModuleStructure, testServices: TestServices)
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.low.level.api.fir;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("analysis/low-level-api-fir/testdata/dependentCopy")
@TestDataPath("$PROJECT_ROOT")
public class ScriptDependentCopyContextTestGenerated extends AbstractScriptDependentCopyContextTest {
@Test
public void testAllFilesPresentInDependentCopy() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/low-level-api-fir/testdata/dependentCopy"), Pattern.compile("^(.+)\\.(kts)$"), null, true);
}
@Test
@TestMetadata("classBodyScript.kts")
public void testClassBodyScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/dependentCopy/classBodyScript.kts");
}
@Test
@TestMetadata("classScript.kts")
public void testClassScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/dependentCopy/classScript.kts");
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.low.level.api.fir;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("analysis/low-level-api-fir/testdata/dependentCopy")
@TestDataPath("$PROJECT_ROOT")
public class ScriptDependentCopyFirTestGenerated extends AbstractScriptDependentCopyFirTest {
@Test
public void testAllFilesPresentInDependentCopy() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/low-level-api-fir/testdata/dependentCopy"), Pattern.compile("^(.+)\\.(kts)$"), null, true);
}
@Test
@TestMetadata("classBodyScript.kts")
public void testClassBodyScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/dependentCopy/classBodyScript.kts");
}
@Test
@TestMetadata("classScript.kts")
public void testClassScript() throws Exception {
runTest("analysis/low-level-api-fir/testdata/dependentCopy/classScript.kts");
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.low.level.api.fir;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("analysis/low-level-api-fir/testdata/dependentCopy")
@TestDataPath("$PROJECT_ROOT")
public class SourceDependentCopyContextTestGenerated extends AbstractSourceDependentCopyContextTest {
@Test
public void testAllFilesPresentInDependentCopy() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/low-level-api-fir/testdata/dependentCopy"), Pattern.compile("^(.+)\\.(kt)$"), null, true);
}
@Test
@TestMetadata("class.kt")
public void testClass() throws Exception {
runTest("analysis/low-level-api-fir/testdata/dependentCopy/class.kt");
}
@Test
@TestMetadata("classBody.kt")
public void testClassBody() throws Exception {
runTest("analysis/low-level-api-fir/testdata/dependentCopy/classBody.kt");
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.low.level.api.fir;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("analysis/low-level-api-fir/testdata/dependentCopy")
@TestDataPath("$PROJECT_ROOT")
public class SourceDependentCopyFirTestGenerated extends AbstractSourceDependentCopyFirTest {
@Test
public void testAllFilesPresentInDependentCopy() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/low-level-api-fir/testdata/dependentCopy"), Pattern.compile("^(.+)\\.(kt)$"), null, true);
}
@Test
@TestMetadata("class.kt")
public void testClass() throws Exception {
runTest("analysis/low-level-api-fir/testdata/dependentCopy/class.kt");
}
@Test
@TestMetadata("classBody.kt")
public void testClassBody() throws Exception {
runTest("analysis/low-level-api-fir/testdata/dependentCopy/classBody.kt");
}
}
@@ -182,6 +182,22 @@ internal fun TestGroupSuite.generateFirLowLevelApiTests() {
testClass<AbstractContextCollectorTest> {
model("contextCollector")
}
testClass<AbstractSourceDependentCopyContextTest> {
model("dependentCopy", pattern = TestGeneratorUtil.KT)
}
testClass<AbstractScriptDependentCopyContextTest> {
model("dependentCopy", pattern = TestGeneratorUtil.KTS)
}
testClass<AbstractSourceDependentCopyFirTest> {
model("dependentCopy", pattern = TestGeneratorUtil.KT)
}
testClass<AbstractScriptDependentCopyFirTest> {
model("dependentCopy", pattern = TestGeneratorUtil.KTS)
}
}
testGroup("analysis/low-level-api-fir/tests", "analysis/analysis-api/testData") {