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
|
package pageMerger
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.model.dfs
import org.jetbrains.dokka.model.withDescendants
import org.jetbrains.dokka.pages.*
import org.junit.jupiter.api.RepeatedTest
import java.lang.IllegalArgumentException
import kotlin.test.assertEquals
class PageNodeMergerTest : BaseAbstractTest() {
private val defaultConfiguration = dokkaConfiguration {
sourceSets {
sourceSet {
sourceRoots = listOf("src/main/kotlin")
}
}
}
@Test
fun sameNameStrategyTest() {
testInline(
"""
|/src/main/kotlin/pageMerger/Test.kt
|package pageMerger
|
|fun testT(): Int = 1
|fun testT(i: Int): Int = i
|
|object Test {
| fun test(): String = ""
| fun test(str: String): String = str
|}
""".trimMargin(),
defaultConfiguration
) {
pagesTransformationStage = {
val allChildren = it.childrenRec().filterIsInstance<ContentPage>()
val testT = allChildren.filter { it.name == "testT" }
val test = allChildren.filter { it.name == "test" }
assertTrue(testT.size == 1) { "There can be only one testT page" }
assertTrue(testT.first().dri.size == 2) { "testT page should have 2 DRI, but has ${testT.first().dri.size}" }
assertTrue(test.size == 1) { "There can be only one test page" }
assertTrue(test.first().dri.size == 2) { "test page should have 2 DRI, but has ${test.first().dri.size}" }
}
}
}
@Disabled("TODO: reenable when we have infrastructure for turning off extensions")
@Test
fun defaultStrategyTest() {
val strList: MutableList<String> = mutableListOf()
testInline(
"""
|/src/main/kotlin/pageMerger/Test.kt
|package pageMerger
|
|fun testT(): Int = 1
|fun testT(i: Int): Int = i
|
|object Test {
| fun test(): String = ""
| fun test(str: String): String = str
|}
""".trimMargin(),
defaultConfiguration
) {
pagesTransformationStage = { root ->
val allChildren = root.childrenRec().filterIsInstance<ContentPage>()
val testT = allChildren.filter { it.name == "testT" }
val test = allChildren.filter { it.name == "test" }
assertTrue(testT.size == 1) { "There can be only one testT page" }
assertTrue(testT.first().dri.size == 1) { "testT page should have single DRI, but has ${testT.first().dri.size}" }
assertTrue(test.size == 1) { "There can be only one test page" }
assertTrue(test.first().dri.size == 1) { "test page should have single DRI, but has ${test.first().dri.size}" }
assertTrue(strList.count() == 2) { "Expected 2 warnings, got ${strList.count()}" }
}
}
}
fun PageNode.childrenRec(): List<PageNode> = listOf(this) + children.flatMap { it.childrenRec() }
@Test
fun `should not be merged`() {
val configuration = dokkaConfiguration {
moduleName = "example"
sourceSets {
val common = sourceSet {
name = "common"
displayName = "common"
analysisPlatform = "common"
sourceRoots = listOf("src/commonMain/kotlin/pageMerger/Test.kt")
}
sourceSet {
name = "js"
displayName = "js"
analysisPlatform = "js"
dependentSourceSets = setOf(common.value.sourceSetID)
sourceRoots = listOf("src/jsMain/kotlin/pageMerger/Test.kt")
}
sourceSet {
name = "jvm"
displayName = "jvm"
analysisPlatform = "jvm"
dependentSourceSets = setOf(common.value.sourceSetID)
sourceRoots = listOf("src/jvmMain/kotlin/pageMerger/Test.kt")
}
}
}
testInline(
"""
|/src/commonMain/kotlin/pageMerger/Test.kt
|package pageMerger
|
|/src/jsMain/kotlin/pageMerger/Test.kt
|package pageMerger
|
|annotation class DoNotMerge
|
|/src/jvmMain/kotlin/pageMerger/Test.kt
|package pageMerger
|
|annotation class DoNotMerge
""".trimMargin(),
configuration
) {
pagesTransformationStage = {
println(it)
val allChildren = it.childrenRec().filterIsInstance<ClasslikePageNode>()
val jvmClass = allChildren.filter { it.name == "[jvm]DoNotMerge" }
val jsClass = allChildren.filter { it.name == "[js]DoNotMerge" }
val noClass = allChildren.filter { it.name == "DoNotMerge" }
assertTrue(jvmClass.size == 1) { "There can be only one DoNotMerge(jvm) page" }
assertTrue(jvmClass.first().documentables.firstOrNull()?.sourceSets?.single()?.analysisPlatform?.key == "jvm") { "[jvm]DoNotMerge should have only jvm sources" }
assertTrue(jsClass.size == 1) { "There can be only one DoNotMerge(js) page" }
assertTrue(jsClass.first().documentables.firstOrNull()?.sourceSets?.single()?.analysisPlatform?.key == "js") { "[js]DoNotMerge should have only js sources" }
assertTrue(noClass.isEmpty()) { "There can't be any DoNotMerge page" }
}
}
}
@RepeatedTest(3)
fun `should deterministically render same name property extensions`() {
testInline(
"""
|/src/main/kotlin/test/Test.kt
|package test
|
|class ExtensionReceiver
|
|/**
| * Top level val extension
| */
|val ExtensionReceiver.foo: String get() = "bar"
|
|class Obj {
| companion object {
| /**
| * Companion val extension
| */
| val ExtensionReceiver.foo: String get() = "bar"
| }
|}
|
|/src/main/kotlin/test/nestedpackage/Pckg.kt
|package test.nestedpackage
|
|import test.ExtensionReceiver
|
|/**
| * From nested package int val extension
| */
|val ExtensionReceiver.foo: Int get() = 42
""".trimMargin(),
defaultConfiguration
) {
renderingStage = { rootPageNode, _ ->
val extensions = rootPageNode.findExtensionsOfClass("ExtensionReceiver")
extensions.assertContainsKDocsInOrder(
"Top level val extension",
"Companion val extension",
"From nested package int val extension"
)
}
}
}
@RepeatedTest(3)
fun `should deterministically render parameterless same name function extensions`() {
testInline(
"""
|/src/main/kotlin/test/Test.kt
|package test
|
|class ExtensionReceiver
|
|/**
| * Top level fun extension
| */
|fun ExtensionReceiver.bar(): String = "bar"
|
|class Obj {
|
| companion object {
| /**
| * Companion fun extension
| */
| fun ExtensionReceiver.bar(): String = "bar"
| }
|}
|
|/src/main/kotlin/test/nestedpackage/Pckg.kt
|package test.nestedpackage
|
|import test.ExtensionReceiver
|
|/**
| * From nested package fun extension
| */
|fun ExtensionReceiver.bar(): String = "bar"
""".trimMargin(),
defaultConfiguration
) {
renderingStage = { rootPageNode, _ ->
val extensions = rootPageNode.findExtensionsOfClass("ExtensionReceiver")
extensions.assertContainsKDocsInOrder(
"Top level fun extension",
"Companion fun extension",
"From nested package fun extension"
)
}
}
}
@RepeatedTest(3)
fun `should deterministically render same name function extensions with parameters`() {
testInline(
"""
|/src/main/kotlin/test/Test.kt
|package test
|
|class ExtensionReceiver
|
|/**
| * Top level fun extension with one string param
| */
|fun ExtensionReceiver.bar(one: String): String = "bar"
|
|/**
| * Top level fun extension with one int param
| */
|fun ExtensionReceiver.bar(one: Int): Int = 42
|
|class Obj {
|
| companion object {
| /**
| * Companion fun extension with two params
| */
| fun ExtensionReceiver.bar(one: String, two: String): String = "bar"
| }
|}
|
|/src/main/kotlin/test/nestedpackage/Pckg.kt
|package test.nestedpackage
|
|import test.ExtensionReceiver
|
|/**
| * From nested package fun extension with two params
| */
|fun ExtensionReceiver.bar(one: String, two: String): String = "bar"
|
|/**
| * From nested package fun extension with three params
| */
|fun ExtensionReceiver.bar(one: String, two: String, three: String): String = "bar"
|
|/**
| * From nested package fun extension with four params
| */
|fun ExtensionReceiver.bar(one: String, two: String, three: String, four: String): String = "bar"
""".trimMargin(),
defaultConfiguration
) {
renderingStage = { rootPageNode, _ ->
val extensions = rootPageNode.findExtensionsOfClass("ExtensionReceiver")
extensions.assertContainsKDocsInOrder(
"Top level fun extension with one int param",
"Top level fun extension with one string param",
"Companion fun extension with two params",
"From nested package fun extension with two params",
"From nested package fun extension with three params",
"From nested package fun extension with four params"
)
}
}
}
@RepeatedTest(3)
fun `should deterministically render same name function extensions with different receiver and return type`() {
testInline(
"""
|/src/main/kotlin/test/Test.kt
|package test
|
|/**
| * Top level fun extension string
| */
|fun Int.bar(): String = "bar"
|
|/**
| * Top level fun extension int
| */
|fun String.bar(): Int = 42
""".trimMargin(),
defaultConfiguration
) {
renderingStage = { rootPageNode, _ ->
val packageFunctionBlocks = rootPageNode.findPackageFunctionBlocks(packageName = "test")
assertEquals(1, packageFunctionBlocks.size, "Expected to find only one group for the functions")
val functionsBlock = packageFunctionBlocks[0]
functionsBlock.assertContainsKDocsInOrder(
"Top level fun extension string",
"Top level fun extension int"
)
}
}
}
@Test
fun `should not ignore case when grouping by name`() {
testInline(
"""
|/src/main/kotlin/test/Test.kt
|package test
|
|/**
| * Top level fun bAr
| */
|fun Int.bAr(): String = "bar"
|
|/**
| * Top level fun BaR
| */
|fun String.BaR(): Int = 42
""".trimMargin(),
defaultConfiguration
) {
renderingStage = { rootPageNode, _ ->
val packageFunctionBlocks = rootPageNode.findPackageFunctionBlocks(packageName = "test")
assertEquals(2, packageFunctionBlocks.size, "Expected two separate function groups")
val firstGroup = packageFunctionBlocks[0]
firstGroup.assertContainsKDocsInOrder(
"Top level fun BaR",
)
val secondGroup = packageFunctionBlocks[1]
secondGroup.assertContainsKDocsInOrder(
"Top level fun bAr",
)
}
}
}
private fun RootPageNode.findExtensionsOfClass(name: String): ContentDivergentGroup {
val extensionReceiverPage = this.dfs { it is ClasslikePageNode && it.name == name } as ClasslikePageNode
return extensionReceiverPage.content
.dfs { it is ContentDivergentGroup && it.groupID.name == "Extensions" } as ContentDivergentGroup
}
private fun RootPageNode.findPackageFunctionBlocks(packageName: String): List<ContentDivergentGroup> {
val packagePage = this.dfs { it is PackagePage && it.name == packageName } as PackagePage
val packageFunctionTable = packagePage.content.dfs {
it is ContentTable && it.dci.kind == ContentKind.Functions
} as ContentTable
return packageFunctionTable.children.map { packageGroup ->
packageGroup.dfs { it is ContentDivergentGroup } as ContentDivergentGroup
}
}
private fun ContentDivergentGroup.assertContainsKDocsInOrder(vararg expectedKDocs: String) {
expectedKDocs.forEachIndexed { index, expectedKDoc ->
assertEquals(expectedKDoc, this.getElementKDocText(index))
}
}
private fun ContentDivergentGroup.getElementKDocText(index: Int): String {
val element = this.children.getOrNull(index) ?: throw IllegalArgumentException("No element with index $index")
val commentNode = element.after
?.withDescendants()
?.singleOrNull { it is ContentText && it.dci.kind == ContentKind.Comment }
?: throw IllegalStateException("Expected the element to contain a single paragraph of text / comment")
return (commentNode as ContentText).text
}
}
|