Skip to content

Commit

Permalink
fix global state by not using System.out
Browse files Browse the repository at this point in the history
  • Loading branch information
christophsturm committed Oct 5, 2022
1 parent fd6551a commit 811640f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package com.bnorm.power
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import org.jetbrains.kotlin.name.FqName
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.fail

class DebugFunctionTest {
@Test
Expand Down Expand Up @@ -71,7 +71,7 @@ private fun executeMainDebug(mainBody: String): String {
fun <T> dbg(value: T): T = value
fun <T> dbg(value: T, msg: String): T {
println(msg)
throw RuntimeException("result:"+msg)
return value
}
Expand All @@ -87,15 +87,18 @@ fun main() {

val kClazz = result.classLoader.loadClass("MainKt")
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
val prevOut = System.out
return getMainResult(main)
}

fun getMainResult(main: Method): String {
try {
val out = ByteArrayOutputStream()
System.setOut(PrintStream(out))
main.invoke(null)
return out.toString("UTF-8")
fail("main did not throw expected exception")
} catch (t: InvocationTargetException) {
with(t.cause) {
if (this is RuntimeException && message != null && message!!.startsWith("result:"))
return message!!.substringAfter("result:")
}
throw t.cause!!
} finally {
System.setOut(prevOut)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ class InfixFunctionTest {
)
}

private fun run(file: SourceFile, fqNames: Set<FqName>): String {
private fun run(file: SourceFile, fqNames: Set<FqName>, main: String = "MainKt"): String {
val result = compile(listOf(file), PowerAssertComponentRegistrar(fqNames))
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode, "Failed with messages: " + result.messages)

val kClazz = result.classLoader.loadClass("MainKt")
val kClazz = result.classLoader.loadClass(main)
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
try {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ package com.bnorm.power
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import org.jetbrains.kotlin.name.FqName
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.lang.reflect.InvocationTargetException
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down Expand Up @@ -140,7 +137,7 @@ private fun executeMainDebug(mainBody: String): String {
fun <T> dbg(key: Any, value: T): T = value
fun <T> dbg(key: Any, value: T, msg: String): T {
println(key.toString() + "=" + value + "\n" + msg)
throw RuntimeException("result:"+key.toString() + "=" + value + "\n" + msg)
return value
}
Expand All @@ -156,15 +153,5 @@ fun main() {

val kClazz = result.classLoader.loadClass("MainKt")
val main = kClazz.declaredMethods.single { it.name == "main" && it.parameterCount == 0 }
val prevOut = System.out
try {
val out = ByteArrayOutputStream()
System.setOut(PrintStream(out))
main.invoke(null)
return out.toString("UTF-8")
} catch (t: InvocationTargetException) {
throw t.cause!!
} finally {
System.setOut(prevOut)
}
return getMainResult(main)
}

0 comments on commit 811640f

Please sign in to comment.