Skip to content

Commit

Permalink
refactor tests to use BaseAbstractTest
Browse files Browse the repository at this point in the history
  • Loading branch information
whyoleg committed Oct 24, 2023
1 parent 888102b commit 626a1cb
Showing 1 changed file with 72 additions and 31 deletions.
103 changes: 72 additions & 31 deletions plugins/base/src/test/kotlin/renderers/html/HeaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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`() {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()

}

0 comments on commit 626a1cb

Please sign in to comment.