Skip to content

Commit

Permalink
Enable tests for as-java and javadoc plugins and fix some
Browse files Browse the repository at this point in the history
  • Loading branch information
vmishenev committed Oct 17, 2023
1 parent b7c093f commit 88f4b62
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class Greeter(val name: String) {

fun main(args: Array<String>) {
Greeter(args[0]).greet()
}
}
15 changes: 15 additions & 0 deletions plugins/base/src/test/kotlin/model/ClassesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class
type.name equals "Unit"
}
}

with((this.companion).cast<DObject>()) {
name equals "Companion"
children counts 5

with((this / "x").cast<DProperty>()) {
name equals "x"
}

with((this / "foo").cast<DFunction>()) {
name equals "foo"
parameters counts 0
type.name equals "Unit"
}
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion plugins/javadoc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal abstract class AbstractJavadocTemplateMapTest : BaseAbstractTest() {
DokkaConfiguration.ExternalDocumentationLink.jdk(8),
DokkaConfiguration.ExternalDocumentationLink.kotlinStdlib()
)
classpath = listOfNotNull(jvmStdlibPath)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class JavadocAccessorNamingTest : AbstractJavadocTemplateMapTest() {
sourceSets {
sourceSet {
sourceRoots = listOf("src/main/kotlin")
classpath = listOfNotNull(jvmStdlibPath)
}
}
}
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,13 +70,29 @@ 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
fun hasAdditionalFunction() =
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 })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class JavadocLocationTest : BaseAbstractTest() {
DokkaConfiguration.ExternalDocumentationLink.kotlinStdlib()
)
analysisPlatform = "jvm"
classpath = listOfNotNull(jvmStdlibPath)
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion plugins/kotlin-as-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ class KotlinAsJavaPluginTest : BaseAbstractTest() {
DokkaConfiguration.ExternalDocumentationLink.jdk(8),
stdlibExternalDocumentationLink
)
classpath = listOfNotNull(jvmStdlibPath)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class KotlinAsJavaSignatureTest : BaseAbstractTest() {
DokkaConfiguration.ExternalDocumentationLink.jdk(8),
stdlibExternalDocumentationLink
)
classpath = listOfNotNull(jvmStdlibPath)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Annotations.Annotation> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -401,7 +393,7 @@ internal class DokkaSymbolVisitor(
val constructors: List<DFunction>,
val functions: List<DFunction>,
val properties: List<DProperty>,
val classlikes: List<DClasslike>
val classlikesWithoutCompanion: List<DClasslike>
)

/**
Expand Down Expand Up @@ -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<KtNamedClassOrObjectSymbol>.filterOutEnumCompanion() =
if (namedClassOrObjectSymbol.classKind == KtClassKind.ENUM_CLASS)
fun List<KtNamedClassOrObjectSymbol>.filterOutCompanion() =
filterNot {
it.name.asString() == "Companion" && it.classKind == KtClassKind.COMPANION_OBJECT
} else this
it.classKind == KtClassKind.COMPANION_OBJECT
}

fun List<KtNamedClassOrObjectSymbol>.filterOutAndMarkAlreadyVisited() = filterNot { symbol ->
visitedNamedClassOrObjectSymbol.contains(symbol.classIdIfNonLocal)
Expand All @@ -465,15 +454,15 @@ internal class DokkaSymbolVisitor(
}

val classlikes = classifiers.filterIsInstance<KtNamedClassOrObjectSymbol>()
.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) }

return DokkaScope(
constructors = constructors,
functions = functions,
properties = properties,
classlikes = classlikes
classlikesWithoutCompanion = classlikes
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand Down

0 comments on commit 88f4b62

Please sign in to comment.