From a44a75a6a40c46431e2429108d57d28462910992 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 17 Apr 2012 07:15:07 +0100 Subject: [PATCH] #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) --- libraries/stdlib/src/kotlin/Standard.kt | 18 +++++++++ libraries/stdlib/src/kotlin/test/Test.kt | 8 ++++ libraries/stdlib/test/ExceptionTest.kt | 47 ++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 libraries/stdlib/test/ExceptionTest.kt diff --git a/libraries/stdlib/src/kotlin/Standard.kt b/libraries/stdlib/src/kotlin/Standard.kt index 412d5d6de5f..5ee821b7dc5 100644 --- a/libraries/stdlib/src/kotlin/Standard.kt +++ b/libraries/stdlib/src/kotlin/Standard.kt @@ -8,6 +8,8 @@ import java.util.LinkedHashSet import java.util.TreeSet import java.util.SortedSet import java.util.Comparator +import java.io.PrintWriter +import java.io.PrintStream /** Helper to make jet.Iterator usable in for @@ -96,4 +98,20 @@ public inline fun runnable(action: ()-> Unit): Runnable { action() } } +} + +/** + * Allows a stack trace to be printed from Kotlin's [[Throwable]] + */ +public inline fun Throwable.printStackTrace(writer: PrintWriter): Unit { + val jlt = this as java.lang.Throwable + jlt.printStackTrace(writer) +} + +/** + * Allows a stack trace to be printed from Kotlin's [[Throwable]] + */ +public inline fun Throwable.printStackTrace(stream: PrintStream): Unit { + val jlt = this as java.lang.Throwable + jlt.printStackTrace(stream) } \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/test/Test.kt b/libraries/stdlib/src/kotlin/test/Test.kt index 9abd522b90e..fa544a26837 100644 --- a/libraries/stdlib/src/kotlin/test/Test.kt +++ b/libraries/stdlib/src/kotlin/test/Test.kt @@ -67,6 +67,14 @@ public inline fun assertNotNull(actual: Any?, message: String = "") { asserter.assertNotNull(message, actual) } +/** Asserts that the expression is not null, with an optional message and a function block to process the not-null value */ +public inline fun assertNotNull(actual: T?, message: String = "", block: (T) -> R) { + asserter.assertNotNull(message, actual) + if (actual != null) { + block(actual) + } +} + /** Asserts that the expression is null, with an optional message */ public inline fun assertNull(actual: Any?, message: String = "") { asserter.assertNull(message, actual) diff --git a/libraries/stdlib/test/ExceptionTest.kt b/libraries/stdlib/test/ExceptionTest.kt new file mode 100644 index 00000000000..15dea08327a --- /dev/null +++ b/libraries/stdlib/test/ExceptionTest.kt @@ -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) + } + } +}