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.")
        }
    }
}