aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/linking/EnumValuesLinkingTest.kt
blob: f95d9860ed7eb835557d6bb3e6645aba8acfcf38 (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 linking

import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.links.DRIExtraContainer
import org.jetbrains.dokka.links.EnumEntryDRIExtra
import org.jetbrains.dokka.model.dfs
import org.jetbrains.dokka.model.doc.DocumentationLink
import org.jetbrains.dokka.pages.ContentDRILink
import org.jetbrains.dokka.pages.ContentPage
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jsoup.Jsoup
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.nio.file.Paths
import utils.TestOutputWriterPlugin
import kotlin.AssertionError

class EnumValuesLinkingTest : BaseAbstractTest() {

    @Test
    fun `check if enum values are correctly linked`() {
        val writerPlugin = TestOutputWriterPlugin()
        val testDataDir = getTestDataDir("linking").toAbsolutePath()
        testFromData(
            dokkaConfiguration {
                sourceSets {
                    sourceSet {
                        sourceRoots = listOf(Paths.get("$testDataDir/jvmMain/kotlin").toString())
                        analysisPlatform = "jvm"
                        name = "jvm"
                    }
                }
            },
            pluginOverrides = listOf(writerPlugin)
        ) {
            documentablesTransformationStage = {
                val classlikes = it.packages.single().children
                assertEquals(4, classlikes.size)

                val javaLinker = classlikes.single { it.name == "JavaLinker" }
                javaLinker.documentation.values.single().children.run {
                    when (val kotlinLink = this[0].children[1].children[1]) {
                        is DocumentationLink -> kotlinLink.dri.run {
                            assertEquals("KotlinEnum.ON_CREATE", this.classNames)
                            assertEquals(null, this.callable)
                            assertNotNull(DRIExtraContainer(extra)[EnumEntryDRIExtra])
                        }
                        else -> throw AssertionError("Link node is not DocumentationLink type")
                    }

                    when (val javaLink = this[0].children[2].children[1]) {
                        is DocumentationLink -> javaLink.dri.run {
                            assertEquals("JavaEnum.ON_DECEIT", this.classNames)
                            assertEquals(null, this.callable)
                            assertNotNull(DRIExtraContainer(extra)[EnumEntryDRIExtra])
                        }
                        else -> throw AssertionError("Link node is not DocumentationLink type")
                    }
                }

                val kotlinLinker = classlikes.single { it.name == "KotlinLinker" }
                kotlinLinker.documentation.values.single().children.run {
                    when (val kotlinLink = this[0].children[0].children[5]) {
                        is DocumentationLink -> kotlinLink.dri.run {
                            assertEquals("KotlinEnum.ON_CREATE", this.classNames)
                            assertEquals(null, this.callable)
                            assertNotNull(DRIExtraContainer(extra)[EnumEntryDRIExtra])
                        }
                        else -> throw AssertionError("Link node is not DocumentationLink type")
                    }

                    when (val javaLink = this[0].children[0].children[9]) {
                        is DocumentationLink -> javaLink.dri.run {
                            assertEquals("JavaEnum.ON_DECEIT", this.classNames)
                            assertEquals(null, this.callable)
                            assertNotNull(DRIExtraContainer(extra)[EnumEntryDRIExtra])
                        }
                        else -> throw AssertionError("Link node is not DocumentationLink type")
                    }
                }

                assertEquals(
                    javaLinker.documentation.values.single().children[0].children[1].children[1].safeAs<DocumentationLink>()?.dri,
                    kotlinLinker.documentation.values.single().children[0].children[0].children[5].safeAs<DocumentationLink>()?.dri
                )

                assertEquals(
                    javaLinker.documentation.values.single().children[0].children[2].children[1].safeAs<DocumentationLink>()?.dri,
                    kotlinLinker.documentation.values.single().children[0].children[0].children[9].safeAs<DocumentationLink>()?.dri
                )
            }

            renderingStage = { rootPageNode, _ ->
                val classlikes = rootPageNode.children.single().children
                assertEquals(4, classlikes.size)

                val javaLinker = classlikes.single { it.name == "JavaLinker" }
                (javaLinker as ContentPage).run {
                    assertNotNull(content.dfs { it is ContentDRILink && it.address.classNames == "KotlinEnum.ON_CREATE" })
                    assertNotNull(content.dfs { it is ContentDRILink && it.address.classNames == "JavaEnum.ON_DECEIT" })
                }

                val kotlinLinker = classlikes.single { it.name == "KotlinLinker" }
                (kotlinLinker as ContentPage).run {
                    assertNotNull(content.dfs { it is ContentDRILink && it.address.classNames == "KotlinEnum.ON_CREATE" })
                    assertNotNull(content.dfs { it is ContentDRILink && it.address.classNames == "JavaEnum.ON_DECEIT" })
                }

                Jsoup
                    .parse(writerPlugin.writer.contents.getValue("root/linking.source/-java-linker/index.html"))
                    .select("a[href=\"../-kotlin-enum/-o-n_-c-r-e-a-t-e/index.html\"]")
                    .assertOnlyOneElement()

                Jsoup
                    .parse(writerPlugin.writer.contents.getValue("root/linking.source/-java-linker/index.html"))
                    .select("a[href=\"../-java-enum/-o-n_-d-e-c-e-i-t/index.html\"]")
                    .assertOnlyOneElement()

                Jsoup
                    .parse(writerPlugin.writer.contents.getValue("root/linking.source/-kotlin-linker/index.html"))
                    .select("a[href=\"../-kotlin-enum/-o-n_-c-r-e-a-t-e/index.html\"]")
                    .assertOnlyOneElement()

                Jsoup
                    .parse(writerPlugin.writer.contents.getValue("root/linking.source/-kotlin-linker/index.html"))
                    .select("a[href=\"../-java-enum/-o-n_-d-e-c-e-i-t/index.html\"]")
                    .assertOnlyOneElement()
            }
        }
    }

    private fun <T> List<T>.assertOnlyOneElement() {
        if (isEmpty() || size > 1) {
            throw AssertionError("Single element expected in list: $this")
        }
    }
}