kiloparsec/src/commonTest/kotlin/assertThrows.kt

14 lines
504 B
Kotlin

import kotlin.test.fail
inline fun <reified T: Throwable>assertThrows(f: ()->Unit): T {
val name = T::class.simpleName
try {
f()
fail("expected to throw $name but threw nothing")
}
catch(x: Throwable) {
if( x is T ) return x
println("expected to throw $name but instead threw ${x::class.simpleName}: $x\b\n${x.stackTraceToString()}")
fail("expected to throw $name but instead threw ${x::class.simpleName}: $x\b\n${x.stackTraceToString()}")
}
}