aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt
blob: be87813b71a15a3e1fd9450a3fab1093b6c39450 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package resourceLinks

import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.pages.RootPageNode
import org.jetbrains.dokka.plugability.DokkaPlugin
import org.jetbrains.dokka.testApi.testRunner.AbstractCoreTest
import org.jetbrains.dokka.transformers.pages.PageTransformer
import org.jsoup.Jsoup
import org.junit.jupiter.api.Test
import utils.TestOutputWriterPlugin

class ResourceLinksTest : AbstractCoreTest() {
    class TestResourcesAppenderPlugin(val resources: List<String>) : DokkaPlugin() {
        class TestResourcesAppender(val resources: List<String>) : PageTransformer {
            override fun invoke(input: RootPageNode) = input.transformContentPagesTree {
                    it.modified(
                        embeddedResources = it.embeddedResources + resources
                    )
                }
        }

        val appender by extending {
            plugin<DokkaBase>().htmlPreprocessors with TestResourcesAppender(resources)
        }
    }
    @Test
    fun resourceLinksTest() {
        val configuration = dokkaConfiguration {
            passes {
                pass {
                    sourceRoots = listOf("src/main/kotlin/test/Test.kt")
                }
            }
        }
        val absoluteResources = listOf(
            "https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css",
            "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
        )
        val relativeResources = listOf(
            "test/relativePath.js",
            "test/relativePath.css"
        )

        val source =
            """
            |/src/main/kotlin/test/Test.kt
            |package example
            """.trimIndent()
        val writerPlugin = TestOutputWriterPlugin()
        testInline(
            source,
            configuration,
            pluginOverrides = listOf(TestResourcesAppenderPlugin(absoluteResources + relativeResources), writerPlugin)
        ) {
            renderingStage = {
                root, context -> Jsoup
                .parse(writerPlugin.writer.contents["root/example.html"])
                .head()
                .select("link, script")
                .let {
                    absoluteResources.forEach {
                        r -> assert(it.`is`("[href=$r], [src=$r]"))
                    }
                    relativeResources.forEach {
                        r -> assert(it.`is`("[href=../$r] , [src=../$r]"))
                    }
                }
            }
        }
    }
}