From 88f4b62d161abc209b69982cd2044f753ebd1559 Mon Sep 17 00:00:00 2001 From: vmishenev Date: Tue, 17 Oct 2023 19:39:02 +0300 Subject: [PATCH] Enable tests for as-java and javadoc plugins and fix some --- .../src/main/kotlin/demo/HelloWorld.kt | 2 +- .../base/src/test/kotlin/model/ClassesTest.kt | 15 ++++++ plugins/javadoc/build.gradle.kts | 7 ++- .../javadoc/AbstractJavadocTemplateMapTest.kt | 1 + .../javadoc/JavadocAccessorNamingTest.kt | 22 ++++++--- .../dokka/javadoc/JavadocIndexTest.kt | 17 +++++++ .../javadoc/location/JavadocLocationTest.kt | 1 + plugins/kotlin-as-java/build.gradle.kts | 7 ++- .../src/test/kotlin/KotlinAsJavaPluginTest.kt | 1 + .../test/kotlin/KotlinAsJavaSignatureTest.kt | 1 + .../translators/AnnotationTranslator.kt | 11 +++-- .../DefaultSymbolToDocumentableTranslator.kt | 47 +++++++------------ .../symbols/translators/TypeTranslator.kt | 2 +- 13 files changed, 91 insertions(+), 43 deletions(-) diff --git a/examples/gradle/dokka-gradle-example/src/main/kotlin/demo/HelloWorld.kt b/examples/gradle/dokka-gradle-example/src/main/kotlin/demo/HelloWorld.kt index 172e18f7cfb..e37fcfb7039 100644 --- a/examples/gradle/dokka-gradle-example/src/main/kotlin/demo/HelloWorld.kt +++ b/examples/gradle/dokka-gradle-example/src/main/kotlin/demo/HelloWorld.kt @@ -17,4 +17,4 @@ class Greeter(val name: String) { fun main(args: Array) { Greeter(args[0]).greet() -} +} \ No newline at end of file diff --git a/plugins/base/src/test/kotlin/model/ClassesTest.kt b/plugins/base/src/test/kotlin/model/ClassesTest.kt index 82b81f2b7f7..0c361821361 100644 --- a/plugins/base/src/test/kotlin/model/ClassesTest.kt +++ b/plugins/base/src/test/kotlin/model/ClassesTest.kt @@ -126,6 +126,21 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class type.name equals "Unit" } } + + with((this.companion).cast()) { + name equals "Companion" + children counts 5 + + with((this / "x").cast()) { + name equals "x" + } + + with((this / "foo").cast()) { + name equals "foo" + parameters counts 0 + type.name equals "Unit" + } + } } } } diff --git a/plugins/javadoc/build.gradle.kts b/plugins/javadoc/build.gradle.kts index e7bdc67e8d5..70570349b6d 100644 --- a/plugins/javadoc/build.gradle.kts +++ b/plugins/javadoc/build.gradle.kts @@ -7,6 +7,7 @@ import org.jetbrains.registerDokkaArtifactPublication plugins { id("org.jetbrains.conventions.kotlin-jvm") id("org.jetbrains.conventions.maven-publish") + id("org.jetbrains.conventions.base-unit-test") } dependencies { @@ -22,7 +23,11 @@ dependencies { implementation(libs.kotlinx.coroutines.core) testImplementation(kotlin("test")) - testImplementation(projects.plugins.base.baseTestUtils) + symbolsTestConfiguration(project(path = ":subprojects:analysis-kotlin-symbols", configuration = "shadow")) + descriptorsTestConfiguration(project(path = ":subprojects:analysis-kotlin-descriptors", configuration = "shadow")) + testImplementation(projects.plugins.base.baseTestUtils) { + exclude(module = "analysis-kotlin-descriptors") + } testImplementation(projects.core.testApi) testImplementation(libs.jsoup) } diff --git a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/AbstractJavadocTemplateMapTest.kt b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/AbstractJavadocTemplateMapTest.kt index 070f1834ed0..60265e3398f 100644 --- a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/AbstractJavadocTemplateMapTest.kt +++ b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/AbstractJavadocTemplateMapTest.kt @@ -28,6 +28,7 @@ internal abstract class AbstractJavadocTemplateMapTest : BaseAbstractTest() { DokkaConfiguration.ExternalDocumentationLink.jdk(8), DokkaConfiguration.ExternalDocumentationLink.kotlinStdlib() ) + classpath = listOfNotNull(jvmStdlibPath) } } } diff --git a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocAccessorNamingTest.kt b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocAccessorNamingTest.kt index 3f7e1610f43..f78506e8914 100644 --- a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocAccessorNamingTest.kt +++ b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocAccessorNamingTest.kt @@ -16,6 +16,7 @@ internal class JavadocAccessorNamingTest : AbstractJavadocTemplateMapTest() { sourceSets { sourceSet { sourceRoots = listOf("src/main/kotlin") + classpath = listOfNotNull(jvmStdlibPath) } } } @@ -54,13 +55,20 @@ internal class JavadocAccessorNamingTest : AbstractJavadocTemplateMapTest() { .select("code") .map { it.text() } .toSet() - - assertEquals(setOf( - "getIssuesFetched()", - "setIssuesFetched(Integer issuesFetched)", - "isFoo()", - "setFoo(String isFoo)", - ), props) + // In K2 name of accessors parameter is `value` + assert( + setOf( + "getIssuesFetched()", + "setIssuesFetched(Integer issuesFetched)", + "isFoo()", + "setFoo(String isFoo)", + ) == props || setOf( + "getIssuesFetched()", + "setIssuesFetched(Integer value)", + "isFoo()", + "setFoo(String value)", + ) == props + ) val descriptionLinks = html .select("div.description") diff --git a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocIndexTest.kt b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocIndexTest.kt index ec36d0e303d..f1ac52b9f00 100644 --- a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocIndexTest.kt +++ b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/JavadocIndexTest.kt @@ -7,6 +7,7 @@ package org.jetbrains.dokka.javadoc import org.jetbrains.dokka.javadoc.pages.IndexPage import org.jetbrains.dokka.javadoc.renderer.TemplateMap import org.jetbrains.dokka.links.DRI +import org.junit.jupiter.api.Tag import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -69,6 +70,7 @@ internal class JavadocIndexTest : AbstractJavadocTemplateMapTest() { } } + @Tag("onlyDescriptors") // K2: does not have getters: getName, getOrdinal @Test fun `handles correct number of elements`() { //We are checking whether we will have an additional function for enum classes @@ -76,6 +78,21 @@ internal class JavadocIndexTest : AbstractJavadocTemplateMapTest() { AnnotationTarget.ANNOTATION_CLASS::class.java.methods.any { it.name == "describeConstable" } testIndexPages(commonTestQuery) { indexPages -> + // K2: does not have getters: getName, getOrdinal + assertEquals("A-index: a, A\n" + + "B-index: b\n" + + "C-index: c, ClassA, ClassB, ClassC, ClassC.InnerClass, ClassCEnum, compareTo\n" + + "D-index: d, D\n" + + "E-index: e, E, equals, equals\n" + + "F-index: f\n" + + "G-index: g, getDeclaringClass, getEntries, getName, getOrdinal\n" + + "H-index: h, hashCode, hashCode\n" + + "J-index: j\n" + + "K-index: k\n" + + "P-index: package0, package1\n" + + "T-index: toString, toString\n" + + "V-index: valueOf, values", + indexPages.joinToString("\n") { it.title + ": " + it.elements.joinToString { it.getId() } }) assertEquals(if (hasAdditionalFunction()) 33 else 32, indexPages.sumBy { it.elements.size }) } } diff --git a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/location/JavadocLocationTest.kt b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/location/JavadocLocationTest.kt index 60de0e3d331..bef81372e8a 100644 --- a/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/location/JavadocLocationTest.kt +++ b/plugins/javadoc/src/test/kotlin/org/jetbrains/dokka/javadoc/location/JavadocLocationTest.kt @@ -247,6 +247,7 @@ class JavadocLocationTest : BaseAbstractTest() { DokkaConfiguration.ExternalDocumentationLink.kotlinStdlib() ) analysisPlatform = "jvm" + classpath = listOfNotNull(jvmStdlibPath) } } } diff --git a/plugins/kotlin-as-java/build.gradle.kts b/plugins/kotlin-as-java/build.gradle.kts index 19a7bf78f19..0b85589575d 100644 --- a/plugins/kotlin-as-java/build.gradle.kts +++ b/plugins/kotlin-as-java/build.gradle.kts @@ -7,6 +7,7 @@ import org.jetbrains.registerDokkaArtifactPublication plugins { id("org.jetbrains.conventions.kotlin-jvm") id("org.jetbrains.conventions.maven-publish") + id("org.jetbrains.conventions.base-unit-test") } dependencies { @@ -20,7 +21,11 @@ dependencies { testImplementation(kotlin("test")) testImplementation(libs.jsoup) testImplementation(projects.plugins.base) - testImplementation(projects.plugins.base.baseTestUtils) + symbolsTestConfiguration(project(path = ":subprojects:analysis-kotlin-symbols", configuration = "shadow")) + descriptorsTestConfiguration(project(path = ":subprojects:analysis-kotlin-descriptors", configuration = "shadow")) + testImplementation(projects.plugins.base.baseTestUtils) { + exclude(module = "analysis-kotlin-descriptors") + } testImplementation(projects.core.contentMatcherTestUtils) testImplementation(projects.core.testApi) } diff --git a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt index f6124f88fe9..93d5c1b5e47 100644 --- a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt +++ b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaPluginTest.kt @@ -377,6 +377,7 @@ class KotlinAsJavaPluginTest : BaseAbstractTest() { DokkaConfiguration.ExternalDocumentationLink.jdk(8), stdlibExternalDocumentationLink ) + classpath = listOfNotNull(jvmStdlibPath) } } } diff --git a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaSignatureTest.kt b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaSignatureTest.kt index c87fda5eda8..253128104d3 100644 --- a/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaSignatureTest.kt +++ b/plugins/kotlin-as-java/src/test/kotlin/KotlinAsJavaSignatureTest.kt @@ -23,6 +23,7 @@ class KotlinAsJavaSignatureTest : BaseAbstractTest() { DokkaConfiguration.ExternalDocumentationLink.jdk(8), stdlibExternalDocumentationLink ) + classpath = listOfNotNull(jvmStdlibPath) } } } diff --git a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/AnnotationTranslator.kt b/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/AnnotationTranslator.kt index faae08e205d..c9882487d4b 100644 --- a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/AnnotationTranslator.kt +++ b/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/AnnotationTranslator.kt @@ -11,6 +11,7 @@ import org.jetbrains.dokka.model.ClassValue import org.jetbrains.kotlin.analysis.api.KtAnalysisSession import org.jetbrains.kotlin.analysis.api.annotations.* import org.jetbrains.kotlin.analysis.api.base.KtConstantValue +import org.jetbrains.kotlin.analysis.api.symbols.KtPropertySymbol import org.jetbrains.kotlin.analysis.api.symbols.KtSymbol import org.jetbrains.kotlin.analysis.api.symbols.KtSymbolOrigin import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget @@ -34,12 +35,16 @@ internal class AnnotationTranslator { annotated.annotations.map { toDokkaAnnotation(it) } /** - * @return direct annotations and file-level annotations + * The examples of annotations from backing field are [JvmField], [JvmSynthetic]. + * + * @return direct annotations, annotations from backing field and file-level annotations */ fun KtAnalysisSession.getAllAnnotationsFrom(annotated: KtAnnotated): List { val directAnnotations = getDirectAnnotationsFrom(annotated) - val fileLevelAnnotations = (annotated as? KtSymbol)?.let { getFileLevelAnnotationsFrom(it) } ?: emptyList() - return directAnnotations + fileLevelAnnotations + val backingFieldAnnotations = + (annotated as? KtPropertySymbol)?.backingFieldSymbol?.let { getDirectAnnotationsFrom(it) }.orEmpty() + val fileLevelAnnotations = (annotated as? KtSymbol)?.let { getFileLevelAnnotationsFrom(it) }.orEmpty() + return directAnnotations + backingFieldAnnotations + fileLevelAnnotations } private fun KtAnnotationApplication.isNoExistedInSource() = psi == null diff --git a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/DefaultSymbolToDocumentableTranslator.kt b/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/DefaultSymbolToDocumentableTranslator.kt index 74d312690f0..f640c7b14d5 100644 --- a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/DefaultSymbolToDocumentableTranslator.kt +++ b/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/DefaultSymbolToDocumentableTranslator.kt @@ -212,7 +212,15 @@ internal class DokkaSymbolVisitor( val isActual = namedClassOrObjectSymbol.isActual val documentation = getDocumentation(namedClassOrObjectSymbol)?.toSourceSetDependent() ?: emptyMap() - val (constructors, functions, properties, classlikes) = getDokkaScopeFrom(namedClassOrObjectSymbol, dri) + val (constructors, functions, properties, classlikesWithoutCompanion) = getDokkaScopeFrom(namedClassOrObjectSymbol, dri) + + val companionObject = namedClassOrObjectSymbol.companionObject?.let { + visitNamedClassOrObjectSymbol( + it, + dri + ) + } as? DObject + val classlikes = if (companionObject == null) classlikesWithoutCompanion else classlikesWithoutCompanion + companionObject val generics = namedClassOrObjectSymbol.typeParameters.mapIndexed { index, symbol -> visitVariantTypeParameter( @@ -229,7 +237,6 @@ internal class DokkaSymbolVisitor( namedClassOrObjectSymbol.superTypes.filterNot { it.isAny } .map { with(typeTranslator) { toTypeConstructorWithKindFrom(it) } } .toSourceSetDependent() - return@withExceptionCatcher when (namedClassOrObjectSymbol.classKind) { KtClassKind.OBJECT, KtClassKind.COMPANION_OBJECT -> DObject( @@ -268,12 +275,7 @@ internal class DokkaSymbolVisitor( generics = generics, documentation = documentation, modifier = namedClassOrObjectSymbol.getDokkaModality().toSourceSetDependent(), - companion = namedClassOrObjectSymbol.companionObject?.let { - visitNamedClassOrObjectSymbol( - it, - dri - ) - } as? DObject, + companion = companionObject, sourceSets = setOf(sourceSet), isExpectActual = (isExpect || isActual), extra = PropertyContainer.withAll( @@ -296,12 +298,7 @@ internal class DokkaSymbolVisitor( supertypes = supertypes, generics = generics, documentation = documentation, - companion = namedClassOrObjectSymbol.companionObject?.let { - visitNamedClassOrObjectSymbol( - it, - dri - ) - } as? DObject, + companion = companionObject, sourceSets = setOf(sourceSet), isExpectActual = (isExpect || isActual), extra = PropertyContainer.withAll( @@ -322,12 +319,7 @@ internal class DokkaSymbolVisitor( expectPresentInSet = sourceSet.takeIf { isExpect }, sourceSets = setOf(sourceSet), isExpectActual = (isExpect || isActual), - companion = namedClassOrObjectSymbol.companionObject?.let { - visitNamedClassOrObjectSymbol( - it, - dri - ) - } as? DObject, + companion = companionObject, visibility = namedClassOrObjectSymbol.getDokkaVisibility().toSourceSetDependent(), generics = generics, constructors = constructors, @@ -401,7 +393,7 @@ internal class DokkaSymbolVisitor( val constructors: List, val functions: List, val properties: List, - val classlikes: List + val classlikesWithoutCompanion: List ) /** @@ -445,13 +437,10 @@ internal class DokkaSymbolVisitor( javaFields.map { visitJavaFieldSymbol(it, dri) } - // hack, by default, compiler adds an empty companion object for enum - // TODO check if it is empty - fun List.filterOutEnumCompanion() = - if (namedClassOrObjectSymbol.classKind == KtClassKind.ENUM_CLASS) + fun List.filterOutCompanion() = filterNot { - it.name.asString() == "Companion" && it.classKind == KtClassKind.COMPANION_OBJECT - } else this + it.classKind == KtClassKind.COMPANION_OBJECT + } fun List.filterOutAndMarkAlreadyVisited() = filterNot { symbol -> visitedNamedClassOrObjectSymbol.contains(symbol.classIdIfNonLocal) @@ -465,7 +454,7 @@ internal class DokkaSymbolVisitor( } val classlikes = classifiers.filterIsInstance() - .filterOutEnumCompanion() // hack to filter out companion for enum + .filterOutCompanion() // also, this is a hack to filter out companion for enum .filterOutAndMarkAlreadyVisited() .map { visitNamedClassOrObjectSymbol(it, dri) } @@ -473,7 +462,7 @@ internal class DokkaSymbolVisitor( constructors = constructors, functions = functions, properties = properties, - classlikes = classlikes + classlikesWithoutCompanion = classlikes ) } diff --git a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/TypeTranslator.kt b/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/TypeTranslator.kt index 7127cbdf3db..f7366294b1e 100644 --- a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/TypeTranslator.kt +++ b/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/TypeTranslator.kt @@ -44,7 +44,7 @@ internal class TypeTranslator( typeAlias = GenericTypeConstructor( dri = getDRIFromNonErrorClassType(classType), projections = classType.ownTypeArguments.map { toProjection(it) }), - inner = toBoundFrom(classSymbol.expandedType), + inner = toBoundFrom(classType.fullyExpandedType), extra = PropertyContainer.withAll( getDokkaAnnotationsFrom(classType)?.toSourceSetDependent()?.toAnnotations() )