#KT-1785 Fixed. adds printStrackTrace(writer|stream) to Throwable so that the intrinsic Throwable behaves more like java.lang.Throwable. (Though not totally sure why we don't just siwzzle Throwable -> java.lang.Throwable like we do for Collection et al)
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package test.collections
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
import java.util.*
|
||||
import org.junit.Test as test
|
||||
import java.io.PrintWriter
|
||||
import java.io.*
|
||||
|
||||
class ExceptionTest {
|
||||
|
||||
test fun printStackTraceOnRuntimeException() {
|
||||
assertPrintStackTrace(RuntimeException("Crikey!"))
|
||||
assertPrintStackTraceStream(RuntimeException("Crikey2"))
|
||||
}
|
||||
|
||||
test fun printStackTraceOnError() {
|
||||
assertPrintStackTrace(Error("Oh dear"))
|
||||
assertPrintStackTraceStream(Error("Oh dear2"))
|
||||
}
|
||||
|
||||
|
||||
fun assertPrintStackTrace(t: Throwable) {
|
||||
val buffer = StringWriter()
|
||||
val writer = PrintWriter(buffer)
|
||||
t.printStackTrace(writer)
|
||||
println(buffer)
|
||||
}
|
||||
|
||||
fun assertPrintStackTraceStream(t: Throwable) {
|
||||
val byteBuffer = ByteArrayOutputStream()
|
||||
/*
|
||||
// TODO compiler error
|
||||
PrintStream(byteBuffer).use {
|
||||
t.printStackTrace(it)
|
||||
}
|
||||
*/
|
||||
val stream = PrintStream(byteBuffer)
|
||||
stream.use {
|
||||
t.printStackTrace(stream)
|
||||
}
|
||||
|
||||
assertNotNull(byteBuffer.toByteArray()) { bytes ->
|
||||
assertTrue(bytes.size > 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user