-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPokoCompilerPluginTest.kt
More file actions
248 lines (222 loc) · 9.07 KB
/
Copy pathPokoCompilerPluginTest.kt
File metadata and controls
248 lines (222 loc) · 9.07 KB
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
package dev.drewhamilton.poko
import assertk.all
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.isEqualTo
import com.tschuchort.compiletesting.JvmCompilationResult
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.PluginOption
import com.tschuchort.compiletesting.SourceFile
import java.io.File
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfigurationKey
import org.jetbrains.kotlin.config.JvmTarget
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
@OptIn(ExperimentalCompilerApi::class)
class PokoCompilerPluginTest {
@JvmField
@Rule var temporaryFolder: TemporaryFolder = TemporaryFolder()
@Test fun `compilation of valid class succeeds`() {
testCompilation("api/Primitives")
}
@Test fun `compilation with value interface succeeds`() {
testCompilation("api/DataInterface")
}
@Test fun `compilation with multiple interfaces succeeds`() {
testCompilation("api/MultipleInterface")
}
@Test fun `compilation of interface fails`() {
testCompilation(
"illegal/Interface",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR
) { result ->
val expectedLocation = "Interface.kt:6:17"
val expectedMessage = "Poko can only be applied to a class"
assertThat(result.messages).all {
contains(expectedLocation)
contains(expectedMessage)
}
}
}
@Test fun `compilation of data class fails`() {
testCompilation(
"illegal/Data",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR
) { result ->
val expectedLocation = "Data.kt:6:7"
assertThat(result.messages).all {
contains(expectedLocation)
contains("Poko cannot be applied to a data class")
}
}
}
@Test fun `compilation of value class fails`() {
testCompilation(
"illegal/Value",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR
) { result ->
val expectedLocation = "Value.kt:6:18"
assertThat(result.messages).all {
contains(expectedLocation)
contains("Poko cannot be applied to a value class")
}
}
}
@Test fun `compilation without primary constructor fails`() {
testCompilation(
"illegal/NoPrimaryConstructor",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR
) { result ->
val expectedLocation = "NoPrimaryConstructor.kt:6:13"
assertThat(result.messages).all {
contains(expectedLocation)
contains("Poko class must have a primary constructor")
}
}
}
@Test fun `compilation without constructor properties fails`() {
testCompilation(
"illegal/NoConstructorProperties",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR
) { result ->
val expectedLocation = "NoConstructorProperties.kt:6:13"
assertThat(result.messages).all {
contains(expectedLocation)
contains("Poko class primary constructor must have at least one not-skipped property")
}
}
}
@Test fun `compilation of inner class fails`() {
testCompilation(
"illegal/OuterClass",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR
) { result ->
val expectedLocation = "OuterClass.kt:8:11"
assertThat(result.messages).all {
contains(expectedLocation)
contains("Poko cannot be applied to an inner class")
}
}
}
//region Array content
@Test fun `compilation reading array content of generic type with unsupported upper bound fails`() {
testCompilation(
"illegal/GenericArrayHolder",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR
) { result ->
assertThat(result.messages)
.contains("@ReadArrayContent is only supported on properties with array type or `Any` type")
}
}
@Test fun `compilation reading array content of non-arrays fails`() {
testCompilation(
"illegal/NotArrayHolder",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR
) { result ->
assertThat(result.messages)
.contains("@ReadArrayContent is only supported on properties with array type or `Any` type")
assertThat(result.messages)
.contains("@ReadArrayContent is only supported on properties with array type or `Any` type")
assertThat(result.messages)
.contains("@ReadArrayContent is only supported on properties with array type or `Any` type")
}
}
//endregion
@Test fun `unknown annotation name produces expected error message`() {
testCompilation(
"api/Primitives",
pokoAnnotationName = "nonexistent/ClassName",
expectedExitCode = KotlinCompilation.ExitCode.COMPILATION_ERROR,
) {
assertThat(it.messages).contains("e: Could not find class <nonexistent/ClassName>${System.lineSeparator()}")
}
}
@OptIn(CompilerConfiguration.Internals::class)
@Test fun `fir IDE mode option parses all values`() {
val commandLineProcessor = PokoCommandLineProcessor()
val option = commandLineProcessor.pluginOptions.single {
it.optionName == CompilerOptions.FIR_IDE_MODE.toString()
}
FirIdeMode.entries.forEach { firIdeMode ->
val configuration = CompilerConfiguration()
commandLineProcessor.processOption(option, firIdeMode.name, configuration)
assertThat(configuration.get(CompilerOptions.FIR_IDE_MODE)).isEqualTo(firIdeMode)
}
}
private inline fun testCompilation(
vararg sourceFileNames: String,
pokoAnnotationName: String = "dev/drewhamilton/poko/Poko",
expectedExitCode: KotlinCompilation.ExitCode = KotlinCompilation.ExitCode.OK,
additionalTesting: (JvmCompilationResult) -> Unit = {}
) = testCompilation(
*sourceFileNames.map { SourceFile.fromPath("src/test/resources/$it.kt") }.toTypedArray(),
pokoAnnotationName = pokoAnnotationName,
expectedExitCode = expectedExitCode,
additionalTesting = additionalTesting
)
private inline fun testCompilation(
vararg sourceFiles: SourceFile,
pokoAnnotationName: String,
expectedExitCode: KotlinCompilation.ExitCode = KotlinCompilation.ExitCode.OK,
additionalTesting: (JvmCompilationResult) -> Unit = {}
) {
val result = prepareCompilation(
*sourceFiles,
pokoAnnotationName = pokoAnnotationName,
).compile()
if (
expectedExitCode == KotlinCompilation.ExitCode.OK &&
result.exitCode != KotlinCompilation.ExitCode.OK
) {
val failureMessage = StringBuilder().apply {
append("expected: OK\nbut was : ")
append(result.exitCode)
append("\n")
result.messages.split("\n").forEach { message ->
if (message.isNotEmpty()) {
append("- ")
append(message)
append("\n")
}
}
}.toString()
fail(failureMessage)
} else {
assertThat(result.exitCode).isEqualTo(expectedExitCode)
}
additionalTesting(result)
}
private fun prepareCompilation(
vararg sourceFiles: SourceFile,
pokoAnnotationName: String,
) = KotlinCompilation().apply {
workingDir = temporaryFolder.root
compilerPluginRegistrars = listOf(PokoCompilerPluginRegistrar())
inheritClassPath = true
sources = sourceFiles.asList()
verbose = false
jvmTarget = JvmTarget.JVM_1_8.description
optIn = emptyList()
val commandLineProcessor = PokoCommandLineProcessor()
commandLineProcessors = listOf(commandLineProcessor)
pluginOptions = listOfNotNull(
commandLineProcessor.option(CompilerOptions.ENABLED, true),
commandLineProcessor.option(CompilerOptions.POKO_ANNOTATION, pokoAnnotationName),
)
}
private fun CommandLineProcessor.option(
key: CompilerConfigurationKey<*>,
value: Any?,
) = PluginOption(
pluginId,
key.toString(),
value.toString()
)
@Suppress("DEPRECATION") // Safe as long as we don't write new files at runtime
private fun SourceFile.Companion.fromPath(path: String): SourceFile = fromPath(File(path))
}