aboutsummaryrefslogtreecommitdiff
path: root/dokka-subprojects/plugin-base/src/test/kotlin/content/samples/ContentForSamplesTest.kt
blob: d166d8f83e33d81fa2d7894a25ae106cc33a7958 (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
/*
 * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package content.samples

import matchers.content.*
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.base.transformers.pages.KOTLIN_PLAYGROUND_SCRIPT
import org.jetbrains.dokka.model.DisplaySourceSet
import utils.TestOutputWriterPlugin
import utils.assertContains
import utils.classSignature
import utils.findTestType
import java.nio.file.Paths
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class ContentForSamplesTest : BaseAbstractTest() {
    private val testDataDir = getTestDataDir("content/samples").toAbsolutePath()

    private val testConfiguration = dokkaConfiguration {
        sourceSets {
            sourceSet {
                sourceRoots = listOf("src/")
                analysisPlatform = "jvm"
                samples = listOf(
                    Paths.get("$testDataDir/samples.kt").toString(),
                )
            }
        }
    }

    private val mppTestConfiguration = dokkaConfiguration {
        moduleName = "example"
        sourceSets {
            val common = sourceSet {
                name = "common"
                displayName = "common"
                analysisPlatform = "common"
                sourceRoots = listOf("src/commonMain/kotlin/pageMerger/Test.kt")
                samples = listOf(
                    Paths.get("$testDataDir/samples.kt").toString(),
                )
            }
            sourceSet {
                name = "jvm"
                displayName = "jvm"
                analysisPlatform = "jvm"
                dependentSourceSets = setOf(common.value.sourceSetID)
                sourceRoots = listOf("src/jvmMain/kotlin/pageMerger/Test.kt")
                samples = listOf(
                    Paths.get("$testDataDir/samples.kt").toString(),
                )
            }
            sourceSet {
                name = "linuxX64"
                displayName = "linuxX64"
                analysisPlatform = "native"
                dependentSourceSets = setOf(common.value.sourceSetID)
                sourceRoots = listOf("src/linuxX64Main/kotlin/pageMerger/Test.kt")
                samples = listOf(
                    Paths.get("$testDataDir/samples.kt").toString(),
                )
            }
        }
    }

    @Test
    fun `samples block is rendered in the description`() {
        val writerPlugin = TestOutputWriterPlugin()
        testInline(
            """
            |/src/main/kotlin/test/source.kt
            |package test
            |
            | /**
            | * @sample [test.sampleForClassDescription]
            | */
            |class Foo
        """.trimIndent(), testConfiguration,
            pluginOverrides = listOf(writerPlugin)
        ) {
            pagesTransformationStage = { module ->
                val page = module.findTestType("test", "Foo")
                assertContains(page.embeddedResources, KOTLIN_PLAYGROUND_SCRIPT)
                page.content.assertNode {
                    group {
                        header(1) { +"Foo" }
                        platformHinted {
                            classSignature(
                                emptyMap(),
                                "",
                                "",
                                emptySet(),
                                "Foo"
                            )
                            header(4) { +"Samples" }
                            group {
                                codeBlock {
                                    +"""|
                                    |fun main() { 
                                    |   //sampleStart 
                                    |   print("Hello") 
                                    |   //sampleEnd
                                    |}""".trimMargin()
                                }
                            }
                        }
                    }
                    skipAllNotMatching()
                }
            }
            renderingStage = { _, _ ->
                assertNotEquals(-1, writerPlugin.writer.contents["root/test/-foo/index.html"]?.indexOf(KOTLIN_PLAYGROUND_SCRIPT))
            }
        }
    }

    @Test
    fun `multiplatofrm class with samples in few platforms`() {
        testInline(
            """
                |/src/commonMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |/**
                |* @sample [test.sampleForClassDescription]
                |*/
                |expect open class Parent
                |
                |/src/jvmMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |/**
                |* @sample unresolved
                |*/
                |actual open class Parent
                |
                |/src/linuxX64Main/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |actual open class Parent
                |
            """.trimMargin(),
            mppTestConfiguration
        ) {
            pagesTransformationStage = { module ->
                val page = module.findTestType("pageMerger", "Parent")
                assertContains(page.embeddedResources, KOTLIN_PLAYGROUND_SCRIPT)
                page.content.assertNode {
                    group {
                        header(1) { +"Parent" }
                        platformHinted {
                            group {
                                +"expect open class "
                                link {
                                    +"Parent"
                                }
                            }
                            group {
                                +"actual open class "
                                link {
                                    +"Parent"
                                }
                            }
                            group {
                                +"actual open class "
                                link {
                                    +"Parent"
                                }
                            }
                            header(4) { +"Samples" }
                            group {
                                codeBlock {
                                    +"""|
                                    |fun main() { 
                                    |   //sampleStart 
                                    |   print("Hello") 
                                    |   //sampleEnd
                                    |}""".trimMargin()
                                }
                                check {
                                    sourceSets.assertSourceSet("common")
                                }
                            }
                            group {
                                +"unresolved"
                                check {
                                    sourceSets.assertSourceSet("jvm")
                                }
                            }
                        }
                    }
                    skipAllNotMatching()
                }
            }
        }
    }
}


private fun Set<DisplaySourceSet>.assertSourceSet(expectedName: String) {
    assertEquals(1, this.size)
    assertEquals(expectedName, this.first().name)
}