aboutsummaryrefslogtreecommitdiff
path: root/plugins/templating/src/test/kotlin/templates/AddToNavigationCommandResolutionTest.kt
blob: fb229643186cbb98040484123db01aff605054f1 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.jetbrains.dokka.templates

import kotlinx.html.a
import kotlinx.html.div
import kotlinx.html.id
import kotlinx.html.span
import kotlinx.html.stream.createHTML
import org.jetbrains.dokka.DokkaModuleDescriptionImpl
import org.jetbrains.dokka.base.renderers.html.templateCommand
import org.jetbrains.dokka.base.templating.AddToNavigationCommand
import org.jetbrains.dokka.plugability.DokkaContext
import org.junit.Rule
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import org.junit.rules.TemporaryFolder
import utils.assertHtmlEqualsIgnoringWhitespace

class AddToNavigationCommandResolutionTest : TemplatingAbstractTest() {
    @get:Rule
    val folder: TemporaryFolder = TemporaryFolder()

    @Test
    fun `should substitute AddToNavigationCommand in root directory`() =
        addToNavigationTest {
            val output = folder.root.resolve("navigation.html").readText()
            val expected = expectedOutput(
                ModuleWithPrefix("module1"),
                ModuleWithPrefix("module2")
            )
            assertHtmlEqualsIgnoringWhitespace(expected, output)
        }

    @ParameterizedTest
    @ValueSource(strings = ["module1", "module2"])
    fun `should substitute AddToNavigationCommand in modules directory`(moduleName: String) =
        addToNavigationTest {
            val output = folder.root.resolve(moduleName).resolve("navigation.html").readText()
            val expected = expectedOutput(
                ModuleWithPrefix("module1", ".."),
                ModuleWithPrefix("module2", "..")
            )
            assertHtmlEqualsIgnoringWhitespace(expected, output)
        }

    private fun expectedOutput(vararg modulesWithPrefix: ModuleWithPrefix) = createHTML(prettyPrint = true)
        .div("sideMenu") {
            modulesWithPrefix.forEach { (moduleName, prefix) ->
                val relativePrefix = prefix?.let { "$it/" } ?: ""
                div("sideMenuPart") {
                    id = "$moduleName-nav-submenu"
                    div("overview") {
                        a {
                            href = "$relativePrefix$moduleName/module-page.html"
                            span {
                                +"module-$moduleName"
                            }
                        }
                    }
                    div("sideMenuPart") {
                        id = "$moduleName-nav-submenu-0"
                        div("overview") {
                            a {
                                href = "$relativePrefix$moduleName/$moduleName/package-page.html"
                                span {
                                    +"package-$moduleName"
                                }
                            }
                        }
                    }
                }
            }
        }

    private fun inputForModule(moduleName: String) = createHTML()
        .templateCommand(AddToNavigationCommand(moduleName)) {
            div("sideMenuPart") {
                id = "$moduleName-nav-submenu"
                div("overview") {
                    a {
                        href = "module-page.html"
                        span {
                            +"module-$moduleName"
                        }
                    }
                }
                div("sideMenuPart") {
                    id = "$moduleName-nav-submenu-0"
                    div("overview") {
                        a {
                            href = "$moduleName/package-page.html"
                            span {
                                +"package-$moduleName"
                            }
                        }
                    }
                }
            }
        }

    private fun addToNavigationTest(test: (DokkaContext) -> Unit) {
        folder.create()
        val module1 = folder.newFolder("module1")
        val module2 = folder.newFolder("module2")

        val configuration = dokkaConfiguration {
            modules = listOf(
                DokkaModuleDescriptionImpl(
                    name = "module1",
                    relativePathToOutputDirectory = module1,
                    includes = emptySet(),
                    sourceOutputDirectory = module1,
                ),
                DokkaModuleDescriptionImpl(
                    name = "module2",
                    relativePathToOutputDirectory = module2,
                    includes = emptySet(),
                    sourceOutputDirectory = module2,
                ),
            )
            this.outputDir = folder.root
        }

        val module1Navigation = module1.resolve("navigation.html")
        module1Navigation.writeText(inputForModule("module1"))
        val module2Navigation = module2.resolve("navigation.html")
        module2Navigation.writeText(inputForModule("module2"))

        testFromData(configuration, preserveOutputLocation = true) {
            finishProcessingSubmodules = { ctx ->
                test(ctx)
            }
        }
    }

    private data class ModuleWithPrefix(val moduleName: String, val prefix: String? = null)
}