From b153740a3740cbda88378bbd6175248c4cb9629f Mon Sep 17 00:00:00 2001 From: Damien O'Hara Date: Sat, 7 Jan 2023 22:48:23 +0800 Subject: [PATCH] suspending block capture test --- testing/build.gradle.kts | 1 + testing/src/test/kotlin/CapturedBlockTests.kt | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/testing/build.gradle.kts b/testing/build.gradle.kts index d4e5dd9..51bbc83 100644 --- a/testing/build.gradle.kts +++ b/testing/build.gradle.kts @@ -16,6 +16,7 @@ application { dependencies { testImplementation(kotlin("test")) + testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4") } tasks.test { diff --git a/testing/src/test/kotlin/CapturedBlockTests.kt b/testing/src/test/kotlin/CapturedBlockTests.kt index 6c2272d..062935a 100644 --- a/testing/src/test/kotlin/CapturedBlockTests.kt +++ b/testing/src/test/kotlin/CapturedBlockTests.kt @@ -1,6 +1,9 @@ import io.koalaql.kapshot.Capturable import io.koalaql.kapshot.CapturedBlock +import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Test +import kotlin.coroutines.resume +import kotlin.coroutines.suspendCoroutine import kotlin.test.assertEquals class CapturedBlockTests { @@ -104,4 +107,23 @@ i""", listOf(1, 2, 3).sourceyMap { it*2 } ) } + + fun interface SuspendTest: Capturable> { + suspend operator fun invoke(): R + + override fun withSource(source: String): SuspendTest = object : SuspendTest by this { + override fun source(): String = source + } + } + + @Test + fun `user defined suspend capture`() { + suspend fun suspends(block: SuspendTest): String = "${block.source()}=${10 + block()})" + + runBlocking { + assertEquals("suspendCoroutine { it.resume(32) }=42)", suspends { + suspendCoroutine { it.resume(32) } + }) + } + } } \ No newline at end of file