aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/transformers/DivisionSwitchTest.kt
blob: fec5fc479fe49d8bc3c3160ba188be14efd8cb6f (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
/*
 * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

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.dfs
import org.jetbrains.dokka.pages.ClasslikePageNode
import org.jetbrains.dokka.pages.ContentHeader
import org.jetbrains.dokka.pages.ContentNode
import org.jetbrains.dokka.pages.ContentText
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class DivisionSwitchTest : BaseAbstractTest() {

    private val query = """
            |/src/source0.kt
            package package0
            /** 
            * Documentation for ClassA 
            */
            class ClassA {
                val A: String = "A"
                fun a() {}
                fun b() {}
            }
            
            /src/source1.kt
            package package0
            /**
            * Documentation for ClassB
            */
            class ClassB : ClassA() {
                val B: String = "B"
                fun d() {}
                fun e() {}
            }
        """.trimMargin()

    private fun configuration(switchOn: Boolean) = dokkaConfiguration {
        sourceSets {
            sourceSet {
                sourceRoots = listOf("src/")
            }
        }
        suppressObviousFunctions = false
        pluginsConfigurations.add(
            PluginConfigurationImpl(
                DokkaBase::class.qualifiedName!!,
                DokkaConfiguration.SerializationFormat.JSON,
                """{ "separateInheritedMembers": $switchOn }""",
            )
        )
    }

    private fun testClassB(switchOn: Boolean, operation: (ClasslikePageNode) -> Unit) {
        testInline(
            query,
            configuration(switchOn),
            cleanupOutput = true
        ) {
            pagesTransformationStage = { root ->
                val classB = root.dfs { it.name == "ClassB" } as? ClasslikePageNode
                assertNotNull(classB, "Tested class not found!")
                operation(classB)
            }
        }
    }

    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 not split inherited and regular methods`() {
        testClassB(false) { classB ->
            val functions = classB.findSectionWithName("Functions")
            assertNotNull(functions, "Functions not found!")
            assertEquals(7, functions.children.size, "Incorrect number of functions found")
        }
    }

    @Test
    fun `should not split inherited and regular properties`() {
        testClassB(false) { classB ->
            val properties = classB.findSectionWithName("Properties")
            assertNotNull(properties, "Properties not found!")
            assertEquals(2, properties.children.size, "Incorrect number of properties found")
        }
    }

    @Test
    fun `should split inherited and regular methods`() {
        testClassB(true) { classB ->
            val functions = classB.findSectionWithName("Functions")
            val inheritedFunctions = classB.findSectionWithName("Inherited functions")
            assertNotNull(functions, "Functions not found!")
            assertEquals(2, functions.children.size, "Incorrect number of functions found")
            assertNotNull(inheritedFunctions, "Inherited functions not found!")
            assertEquals(5, inheritedFunctions.children.size, "Incorrect number of inherited functions found")
        }
    }

    @Test
    fun `should split inherited and regular properties`() {
        testClassB(true) { classB ->
            val properties = classB.findSectionWithName("Properties")
            assertNotNull(properties, "Properties not found!")
            assertEquals(1, properties.children.size, "Incorrect number of properties found")
            val inheritedProperties = classB.findSectionWithName("Inherited properties")
            assertNotNull(inheritedProperties, "Inherited properties not found!")
            assertEquals(1, inheritedProperties.children.size, "Incorrect number of inherited properties found")
        }
    }
}