From 626a1cbb994b8632e6ddd5f634a12872f70d8dbe Mon Sep 17 00:00:00 2001 From: Oleg Yukhnevich Date: Tue, 24 Oct 2023 12:48:41 +0300 Subject: [PATCH] refactor tests to use BaseAbstractTest --- .../test/kotlin/renderers/html/HeaderTest.kt | 103 ++++++++++++------ 1 file changed, 72 insertions(+), 31 deletions(-) diff --git a/plugins/base/src/test/kotlin/renderers/html/HeaderTest.kt b/plugins/base/src/test/kotlin/renderers/html/HeaderTest.kt index 449acf5ecc7..6f82cee473d 100644 --- a/plugins/base/src/test/kotlin/renderers/html/HeaderTest.kt +++ b/plugins/base/src/test/kotlin/renderers/html/HeaderTest.kt @@ -4,20 +4,33 @@ package renderers.html +import org.jetbrains.dokka.DokkaConfigurationImpl import org.jetbrains.dokka.SourceLinkDefinitionImpl -import org.jetbrains.dokka.base.renderers.html.HtmlRenderer +import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest +import org.jetbrains.dokka.pages.RootPageNode +import org.jetbrains.dokka.plugability.DokkaContext import org.jsoup.Jsoup -import org.jsoup.nodes.Element -import renderers.testPage +import utils.TestOutputWriter +import utils.TestOutputWriterPlugin import java.net.URL import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull -class HeaderTest : HtmlRenderingOnlyTestBase() { - override val renderedContent: Element - get() = files.contents.getValue("test-page.html").let { Jsoup.parse(it) }.select(".navigation").single() +class HeaderTest : BaseAbstractTest() { + private val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + name = "jvm" + sourceRoots = listOf("src/jvm") + } + sourceSet { + name = "js" + sourceRoots = listOf("src/js") + } + } + } @Test fun `should include link to github if sourceLinks are pointed to github`() { @@ -26,20 +39,20 @@ class HeaderTest : HtmlRenderingOnlyTestBase() { remoteUrl = URL("https://github.com/Kotlin/dokka/tree/main"), remoteLineSuffix = null ) - val context = context( + testRendering( configuration.copy( sourceSets = configuration.sourceSets.map { it.copy(sourceLinks = setOf(sourceLink)) } ) - ) - - HtmlRenderer(context).render(testPage { }) + ) { _, _, writer -> + val renderedContent = navigationElement(writer) - val sourceLinkElement = - assertNotNull(renderedContent.getElementById("source-link"), "Source link element not found") - val aElement = assertNotNull(sourceLinkElement.selectFirst("a")) - assertEquals("https://github.com/Kotlin/dokka/", aElement.attr("href")) + val sourceLinkElement = + assertNotNull(renderedContent.getElementById("source-link"), "Source link element not found") + val aElement = assertNotNull(sourceLinkElement.selectFirst("a")) + assertEquals("https://github.com/Kotlin/dokka/", aElement.attr("href")) + } } @Test @@ -49,18 +62,17 @@ class HeaderTest : HtmlRenderingOnlyTestBase() { remoteUrl = URL("https://github.com/Kotlin/dokka/tree/main"), remoteLineSuffix = null ) - val context = context( + testRendering( configuration.copy( sourceSets = listOf( - js.copy(sourceLinks = setOf(sourceLink)), - jvm.copy(sourceLinks = setOf(sourceLink.copy(remoteUrl = URL("https://github.com/Kotlin/dokkatoo/tree/main")))) + configuration.sourceSets[0].copy(sourceLinks = setOf(sourceLink)), + configuration.sourceSets[1].copy(sourceLinks = setOf(sourceLink.copy(remoteUrl = URL("https://github.com/Kotlin/dokkatoo/tree/main")))) ) ) - ) - - HtmlRenderer(context).render(testPage { }) - - assertNull(renderedContent.getElementById("source-link"), "Source link element found") + ) { _, _, writer -> + val renderedContent = navigationElement(writer) + assertNull(renderedContent.getElementById("source-link"), "Source link element found") + } } @Test @@ -70,25 +82,54 @@ class HeaderTest : HtmlRenderingOnlyTestBase() { remoteUrl = URL("https://gitlab.com/Kotlin/dokka/tree/main"), remoteLineSuffix = null ) - val context = context( + testRendering( configuration.copy( sourceSets = configuration.sourceSets.map { it.copy(sourceLinks = setOf(sourceLink)) } ) - ) - - HtmlRenderer(context).render(testPage { }) - - assertNull(renderedContent.getElementById("source-link"), "Source link element found") + ) { _, _, writer -> + val renderedContent = navigationElement(writer) + assertNull(renderedContent.getElementById("source-link"), "Source link element found") + } } @Test fun `should not include link to github if there are no sourceLinks`() { - val context = context(configuration) + testRendering(configuration) { _, _, writer -> + val renderedContent = navigationElement(writer) + assertNull(renderedContent.getElementById("source-link"), "Source link element found") + } + } - HtmlRenderer(context).render(testPage { }) - assertNull(renderedContent.getElementById("source-link"), "Source link element found") + private fun testRendering( + configuration: DokkaConfigurationImpl = this.configuration, + block: (RootPageNode, DokkaContext, writer: TestOutputWriter) -> Unit + ) { + val writerPlugin = TestOutputWriterPlugin() + testInline( + """ + |/src/jvm/Test.kt + |fun test() {} + |/src/js/Test.kt + |fun test() {} + """, + configuration, + pluginOverrides = listOf(writerPlugin) + ) { + renderingStage = { node, context -> + block(node, context, writerPlugin.writer) + } + } } + + private fun navigationElement(writer: TestOutputWriter) = + writer + .contents + .getValue("index.html") + .let(Jsoup::parse) + .select(".navigation") + .single() + }