kotlin.test: Make inline methods 'todo' and 'currentStackTrace' @InlineOnly not to loose stack trace.

Move related tests to separate file and verify the line numbers.
#KT-11348 Fixed
This commit is contained in:
Ilya Gorbunov
2016-03-04 03:34:05 +03:00
parent c3a1643c6c
commit 92003f626b
3 changed files with 33 additions and 15 deletions
@@ -36,11 +36,18 @@ inline fun <reified T : Throwable> assertFailsWith(message: String? = null, noin
* Comments out a [block] of test code until it is implemented while keeping a link to the code
* to implement in your unit test output
*/
@kotlin.internal.InlineOnly
inline fun todo(@Suppress("UNUSED_PARAMETER") block: () -> Unit) {
System.out.println("TODO at " + currentStackTrace()[1])
System.out.println("TODO at " + currentStackTrace()[0])
}
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE")
/**
* Returns an array of stack trace elements, each representing one stack frame.
* The first element of the array (assuming the array is not empty) represents the top of the
* stack, which is the place where [currentStackTrace] function was called from.
*/
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
@kotlin.internal.InlineOnly
inline fun currentStackTrace() = (java.lang.Exception() as java.lang.Throwable).stackTrace
/**
@@ -1,4 +1,4 @@
package kotlinx.testing.tests
package kotlin.test.tests
import kotlin.test.*
import org.junit.*
@@ -31,16 +31,4 @@ class BasicAssertionsJVMTest {
throw IllegalArgumentException()
})
}
@Test
fun testToDo() {
todo {
fail("Shouldn't pass here")
}
}
@Test
fun testCurrentStackTrace() {
assertEquals("BasicAssertionsJVMTest.kt", currentStackTrace()[0].fileName)
}
}
@@ -0,0 +1,23 @@
package kotlin.test.tests
import org.junit.Test
import kotlin.test.*
// NOTE: These tests verify line numbers of stack frames, so they might be quite fragile to reordering etc
class StackTraceJVMTest {
@Test
fun testCurrentStackTrace() {
/* <-- line number */ val topFrame = currentStackTrace()[0]
assertEquals("StackTraceJVMTest.kt", topFrame.fileName)
assertEquals(12, topFrame.lineNumber)
}
@Test
fun testToDo() {
todo {
fail("Shouldn't pass here")
}
}
}