xref: /aosp_15_r20/external/kotlinx.coroutines/test-utils/native/src/TestBase.kt (revision 7a7160fed73afa6648ef8aa100d4a336fe921d9a)

<lambda>null1 package kotlinx.coroutines.testing
2 
3 import kotlin.test.*
4 import kotlinx.coroutines.*
5 
6 actual val VERBOSE = false
7 
8 actual typealias NoNative = Ignore
9 
10 public actual val isStressTest: Boolean = false
11 public actual val stressTestMultiplier: Int = 1
12 public actual val stressTestMultiplierSqrt: Int = 1
13 
14 @Suppress("ACTUAL_WITHOUT_EXPECT")
15 public actual typealias TestResult = Unit
16 
17 internal actual fun lastResortReportException(error: Throwable) {
18     println(error)
19 }
20 
<lambda>null21 public actual open class TestBase actual constructor(): OrderedExecutionTestBase(), ErrorCatching by ErrorCatching.Impl() {
22     actual fun println(message: Any?) {
23         kotlin.io.println(message)
24     }
25 
26     public actual fun runTest(
27         expected: ((Throwable) -> Boolean)?,
28         unhandled: List<(Throwable) -> Boolean>,
29         block: suspend CoroutineScope.() -> Unit
30     ): TestResult {
31         var exCount = 0
32         var ex: Throwable? = null
33         try {
34             runBlocking(block = block, context = CoroutineExceptionHandler { _, e ->
35                 if (e is CancellationException) return@CoroutineExceptionHandler // are ignored
36                 exCount++
37                 when {
38                     exCount > unhandled.size ->
39                         error("Too many unhandled exceptions $exCount, expected ${unhandled.size}, got: $e", e)
40                     !unhandled[exCount - 1](e) ->
41                         error("Unhandled exception was unexpected: $e", e)
42                 }
43             })
44         } catch (e: Throwable) {
45             ex = e
46             if (expected != null) {
47                 if (!expected(e))
48                     error("Unexpected exception: $e", e)
49             } else
50                 throw e
51         } finally {
52             if (ex == null && expected != null) error("Exception was expected but none produced")
53         }
54         if (exCount < unhandled.size)
55             error("Too few unhandled exceptions $exCount, expected ${unhandled.size}")
56     }
57 }
58 
59 public actual val isNative = true
60 
61 public actual val isBoundByJsTestTimeout = false
62 
63 public actual val isJavaAndWindows: Boolean get() = false
64