[Analysis API] render PsiType in tests properly

It should contain fully qualified names and annotations

^KT-66603
This commit is contained in:
Dmitrii Gridin
2024-03-14 16:46:25 +01:00
committed by Space Team
parent 72235b8527
commit fdc0a8ec07
44 changed files with 283 additions and 268 deletions
@@ -20,7 +20,6 @@ import org.jetbrains.kotlin.psi.KtValueArgument
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.types.Variance
abstract class AbstractAnalysisApiExpressionPsiTypeProviderTest : AbstractAnalysisApiBasedTest() {
override fun doTestByMainFile(mainFile: KtFile, mainModule: TestModule, testServices: TestServices) {
@@ -29,34 +28,37 @@ abstract class AbstractAnalysisApiExpressionPsiTypeProviderTest : AbstractAnalys
is KtValueArgument -> element.getArgumentExpression()!!
else -> error("Unexpected element: $element of ${element::class}")
}
val containingDeclaration = declarationAtCaret.parentOfType<KtDeclaration>()
?: error("Can't find containing declaration for $declarationAtCaret")
val containingClass = getContainingKtLightClass(containingDeclaration, mainFile)
val psiContext = containingClass.findLightDeclarationContext(containingDeclaration)
?: error("Can't find psi context for $containingDeclaration")
val actual = analyze(mainFile) {
val returnType = declarationAtCaret.getKtType()
if (returnType != null) {
prettyPrint {
appendLine("KtType: ${returnType.render(position = Variance.INVARIANT)}")
appendLine("KtType: ${AnalysisApiPsiTypeProviderTestUtils.render(returnType)}")
for (allowErrorTypes in listOf(false, true)) {
for (typeMappingMode in KtTypeMappingMode.entries) {
for (isAnnotationMethod in listOf(false, true)) {
val psiType = returnType.asPsiType(psiContext, allowErrorTypes, typeMappingMode, isAnnotationMethod)
appendLine("asPsiType(allowErrorTypes=$allowErrorTypes, mode=$typeMappingMode, isAnnotationMethod=$isAnnotationMethod):")
withIndent {
appendLine(psiType.toString())
appendLine("PsiType: ${AnalysisApiPsiTypeProviderTestUtils.render(psiType)}")
}
appendLine()
}
}
}
}
} else {
"null"
}
}
testServices.assertions.assertEqualsToTestDataFileSibling(actual)
}
}
@@ -26,11 +26,11 @@ abstract class AbstractAnalysisApiKtTypeByPsiTypeProviderTest : AbstractAnalysis
val actual = buildString {
executeOnPooledThreadInReadAction {
analyseForTest(mainFile) {
val returnType = psiMethod.returnType
testServices.assertions.assertNotNull(returnType)
val asKtTypeSuper = returnType!!.asKtType(useSitePosition ?: psiMethod)!!
appendLine("PsiType: $returnType")
appendLine("KtType: ${asKtTypeSuper.render(position = Variance.OUT_VARIANCE)}")
val psiType = psiMethod.returnType
testServices.assertions.assertNotNull(psiType)
val ktType = psiType!!.asKtType(useSitePosition ?: psiMethod)!!
appendLine("PsiType: ${AnalysisApiPsiTypeProviderTestUtils.render(psiType)}")
appendLine("KtType: ${AnalysisApiPsiTypeProviderTestUtils.render(ktType, Variance.OUT_VARIANCE)}")
}
}
}
@@ -17,7 +17,6 @@ import org.jetbrains.kotlin.psi.KtPsiUtil
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.types.Variance
abstract class AbstractAnalysisApiPsiTypeProviderTest : AbstractAnalysisApiBasedTest() {
override fun doTestByMainFile(mainFile: KtFile, mainModule: TestModule, testServices: TestServices) {
@@ -33,8 +32,9 @@ abstract class AbstractAnalysisApiPsiTypeProviderTest : AbstractAnalysisApiBased
executeOnPooledThreadInReadAction {
analyze(declaration) {
val ktType = declaration.getReturnKtType()
appendLine("KtType: ${ktType.render(position = Variance.INVARIANT)}")
appendLine("PsiType: ${ktType.asPsiType(psiContext, allowErrorTypes = false)}")
appendLine("KtType: ${AnalysisApiPsiTypeProviderTestUtils.render(ktType)}")
val psiType = ktType.asPsiType(psiContext, allowErrorTypes = false)
appendLine("PsiType: ${AnalysisApiPsiTypeProviderTestUtils.render(psiType)}")
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 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.
*/
@@ -9,6 +9,9 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiType
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.types.KtType
import org.jetbrains.kotlin.asJava.KotlinAsJavaSupport
import org.jetbrains.kotlin.asJava.classes.KtLightClass
import org.jetbrains.kotlin.asJava.elements.KtLightElement
@@ -16,9 +19,15 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
object AnalysisApiPsiTypeProviderTestUtils {
context(KtAnalysisSession)
fun render(type: KtType?, variance: Variance = Variance.INVARIANT): String? = type?.render(position = variance)
fun render(type: PsiType?): String? = type?.getCanonicalText(/* annotated = */ true)
internal fun getContainingKtLightClass(
declaration: KtDeclaration,
ktFile: KtFile,