aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/locationProvider/DokkaLocationProviderTest.kt
blob: 59406e1e60b08ce86eed1dd1f2235972f57cca68 (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
package locationProvider

import org.jetbrains.dokka.base.resolvers.local.DokkaLocationProvider
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.pages.*
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.kotlin.backend.common.push
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class DokkaLocationProviderTest : BaseAbstractTest() {

    private val configuration = dokkaConfiguration {
        sourceSets {
            sourceSet {
                sourceRoots = listOf("src/")
                classpath += jvmStdlibPath!!
            }
        }
    }

    private fun getTestLocationProvider(root: RootPageNode, context: DokkaContext? = null): DokkaLocationProvider {
        val dokkaContext = context ?: DokkaContext.create(configuration, logger, emptyList())
        return DokkaLocationProvider(root, dokkaContext, ".html")
    }

    @DslMarker
    annotation class TestNavigationDSL

    @TestNavigationDSL
    class NavigationDSL {
        companion object {
            private val stubDCI = DCI(
                setOf(
                    DRI("kotlin", "Any")
                ),
                ContentKind.Comment
            )
            val stubContentNode = ContentText("", stubDCI, emptySet())
        }

        operator fun invoke(name: String, fn: ModulesDsl.() -> Unit): RendererSpecificRootPage {
            val modules = ModulesDsl().also { it.fn() }
            return RendererSpecificRootPage(name = name, children = modules.pages, RenderingStrategy.DoNothing)
        }

        @TestNavigationDSL
        class ModulesDsl(val pages: MutableList<ModulePageNode> = mutableListOf()) {
            fun modulePage(name: String, fn: PackageDsl.() -> Unit) {
                val packages = PackageDsl().also { it.fn() }
                pages.push(
                    ModulePageNode(
                        name = name,
                        children = packages.pages,
                        content = stubContentNode
                    )
                )
            }
        }

        @TestNavigationDSL
        class PackageDsl(val pages: MutableList<PackagePageNode> = mutableListOf()) {
            fun packagePage(name: String, fn: ClassDsl.() -> Unit) {
                val packages = ClassDsl().also { it.fn() }
                pages.push(
                    PackagePageNode(
                        name = name,
                        children = packages.pages,
                        content = stubContentNode,
                        dri = emptySet()
                    )
                )
            }
        }

        @TestNavigationDSL
        class ClassDsl(val pages: MutableList<ClasslikePageNode> = mutableListOf()) {
            fun classPage(name: String) {
                pages.push(
                    ClasslikePageNode(
                        name = name,
                        children = emptyList(),
                        content = stubContentNode,
                        dri = emptySet()
                    )
                )
            }
        }
    }

    @Test
    fun `links to a package with or without a class`() {
        val root = NavigationDSL()("Root") {
            modulePage("Module") {
                packagePage("Package") {}
            }
        }
        val packagePage = root.children.first().children.first() as PackagePageNode
        val locationProvider = getTestLocationProvider(root)
        val resolvedLink = locationProvider.resolve(packagePage)
        val localToRoot = locationProvider.pathToRoot(packagePage)

        val rootWithClass = NavigationDSL()("Root") {
            modulePage("Module") {
                packagePage("Package") {
                    classPage("ClassA")
                }
            }
        }
        val packagePageWithClass = rootWithClass.children.first().children.first() as PackagePageNode

        val locationProviderWithClass = getTestLocationProvider(rootWithClass)
        val localToRootWithClass = locationProviderWithClass.pathToRoot(packagePageWithClass)
        val resolvedLinkWithClass = locationProviderWithClass.resolve(packagePageWithClass)

        assertEquals("-module/Package.html", resolvedLink)
        assertEquals("../", localToRoot)

        assertEquals("-module/Package/index.html", resolvedLinkWithClass)
        assertEquals("../../", localToRootWithClass)
    }
}