Skip to content

Instantly share code, notes, and snippets.

@uzzu
Last active January 10, 2019 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uzzu/58cb8bb12c17605017edd01b8498915a to your computer and use it in GitHub Desktop.
Save uzzu/58cb8bb12c17605017edd01b8498915a to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
/**
* To use test suspending function
* @param expected A conditions of expected exception. Default is null (unchecked)
* @param context coroutine context to use
* @param block testing body
*/
expect fun runTest(
expected: ((Throwable) -> Boolean)? = null,
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> Unit
)
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import kotlin.coroutines.CoroutineContext
import kotlin.test.assertTrue
import kotlin.test.fail
actual fun runTest(
expected: ((Throwable) -> Boolean)?,
context: CoroutineContext,
block: suspend CoroutineScope.() -> Unit
) {
var produced: Throwable? = null
try {
runBlocking(context = context, block = block)
} catch (e: Throwable) {
produced = e
}
if (produced != null) {
if (expected != null) {
assertTrue(expected(produced), "Unexpected exception was produced: $produced")
} else {
throw produced
}
} else {
if (expected != null) {
fail("Expected exception was not produced.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment