TraceLog/compiler-plugin/src/test/kotlin/dev/msfjarvis/tracelog/compiler/plugin/DebugLogTransformerTest.kt

97 lines
2.9 KiB
Kotlin

package dev.msfjarvis.tracelog.compiler.plugin
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode
import com.tschuchort.compiletesting.SourceFile.Companion.kotlin
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.nio.charset.StandardCharsets
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertInstanceOf
import org.junit.jupiter.api.Test
@OptIn(ExperimentalCompilerApi::class)
class DebugLogTransformerTest {
private val standardOut = System.out
private val outputStreamCaptor = ByteArrayOutputStream()
@AfterEach
fun tearDown() {
System.setOut(standardOut)
}
@Test
fun `compiler plugin successfully transforms code`() {
val srcFile =
kotlin(
"SourceFile.kt",
"""
import ${BuildConfig.KOTLIN_PLUGIN_GROUP}.annotations.DebugLog
@DebugLog
fun transformable() {
println("In a transformable function!")
}
@DebugLog
fun transformableWithReturnValue(): String {
println("In a transformable function!")
return "Return value!"
}
fun nonTransformable() {
println("Not in a transformable function!")
}
class TracingTest {
@DebugLog
fun transformableInClass() {
println("In a transformable function!")
}
fun nonTransformable() {
println("Not in a transformable function!")
}
}
"""
.trimIndent())
val result =
KotlinCompilation()
.apply {
sources = listOf(srcFile)
compilerPluginRegistrars = listOf(TracingCompilerPluginRegistrar())
commandLineProcessors = listOf(TracingCommandLineProcessor())
noOptimize = true
inheritClassPath = true
messageOutputStream = System.out
}
.compile()
assertEquals(ExitCode.OK, result.exitCode)
System.setOut(PrintStream(outputStreamCaptor))
val kClazz = result.classLoader.loadClass("SourceFileKt")
val transformableWithReturnValue =
kClazz.declaredMethods.first { it.name == "transformableWithReturnValue" }
val retVal = transformableWithReturnValue.invoke(null)
assertInstanceOf(String::class.java, retVal)
assertEquals("Return value!", retVal)
assertEquals(
"""
⇢ transformableWithReturnValue()
In a transformable function!
⇠ transformableWithReturnValue [] = Return value!
"""
.trimIndent(),
outputStreamCaptor
.toString(StandardCharsets.UTF_8)
.trim()
.replace("\\[.*]".toRegex(), "[]"),
)
}
}