Skip to content

Commit

Permalink
suspending block capture test
Browse files Browse the repository at this point in the history
  • Loading branch information
mfwgenerics committed Jan 7, 2023
1 parent d5d8d3b commit b153740
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ application {

dependencies {
testImplementation(kotlin("test"))
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
}

tasks.test {
Expand Down
22 changes: 22 additions & 0 deletions testing/src/test/kotlin/CapturedBlockTests.kt
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -104,4 +107,23 @@ i""",
listOf(1, 2, 3).sourceyMap { it*2 }
)
}

fun interface SuspendTest<R>: Capturable<SuspendTest<R>> {
suspend operator fun invoke(): R

override fun withSource(source: String): SuspendTest<R> = object : SuspendTest<R> by this {
override fun source(): String = source
}
}

@Test
fun `user defined suspend capture`() {
suspend fun suspends(block: SuspendTest<Int>): String = "${block.source()}=${10 + block()})"

runBlocking {
assertEquals("suspendCoroutine { it.resume(32) }=42)", suspends {
suspendCoroutine { it.resume(32) }
})
}
}
}

0 comments on commit b153740

Please sign in to comment.