[AA] Add support for printing meta-annotations in annotation tests.

This functionality will be used in follow-on commits.
This commit is contained in:
Justin Paupore
2022-12-08 11:40:38 -08:00
committed by Yan Zhulanow
parent a90983ca6a
commit 686551d1eb
3 changed files with 52 additions and 4 deletions
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.analysis.api.impl.base.test.cases.annotations
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationsList
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtAnnotatedSymbol
import org.jetbrains.kotlin.analysis.test.framework.base.AbstractAnalysisApiSingleFileTest
import org.jetbrains.kotlin.analysis.test.framework.services.expressionMarkerProvider
@@ -16,6 +18,9 @@ import org.jetbrains.kotlin.test.services.assertions
abstract class AbstractAnalysisApiAnnotationsOnDeclarationsTest : AbstractAnalysisApiSingleFileTest() {
context(KtAnalysisSession)
open fun renderAnnotations(annotations: KtAnnotationsList): String = TestAnnotationRenderer.renderAnnotations(annotations)
override fun doTestByFileStructure(ktFile: KtFile, module: TestModule, testServices: TestServices) {
val ktDeclaration = testServices.expressionMarkerProvider
.getElementOfTypeAtCaret<KtDeclaration>(ktFile)
@@ -23,7 +28,7 @@ abstract class AbstractAnalysisApiAnnotationsOnDeclarationsTest : AbstractAnalys
val declarationSymbol = ktDeclaration.getSymbol() as KtAnnotatedSymbol
buildString {
appendLine("KtDeclaration: ${ktDeclaration::class.simpleName} ${ktDeclaration.name}")
append(TestAnnotationRenderer.renderAnnotations(declarationSymbol.annotationsList))
append(renderAnnotations(declarationSymbol.annotationsList))
}
}
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2022 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.api.impl.base.test.cases.annotations
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationsList
abstract class AbstractAnalysisApiAnnotationsOnDeclarationsWithMetaTest : AbstractAnalysisApiAnnotationsOnDeclarationsTest() {
context(KtAnalysisSession)
override fun renderAnnotations(annotations: KtAnnotationsList) = TestAnnotationRenderer.renderAnnotationsWithMeta(annotations)
}
@@ -9,14 +9,43 @@ import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.annotations.KtAnnotationsList
import org.jetbrains.kotlin.analysis.api.symbols.DebugSymbolRenderer
import org.jetbrains.kotlin.analysis.test.framework.utils.indented
import org.jetbrains.kotlin.name.ClassId
object TestAnnotationRenderer {
context (KtAnalysisSession)
fun renderAnnotations(annotations: KtAnnotationsList) = buildString {
appendLine("annotations: [")
renderAnnotationsRecursive(annotations, currentMetaAnnotations = null, indent = 0)
}
context(KtAnalysisSession)
fun renderAnnotationsWithMeta(annotations: KtAnnotationsList) = buildString {
renderAnnotationsRecursive(annotations, currentMetaAnnotations = setOf(), indent = 0)
}
context(KtAnalysisSession)
private fun StringBuilder.renderAnnotationsRecursive(
annotations: KtAnnotationsList,
currentMetaAnnotations: Set<ClassId>?,
indent: Int
) {
appendLine("annotations: [".indented(indent))
for (annotation in annotations.annotations) {
appendLine(DebugSymbolRenderer().renderAnnotationApplication(annotation).indented(indent = 2))
appendLine(DebugSymbolRenderer().renderAnnotationApplication(annotation).indented(indent = indent + 2))
if (currentMetaAnnotations != null) {
val classId = annotation.classId ?: continue
if (classId in currentMetaAnnotations) {
appendLine("<recursive meta-annotation ${classId}>".indented(indent + 4))
continue
}
val metaAnnotations = getClassOrObjectSymbolByClassId(classId)?.annotationsList
if (metaAnnotations != null) {
renderAnnotationsRecursive(metaAnnotations, currentMetaAnnotations + classId, indent = indent + 4)
} else {
appendLine("<unknown meta-annotation ${classId}>".indented(indent + 4))
}
}
}
appendLine("]")
appendLine("]".indented(indent))
}
}