From 3935b4e05c13528e5b8f4b99aea1962ab904a8ee Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Wed, 26 Aug 2020 16:09:04 +0200 Subject: Implement `root package is matched by empty string and the root keyword` test --- .../ModuleAndPackageDocumentationReader.kt | 10 +++- ...ntextModuleAndPackageDocumentationReaderTest.kt | 15 +++--- ...textModuleAndPackageDocumentationReaderTest3.kt | 59 ++++++++++++++++++++++ ...ackageDocumentationTransformerFunctionalTest.kt | 37 ++++++++++++++ 4 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 plugins/base/src/test/kotlin/transformers/ContextModuleAndPackageDocumentationReaderTest3.kt diff --git a/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationReader.kt b/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationReader.kt index 4900e9a8..e712d6e5 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationReader.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationReader.kt @@ -56,6 +56,13 @@ private class ContextModuleAndPackageDocumentationReader( } } + private val ModuleAndPackageDocumentationFragment.canonicalPackageName: String + get() { + check(classifier == Classifier.Package) + if (name == "[root]") return "" + return name + } + override fun get(module: DModule): SourceSetDependent { return findDocumentationNodes(module.sourceSets) { fragment -> fragment.classifier == Classifier.Module && ( @@ -68,8 +75,7 @@ private class ContextModuleAndPackageDocumentationReader( override fun get(pkg: DPackage): SourceSetDependent { return findDocumentationNodes(pkg.sourceSets) { fragment -> - // TODO NOW: handle JS Root thing - fragment.classifier == Classifier.Package && fragment.name == pkg.dri.packageName + fragment.classifier == Classifier.Package && fragment.canonicalPackageName == pkg.dri.packageName } } } diff --git a/plugins/base/src/test/kotlin/transformers/AbstractContextModuleAndPackageDocumentationReaderTest.kt b/plugins/base/src/test/kotlin/transformers/AbstractContextModuleAndPackageDocumentationReaderTest.kt index 7f435013..0f26b2f5 100644 --- a/plugins/base/src/test/kotlin/transformers/AbstractContextModuleAndPackageDocumentationReaderTest.kt +++ b/plugins/base/src/test/kotlin/transformers/AbstractContextModuleAndPackageDocumentationReaderTest.kt @@ -12,11 +12,12 @@ abstract class AbstractContextModuleAndPackageDocumentationReaderTest { protected lateinit var temporaryDirectory: Path - protected val SourceSetDependent.texts: List - get() = values.flatMap { it.withDescendants() } - .flatMap { it.children } - .flatMap { it.children } - .mapNotNull { it as? Text } - .map { it.body } - + companion object { + val SourceSetDependent.texts: List + get() = values.flatMap { it.withDescendants() } + .flatMap { it.children } + .flatMap { it.children } + .mapNotNull { it as? Text } + .map { it.body } + } } diff --git a/plugins/base/src/test/kotlin/transformers/ContextModuleAndPackageDocumentationReaderTest3.kt b/plugins/base/src/test/kotlin/transformers/ContextModuleAndPackageDocumentationReaderTest3.kt new file mode 100644 index 00000000..e1b9d199 --- /dev/null +++ b/plugins/base/src/test/kotlin/transformers/ContextModuleAndPackageDocumentationReaderTest3.kt @@ -0,0 +1,59 @@ +package transformers + +import org.jetbrains.dokka.base.transformers.documentables.ModuleAndPackageDocumentationReader +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.model.DPackage +import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.utilities.DokkaConsoleLogger +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import testApi.testRunner.dokkaConfiguration +import testApi.testRunner.sourceSet +import kotlin.test.assertEquals + +class ContextModuleAndPackageDocumentationReaderTest3 : AbstractContextModuleAndPackageDocumentationReaderTest() { + + private val include by lazy { temporaryDirectory.resolve("include.md").toFile() } + + @BeforeEach + fun materializeInclude() { + include.writeText( + """ + # Package + This is the root package + + # Package [root] + This is also the root package + """.trimIndent() + ) + } + + private val sourceSet by lazy { + sourceSet { + includes = listOf(include.canonicalPath) + } + } + + private val context by lazy { + DokkaContext.create( + configuration = dokkaConfiguration { + sourceSets { + add(sourceSet) + } + }, + logger = DokkaConsoleLogger, + pluginOverrides = emptyList() + ) + } + + private val reader by lazy { ModuleAndPackageDocumentationReader(context) } + + + @Test + fun `root package is matched by empty string and the root keyword`() { + val documentation = reader[DPackage(DRI(""), sourceSets = setOf(sourceSet))] + assertEquals( + listOf("This is the root package", "This is also the root package"), documentation.texts + ) + } +} diff --git a/plugins/base/src/test/kotlin/transformers/ModuleAndPackageDocumentationTransformerFunctionalTest.kt b/plugins/base/src/test/kotlin/transformers/ModuleAndPackageDocumentationTransformerFunctionalTest.kt index e622e79d..2d356a81 100644 --- a/plugins/base/src/test/kotlin/transformers/ModuleAndPackageDocumentationTransformerFunctionalTest.kt +++ b/plugins/base/src/test/kotlin/transformers/ModuleAndPackageDocumentationTransformerFunctionalTest.kt @@ -4,6 +4,7 @@ import org.jetbrains.dokka.DokkaSourceSetID import org.jetbrains.dokka.testApi.testRunner.AbstractCoreTest import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir +import transformers.AbstractContextModuleAndPackageDocumentationReaderTest.Companion.texts import java.nio.file.Path import kotlin.test.assertEquals @@ -20,6 +21,9 @@ class ModuleAndPackageDocumentationTransformerFunctionalTest : AbstractCoreTest( # Package This is the root package + # Package [root] + This is also the root package + # Package common This is the common package @@ -47,6 +51,7 @@ class ModuleAndPackageDocumentationTransformerFunctionalTest : AbstractCoreTest( analysisPlatform = "js" sourceRoots = listOf("src/jsMain/kotlin") dependentSourceSets = setOf(DokkaSourceSetID("moduleA", "commonMain")) + includes = listOf(include.canonicalPath) } sourceSet { moduleName = "moduleA" @@ -55,6 +60,7 @@ class ModuleAndPackageDocumentationTransformerFunctionalTest : AbstractCoreTest( analysisPlatform = "jvm" sourceRoots = listOf("src/jvmMain/kotlin") dependentSourceSets = setOf(DokkaSourceSetID("moduleA", "commonMain")) + includes = listOf(include.canonicalPath) } } } @@ -90,6 +96,37 @@ class ModuleAndPackageDocumentationTransformerFunctionalTest : AbstractCoreTest( listOf("", "common", "js", "jvm").sorted(), packageNames.sorted(), "Expected all packages to be present" ) + + /* Assert module documentation */ + assertEquals(3, module.documentation.keys.size, "Expected all three source sets") + assertEquals("This is moduleA", module.documentation.texts.distinct().joinToString()) + + /* Assert root package */ + val rootPackage = module.packages.single { it.dri.packageName == "" } + assertEquals(3, rootPackage.documentation.keys.size, "Expected all three source sets") + assertEquals( + listOf("This is the root package", "This is also the root package"), + rootPackage.documentation.texts.distinct() + ) + + /* Assert common package */ + val commonPackage = module.packages.single { it.dri.packageName == "common" } + assertEquals(3, commonPackage.documentation.keys.size, "Expected all three source sets") + assertEquals("This is the common package", commonPackage.documentation.texts.distinct().joinToString()) + + /* Assert js package */ + val jsPackage = module.packages.single { it.dri.packageName == "js" } + assertEquals( + "This is the js package", + jsPackage.documentation.texts.joinToString() + ) + + /* Assert the jvm package */ + val jvmPackage = module.packages.single { it.dri.packageName == "jvm" } + assertEquals( + "This is the jvm package", + jvmPackage.documentation.texts.joinToString() + ) } } } -- cgit