Remove coroutinesPackage from tests, since it is kotlin.coroutines
anyway. Simplify createTextForHelpers: remove experimental coroutines version.
This commit is contained in:
committed by
TeamCityServer
parent
c868116535
commit
5617d83c6b
@@ -11,7 +11,6 @@ import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import java.util.regex.Matcher
|
||||
import java.util.regex.Pattern
|
||||
import java.io.File
|
||||
|
||||
private const val MODULE_DELIMITER = ",\\s*"
|
||||
// These patterns are copies from
|
||||
@@ -34,8 +33,7 @@ fun buildCompileList(source: Path, outputDirectory: String): List<TestFile> {
|
||||
val srcText = srcFile.readText().replace(Regex("<!.*?!>(.*?)<!>")) { match -> match.groupValues[1] }
|
||||
|
||||
if (srcText.contains("// WITH_COROUTINES")) {
|
||||
result.add(TestFile("helpers.kt", "$outputDirectory/helpers.kt",
|
||||
createTextForHelpers(true), TestModule.support))
|
||||
result.add(TestFile("helpers.kt", "$outputDirectory/helpers.kt", createTextForHelpers(), TestModule.support))
|
||||
}
|
||||
|
||||
val matcher = FILE_OR_MODULE_PATTERN.matcher(srcText)
|
||||
|
||||
+69
-133
@@ -7,138 +7,74 @@ package org.jetbrains.kotlin
|
||||
|
||||
// This is almost a full copy of kotlin/compiler/tests-common/tests/org/jetbrains/kotlin/coroutineTestUtil.kt
|
||||
// TODO: get it automatically as a dependency
|
||||
fun createTextForHelpers(isReleaseCoroutines: Boolean): String {
|
||||
val coroutinesPackage = "kotlin.coroutines"
|
||||
fun createTextForHelpers(): String = """
|
||||
package helpers
|
||||
import kotlin.coroutines.*
|
||||
|
||||
val emptyContinuationBody =
|
||||
if (isReleaseCoroutines)
|
||||
"""
|
||||
|override fun resumeWith(result: Result<Any?>) {
|
||||
| result.getOrThrow()
|
||||
|}
|
||||
""".trimMargin()
|
||||
else
|
||||
"""
|
||||
|override fun resume(data: Any?) {}
|
||||
|override fun resumeWithException(exception: Throwable) { throw exception }
|
||||
""".trimMargin()
|
||||
|
||||
val handleResultContinuationBody =
|
||||
if (isReleaseCoroutines)
|
||||
"""
|
||||
|override fun resumeWith(result: Result<T>) {
|
||||
| x(result.getOrThrow())
|
||||
|}
|
||||
""".trimMargin()
|
||||
else
|
||||
"""
|
||||
|override fun resumeWithException(exception: Throwable) {
|
||||
| throw exception
|
||||
|}
|
||||
|
|
||||
|override fun resume(data: T) = x(data)
|
||||
""".trimMargin()
|
||||
|
||||
val handleExceptionContinuationBody =
|
||||
if (isReleaseCoroutines)
|
||||
"""
|
||||
|override fun resumeWith(result: Result<Any?>) {
|
||||
| result.exceptionOrNull()?.let(x)
|
||||
|}
|
||||
""".trimMargin()
|
||||
else
|
||||
"""
|
||||
|override fun resumeWithException(exception: Throwable) {
|
||||
| x(exception)
|
||||
|}
|
||||
|
|
||||
|override fun resume(data: Any?) {}
|
||||
""".trimMargin()
|
||||
|
||||
val continuationAdapterBody =
|
||||
if (isReleaseCoroutines)
|
||||
"""
|
||||
|override fun resumeWith(result: Result<T>) {
|
||||
| if (result.isSuccess) {
|
||||
| resume(result.getOrThrow())
|
||||
| } else {
|
||||
| resumeWithException(result.exceptionOrNull()!!)
|
||||
| }
|
||||
|}
|
||||
|
|
||||
|abstract fun resumeWithException(exception: Throwable)
|
||||
|abstract fun resume(value: T)
|
||||
""".trimMargin()
|
||||
else
|
||||
""
|
||||
|
||||
return """
|
||||
|package helpers
|
||||
|import $coroutinesPackage.*
|
||||
|
|
||||
|fun <T> handleResultContinuation(x: (T) -> Unit): Continuation<T> = object: Continuation<T> {
|
||||
| override val context = EmptyCoroutineContext
|
||||
| $handleResultContinuationBody
|
||||
|}
|
||||
|
|
||||
|
|
||||
|fun handleExceptionContinuation(x: (Throwable) -> Unit): Continuation<Any?> = object: Continuation<Any?> {
|
||||
| override val context = EmptyCoroutineContext
|
||||
| $handleExceptionContinuationBody
|
||||
|}
|
||||
|
|
||||
|open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
| companion object : EmptyContinuation()
|
||||
| $emptyContinuationBody
|
||||
|}
|
||||
|
|
||||
|abstract class ContinuationAdapter<in T> : Continuation<T> {
|
||||
| override val context: CoroutineContext = EmptyCoroutineContext
|
||||
| $continuationAdapterBody
|
||||
|}
|
||||
|class StateMachineCheckerClass {
|
||||
| private var counter = 0
|
||||
| var finished = false
|
||||
|
|
||||
| var proceed: () -> Unit = {}
|
||||
|
|
||||
| fun reset() {
|
||||
| counter = 0
|
||||
| finished = false
|
||||
| proceed = {}
|
||||
| }
|
||||
|
|
||||
| suspend fun suspendHere() = suspendCoroutine<Unit> { c ->
|
||||
| counter++
|
||||
| proceed = { c.resume(Unit) }
|
||||
| }
|
||||
|
|
||||
| fun check(numberOfSuspensions: Int, checkFinished: Boolean = true) {
|
||||
| for (i in 1..numberOfSuspensions) {
|
||||
| if (counter != i) error("Wrong state-machine generated: suspendHere called should be called exactly once in one state. Expected " + i + ", got " + counter)
|
||||
| proceed()
|
||||
| }
|
||||
| if (counter != numberOfSuspensions)
|
||||
| error("Wrong state-machine generated: suspendHere called should be called exactly once in one state. Expected " + numberOfSuspensions + ", got " + counter)
|
||||
| if (finished) error("Wrong state-machine generated: it is finished early")
|
||||
| proceed()
|
||||
| if (checkFinished && !finished) error("Wrong state-machine generated: it is not finished yet")
|
||||
| }
|
||||
|}
|
||||
|val StateMachineChecker = StateMachineCheckerClass()
|
||||
|object CheckStateMachineContinuation: ContinuationAdapter<Unit>() {
|
||||
| override val context: CoroutineContext
|
||||
| get() = EmptyCoroutineContext
|
||||
|
|
||||
| override fun resume(value: Unit) {
|
||||
| StateMachineChecker.proceed = {
|
||||
| StateMachineChecker.finished = true
|
||||
| }
|
||||
| }
|
||||
|
|
||||
| override fun resumeWithException(exception: Throwable) {
|
||||
| throw exception
|
||||
| }
|
||||
|}
|
||||
""".trimMargin()
|
||||
fun <T> handleResultContinuation(x: (T) -> Unit): Continuation<T> = object: Continuation<T> {
|
||||
override val context = EmptyCoroutineContext
|
||||
|
||||
override fun resumeWith(result: Result<T>) {
|
||||
x(result.getOrThrow())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun handleExceptionContinuation(x: (Throwable) -> Unit): Continuation<Any?> = object: Continuation<Any?> {
|
||||
override val context = EmptyCoroutineContext
|
||||
|
||||
override fun resumeWith(result: Result<Any?>) {
|
||||
result.exceptionOrNull()?.let(x)
|
||||
}
|
||||
}
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
|
||||
override fun resumeWith(result: Result<Any?>) {
|
||||
result.getOrThrow()
|
||||
}
|
||||
}
|
||||
|
||||
class StateMachineCheckerClass {
|
||||
private var counter = 0
|
||||
var finished = false
|
||||
|
||||
var proceed: () -> Unit = {}
|
||||
|
||||
fun reset() {
|
||||
counter = 0
|
||||
finished = false
|
||||
proceed = {}
|
||||
}
|
||||
|
||||
suspend fun suspendHere() = suspendCoroutine<Unit> { c ->
|
||||
counter++
|
||||
proceed = { c.resume(Unit) }
|
||||
}
|
||||
|
||||
fun check(numberOfSuspensions: Int, checkFinished: Boolean = true) {
|
||||
for (i in 1..numberOfSuspensions) {
|
||||
if (counter != i) error("Wrong state-machine generated: suspendHere called should be called exactly once in one state. Expected " + i + ", got " + counter)
|
||||
proceed()
|
||||
}
|
||||
if (counter != numberOfSuspensions)
|
||||
error("Wrong state-machine generated: suspendHere called should be called exactly once in one state. Expected " + numberOfSuspensions + ", got " + counter)
|
||||
if (finished) error("Wrong state-machine generated: it is finished early")
|
||||
proceed()
|
||||
if (checkFinished && !finished) error("Wrong state-machine generated: it is not finished yet")
|
||||
}
|
||||
}
|
||||
val StateMachineChecker = StateMachineCheckerClass()
|
||||
object CheckStateMachineContinuation: Continuation<Unit>() {
|
||||
override val context: CoroutineContext
|
||||
get() = EmptyCoroutineContext
|
||||
|
||||
override fun resumeWith(result: Result<Unit>) {
|
||||
result.getOrThrow()
|
||||
StateMachineChecker.proceed = {
|
||||
StateMachineChecker.finished = true
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user