aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/model/ExtensionsTest.kt
blob: f0f86d7f59fecda9454e5211d215da4f3ae05dc9 (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
package model

import org.jetbrains.dokka.base.transformers.documentables.CallableExtensions
import org.jetbrains.dokka.model.DClass
import org.jetbrains.dokka.model.DFunction
import org.jetbrains.dokka.model.DInterface
import org.jetbrains.dokka.model.Documentable
import org.jetbrains.dokka.model.properties.WithExtraProperties
import utils.AbstractModelTest
import kotlin.test.Test
import utils.UsingJDK

class ExtensionsTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "classes") {
    private fun <T : WithExtraProperties<R>, R : Documentable> T.checkExtension(name: String = "extension") =
        with(extra[CallableExtensions]?.extensions) {
            this notNull "extensions"
            this counts 1
            (this?.single() as? DFunction)?.name equals name
        }

    @Test
    fun `should be extension for subclasses`() {
        inlineModelTest(
            """
            |open class A
            |open class B: A()
            |open class C: B()
            |open class D: C()
            |fun B.extension() = ""
            """
        ) {
            with((this / "classes" / "B").cast<DClass>()) {
                checkExtension()
            }
            with((this / "classes" / "C").cast<DClass>()) {
                checkExtension()
            }
            with((this / "classes" / "D").cast<DClass>()) {
                checkExtension()
            }
            with((this / "classes" / "A").cast<DClass>()) {
                extra[CallableExtensions] equals null
            }
        }
    }

    @Test
    fun `should be extension for interfaces`() {
        inlineModelTest(
            """
            |interface I
            |interface I2 : I
            |open class A: I2
            |fun I.extension() = ""
            """
        ) {

            with((this / "classes" / "A").cast<DClass>()) {
                checkExtension()
            }
            with((this / "classes" / "I2").cast<DInterface>()) {
                checkExtension()
            }
            with((this / "classes" / "I").cast<DInterface>()) {
                checkExtension()
            }
        }
    }

    @UsingJDK
    @Test
    fun `should be extension for external classes`() {
        inlineModelTest(
            """
            |abstract class A<T>: AbstractList<T>()
            |fun<T> AbstractCollection<T>.extension() {}
            |
            |class B:Exception()
            |fun Throwable.extension() = ""
            """
        ) {
            with((this / "classes" / "A").cast<DClass>()) {
                checkExtension()
            }
            with((this / "classes" / "B").cast<DClass>()) {
                checkExtension()
            }
        }
    }

    @Test
    fun `should be extension for typealias`() {
        inlineModelTest(
            """
            |open class A
            |open class B: A()
            |open class C: B()
            |open class D: C()
            |typealias B2 = B
            |fun B2.extension() = ""
            """
        ) {
            with((this / "classes" / "B").cast<DClass>()) {
                checkExtension()
            }
            with((this / "classes" / "C").cast<DClass>()) {
                checkExtension()
            }
            with((this / "classes" / "D").cast<DClass>()) {
                checkExtension()
            }
            with((this / "classes" / "A").cast<DClass>()) {
                extra[CallableExtensions] equals null
            }
        }
    }

    @Test
    fun `should be extension for java classes`() {
        val testConfiguration = dokkaConfiguration {
            suppressObviousFunctions = false
            sourceSets {
                sourceSet {
                    sourceRoots = listOf("src/main/kotlin/")
                    classpath += jvmStdlibPath!!
                }
            }
        }
        testInline(
            """
            |/src/main/kotlin/classes/Test.kt
            | package classes
            | fun A.extension() = ""
            | 
            |/src/main/kotlin/classes/A.java
            | package classes;
            | public class A {}
            | 
            | /src/main/kotlin/classes/B.java
            | package classes;
            | public class B extends A {}
            """,
            configuration = testConfiguration
        ) {
            documentablesTransformationStage = {
                it.run {
                    with((this / "classes" / "B").cast<DClass>()) {
                        checkExtension()
                    }
                    with((this / "classes" / "A").cast<DClass>()) {
                        checkExtension()
                    }
                }
            }
        }
    }
}