-
Notifications
You must be signed in to change notification settings - Fork 392
/
DefaultExternalLocationProviderTest.kt
161 lines (145 loc) · 6.95 KB
/
DefaultExternalLocationProviderTest.kt
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package locationProvider
import org.jetbrains.dokka.base.resolvers.external.DefaultExternalLocationProvider
import org.jetbrains.dokka.base.resolvers.local.DokkaLocationProvider
import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentation
import org.jetbrains.dokka.base.resolvers.shared.PackageList
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.links.Callable
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.TypeConstructor
import org.jetbrains.dokka.model.dfs
import org.jetbrains.dokka.pages.ClasslikePageNode
import org.jetbrains.dokka.plugability.DokkaContext
import utils.TestOutputWriterPlugin
import java.net.URL
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class DefaultExternalLocationProviderTest : BaseAbstractTest() {
private val testDataDir =
getTestDataDir("locationProvider").toAbsolutePath().toString().removePrefix("/").let { "/$it" }
private val kotlinLang = "https://kotlinlang.org/api/latest/jvm/stdlib"
private val packageListURL = URL("file://$testDataDir/stdlib-package-list")
private val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
sourceRoots = listOf("src/")
classpath += jvmStdlibPath!!
}
}
}
private fun getTestLocationProvider(context: DokkaContext? = null): DefaultExternalLocationProvider {
val dokkaContext = context ?: DokkaContext.create(configuration, logger, emptyList())
val packageList = PackageList.load(packageListURL, 8, true)!!
val externalDocumentation =
ExternalDocumentation(URL(kotlinLang), packageList)
return DefaultExternalLocationProvider(externalDocumentation, ".html", dokkaContext)
}
@Test
fun `ordinary link`() {
val locationProvider = getTestLocationProvider()
val dri = DRI("kotlin.reflect", "KVisibility")
assertEquals("$kotlinLang/kotlin.reflect/-k-visibility/index.html", locationProvider.resolve(dri))
}
@Test
fun `relocation in package list`() {
val locationProvider = getTestLocationProvider()
val dri = DRI(
"",
"",
Callable(
"longArray",
null,
listOf(
TypeConstructor("kotlin.Int", emptyList()),
TypeConstructor("kotlin.Any", emptyList())
)
)
)
assertEquals("$kotlinLang/kotlin-stdlib/[JS root]/long-array.html", locationProvider.resolve(dri))
}
@Test
fun `should return null for class not in list`() {
val locationProvider = getTestLocationProvider()
val dri = DRI(
"foo",
"Bar"
)
assertEquals(null, locationProvider.resolve(dri))
}
@Test
fun `should have a correct url to an external inherited member #2879`() {
val writerPlugin = TestOutputWriterPlugin()
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
externalDocumentationLinks = listOf(stdlibExternalDocumentationLink)
sourceRoots = listOf("src/")
classpath = listOfNotNull(jvmStdlibPath)
}
}
}
testInline(
"""
/src/kotlin/main.kt
open interface C : Collection<C>
interface A : C()
interface B : C()
""".trimIndent()
,
pluginOverrides = listOf(writerPlugin),
configuration = configuration
) {
renderingStage = { rootPage, ctx ->
val location = DokkaLocationProvider(rootPage, ctx, ".html")
val classA = rootPage.dfs { it is ClasslikePageNode && it.name == "A" }
val classB = rootPage.dfs { it is ClasslikePageNode && it.name == "B" }
val classC = rootPage.dfs { it is ClasslikePageNode && it.name == "C" }
val sourceSet = (classA as ClasslikePageNode).content.sourceSets
val dri = DRI("kotlin.collections", "Collection", Callable(name="isEmpty", receiver=null, params=emptyList()))
assertEquals("https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html", location.resolve(dri, sourceSet, classA))
assertEquals("https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html", location.resolve(dri, sourceSet, classB))
assertEquals("https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html", location.resolve(dri, sourceSet, classC))
}
}
}
@Test
fun `should have external links for external inherited members`() {
val writerPlugin = TestOutputWriterPlugin()
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
externalDocumentationLinks = listOf(stdlibExternalDocumentationLink)
sourceRoots = listOf("src/")
classpath = listOfNotNull(jvmStdlibPath)
}
}
}
testInline(
"""
/src/kotlin/main.kt
interface MyCharSequence: CharSequence
""".trimIndent()
,
pluginOverrides = listOf(writerPlugin),
configuration = configuration
) {
renderingStage = { _, _ ->
"".chars()
val content = writerPlugin.writer.contents["root/[root]/-my-char-sequence/index.html"] ?: ""
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/length.html\"><span><span>length</span></span></a>"))
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/get.html\"><span><span>get</span></span></a>"))
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/sub-sequence.html\"><span>sub</span><wbr></wbr><span><span>Sequence</span></span></a>"))
// TODO #3542
// these links are invalid
// chars() and codePoints() are absent in https://kotlinlang.org/ since they come from mapping Kotlin to Java
// see https://kotlinlang.org/docs/java-interop.html#mapped-types
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/chars.html\"><span><span>chars</span></span></a>"))
assertTrue(content.contains("<a href=\"https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char-sequence/code-points.html\"><span>code</span><wbr></wbr><span><span>Points</span></span></a>"))
}
}
}
}