aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/transformers/SourceLinkTransformerTest.kt
blob: 469c1a1eb90ee0b2d0122b7060ae62c7c86e56bc (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
package transformers

import org.jetbrains.dokka.DokkaSourceSetID
import org.jetbrains.dokka.SourceLinkDefinitionImpl
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jsoup.nodes.Element
import org.junit.jupiter.api.Test
import signatures.renderedContent
import utils.TestOutputWriterPlugin
import java.net.URL
import kotlin.test.assertEquals

class SourceLinkTransformerTest : BaseAbstractTest() {

    private fun Element.getSourceLink() = select(".symbol .floating-right")
        .select("a[href]")
        .attr("href")

    @Test
    fun `source link should lead to name`() {
        val configuration = dokkaConfiguration {
            sourceSets {
                sourceSet {
                    sourceRoots = listOf("src/")
                    sourceLinks = listOf(
                        SourceLinkDefinitionImpl(
                            localDirectory = "src/main/kotlin",
                            remoteUrl = URL("https://github.com/user/repo/tree/master/src/main/kotlin"),
                            remoteLineSuffix = "#L"
                        )
                    )
                }
            }
        }

        val writerPlugin = TestOutputWriterPlugin()

        testInline(
            """
            |/src/main/kotlin/basic/Deprecated.kt
            |package testpackage
            |
            |/**
            |* Marks the annotated declaration as deprecated. ...
            |*/
            |@Target(CLASS, FUNCTION, PROPERTY, ANNOTATION_CLASS, CONSTRUCTOR, PROPERTY_SETTER, PROPERTY_GETTER, TYPEALIAS)
            |@MustBeDocumented
            |public annotation class Deprecated(
            |    val message: String,
            |    val replaceWith: ReplaceWith = ReplaceWith(""),
            |    val level: DeprecationLevel = DeprecationLevel.WARNING
            |)
        """.trimMargin(),
            configuration,
            pluginOverrides = listOf(writerPlugin)
        ) {
            renderingStage = { _, _ ->
                val page = writerPlugin.writer.renderedContent("root/testpackage/-deprecated/index.html")
                val sourceLink = page.getSourceLink()

                assertEquals(
                    "https://github.com/user/repo/tree/master/src/main/kotlin/basic/Deprecated.kt#L8",
                    sourceLink
                )
            }
        }
    }

    @Test
    fun `source link should be for actual typealias`() {
        val mppConfiguration = dokkaConfiguration {
            moduleName = "test"
            sourceSets {
                sourceSet {
                    name = "common"
                    sourceRoots = listOf("src/main/kotlin/common/Test.kt")
                    classpath = listOf(commonStdlibPath!!)
                    externalDocumentationLinks = listOf(stdlibExternalDocumentationLink)
                }
                sourceSet {
                    name = "jvm"
                    dependentSourceSets = setOf(DokkaSourceSetID("test", "common"))
                    sourceRoots = listOf("src/main/kotlin/jvm/Test.kt")
                    classpath = listOf(commonStdlibPath!!)
                    externalDocumentationLinks = listOf(stdlibExternalDocumentationLink)
                    sourceLinks = listOf(
                        SourceLinkDefinitionImpl(
                            localDirectory = "src/main/kotlin",
                            remoteUrl = URL("https://github.com/user/repo/tree/master/src/main/kotlin"),
                            remoteLineSuffix = "#L"
                        )
                    )
                }
            }
        }

        val writerPlugin = TestOutputWriterPlugin()

        testInline(
            """
                |/src/main/kotlin/common/Test.kt
                |package example
                |
                |expect class Foo
                |
                |/src/main/kotlin/jvm/Test.kt
                |package example
                |
                |class Bar
                |actual typealias Foo = Bar
                |
            """.trimMargin(),
            mppConfiguration,
            pluginOverrides = listOf(writerPlugin)
        ) {
            renderingStage = { _, _ ->
                val page = writerPlugin.writer.renderedContent("test/example/-foo/index.html")
                val sourceLink = page.getSourceLink()

                assertEquals(
                    "https://github.com/user/repo/tree/master/src/main/kotlin/jvm/Test.kt#L4",
                    sourceLink
                )
            }
        }
    }
}