Last active
January 10, 2019 03:18
-
-
Save uzzu/58cb8bb12c17605017edd01b8498915a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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