Skip to content

Commit

Permalink
[K2] Fix missing actual keyword for constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
vmishenev committed Feb 18, 2024
1 parent 1cfd012 commit dd76490
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,8 @@ internal class DokkaSymbolVisitor(
val name = constructorSymbol.containingClassIdIfNonLocal?.shortClassName?.asString()
?: throw IllegalStateException("Unknown containing class of constructor")
val dri = createDRIWithOverridden(constructorSymbol).origin
val isExpect = false // TODO
val isActual = false // TODO
val isExpect = constructorSymbol.isExpect
val isActual = constructorSymbol.isActual

val generics = constructorSymbol.typeParameters.mapIndexed { index, symbol ->
visitVariantTypeParameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,123 @@ class ConstructorsTest : AbstractModelTest("/src/main/kotlin/constructors/Test.k
}
}

@Test
fun `should have KMP documentation for @constructor tag`() {
val expectedDescriptionTag = Description(
CustomDocTag(
listOf(
P(
listOf(
Text("some doc"),
)
)
),
params = emptyMap(),
name = MARKDOWN_ELEMENT_FILE_NAME
)
)
val configuration = dokkaConfiguration {
sourceSets {
val common = sourceSet {
name = "common"
displayName = "common"
analysisPlatform = "common"
sourceRoots = listOf("src/main/kotlin/common/Test.kt")
}

sourceSet {
name = "jvm"
displayName = "jvm"
analysisPlatform = "jvm"
sourceRoots = listOf("src/main/kotlin/jvm/Test.kt")
dependentSourceSets = setOf(common.value.sourceSetID)
}
}
}

testInline(
"""
|/src/main/kotlin/common/Test.kt
|package multiplatform
|
|expect class A()
|
|/src/main/kotlin/jvm/Test.kt
|package multiplatform
|/**
||* @constructor some doc
|*/
|actual class A()
""".trimMargin(),
configuration
) {
documentablesMergingStage = {
val classlike = it.packages.flatMap { it.classlikes }.first() as DClass
classlike.name equals "A"
val actualConstructor = classlike.constructors.first { it.sourceSets.single().displayName == "jvm" }
actualConstructor.documentation.values.single() equals DocumentationNode(listOf(expectedDescriptionTag))

val expectConstructor = classlike.constructors.first { it.sourceSets.single().displayName == "common" }
expectConstructor.documentation.isEmpty() equals true
}

}
}

@Test
fun `should have actual constructor`() {
val configuration = dokkaConfiguration {
sourceSets {
val common = sourceSet {
name = "common"
displayName = "common"
analysisPlatform = "common"
sourceRoots = listOf("src/main/kotlin/common/Test.kt")
}

sourceSet {
name = "jvm"
displayName = "jvm"
analysisPlatform = "jvm"
sourceRoots = listOf("src/main/kotlin/jvm/Test.kt")
dependentSourceSets = setOf(common.value.sourceSetID)
}
}
}

testInline(
"""
|/src/main/kotlin/common/Test.kt
|package multiplatform
|
|expect class A()
|
|/src/main/kotlin/jvm/Test.kt
|package multiplatform
|actual class A{
| actual constructor(){}
|}
""".trimMargin(),
configuration
) {
preMergeDocumentablesTransformationStage = {
val actualClasslike = it.first { it.sourceSets.single().displayName == "common" }.packages.flatMap { it.classlikes }.first() as DClass
actualClasslike.name equals "A"
val actualConstructor = actualClasslike.constructors.first { it.sourceSets.single().displayName == "common" }
actualConstructor.isExpectActual equals true

val expectClasslike = it.first { it.sourceSets.single().displayName == "jvm" }.packages.flatMap { it.classlikes }.first() as DClass
expectClasslike.name equals "A"
val expectConstructor = expectClasslike.constructors.first { it.sourceSets.single().displayName == "jvm" }
expectConstructor.isExpectActual equals true
}
documentablesMergingStage = {
val classlike = it.packages.flatMap { it.classlikes }.first() as DClass
classlike.name equals "A"
val constructor = classlike.constructors.single()
constructor.isExpectActual equals true
constructor.sourceSets counts 2
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,42 @@ class SignatureTest : BaseAbstractTest() {
}
}

@Test
fun `should render actual keyword for constructor`() {
val writerPlugin = TestOutputWriterPlugin()

testInline(
"""
|/src/main/kotlin/common/Test.kt
|package example
|
|expect class A()
|
|/src/main/kotlin/jvm/Test.kt
|package example
|
|actual class A{
| actual constructor(){}
|}
""".trimMargin(),
mppConfiguration,
pluginOverrides = listOf(writerPlugin)
) {
renderingStage = { _, _ ->
val signatures = writerPlugin.writer.renderedContent("test/example/-a/-a.html").signature().toList()

signatures[0].match(
"expect constructor()",
ignoreSpanWithTokenStyle = true
)
signatures[1].match(
"actual constructor()",
ignoreSpanWithTokenStyle = true
)
}
}
}

private fun testRender(
query: String,
configuration: DokkaConfigurationImpl = this.configuration,
Expand Down

0 comments on commit dd76490

Please sign in to comment.