Skip to content

Commit 1dfd6de

Browse files
qwwdfsadpablobaxter
authored andcommitted
Introduce TestResult into TestBase as the first preparation step to m… (Kotlin#2827)
* Introduce TestResult into TestBase as the first preparation step to multiplatform tests * The goal of this change is to test our IDE and tooling tolerance to hack-ish TestResult
1 parent 84c65a8 commit 1dfd6de

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

kotlinx-coroutines-core/common/test/TestBase.common.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import kotlin.test.*
1313
public expect val isStressTest: Boolean
1414
public expect val stressTestMultiplier: Int
1515

16+
/**
17+
* The result of a multiplatform asynchronous test.
18+
* Aliases into Unit on K/JVM and K/N, and into Promise on K/JS.
19+
*/
20+
@Suppress("NO_ACTUAL_FOR_EXPECT")
21+
public expect class TestResult
22+
1623
public expect open class TestBase constructor() {
1724
/*
1825
* In common tests we emulate parameterized tests
@@ -33,7 +40,7 @@ public expect open class TestBase constructor() {
3340
expected: ((Throwable) -> Boolean)? = null,
3441
unhandled: List<(Throwable) -> Boolean> = emptyList(),
3542
block: suspend CoroutineScope.() -> Unit
36-
)
43+
): TestResult
3744
}
3845

3946
public suspend inline fun hang(onCancellation: () -> Unit) {

kotlinx-coroutines-core/common/test/flow/operators/FlatMapMergeBaseTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ abstract class FlatMapMergeBaseTest : FlatMapBaseTest() {
9090
}
9191

9292
@Test
93-
abstract fun testFlatMapConcurrency()
93+
abstract fun testFlatMapConcurrency(): TestResult
9494
}

kotlinx-coroutines-core/js/test/PromiseTest.kt

+13-1
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ class PromiseTest : TestBase() {
7474
assertSame(d2, deferred)
7575
assertEquals("OK", d2.await())
7676
}
77-
}
77+
78+
@Test
79+
fun testLeverageTestResult(): TestResult {
80+
// Cannot use expect(..) here
81+
var seq = 0
82+
val result = runTest {
83+
++seq
84+
}
85+
return result.then {
86+
if (seq != 1) error("Unexpected result: $seq")
87+
}
88+
}
89+
}

kotlinx-coroutines-core/js/test/TestBase.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import kotlin.js.*
99
public actual val isStressTest: Boolean = false
1010
public actual val stressTestMultiplier: Int = 1
1111

12+
@Suppress("ACTUAL_WITHOUT_EXPECT", "ACTUAL_TYPE_ALIAS_TO_CLASS_WITH_DECLARATION_SITE_VARIANCE")
13+
public actual typealias TestResult = Promise<Unit>
14+
1215
public actual open class TestBase actual constructor() {
1316
public actual val isBoundByJsTestTimeout = true
1417
private var actionIndex = 0
@@ -77,7 +80,7 @@ public actual open class TestBase actual constructor() {
7780
expected: ((Throwable) -> Boolean)? = null,
7881
unhandled: List<(Throwable) -> Boolean> = emptyList(),
7982
block: suspend CoroutineScope.() -> Unit
80-
): dynamic {
83+
): TestResult {
8184
var exCount = 0
8285
var ex: Throwable? = null
8386
/*

kotlinx-coroutines-core/jvm/test/TestBase.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public actual val stressTestMultiplier = stressTestMultiplierSqrt * stressTestMu
3030

3131
public val stressTestMultiplierCbrt = cbrt(stressTestMultiplier.toDouble()).roundToInt()
3232

33+
@Suppress("ACTUAL_WITHOUT_EXPECT")
34+
public actual typealias TestResult = Unit
35+
3336
/**
3437
* Base class for tests, so that tests for predictable scheduling of actions in multiple coroutines sharing a single
3538
* thread can be written. Use it like this:
@@ -188,7 +191,7 @@ public actual open class TestBase actual constructor() {
188191
expected: ((Throwable) -> Boolean)? = null,
189192
unhandled: List<(Throwable) -> Boolean> = emptyList(),
190193
block: suspend CoroutineScope.() -> Unit
191-
) {
194+
): TestResult {
192195
var exCount = 0
193196
var ex: Throwable? = null
194197
try {

kotlinx-coroutines-core/native/test/TestBase.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ package kotlinx.coroutines
77
public actual val isStressTest: Boolean = false
88
public actual val stressTestMultiplier: Int = 1
99

10+
@Suppress("ACTUAL_WITHOUT_EXPECT")
11+
public actual typealias TestResult = Unit
12+
1013
public actual open class TestBase actual constructor() {
1114
public actual val isBoundByJsTestTimeout = false
1215
private var actionIndex = 0
@@ -71,7 +74,7 @@ public actual open class TestBase actual constructor() {
7174
expected: ((Throwable) -> Boolean)? = null,
7275
unhandled: List<(Throwable) -> Boolean> = emptyList(),
7376
block: suspend CoroutineScope.() -> Unit
74-
) {
77+
): TestResult {
7578
var exCount = 0
7679
var ex: Throwable? = null
7780
try {

0 commit comments

Comments
 (0)