aboutsummaryrefslogtreecommitdiff
path: root/plugins/all-modules-page/src/test/kotlin/templates/ResolveLinkCommandResolutionTest.kt
blob: 5e785e036dc26b9a1e429ccfa452e02e6c4726bf (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
package org.jetbrains.dokka.allModulesPage.templates

import kotlinx.html.a
import kotlinx.html.span
import kotlinx.html.stream.createHTML
import org.jetbrains.dokka.DokkaModuleDescriptionImpl
import org.jetbrains.dokka.allModulesPage.MultiModuleAbstractTest
import org.jetbrains.dokka.base.renderers.html.templateCommand
import org.jetbrains.dokka.base.resolvers.shared.RecognizedLinkFormat
import org.jetbrains.dokka.base.templating.ResolveLinkCommand
import org.jetbrains.dokka.links.DRI
import org.junit.jupiter.api.io.TempDir
import utils.assertHtmlEqualsIgnoringWhitespace
import java.io.File
import kotlin.test.Test
import kotlin.test.assertTrue

class ResolveLinkCommandResolutionTest : MultiModuleAbstractTest() {

    @Test
    fun `should resolve link to another module`(@TempDir outputDirectory: File) {
        val testedDri = DRI(
            packageName = "package2",
            classNames = "Sample",
        )
        val link = createHTML().templateCommand(ResolveLinkCommand(testedDri)) {
            span {
                +"Sample"
            }
        }

        val expected = createHTML().a {
            href = "../module2/package2/-sample/index.html"
            span {
                +"Sample"
            }
        }

        val contentFile = setup(outputDirectory, link)
        val configuration = createConfiguration(outputDirectory)

        testFromData(configuration, useOutputLocationFromConfig = true) {
            finishProcessingSubmodules = {
                assertHtmlEqualsIgnoringWhitespace(expected, contentFile.readText())
            }
        }
    }

    @Test
    fun `should produce content when link is not resolvable`(@TempDir outputDirectory: File) {
        val testedDri = DRI(
            packageName = "not-resolvable-package",
            classNames = "Sample",
        )
        val link = createHTML().templateCommand(ResolveLinkCommand(testedDri)) {
            span {
                +"Sample"
            }
        }

        val expected = createHTML().span {
            attributes["data-unresolved-link"] = testedDri.toString()
            span {
                +"Sample"
            }
        }

        val contentFile = setup(outputDirectory, link)
        val configuration = createConfiguration(outputDirectory)

        testFromData(configuration, useOutputLocationFromConfig = true) {
            finishProcessingSubmodules = {
                assertHtmlEqualsIgnoringWhitespace(expected, contentFile.readText())
            }
        }
    }

    private fun setup(outputDirectory: File, content: String): File {
        val innerModule1 = outputDirectory.resolve("module1").also { assertTrue(it.mkdirs()) }
        val innerModule2 = outputDirectory.resolve("module2").also { assertTrue(it.mkdirs()) }
        val packageList = innerModule2.resolve("package-list")
        packageList.writeText(mockedPackageListForPackages(RecognizedLinkFormat.DokkaHtml, "package2"))
        val contentFile = innerModule1.resolve("index.html")
        contentFile.writeText(content)
        return contentFile
    }

    private fun createConfiguration(outputDirectory: File) = dokkaConfiguration {
        modules = listOf(
            DokkaModuleDescriptionImpl(
                name = "module1",
                relativePathToOutputDirectory = outputDirectory.resolve("module1"),
                includes = emptySet(),
                sourceOutputDirectory = outputDirectory.resolve("module1"),
            ),
            DokkaModuleDescriptionImpl(
                name = "module2",
                relativePathToOutputDirectory = outputDirectory.resolve("module2"),
                includes = emptySet(),
                sourceOutputDirectory = outputDirectory.resolve("module2"),
            )
        )
        this.outputDir = outputDirectory
    }
}