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

import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.PluginConfigurationImpl
import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.model.childrenOfType
import org.jetbrains.dokka.model.dfs
import org.jetbrains.dokka.model.firstChildOfType
import org.jetbrains.dokka.pages.*
import org.jetbrains.kotlin.utils.addIfNotNull
import org.junit.jupiter.api.Test
import utils.assertNotNull
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class MergeImplicitExpectActualDeclarationsTest : BaseAbstractTest() {

    @Suppress("UNUSED_VARIABLE")
    private fun configuration(switchOn: Boolean) = dokkaConfiguration {
        sourceSets {
            val common = sourceSet {
                name = "common"
                displayName = "common"
                analysisPlatform = "common"
                sourceRoots = listOf("src/commonMain/kotlin/pageMerger/Test.kt")
            }
            val js = sourceSet {
                name = "js"
                displayName = "js"
                analysisPlatform = "js"
                dependentSourceSets = setOf(common.value.sourceSetID)
                sourceRoots = listOf("src/jsMain/kotlin/pageMerger/Test.kt")
            }
            val jvm = sourceSet {
                name = "jvm"
                displayName = "jvm"
                analysisPlatform = "jvm"
                sourceRoots = listOf("src/jvmMain/kotlin/pageMerger/Test.kt")
            }
        }
        pluginsConfigurations.addIfNotNull(
            PluginConfigurationImpl(
                DokkaBase::class.qualifiedName!!,
                DokkaConfiguration.SerializationFormat.JSON,
                """{ "mergeImplicitExpectActualDeclarations": $switchOn }""",
            )
        )
    }

    private fun ClasslikePageNode.findSectionWithName(name: String) : ContentNode? {
        var sectionHeader: ContentHeader? = null
        return content.dfs { node ->
            node.children.filterIsInstance<ContentHeader>().any { header ->
                header.children.firstOrNull { it is ContentText && it.text == name }?.also { sectionHeader = header } != null
            }
        }?.children?.dropWhile { child -> child != sectionHeader  }?.drop(1)?.firstOrNull()
    }

    @Test
    fun `should merge fun`() {
        testInline(
            """

                |/src/jvmMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |class classA {
                |   fun method1(): String
                |}
                |
                |/src/jsMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |class classA {
                |   fun method1(): Int
                |}
                |
        """.trimMargin(),
            configuration(true),
            cleanupOutput = true
        ) {
            pagesTransformationStage = { root ->
                val classPage = root.dfs { it.name == "classA" } as? ClasslikePageNode
                assertNotNull(classPage, "Tested class not found!")

                val functions = classPage.findSectionWithName("Functions").assertNotNull("Functions")
                val method1 = functions.children.singleOrNull().assertNotNull("method1")

                assertEquals(
                    2,
                    method1.firstChildOfType<ContentDivergentGroup>().childrenOfType<ContentDivergentInstance>().size,
                    "Incorrect number of divergent instances found"
                )

                val methodPage = root.dfs { it.name == "method1" } as? MemberPageNode
                assertNotNull(methodPage, "Tested method not found!")

                val divergentGroup = methodPage.content.dfs { it is ContentDivergentGroup } as? ContentDivergentGroup

                assertEquals(
                    2,
                    divergentGroup?.childrenOfType<ContentDivergentInstance>()?.size,
                    "Incorrect number of divergent instances found in method page"
                )
            }
        }
    }

    @Test
    fun `should merge method and prop`() {
        testInline(
            """
                |/src/jvmMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |class classA {
                |   fun method1(): String
                |}
                |
                |/src/jsMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |class classA {
                |   val prop1: Int
                |}
                |
        """.trimMargin(),
            configuration(true),
            cleanupOutput = true
        ) {
            pagesTransformationStage = { root ->
                val classPage = root.dfs { it.name == "classA" } as? ClasslikePageNode
                assertNotNull(classPage, "Tested class not found!")

                val props = classPage.findSectionWithName("Properties").assertNotNull("Properties")
                val prop1 = props.children.singleOrNull().assertNotNull("prop1")

                val functions = classPage.findSectionWithName("Functions").assertNotNull("Functions")
                val method1 = functions.children.singleOrNull().assertNotNull("method1")
            }
        }
    }

    @Test
    fun `should merge prop`() {
        testInline(
            """
                |/src/jvmMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |class classA {
                |   val prop1: String
                |}
                |
                |/src/jsMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |class classA {
                |   val prop1: Int
                |}
                |
        """.trimMargin(),
            configuration(true),
            cleanupOutput = true
        ) {
            pagesTransformationStage = { root ->
                val classPage = root.dfs { it.name == "classA" } as? ClasslikePageNode
                assertNotNull(classPage, "Tested class not found!")

                val props = classPage.findSectionWithName("Properties").assertNotNull("Properties")
                val prop1 = props.children.singleOrNull().assertNotNull("prop1")

                assertEquals(
                    2,
                    prop1.firstChildOfType<PlatformHintedContent>().inner.children.size,
                    "Incorrect number of divergent instances found"
                )

                val propPage = root.dfs { it.name == "prop1" } as? MemberPageNode
                assertNotNull(propPage, "Tested method not found!")

                val divergentGroup = propPage.content.dfs { it is ContentDivergentGroup } as? ContentDivergentGroup

                assertEquals(
                    2,
                    divergentGroup?.childrenOfType<ContentDivergentInstance>()?.size,
                    "Incorrect number of divergent instances found in method page"
                )
            }
        }
    }

    @Test
    fun `should merge enum and class`() {
        testInline(
            """
                |/src/jvmMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |class classA {
                |   val prop1: String
                |}
                |
                |/src/jsMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |enum class classA {
                |   ENTRY
                |}
                |
        """.trimMargin(),
            configuration(true),
            cleanupOutput = true
        ) {
            pagesTransformationStage = { root ->
                val classPage = root.dfs { it.name == "classA" } as? ClasslikePageNode
                assertNotNull(classPage, "Tested class not found!")

                val entries = classPage.findSectionWithName("Entries").assertNotNull("Entries")
                val entry = entries.children.singleOrNull().assertNotNull("ENTRY")

                val props = classPage.findSectionWithName("Properties").assertNotNull("Properties")
                assertEquals(
                    3,
                    props.children.size,
                    "Incorrect number of properties found in method page"
                )
            }
        }
    }

    fun PageNode.childrenRec(): List<PageNode> = listOf(this) + children.flatMap { it.childrenRec() }

    @Test
    fun `should merge enum entries`() {
        testInline(
            """
                |/src/jvmMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |enum class classA {
                |   SMTH;
                |   fun method1(): Int
                |}
                |
                |/src/jsMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |enum class classA {
                |   SMTH;
                |   fun method1(): Int
                |}
                |
        """.trimMargin(),
            configuration(true),
            cleanupOutput = true
        ) {
            pagesTransformationStage = { root ->
                val classPage = root.dfs { it.name == "SMTH" } as? ClasslikePageNode
                assertNotNull(classPage, "Tested class not found!")

                val functions = classPage.findSectionWithName("Functions").assertNotNull("Functions")
                val method1 = functions.children.singleOrNull().assertNotNull("method1")

                assertEquals(
                    2,
                    method1.firstChildOfType<ContentDivergentGroup>().childrenOfType<ContentDivergentInstance>().size,
                    "Incorrect number of divergent instances found"
                )
            }
        }
    }

    /**
     * There is a case when a property and fun from different source sets
     * have the same name so pages have the same urls respectively.
     */
    @Test
    fun `should no merge prop and method with the same name`() {
        testInline(
            """
                |/src/jvmMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |class classA {
                |   fun merged():String
                |}
                |
                |/src/jsMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |class classA {
                |   val merged:String
                |}
                |
        """.trimMargin(),
            configuration(true),
            cleanupOutput = true
        ) {
            pagesTransformationStage = { root ->
                val allChildren = root.childrenRec().filterIsInstance<MemberPageNode>()

                assertEquals(
                    1,
                    allChildren.filter { it.name == "merged" }.size,
                    "Incorrect number of fun pages"
                )
            }
        }
    }

    @Test
    fun `should always merge constructor`() {
        testInline(
            """
                |/src/commonMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |expect class classA(a: Int)
                |
                |/src/jsMain/kotlin/pageMerger/Test.kt
                |package pageMerger
                |
                |actual class classA(a: Int)
        """.trimMargin(),
            configuration(false),
            cleanupOutput = true
        ) {
            pagesTransformationStage = { root ->
                val classPage = root.dfs { it.name == "classA" } as? ClasslikePageNode
                assertNotNull(classPage, "Tested class not found!")

                val constructors = classPage.findSectionWithName("Constructors").assertNotNull("Constructors")

                assertEquals(
                    1,
                    constructors.children.size,
                    "Incorrect number of constructors"
                )

                val platformHinted = constructors.dfs { it is PlatformHintedContent } as? PlatformHintedContent

                assertEquals(
                    2,
                    platformHinted?.sourceSets?.size,
                    "Incorrect number of source sets"
                )
            }
        }
    }
}