aboutsummaryrefslogtreecommitdiff
path: root/dokka-subprojects/plugin-base/src/test/kotlin/linkableContent/LinkableContentTest.kt
blob: 1b73ffee35d961aa3b6981c4f549fd912906c966 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package linkableContent

import org.jetbrains.dokka.SourceLinkDefinitionImpl
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.base.transformers.pages.sourcelinks.SourceLinksTransformer
import org.jetbrains.dokka.model.WithGenerics
import org.jetbrains.dokka.model.dfs
import org.jetbrains.dokka.model.doc.Text
import org.jetbrains.dokka.pages.*
import org.jsoup.Jsoup
import utils.TestOutputWriterPlugin
import utils.assertNotNull
import java.net.URL
import java.nio.file.Paths
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import utils.OnlyDescriptorsMPP

class LinkableContentTest : BaseAbstractTest() {

    @OnlyDescriptorsMPP("#3238")
    @Test
    fun `Include module and package documentation`() {

        val testDataDir = getTestDataDir("multiplatform/basicMultiplatformTest").toAbsolutePath()
        val includesDir = getTestDataDir("linkable/includes").toAbsolutePath()

        val configuration = dokkaConfiguration {
            moduleName = "example"
            sourceSets {
                val common = sourceSet {
                    name = "common"
                    displayName = "common"
                    analysisPlatform = "common"
                    sourceRoots = listOf(Paths.get("$testDataDir/commonMain/kotlin").toString())
                }
                val jvmAndJsSecondCommonMain = sourceSet {
                    name = "jvmAndJsSecondCommonMain"
                    displayName = "jvmAndJsSecondCommonMain"
                    analysisPlatform = "common"
                    dependentSourceSets = setOf(common.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jvmAndJsSecondCommonMain/kotlin").toString())
                }
                sourceSet {
                    name = "js"
                    displayName = "js"
                    analysisPlatform = "js"
                    dependentSourceSets = setOf(common.value.sourceSetID, jvmAndJsSecondCommonMain.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jsMain/kotlin").toString())
                    includes = listOf(Paths.get("$includesDir/include2.md").toString())
                }
                sourceSet {
                    name = "jvm"
                    displayName = "jvm"
                    analysisPlatform = "jvm"
                    dependentSourceSets = setOf(common.value.sourceSetID, jvmAndJsSecondCommonMain.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jvmMain/kotlin").toString())
                    includes = listOf(Paths.get("$includesDir/include1.md").toString())
                }
            }
        }

        testFromData(configuration) {
            documentablesMergingStage = {
                assertEquals(2, it.documentation.size)
                assertEquals(2, it.packages.size)
                assertEquals(1, it.packages.first().documentation.size)
                assertEquals(1, it.packages.last().documentation.size)
            }
        }

    }

    @Test
    fun `Sources multiplatform class documentation`() {

        val testDataDir = getTestDataDir("linkable/sources").toAbsolutePath()

        val configuration = dokkaConfiguration {
            moduleName = "example"

            sourceSets {
                val common = sourceSet {
                    name = "common"
                    displayName = "common"
                    analysisPlatform = "common"
                    sourceRoots = listOf(Paths.get("$testDataDir/commonMain/kotlin").toString())
                }
                val jvmAndJsSecondCommonMain = sourceSet {
                    name = "jvmAndJsSecondCommonMain"
                    displayName = "jvmAndJsSecondCommonMain"
                    analysisPlatform = "common"
                    dependentSourceSets = setOf(common.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jvmAndJsSecondCommonMain/kotlin").toString())
                }
                sourceSet {
                    name = "js"
                    displayName = "js"
                    analysisPlatform = "js"
                    dependentSourceSets = setOf(common.value.sourceSetID, jvmAndJsSecondCommonMain.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jsMain/kotlin").toString())
                    sourceLinks = listOf(
                        SourceLinkDefinitionImpl(
                            localDirectory = "$testDataDir/jsMain/kotlin",
                            remoteUrl = URL("https://github.com/user/repo/tree/master/src/jsMain/kotlin"),
                            remoteLineSuffix = "#L"
                        )
                    )
                }
                sourceSet {
                    name = "jvm"
                    displayName = "jvm"
                    analysisPlatform = "jvm"
                    dependentSourceSets = setOf(common.value.sourceSetID, jvmAndJsSecondCommonMain.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jvmMain/kotlin").toString())
                    sourceLinks = listOf(
                        SourceLinkDefinitionImpl(
                            localDirectory = "$testDataDir/jvmMain/kotlin",
                            remoteUrl = URL("https://github.com/user/repo/tree/master/src/jvmMain/kotlin"),
                            remoteLineSuffix = "#L"
                        )
                    )
                }
            }
        }

        testFromData(configuration) {
            renderingStage = { rootPageNode, dokkaContext ->
                val newRoot = SourceLinksTransformer(dokkaContext).invoke(rootPageNode)
                val moduleChildren = newRoot.children
                assertEquals(1, moduleChildren.size)
                val packageChildren = moduleChildren.first().children
                assertEquals(2, packageChildren.size)
                packageChildren.forEach {
                    val name = it.name.substringBefore("Class")
                    val signature = (it as? ClasslikePageNode)?.content?.dfs { it is ContentGroup && it.dci.kind == ContentKind.Symbol }.assertNotNull("signature")
                    val crl = signature.children.last().children[1] as? ContentResolvedLink
                    assertEquals(
                        "https://github.com/user/repo/tree/master/src/${name.toLowerCase()}Main/kotlin/${name}Class.kt#L7",
                        crl?.address
                    )
                }
            }
        }
    }

    @OnlyDescriptorsMPP("#3238")
    @Test
    fun `Samples multiplatform documentation`() {

        val testDataDir = getTestDataDir("linkable/samples").toAbsolutePath()

        val configuration = dokkaConfiguration {
            moduleName = "example"
            sourceSets {
                val common = sourceSet {
                    name = "common"
                    displayName = "common"
                    analysisPlatform = "common"
                    sourceRoots = listOf(Paths.get("$testDataDir/commonMain/kotlin").toString())
                }
                val jvmAndJsSecondCommonMain = sourceSet {
                    name = "jvmAndJsSecondCommonMain"
                    displayName = "jvmAndJsSecondCommonMain"
                    analysisPlatform = "common"
                    dependentSourceSets = setOf(common.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jvmAndJsSecondCommonMain/kotlin").toString())
                }
                sourceSet {
                    name = "js"
                    displayName = "js"
                    analysisPlatform = "js"
                    dependentSourceSets = setOf(common.value.sourceSetID, jvmAndJsSecondCommonMain.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jsMain/kotlin").toString())
                    samples = listOf("$testDataDir/jsMain/resources/Samples.kt")
                }
                sourceSet {
                    name = "jvm"
                    displayName = "jvm"
                    analysisPlatform = "jvm"
                    dependentSourceSets = setOf(common.value.sourceSetID, jvmAndJsSecondCommonMain.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jvmMain/kotlin").toString())
                    samples = listOf("$testDataDir/jvmMain/resources/Samples.kt")
                }
            }
        }

        testFromData(configuration) {
            renderingStage = { rootPageNode, _ ->
                // TODO [beresnev] :(((
//                val newRoot = DefaultSamplesTransformer(dokkaContext).invoke(rootPageNode)
                val newRoot = rootPageNode
                val moduleChildren = newRoot.children
                assertEquals(1, moduleChildren.size)
                val packageChildren = moduleChildren.first().children
                assertEquals(2, packageChildren.size)
                packageChildren.forEach { pageNode ->
                    val name = pageNode.name.substringBefore("Class")
                    val classChildren = pageNode.children
                    assertEquals(2, classChildren.size)
                    val function = classChildren.find { it.name == "printWithExclamation" }
                    val text = (function as MemberPageNode).content.let { it as ContentGroup }.children.last()
                        .let { it as ContentDivergentGroup }.children.single().after
                        .let { it as ContentGroup }.children.last()
                        .let { it as ContentGroup }.children.single()
                        .let { it as ContentCodeBlock }.children.single()
                        .let { it as ContentText }.text
                    assertEquals(
                        """|import p2.${name}Class
                                |fun main() { 
                                |   //sampleStart 
                                |   ${name}Class().printWithExclamation("Hi, $name") 
                                |   //sampleEnd
                                |}""".trimMargin(),
                        text
                    )
                }
            }
        }
    }

    @Test
    fun `Documenting return type for a function in inner class with generic parent`() {
        testInline(
            """
            |/src/main/kotlin/test/source.kt
            |package test
            |
            |class Sample<S>(first: S){
            |    inner class SampleInner {
            |       fun foo(): S = TODO()
            |    }
            |}
            |
        """.trimIndent(),
            dokkaConfiguration {
                sourceSets {
                    sourceSet {
                        sourceRoots = listOf("src/")
                        analysisPlatform = "jvm"
                        name = "js"
                    }
                }
            }
        ) {
            renderingStage = { module, _ ->
                val sample = module.children.single { it.name == "test" }
                    .children.single { it.name == "Sample" } as ClasslikePageNode
                val foo = sample
                    .children
                    .single { it.name == "SampleInner" }
                    .let { it as ClasslikePageNode }
                    .children
                    .single { it.name == "foo" }
                    .let { it as MemberPageNode }

                val returnTypeNode = foo.content.dfs {
                    val link = (it as? ContentDRILink)?.children
                    val child = link?.first() as? ContentText
                    child?.text == "S"
                } as? ContentDRILink

                assertEquals(
                    (sample.documentables.firstOrNull() as WithGenerics).generics.first().dri,
                    returnTypeNode?.address
                )
            }
        }
    }

    @Test
    fun `Include module and package documentation with codeblock`() {

        val testDataDir = getTestDataDir("multiplatform/basicMultiplatformTest").toAbsolutePath()
        val includesDir = getTestDataDir("linkable/includes").toAbsolutePath()

        val configuration = dokkaConfiguration {
            moduleName = "example"
            sourceSets {
                sourceSet {
                    analysisPlatform = "js"
                    sourceRoots = listOf("jsMain").map {
                        Paths.get("$testDataDir/$it/kotlin").toString()
                    }
                    name = "js"
                    includes = listOf(Paths.get("$includesDir/include2.md").toString())
                }
                sourceSet {
                    analysisPlatform = "jvm"
                    sourceRoots = listOf("jvmMain").map {
                        Paths.get("$testDataDir/$it/kotlin").toString()
                    }
                    name = "jvm"
                    includes = listOf(Paths.get("$includesDir/include1.md").toString())
                }
            }
        }

        testFromData(configuration) {
            documentablesMergingStage = {
                assertNotEquals(null, it.packages.first().documentation.values.single().dfs {
                    (it as? Text)?.body?.contains("@SqlTable") ?: false
                })
            }
        }

    }

    @Test
    fun `Include module with description parted in two files`() {

        val testDataDir = getTestDataDir("multiplatform/basicMultiplatformTest").toAbsolutePath()
        val includesDir = getTestDataDir("linkable/includes").toAbsolutePath()

        val configuration = dokkaConfiguration {
            moduleName = "example"
            sourceSets {
                val common = sourceSet {
                    name = "common"
                    displayName = "common"
                    analysisPlatform = "common"
                    sourceRoots = listOf(Paths.get("$testDataDir/commonMain/kotlin").toString())
                }
                val jvmAndJsSecondCommonMain = sourceSet {
                    name = "jvmAndJsSecondCommonMain"
                    displayName = "jvmAndJsSecondCommonMain"
                    analysisPlatform = "common"
                    dependentSourceSets = setOf(common.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jvmAndJsSecondCommonMain/kotlin").toString())
                }
                sourceSet {
                    name = "js"
                    displayName = "js"
                    analysisPlatform = "js"
                    dependentSourceSets = setOf(common.value.sourceSetID, jvmAndJsSecondCommonMain.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jsMain/kotlin").toString())
                    includes = listOf(Paths.get("$includesDir/include2.md").toString())
                }
                sourceSet {
                    name = "jvm"
                    displayName = "jvm"
                    analysisPlatform = "jvm"
                    dependentSourceSets = setOf(common.value.sourceSetID, jvmAndJsSecondCommonMain.value.sourceSetID)
                    sourceRoots = listOf(Paths.get("$testDataDir/jvmMain/kotlin").toString())
                    includes = listOf(
                        Paths.get("$includesDir/include1.md").toString(),
                        Paths.get("$includesDir/include11.md").toString()
                    )
                }
            }
        }

        testFromData(configuration) {
            documentablesMergingStage = { module ->
                val value = module.documentation.entries.single {
                    it.key.displayName == "jvm"
                }.value
                assertNotNull(value.dfs {
                    (it as? Text)?.body == "This is second JVM documentation for module example"
                })

                assertNotNull(value.dfs {
                    (it as? Text)?.body == "This is JVM documentation for module example"
                })
            }
        }
    }

    @Test
    fun `should have a correct link to declaration from another source set`() {
        val writerPlugin = TestOutputWriterPlugin()
        val configuration = dokkaConfiguration {
            sourceSets {
                val common = sourceSet {
                    sourceRoots = listOf("src/commonMain")
                    analysisPlatform = "common"
                    name = "common"
                    displayName = "common"
                }
                sourceSet {
                    sourceRoots = listOf("src/jvmMain/")
                    analysisPlatform = "jvm"
                    name = "jvm"
                    displayName = "jvm"
                    dependentSourceSets = setOf(common.value.sourceSetID)
                }
            }
        }

        testInline(
            """
            /src/commonMain/main.kt
            class A
            /src/jvmMain/main.kt
            /**
            * link to [A]
            */
             class B
            """.trimIndent()
            ,
            pluginOverrides = listOf(writerPlugin),
            configuration = configuration
        ) {
            renderingStage = { _, _ ->
                val page =
                    Jsoup.parse(writerPlugin.writer.contents.getValue("root/[root]/-b/index.html"))
                val link = page.select(".paragraph a").single()
                assertEquals("../-a/index.html", link.attr("href"))
            }
        }
    }
}