aboutsummaryrefslogtreecommitdiff
path: root/plugins/kotlin-as-java/src/test/kotlin/JvmNameTest.kt
blob: 448ca26bc5bacf88610fc53135489d2c7f46ddd3 (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
package kotlinAsJavaPlugin

import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.links.Callable
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.TypeConstructor
import org.jetbrains.dokka.model.Annotations
import org.jetbrains.dokka.model.DClass
import org.jetbrains.dokka.model.isJvmName
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class JvmNameTest : BaseAbstractTest() {
    val configuration = dokkaConfiguration {
        sourceSets {
            sourceSet {
                sourceRoots = listOf("src/")
                classpath += jvmStdlibPath!!
            }
        }
    }

    @Test
    fun `should change name for class containing top level function`() {
        testInline(
            """
            |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
            |@file:JvmName("CustomJvmName")
            |package kotlinAsJavaPlugin
            |fun sample(): String = ""
        """.trimMargin(),
            configuration,
        ) {
            documentablesTransformationStage = { module ->
                val expectedClassLikeDri = DRI(
                    packageName = "kotlinAsJavaPlugin",
                    classNames = "CustomJvmName",
                )
                val classLike = module.packages.flatMap { it.classlikes }.first()
                assertEquals(expectedClassLikeDri, classLike.dri)
                assertEquals("CustomJvmName", classLike.name)
            }
        }
    }

    @Test
    fun `should change name for top level function`() {
        testInline(
            """
            |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
            |@file:JvmName("CustomJvmName")
            |package kotlinAsJavaPlugin
            |@JvmName("jvmSample")
            |fun sample(): String = ""
        """.trimMargin(),
            configuration,
        ) {
            documentablesTransformationStage = { module ->
                val expectedFunctionDri = DRI(
                    packageName = "kotlinAsJavaPlugin",
                    classNames = "CustomJvmName",
                    callable = Callable(
                        "jvmSample",
                        receiver = null,
                        params = emptyList()
                    )
                )
                val function = module.packages.flatMap { it.classlikes }.flatMap { it.functions }.first()
                assertEquals(expectedFunctionDri, function.dri)
                assertEquals("jvmSample", function.name)
            }
        }
    }

    @Test
    fun `should change name of a setter for top level property`() {
        testInline(
            """
            |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
            |@file:JvmName("CustomJvmName")
            |package kotlinAsJavaPlugin
            |@get:JvmName("xd")
            |@set:JvmName("asd")
            |var property: String
            |    get() = ""
            |    set(value) {}
        """.trimMargin(),
            configuration,
        ) {
            documentablesTransformationStage = { module ->
                val expectedSetterDri = DRI(
                    packageName = "kotlinAsJavaPlugin",
                    classNames = "CustomJvmName",
                    callable = Callable(
                        "asd",
                        receiver = null,
                        //Todo this is bad, this should be a type in java, look at the bytecode
                        params = listOf(TypeConstructor("kotlin.String", emptyList()))
                    )
                )
                val function =
                    module.packages.flatMap { it.classlikes }.flatMap { it.functions }.first { it.name == "asd" }
                assertEquals(expectedSetterDri, function.dri)
                assertEquals("asd", function.name)
            }
        }
    }

    @Test
    fun `should change name of a getter for top level property`() {
        testInline(
            """
            |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
            |@file:JvmName("CustomJvmName")
            |package kotlinAsJavaPlugin
            |@get:JvmName("xd")
            |@set:JvmName("asd")
            |var property: String
            |    get() = ""
            |    set(value) {}
        """.trimMargin(),
            configuration,
        ) {
            documentablesTransformationStage = { module ->
                val expectedGetterDri = DRI(
                    packageName = "kotlinAsJavaPlugin",
                    classNames = "CustomJvmName",
                    callable = Callable(
                        "xd",
                        receiver = null,
                        params = emptyList()
                    )
                )
                val function =
                    module.packages.flatMap { it.classlikes }.flatMap { it.functions }.first { it.name == "xd" }
                assertEquals(expectedGetterDri, function.dri)
                assertEquals("xd", function.name)
            }
        }
    }

    @Test
    fun `should leave the name as default if annotation is not provided`() {
        testInline(
            """
            |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
            |package kotlinAsJavaPlugin
            |fun sample(): String = ""
        """.trimMargin(),
            configuration,
        ) {
            documentablesTransformationStage = { module ->
                val expectedClassLikeDri = DRI(
                    packageName = "kotlinAsJavaPlugin",
                    classNames = "SampleKt",
                )
                val classLike = module.packages.flatMap { it.classlikes }.first()
                assertEquals(expectedClassLikeDri, classLike.dri)
                assertEquals("SampleKt", classLike.name)
            }
        }
    }

    @Test
    fun `jvmName extra should be removed after the name swap`() {
        testInline(
            """
            |/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
            |package kotlinAsJavaPlugin
            |@JvmName("CustomJvmName")
            |fun sample(): String = ""
        """.trimMargin(),
            configuration,
        ) {
            documentablesTransformationStage = { module ->
                val classLike = module.packages.flatMap { it.classlikes }.first() as DClass
                assertNull(
                    classLike.extra[Annotations]?.directAnnotations?.flatMap { it.value }
                        ?.map { it.dri }
                        ?.firstOrNull { it.isJvmName() }
                )
            }
        }
    }
}