From cacf1e0c6cda4e42fe6581946cad53a377c71ec7 Mon Sep 17 00:00:00 2001 From: Szymon Świstun Date: Wed, 26 Feb 2020 11:52:03 +0100 Subject: Port some of the core tests from the previous model --- plugins/base/src/test/kotlin/model/ClassesTest.kt | 448 ++++++++++++++++++++++ 1 file changed, 448 insertions(+) create mode 100644 plugins/base/src/test/kotlin/model/ClassesTest.kt (limited to 'plugins/base/src/test/kotlin/model/ClassesTest.kt') diff --git a/plugins/base/src/test/kotlin/model/ClassesTest.kt b/plugins/base/src/test/kotlin/model/ClassesTest.kt new file mode 100644 index 00000000..2332df61 --- /dev/null +++ b/plugins/base/src/test/kotlin/model/ClassesTest.kt @@ -0,0 +1,448 @@ +package model + +import org.jetbrains.dokka.model.* +import org.jetbrains.dokka.model.Function +import org.jetbrains.dokka.model.Object +import org.jetbrains.dokka.model.WithAbstraction.Modifier +import org.jetbrains.kotlin.descriptors.Visibilities +import org.junit.Test +import utils.AbstractModelTest +import utils.assertNotNull +import utils.supers + + +class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "classes") { + + @Test + fun emptyClass() { + inlineModelTest( + """ + |class Klass {}""" + ) { + with((this / "classes" / "Klass").cast()) { + name equals "Klass" + children counts 4 + } + } + } + + @Test + fun emptyObject() { + inlineModelTest( + """ + |object Obj {} + """ + ) { + with((this / "classes" / "Obj").cast()) { + name equals "Obj" + children counts 3 + } + } + } + + @Test + fun classWithConstructor() { + inlineModelTest( + """ + |class Klass(name: String) + """ + ) { + with((this / "classes" / "Klass").cast()) { + name equals "Klass" + children counts 4 + + with(constructors.firstOrNull().assertNotNull("Constructor")) { + visibility.values allEquals Visibilities.PUBLIC + parameters counts 1 + with(parameters.firstOrNull().assertNotNull("Constructor parameter")) { + name equals "name" + type.constructorFqName equals "kotlin.String" + } + } + + } + } + } + + @Test + fun classWithFunction() { + inlineModelTest( + """ + |class Klass { + | fun fn() {} + |} + """ + ) { + with((this / "classes" / "Klass").cast()) { + name equals "Klass" + children counts 5 + + with((this / "fn").cast()) { + type.constructorFqName equals "kotlin.Unit" + parameters counts 0 + visibility.values allEquals Visibilities.PUBLIC + } + } + } + } + + @Test + fun classWithProperty() { + inlineModelTest( + """ + |class Klass { + | val name: String = "" + |} + """ + ) { + with((this / "classes" / "Klass").cast()) { + name equals "Klass" + children counts 5 + + with((this / "name").cast()) { + name equals "name" + // TODO property name + } + } + } + } + + @Test + fun classWithCompanionObject() { + inlineModelTest( + """ + |class Klass() { + | companion object { + | val x = 1 + | fun foo() {} + | } + |} + """ + ) { + with((this / "classes" / "Klass").cast()) { + name equals "Klass" + children counts 5 + + 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.constructorFqName equals "kotlin.Unit" + } + } + } + } + } + + @Test + fun dataClass() { + inlineModelTest( + """ + |data class Klass() {} + """ + ) { + with((this / "classes" / "Klass").cast()) { + name equals "Klass" + visibility.values allEquals Visibilities.PUBLIC + // TODO data modifier + } + } + } + +// @Test fun dataClass() { +// verifyPackageMember("testdata/classes/dataClass.kt", defaultModelConfig) { cls -> +// val modifiers = cls.details(NodeKind.Modifier).map { it.name } +// assertTrue("data" in modifiers) +// } +// } + + @Test + fun sealedClass() { + inlineModelTest( + """ + |sealed class Klass() {} + """ + ) { + with((this / "classes" / "Klass").cast()) { + name equals "Klass" + modifier equals WithAbstraction.Modifier.Sealed + } + } + } + +// // TODO modifiers +// @Test fun annotatedClassWithAnnotationParameters() { +// checkSourceExistsAndVerifyModel( +// "testdata/classes/annotatedClassWithAnnotationParameters.kt", +// defaultModelConfig +// ) { model -> +// with(model.members.single().members.single()) { +// with(deprecation!!) { +// assertEquals("Deprecated", name) +// assertEquals(Content.Empty, content) +// assertEquals(NodeKind.Annotation, kind) +// assertEquals(1, details.count()) +// with(details[0]) { +// assertEquals(NodeKind.Parameter, kind) +// assertEquals(1, details.count()) +// with(details[0]) { +// assertEquals(NodeKind.Value, kind) +// assertEquals("\"should no longer be used\"", name) +// } +// } +// } +// } +// } +// } + + @Test + fun notOpenClass() { + inlineModelTest( + """ + |open class C() { + | open fun f() {} + |} + | + |class D() : C() { + | override fun f() {} + |} + """ + ) { + val C = (this / "classes" / "C").cast() + val D = (this / "classes" / "D").cast() + + with(C) { + modifier equals Modifier.Open + with((this / "f").cast()) { + modifier equals Modifier.Open + } + } + with(D) { + modifier equals Modifier.Final + with((this / "f").cast()) { + modifier equals Modifier.Open + } + D.supertypes.flatMap { it.component2() }.firstOrNull() equals C.dri + } + } + } + + @Test + fun indirectOverride() { + inlineModelTest( + """ + |abstract class C() { + | abstract fun foo() + |} + | + |abstract class D(): C() + | + |class E(): D() { + | override fun foo() {} + |} + """ + ) { + val C = (this / "classes" / "C").cast() + val D = (this / "classes" / "D").cast() + val E = (this / "classes" / "E").cast() + + with(C) { + modifier equals Modifier.Abstract + ((this / "foo").cast()).modifier equals Modifier.Abstract + } + + with(D) { + modifier equals Modifier.Abstract + } + + with(E) { + modifier equals Modifier.Final + + } + D.supers.firstOrNull() equals C.dri + E.supers.firstOrNull() equals D.dri + } + } + + @Test // todo inner class + fun innerClass() { + inlineModelTest( + """ + |class C { + | inner class D {} + |} + """ + ) { + with((this / "classes" / "C").cast()) { + + with((this / "D").cast()) { + } + } + } + } + +// // TODO modifiers +// @Test fun innerClass() { +// verifyPackageMember("testdata/classes/innerClass.kt", defaultModelConfig) { cls -> +// val innerClass = cls.members.single { it.name == "D" } +// val modifiers = innerClass.details(NodeKind.Modifier) +// assertEquals(3, modifiers.size) +// assertEquals("inner", modifiers[2].name) +// } +// } + + @Test + fun companionObjectExtension() { + inlineModelTest( + """ + |class Klass { + | companion object Default {} + |} + | + |/** + | * The def + | */ + |val Klass.Default.x: Int get() = 1 + """ + ) { + with((this / "classes" / "Klass").cast()) { + name equals "Klass" + + with((this / "Default").cast()) { + name equals "Default" + // TODO extensions + } + } + } + } + +// @Test fun companionObjectExtension() { +// checkSourceExistsAndVerifyModel("testdata/classes/companionObjectExtension.kt", defaultModelConfig) { model -> +// val pkg = model.members.single() +// val cls = pkg.members.single { it.name == "Foo" } +// val extensions = cls.extensions.filter { it.kind == NodeKind.CompanionObjectProperty } +// assertEquals(1, extensions.size) +// } +// } + + @Test + fun secondaryConstructor() { + inlineModelTest( + """ + |class C() { + | /** This is a secondary constructor. */ + | constructor(s: String): this() {} + |} + """ + ) { + with((this / "classes" / "C").cast()) { + name equals "C" + constructors counts 2 + + constructors.map { it.name } allEquals "" + + with(constructors.find { it.parameters.isNullOrEmpty() } notNull "C()") { + parameters counts 0 + } + + with(constructors.find { it.parameters.isNotEmpty() } notNull "C(String)") { + parameters counts 1 + with(parameters.firstOrNull() notNull "Constructor parameter") { + name equals "s" + type.constructorFqName equals "kotlin.String" + } + } + } + } + } + + // TODO modifiers +// @Test fun sinceKotlin() { +// checkSourceExistsAndVerifyModel("testdata/classes/sinceKotlin.kt", defaultModelConfig) { model -> +// with(model.members.single().members.single()) { +// assertEquals("1.1", sinceKotlin) +// } +// } +// } + + @Test + fun privateCompanionObject() { + inlineModelTest( + """ + |class Klass { + | private companion object { + | fun fn() {} + | val a = 0 + | } + |} + """ + ) { + with((this / "classes" / "Klass").cast()) { + name equals "Klass" + + with((this / "Companion").cast()) { + name equals "Companion" + visibility.values allEquals Visibilities.PRIVATE + + with((this / "fn").cast()) { + name equals "fn" + parameters counts 0 + receiver equals null + } + } + } + } + } + + // TODO annotations +// @Test +// fun annotatedClass() { +// verifyPackageMember("testdata/classes/annotatedClass.kt", ModelConfig( +// analysisPlatform = analysisPlatform, +// withKotlinRuntime = true +// ) +// ) { cls -> +// Assert.assertEquals(1, cls.annotations.count()) +// with(cls.annotations[0]) { +// Assert.assertEquals("Strictfp", name) +// Assert.assertEquals(Content.Empty, content) +// Assert.assertEquals(NodeKind.Annotation, kind) +// } +// } +// } + + +// TODO annotations + +// @Test fun javaAnnotationClass() { +// checkSourceExistsAndVerifyModel( +// "testdata/classes/javaAnnotationClass.kt", +// modelConfig = ModelConfig(analysisPlatform = analysisPlatform, withJdk = true) +// ) { model -> +// with(model.members.single().members.single()) { +// Assert.assertEquals(1, annotations.count()) +// with(annotations[0]) { +// Assert.assertEquals("Retention", name) +// Assert.assertEquals(Content.Empty, content) +// Assert.assertEquals(NodeKind.Annotation, kind) +// with(details[0]) { +// Assert.assertEquals(NodeKind.Parameter, kind) +// Assert.assertEquals(1, details.count()) +// with(details[0]) { +// Assert.assertEquals(NodeKind.Value, kind) +// Assert.assertEquals("RetentionPolicy.SOURCE", name) +// } +// } +// } +// } +// } +// } + +} \ No newline at end of file -- cgit From 9b7a59cdc93a6dcd9ab0623f6f476abf92d9427d Mon Sep 17 00:00:00 2001 From: Szymon Świstun Date: Mon, 2 Mar 2020 11:07:47 +0100 Subject: Add information about modifiers to Documentables' extra properties --- core/src/main/kotlin/model/Documentable.kt | 7 +- core/src/main/kotlin/model/aditionalExtras.kt | 11 +++ .../src/main/kotlin/model/properties/properties.kt | 13 +-- .../DefaultDescriptorToDocumentableTranslator.kt | 93 +++++++++++++++++++--- plugins/base/src/test/kotlin/model/ClassesTest.kt | 4 +- 5 files changed, 107 insertions(+), 21 deletions(-) create mode 100644 core/src/main/kotlin/model/aditionalExtras.kt (limited to 'plugins/base/src/test/kotlin/model/ClassesTest.kt') diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index 3bc1690b..9de06ba7 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -3,8 +3,6 @@ package org.jetbrains.dokka.model import com.intellij.psi.PsiNamedElement import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.doc.DocumentationNode -import org.jetbrains.dokka.model.properties.ExtraProperty -import org.jetbrains.dokka.model.properties.MergeStrategy import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.model.properties.WithExtraProperties import org.jetbrains.dokka.pages.PlatformData @@ -335,6 +333,11 @@ sealed class Projection { data class Nullable(val inner: Projection) : Projection() } +enum class ExtraModifiers { + STATIC, INLINE, INFIX, SUSPEND, REIFIED, CROSSINLINE, NOINLINE, + OVERRIDE, DATA, CONST, DYNAMIC, EXTERNAL, INNER, LATEINIT, OPERATOR, TAILREC, VARARG +} + private fun String.shorten(maxLength: Int) = lineSequence().first().let { if (it.length != length || it.length > maxLength) it.take(maxLength - 3) + "..." else it } diff --git a/core/src/main/kotlin/model/aditionalExtras.kt b/core/src/main/kotlin/model/aditionalExtras.kt new file mode 100644 index 00000000..69e89a2a --- /dev/null +++ b/core/src/main/kotlin/model/aditionalExtras.kt @@ -0,0 +1,11 @@ +package org.jetbrains.dokka.model + +import org.jetbrains.dokka.model.properties.ExtraProperty + +class AdditionalModifiers(val content: List) : ExtraProperty { + object AdditionalKey : ExtraProperty.Key + + override fun equals(other: Any?): Boolean = if (other is AdditionalModifiers) other.content == content else false + override fun hashCode() = content.hashCode() + override val key: ExtraProperty.Key = AdditionalKey +} \ No newline at end of file diff --git a/core/src/main/kotlin/model/properties/properties.kt b/core/src/main/kotlin/model/properties/properties.kt index 83f8d63d..7010d0df 100644 --- a/core/src/main/kotlin/model/properties/properties.kt +++ b/core/src/main/kotlin/model/properties/properties.kt @@ -1,21 +1,22 @@ package org.jetbrains.dokka.model.properties interface ExtraProperty { - interface Key { + interface Key { fun mergeStrategyFor(left: T, right: T): MergeStrategy = MergeStrategy.Fail { throw NotImplementedError("Property merging for $this is not implemented") } } + val key: Key } -interface CalculatedProperty: ExtraProperty.Key { +interface CalculatedProperty : ExtraProperty.Key { fun calculate(subject: C): T } sealed class MergeStrategy { - class Replace(val newProperty: ExtraProperty): MergeStrategy() - object Remove: MergeStrategy() - class Full(val merger: (preMerged: C, left: C, right: C) -> C): MergeStrategy() - class Fail(val error: () -> Nothing): MergeStrategy() + class Replace(val newProperty: ExtraProperty) : MergeStrategy() + object Remove : MergeStrategy() + class Full(val merger: (preMerged: C, left: C, right: C) -> C) : MergeStrategy() + class Fail(val error: () -> Nothing) : MergeStrategy() } diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 531537a4..cea408d7 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -8,17 +8,23 @@ import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.Enum import org.jetbrains.dokka.model.Function import org.jetbrains.dokka.model.doc.DocumentationNode +import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.dokka.parsers.MarkdownParser import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.transformers.descriptors.DescriptorToDocumentableTranslator +import org.jetbrains.kotlin.codegen.isJvmStaticInObjectOrClassOrInterface import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies import org.jetbrains.kotlin.idea.kdoc.findKDoc import org.jetbrains.kotlin.idea.refactoring.fqName.fqName +import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.calls.components.isVararg +import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperclassesWithoutAny +import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperInterfaces import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.MemberScope @@ -102,7 +108,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private documentation = info.docs, generics = descriptor.typeConstructor.parameters.map { it.toTypeParameter() }, companion = descriptor.companion(driWithPlatform), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -121,7 +128,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private visibility = PlatformDependent(mapOf(platformData to descriptor.visibility)), supertypes = PlatformDependent.from(platformData, info.supertypes), documentation = info.docs, - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -143,7 +151,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private supertypes = PlatformDependent.from(platformData, info.supertypes), documentation = info.docs, companion = descriptor.companion(driWithPlatform), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -158,7 +167,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private classlikes = scope.classlikes(driWithPlatform), functions = scope.functions(driWithPlatform), properties = scope.properties(driWithPlatform), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -188,7 +198,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private modifier = descriptor.modifier(), companion = descriptor.companion(driWithPlatform), supertypes = PlatformDependent.from(platformData, info.supertypes), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -213,7 +224,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private documentation = descriptor.resolveDescriptorData(platformData), modifier = descriptor.modifier(), type = KotlinTypeWrapper(descriptor.returnType!!), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -237,7 +249,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private documentation = descriptor.resolveDescriptorData(platformData), modifier = descriptor.modifier(), type = KotlinTypeWrapper(descriptor.returnType!!), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -260,7 +273,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private type = KotlinTypeWrapper(descriptor.returnType), modifier = descriptor.modifier(), generics = descriptor.typeParameters.map { it.toTypeParameter() }, - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -289,7 +303,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private this.name.asString(), type = KotlinTypeWrapper(this.type), documentation = descriptor.resolveDescriptorData(platformData), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) val name = run { @@ -322,7 +337,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private ) }, sources = descriptor.createSources(), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -332,7 +348,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private name = descriptor.name.asString(), type = KotlinTypeWrapper(descriptor.type), documentation = descriptor.resolveDescriptorData(platformData), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) private fun MemberScope.functions(parent: DRIWithPlatformInfo): List = @@ -378,7 +395,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private fqNameSafe.asString(), PlatformDependent.from(platformData, getDocumentation()), upperBounds.map { it.toProjection() }, - listOf(platformData) + listOf(platformData), + extra = additionalExtras() ) private fun KotlinType.toProjection(): Projection = when (constructor.declarationDescriptor) { @@ -419,5 +437,56 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private PlatformDependent(mapOf(platformData to DescriptorDocumentableSource(this))) } + inline fun FunctionDescriptor.additionalExtras(): PropertyContainer = listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.INFIX.takeIf { isInfix }, + ExtraModifiers.INLINE.takeIf { isInline }, + ExtraModifiers.SUSPEND.takeIf { isSuspend }, + ExtraModifiers.OPERATOR.takeIf { isOperator }, + ExtraModifiers.STATIC.takeIf { isJvmStaticInObjectOrClassOrInterface() }, + ExtraModifiers.TAILREC.takeIf { isTailrec }, + ExtraModifiers.EXTERNAL.takeIf { isExternal }, + ExtraModifiers.OVERRIDE.takeIf { DescriptorUtils.isOverride(this) } + ).toContainer() + + inline fun ClassDescriptor.additionalExtras(): PropertyContainer = listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.INLINE.takeIf { isInline }, + ExtraModifiers.EXTERNAL.takeIf { isExternal }, + ExtraModifiers.INNER.takeIf { isInner }, + ExtraModifiers.DATA.takeIf { isData }, + ExtraModifiers.OVERRIDE.takeIf { getSuperInterfaces().isNotEmpty() || getSuperClassNotAny() != null } + ).toContainer() + + fun ValueParameterDescriptor.additionalExtras(): PropertyContainer = + listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.NOINLINE.takeIf { isNoinline }, + ExtraModifiers.CROSSINLINE.takeIf { isCrossinline }, + ExtraModifiers.CONST.takeIf { isConst }, + ExtraModifiers.LATEINIT.takeIf { isLateInit }, + ExtraModifiers.VARARG.takeIf { isVararg } + ).toContainer() + + fun TypeParameterDescriptor.additionalExtras(): PropertyContainer = + listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.REIFIED.takeIf { isReified } + ).toContainer() + + fun PropertyDescriptor.additionalExtras(): PropertyContainer = listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.CONST.takeIf { isConst }, + ExtraModifiers.LATEINIT.takeIf { isLateInit }, + ExtraModifiers.STATIC.takeIf { isJvmStaticInObjectOrClassOrInterface() }, + ExtraModifiers.EXTERNAL.takeIf { isExternal }, + ExtraModifiers.OVERRIDE.takeIf { DescriptorUtils.isOverride(this) } + ).toContainer() + + inline fun List.toContainer( + container: PropertyContainer = PropertyContainer.empty() + ): PropertyContainer = + container + AdditionalModifiers(this) + data class ClassInfo(val supertypes: List, val docs: PlatformDependent) } diff --git a/plugins/base/src/test/kotlin/model/ClassesTest.kt b/plugins/base/src/test/kotlin/model/ClassesTest.kt index 2332df61..18288442 100644 --- a/plugins/base/src/test/kotlin/model/ClassesTest.kt +++ b/plugins/base/src/test/kotlin/model/ClassesTest.kt @@ -151,7 +151,9 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class with((this / "classes" / "Klass").cast()) { name equals "Klass" visibility.values allEquals Visibilities.PUBLIC - // TODO data modifier + with(extra[AdditionalModifiers.AdditionalKey].assertNotNull("Extras")) { + content.find{it == ExtraModifiers.DATA}.assertNotNull("data modifier") + } } } } -- cgit From 5a02fd66982b92984f76eaaca16ba6045f55ff31 Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Wed, 26 Feb 2020 13:41:31 +0100 Subject: Replace Visibility used in Documentables with custom classes --- core/src/main/kotlin/model/Documentable.kt | 18 +++++++++++-- .../psi/DefaultPsiToDocumentableTranslator.kt | 9 ++++--- .../DefaultDescriptorToDocumentableTranslator.kt | 31 ++++++++++++++-------- plugins/base/src/test/kotlin/model/ClassesTest.kt | 12 ++++----- plugins/base/src/test/kotlin/model/PropertyTest.kt | 6 ++--- 5 files changed, 49 insertions(+), 27 deletions(-) (limited to 'plugins/base/src/test/kotlin/model/ClassesTest.kt') diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index 9de06ba7..c16f8a87 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -7,7 +7,6 @@ import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.model.properties.WithExtraProperties import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.load.kotlin.toSourceElement abstract class Documentable { @@ -65,7 +64,7 @@ interface WithPackages { } interface WithVisibility { - val visibility: PlatformDependent // TODO custom visibility + val visibility: PlatformDependent } interface WithType { @@ -349,6 +348,21 @@ fun Documentable.dfs(predicate: (Documentable) -> Boolean): Documentable? = this.children.asSequence().mapNotNull { it.dfs(predicate) }.firstOrNull() } +sealed class Visibility +sealed class KotlinVisibility : Visibility() { + object Public : KotlinVisibility() + object Private : KotlinVisibility() + object Protected : KotlinVisibility() + object Internal : KotlinVisibility() +} + +sealed class JavaVisibility : Visibility() { + object Public : JavaVisibility() + object Private : JavaVisibility() + object Protected : JavaVisibility() + object Default : JavaVisibility() +} + fun PlatformDependent?.orEmpty(): PlatformDependent = this ?: PlatformDependent.empty() sealed class DocumentableSource(val path: String) diff --git a/plugins/base/src/main/kotlin/transformers/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/transformers/psi/DefaultPsiToDocumentableTranslator.kt index 1001ffb3..6146ce5f 100644 --- a/plugins/base/src/main/kotlin/transformers/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/transformers/psi/DefaultPsiToDocumentableTranslator.kt @@ -61,11 +61,12 @@ object DefaultPsiToDocumentableTranslator : PsiToDocumentableTranslator { private fun PsiModifierListOwner.getVisibility() = modifierList?.children?.toList()?.let { ml -> when { - ml.any { it.text == PsiKeyword.PUBLIC } -> Visibilities.PUBLIC - ml.any { it.text == PsiKeyword.PROTECTED } -> Visibilities.PROTECTED - else -> Visibilities.PRIVATE + ml.any { it.text == PsiKeyword.PUBLIC } -> JavaVisibility.Public + ml.any { it.text == PsiKeyword.PROTECTED } -> JavaVisibility.Protected + ml.any { it.text == PsiKeyword.PRIVATE } -> JavaVisibility.Private + else -> JavaVisibility.Default } - } ?: Visibilities.PRIVATE + } ?: JavaVisibility.Default private val PsiMethod.hash: Int get() = "$returnType $name$parameterList".hashCode() diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index cea408d7..83d3d178 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -16,6 +16,7 @@ import org.jetbrains.dokka.transformers.descriptors.DescriptorToDocumentableTran import org.jetbrains.kotlin.codegen.isJvmStaticInObjectOrClassOrInterface import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.ClassKind +import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies import org.jetbrains.kotlin.idea.kdoc.findKDoc import org.jetbrains.kotlin.idea.refactoring.fqName.fqName @@ -103,7 +104,7 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private properties = scope.properties(driWithPlatform), classlikes = scope.classlikes(driWithPlatform), sources = descriptor.createSources(), - visibility = PlatformDependent.from(platformData, descriptor.visibility), + visibility = PlatformDependent.from(platformData, descriptor.visibility.toDokkaVisibility()), supertypes = PlatformDependent.from(platformData, info.supertypes), documentation = info.docs, generics = descriptor.typeConstructor.parameters.map { it.toTypeParameter() }, @@ -125,7 +126,7 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private properties = scope.properties(driWithPlatform), classlikes = scope.classlikes(driWithPlatform), sources = descriptor.createSources(), - visibility = PlatformDependent(mapOf(platformData to descriptor.visibility)), + visibility = PlatformDependent(mapOf(platformData to descriptor.visibility.toDokkaVisibility())), supertypes = PlatformDependent.from(platformData, info.supertypes), documentation = info.docs, platformData = listOf(platformData), @@ -147,7 +148,7 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private properties = scope.properties(driWithPlatform), classlikes = scope.classlikes(driWithPlatform), sources = descriptor.createSources(), - visibility = PlatformDependent(mapOf(platformData to descriptor.visibility)), + visibility = PlatformDependent(mapOf(platformData to descriptor.visibility.toDokkaVisibility())), supertypes = PlatformDependent.from(platformData, info.supertypes), documentation = info.docs, companion = descriptor.companion(driWithPlatform), @@ -192,7 +193,7 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private properties = scope.properties(driWithPlatform), classlikes = scope.classlikes(driWithPlatform), sources = actual, - visibility = PlatformDependent.from(platformData, descriptor.visibility), + visibility = PlatformDependent.from(platformData, descriptor.visibility.toDokkaVisibility()), generics = descriptor.typeConstructor.parameters.map { it.toTypeParameter() }, documentation = info.docs, modifier = descriptor.modifier(), @@ -215,12 +216,12 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private }, sources = actual, getter = descriptor.accessors.filterIsInstance().singleOrNull()?.let { - visitPropertyAccessorDescriptor(it, descriptor, dri) + visitPropertyAccessorDescriptor(it, descriptor, dri) }!!, setter = descriptor.accessors.filterIsInstance().singleOrNull()?.let { - visitPropertyAccessorDescriptor(it, descriptor, dri) + visitPropertyAccessorDescriptor(it, descriptor, dri) }, - visibility = PlatformDependent(mapOf(platformData to descriptor.visibility)), + visibility = PlatformDependent(mapOf(platformData to descriptor.visibility.toDokkaVisibility())), documentation = descriptor.resolveDescriptorData(platformData), modifier = descriptor.modifier(), type = KotlinTypeWrapper(descriptor.returnType!!), @@ -244,7 +245,7 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private parameter(index, desc, DRIWithPlatformInfo(dri, actual)) }, sources = actual, - visibility = PlatformDependent.from(platformData, descriptor.visibility), + visibility = PlatformDependent.from(platformData, descriptor.visibility.toDokkaVisibility()), generics = descriptor.typeParameters.map { it.toTypeParameter() }, documentation = descriptor.resolveDescriptorData(platformData), modifier = descriptor.modifier(), @@ -268,7 +269,7 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private parameter(index, desc, DRIWithPlatformInfo(dri, actual)) }, sources = actual, - visibility = PlatformDependent(mapOf(platformData to descriptor.visibility)), + visibility = PlatformDependent(mapOf(platformData to descriptor.visibility.toDokkaVisibility())), documentation = descriptor.resolveDescriptorData(platformData), type = KotlinTypeWrapper(descriptor.returnType), modifier = descriptor.modifier(), @@ -325,7 +326,7 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private name, isConstructor = false, parameters = parameters, - visibility = PlatformDependent(mapOf(platformData to descriptor.visibility)), + visibility = PlatformDependent(mapOf(platformData to descriptor.visibility.toDokkaVisibility())), documentation = descriptor.resolveDescriptorData(platformData), type = KotlinTypeWrapper(descriptor.returnType!!), generics = descriptor.typeParameters.map { it.toTypeParameter() }, @@ -431,7 +432,7 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private else -> WithAbstraction.Modifier.Empty } - private fun MemberDescriptor.createSources(): PlatformDependent = if(isExpect()) { + private fun MemberDescriptor.createSources(): PlatformDependent = if (isExpect()) { PlatformDependent(emptyMap(), DescriptorDocumentableSource(this)) } else { PlatformDependent(mapOf(platformData to DescriptorDocumentableSource(this))) @@ -489,4 +490,12 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private container + AdditionalModifiers(this) data class ClassInfo(val supertypes: List, val docs: PlatformDependent) + + private fun Visibility.toDokkaVisibility(): org.jetbrains.dokka.model.Visibility = when (this) { + Visibilities.PUBLIC -> KotlinVisibility.Public + Visibilities.PROTECTED -> KotlinVisibility.Protected + Visibilities.INTERNAL -> KotlinVisibility.Internal + Visibilities.PRIVATE -> KotlinVisibility.Private + else -> KotlinVisibility.Public + } } diff --git a/plugins/base/src/test/kotlin/model/ClassesTest.kt b/plugins/base/src/test/kotlin/model/ClassesTest.kt index 18288442..a22bd775 100644 --- a/plugins/base/src/test/kotlin/model/ClassesTest.kt +++ b/plugins/base/src/test/kotlin/model/ClassesTest.kt @@ -2,9 +2,7 @@ package model import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.Function -import org.jetbrains.dokka.model.Object import org.jetbrains.dokka.model.WithAbstraction.Modifier -import org.jetbrains.kotlin.descriptors.Visibilities import org.junit.Test import utils.AbstractModelTest import utils.assertNotNull @@ -52,7 +50,7 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class children counts 4 with(constructors.firstOrNull().assertNotNull("Constructor")) { - visibility.values allEquals Visibilities.PUBLIC + visibility.values allEquals KotlinVisibility.Public parameters counts 1 with(parameters.firstOrNull().assertNotNull("Constructor parameter")) { name equals "name" @@ -80,7 +78,7 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class with((this / "fn").cast()) { type.constructorFqName equals "kotlin.Unit" parameters counts 0 - visibility.values allEquals Visibilities.PUBLIC + visibility.values allEquals KotlinVisibility.Public } } } @@ -150,7 +148,7 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class ) { with((this / "classes" / "Klass").cast()) { name equals "Klass" - visibility.values allEquals Visibilities.PUBLIC + visibility.values allEquals KotlinVisibility.Public with(extra[AdditionalModifiers.AdditionalKey].assertNotNull("Extras")) { content.find{it == ExtraModifiers.DATA}.assertNotNull("data modifier") } @@ -174,7 +172,7 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class ) { with((this / "classes" / "Klass").cast()) { name equals "Klass" - modifier equals WithAbstraction.Modifier.Sealed + modifier equals Modifier.Sealed } } } @@ -391,7 +389,7 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class with((this / "Companion").cast()) { name equals "Companion" - visibility.values allEquals Visibilities.PRIVATE + visibility.values allEquals KotlinVisibility.Private with((this / "fn").cast()) { name equals "fn" diff --git a/plugins/base/src/test/kotlin/model/PropertyTest.kt b/plugins/base/src/test/kotlin/model/PropertyTest.kt index 633796e7..e4ef0aec 100644 --- a/plugins/base/src/test/kotlin/model/PropertyTest.kt +++ b/plugins/base/src/test/kotlin/model/PropertyTest.kt @@ -1,8 +1,8 @@ package model +import org.jetbrains.dokka.model.KotlinVisibility import org.jetbrains.dokka.model.Package import org.jetbrains.dokka.model.Property -import org.jetbrains.kotlin.descriptors.Visibilities import org.junit.Test import utils.AbstractModelTest import utils.assertNotNull @@ -80,7 +80,7 @@ class PropertyTest : AbstractModelTest("/src/main/kotlin/property/Test.kt", "pro with(getter.assertNotNull("Getter")) { type.constructorFqName equals "kotlin.String" } - visibility.values allEquals Visibilities.PUBLIC + visibility.values allEquals KotlinVisibility.Public } } } @@ -103,7 +103,7 @@ class PropertyTest : AbstractModelTest("/src/main/kotlin/property/Test.kt", "pro with(getter.assertNotNull("Getter")) { type.constructorFqName equals "kotlin.Int" } - visibility.values allEquals Visibilities.PUBLIC + visibility.values allEquals KotlinVisibility.Public } } } -- cgit From e34f813a6c3130b574bacb0a31e727ceb39d0e39 Mon Sep 17 00:00:00 2001 From: Szymon Świstun Date: Thu, 5 Mar 2020 10:49:54 +0100 Subject: fixed test compilation --- plugins/base/src/test/kotlin/model/ClassesTest.kt | 20 ++++++++++---------- .../test/kotlin/renderers/RenderingOnlyTestBase.kt | 3 ++- 2 files changed, 12 insertions(+), 11 deletions(-) (limited to 'plugins/base/src/test/kotlin/model/ClassesTest.kt') diff --git a/plugins/base/src/test/kotlin/model/ClassesTest.kt b/plugins/base/src/test/kotlin/model/ClassesTest.kt index a22bd775..a59174bf 100644 --- a/plugins/base/src/test/kotlin/model/ClassesTest.kt +++ b/plugins/base/src/test/kotlin/model/ClassesTest.kt @@ -1,8 +1,8 @@ package model import org.jetbrains.dokka.model.* +import org.jetbrains.dokka.model.KotlinModifier.* import org.jetbrains.dokka.model.Function -import org.jetbrains.dokka.model.WithAbstraction.Modifier import org.junit.Test import utils.AbstractModelTest import utils.assertNotNull @@ -172,7 +172,7 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class ) { with((this / "classes" / "Klass").cast()) { name equals "Klass" - modifier equals Modifier.Sealed + modifier equals KotlinModifier.Sealed } } } @@ -219,15 +219,15 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class val D = (this / "classes" / "D").cast() with(C) { - modifier equals Modifier.Open + modifier equals Open with((this / "f").cast()) { - modifier equals Modifier.Open + modifier equals Open } } with(D) { - modifier equals Modifier.Final + modifier equals Final with((this / "f").cast()) { - modifier equals Modifier.Open + modifier equals Open } D.supertypes.flatMap { it.component2() }.firstOrNull() equals C.dri } @@ -254,16 +254,16 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class val E = (this / "classes" / "E").cast() with(C) { - modifier equals Modifier.Abstract - ((this / "foo").cast()).modifier equals Modifier.Abstract + modifier equals Abstract + ((this / "foo").cast()).modifier equals Abstract } with(D) { - modifier equals Modifier.Abstract + modifier equals Abstract } with(E) { - modifier equals Modifier.Final + modifier equals Final } D.supers.firstOrNull() equals C.dri diff --git a/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt b/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt index 9f148369..a6977258 100644 --- a/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt +++ b/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt @@ -10,6 +10,7 @@ import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.Documentable import org.jetbrains.dokka.model.doc.DocTag +import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.testApi.context.MockContext import org.jetbrains.dokka.utilities.DokkaConsoleLogger @@ -66,7 +67,7 @@ internal object EmptyCommentConverter : CommentsToContentConverter { dci: DCI, platforms: Set, styles: Set