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
|
package kotlinAsJavaPlugin
import org.jetbrains.dokka.base.testApi.testRunner.BaseAbstractTest
import org.jetbrains.dokka.model.AdditionalModifiers
import org.jetbrains.dokka.model.ExtraModifiers
import org.jetbrains.dokka.model.JavaVisibility
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
class JvmFieldTest : BaseAbstractTest() {
val configuration = dokkaConfiguration {
sourceSets {
sourceSet {
sourceRoots = listOf("src/")
classpath += jvmStdlibPath!!
}
}
}
@Test
fun `should keep properties annotated with JvmField as properties`() {
testInline(
"""
|/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
|package kotlinAsJavaPlugin
|class SampleClass(@JvmField val property: String, val otherProperty: String)
""".trimMargin(),
configuration,
) {
documentablesTransformationStage = { module ->
val classLike = module.packages.flatMap { it.classlikes }.first()
assertNotNull(classLike.properties.firstOrNull { it.name == "property" })
assertEquals(
listOf("getOtherProperty"),
classLike.functions.map { it.name })
}
}
}
@Test
fun `should work for top-level property`(){
testInline(
"""
|/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
|package kotlinAsJavaPlugin
|@JvmField
|val property: String = TODO()
""".trimMargin(),
configuration,
) {
documentablesTransformationStage = { module ->
val classLike = module.packages.flatMap { it.classlikes }.first()
assertNotNull(classLike.properties.firstOrNull { it.name == "property" })
assertEquals(
emptyList(),
classLike.functions.map { it.name })
}
}
}
@Test
fun `properties without JvmName should be kept private`() {
testInline(
"""
|/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
|package kotlinAsJavaPlugin
|class SampleClass(val property: String)
""".trimMargin(),
configuration,
) {
documentablesTransformationStage = { module ->
val classLike = module.packages.flatMap { it.classlikes }.first()
assertEquals(JavaVisibility.Private, classLike.properties.firstOrNull()?.visibility?.values?.first())
assertNotNull(classLike.functions.firstOrNull { it.name.startsWith("get") })
assertEquals(
JavaVisibility.Public,
classLike.functions.first { it.name.startsWith("get") }.visibility.values.first()
)
}
}
}
@Test
fun `object jvmfield property should have no getters`(){
testInline(
"""
|/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
|package kotlinAsJavaPlugin
|object MyObject {
| @JvmField
| val property: String = TODO()
|}
""".trimMargin(),
configuration,
) {
documentablesTransformationStage = { module ->
val classLike = module.packages.flatMap { it.classlikes }.first()
val property = classLike.properties.singleOrNull { it.name == "property" }
assertNotNull(property)
assertEquals(
emptyList(),
classLike.functions.map { it.name }
)
assertNull(property.getter)
assertNull(property.setter)
}
}
}
@Test
fun `enum jvmfield property should have no getters`(){
testInline(
"""
|/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
|package kotlinAsJavaPlugin
|enum class MyEnum {
| ITEM;
|
| @JvmField
| val property: String = TODO()
|}
""".trimMargin(),
configuration,
) {
documentablesTransformationStage = { module ->
val classLike = module.packages.flatMap { it.classlikes }.first()
val property = classLike.properties.singleOrNull { it.name == "property" }
assertNotNull(property)
assertEquals(
emptyList(),
classLike.functions
.filter{ it.name.contains("property", ignoreCase = true) }
.map { it.name }
)
assertNull(property.getter)
assertNull(property.setter)
}
}
}
@Test
fun `object jvmfield property should be static`(){
testInline(
"""
|/src/main/kotlin/kotlinAsJavaPlugin/sample.kt
|package kotlinAsJavaPlugin
|object MyObject {
| @JvmField
| val property: String = TODO()
|}
""".trimMargin(),
configuration,
) {
documentablesTransformationStage = { module ->
val classLike = module.packages.flatMap { it.classlikes }.first()
val property = classLike.properties.singleOrNull { it.name == "property" }
assertNotNull(property)
val extra = property.extra[AdditionalModifiers]
assertNotNull(extra, "Additional modifiers for property are exist")
assertTrue(extra.content.values.contains(setOf(ExtraModifiers.JavaOnlyModifiers.Static)),
"Extra modifiers contains static")
}
}
}
}
|