Skip to content

Commit

Permalink
Improved IRPlugin test structure
Browse files Browse the repository at this point in the history
  • Loading branch information
LachlanMcKee committed Dec 30, 2020
1 parent 59f637a commit dc39bba
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions debuglog-plugin/src/test/kotlin/com/bnorm/debug/log/IrPluginTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import com.strobel.decompiler.PlainTextOutput
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.jetbrains.kotlin.utils.indexOfFirst
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.io.ByteArrayOutputStream
import java.io.PrintStream
Expand Down Expand Up @@ -64,7 +64,7 @@ fun doSomething() {
@Test
fun `IR plugin enabled`() {
testIrPlugin(
isEnabled = true,
isPluginEnabled = true,
expectedGreetMethod = """
public static final String greet(@NotNull final String greeting, @NotNull final String name) {
Intrinsics.checkNotNullParameter((Object)greeting, "greeting");
Expand Down Expand Up @@ -120,7 +120,7 @@ fun doSomething() {
@Test
fun `IR plugin disabled`() {
testIrPlugin(
isEnabled = false,
isPluginEnabled = false,
expectedGreetMethod = """
public static final String greet(@NotNull final String greeting, @NotNull final String name) {
Intrinsics.checkNotNullParameter((Object)greeting, "greeting");
Expand Down Expand Up @@ -148,43 +148,61 @@ fun doSomething() {
}

private fun testIrPlugin(
isEnabled: Boolean,
isPluginEnabled: Boolean,
expectedGreetMethod: String,
expectedDoSomethingMethod: String,
outputVerifyFunc: (List<String>) -> Unit
) {
val result = compile(sourceFile = main, DebugLogComponentRegistrar(isEnabled))
val result = compile(sourceFile = main, DebugLogComponentRegistrar(isPluginEnabled))
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode)

val mainJavaCode = result.javaCode("MainKt")
assertEquals(expectedGreetMethod, methodGetter(mainJavaCode, "public static final String greet"))
assertEquals(expectedDoSomethingMethod, methodGetter(mainJavaCode, "public static final void doSomething"))
result.javaCode("MainKt").also { javaCode ->
assertMethod(expectedGreetMethod, javaCode, "public static final String greet")
assertMethod(expectedDoSomethingMethod, javaCode, "public static final void doSomething")
}

outputVerifyFunc(invokeMain(result, "MainKt").trim().split(System.lineSeparator()))
}

private fun methodGetter(classText: String, methodPrefix: String): String {
val lines = classText.split("\n")
val firstLineIndex = lines.indexOfFirst { it.contains(methodPrefix) }
val indentationCount = lines[firstLineIndex].takeWhile { it == ' ' }.length
private fun assertMethod(expectedMethod: String, javaCode: String, actualMethod: String) {
assertEquals(expectedMethod, fetchMethodByPrefix(javaCode, actualMethod))
}

private fun fetchMethodByPrefix(classText: String, methodSignaturePrefix: String): String {
val classLines = classText.split("\n")
val methodSignaturePredicate: (String) -> Boolean = { line -> line.contains(methodSignaturePrefix) }
val methodFirstLineIndex = classLines.indexOfFirst(methodSignaturePredicate)

check(methodFirstLineIndex != -1) {
"Method with prefix '$methodSignaturePrefix' not found within class:\n$classText"
}

val multiplePrefixMatches = classLines
.indexOfFirst(methodFirstLineIndex + 1, methodSignaturePredicate)
.let { index -> index != -1 }

check(!multiplePrefixMatches) {
"Multiple methods with prefix '$methodSignaturePrefix' found within class:\n$classText"
}

val indentationSize = classLines[methodFirstLineIndex].takeWhile { it == ' ' }.length

var curleyBraceCount = 1
var currentIndex: Int = firstLineIndex + 1
var currentLineIndex: Int = methodFirstLineIndex + 1

while (curleyBraceCount != 0 && currentIndex < lines.lastIndex) {
if (lines[currentIndex].contains("{")) {
while (curleyBraceCount != 0 && currentLineIndex < classLines.lastIndex) {
if (classLines[currentLineIndex].contains("{")) {
curleyBraceCount++
}
if (lines[currentIndex].contains("}")) {
if (classLines[currentLineIndex].contains("}")) {
curleyBraceCount--
}

currentIndex++
currentLineIndex++
}

return lines
.subList(firstLineIndex, currentIndex)
.joinToString("\n") { it.substring(indentationCount) }
return classLines
.subList(methodFirstLineIndex, currentLineIndex)
.joinToString("\n") { it.substring(indentationSize) }
}
}

Expand Down

0 comments on commit dc39bba

Please sign in to comment.