<lambda>null1 package kotlinx.coroutines.testing
2
3 import kotlinx.coroutines.*
4 import kotlin.test.*
5 import kotlin.js.*
6
7 actual typealias NoJs = Ignore
8
9 actual val VERBOSE = false
10
11 actual val isStressTest: Boolean = false
12 actual val stressTestMultiplier: Int = 1
13 actual val stressTestMultiplierSqrt: Int = 1
14
15 @Suppress("ACTUAL_WITHOUT_EXPECT", "ACTUAL_TYPE_ALIAS_TO_CLASS_WITH_DECLARATION_SITE_VARIANCE")
16 actual typealias TestResult = Promise<Unit>
17
18 internal actual fun lastResortReportException(error: Throwable) {
19 println(error)
20 console.log(error)
21 }
22
23 actual open class TestBase(
24 private val errorCatching: ErrorCatching.Impl
25 ): OrderedExecutionTestBase(), ErrorCatching by errorCatching {
26 private var lastTestPromise: Promise<*>? = null
27
28 actual constructor(): this(errorCatching = ErrorCatching.Impl())
29
printlnnull30 actual fun println(message: Any?) {
31 kotlin.io.println(message)
32 }
33
runTestnull34 actual fun runTest(
35 expected: ((Throwable) -> Boolean)?,
36 unhandled: List<(Throwable) -> Boolean>,
37 block: suspend CoroutineScope.() -> Unit
38 ): TestResult {
39 var exCount = 0
40 var ex: Throwable? = null
41 /*
42 * This is an additional sanity check against `runTest` mis-usage on JS.
43 * The only way to write an async test on JS is to return Promise from the test function.
44 * _Just_ launching promise and returning `Unit` won't suffice as the underlying test framework
45 * won't be able to detect an asynchronous failure in a timely manner.
46 * We cannot detect such situations, but we can detect the most common erroneous pattern
47 * in our code base, an attempt to use multiple `runTest` in the same `@Test` method,
48 * which typically is a premise to the same error:
49 * ```
50 * @Test
51 * fun incorrectTestForJs() { // <- promise is not returned
52 * for (parameter in parameters) {
53 * runTest {
54 * runTestForParameter(parameter)
55 * }
56 * }
57 * }
58 * ```
59 */
60 if (lastTestPromise != null) {
61 error("Attempt to run multiple asynchronous test within one @Test method")
62 }
63 val result = GlobalScope.promise(block = block, context = CoroutineExceptionHandler { _, e ->
64 if (e is CancellationException) return@CoroutineExceptionHandler // are ignored
65 exCount++
66 when {
67 exCount > unhandled.size ->
68 error("Too many unhandled exceptions $exCount, expected ${unhandled.size}, got: $e", e)
69 !unhandled[exCount - 1](e) ->
70 error("Unhandled exception was unexpected: $e", e)
71 }
72 }).catch { e ->
73 ex = e
74 if (expected != null) {
75 if (!expected(e)) {
76 console.log(e)
77 error("Unexpected exception $e", e)
78 }
79 } else
80 throw e
81 }.finally {
82 if (ex == null && expected != null) error("Exception was expected but none produced")
83 if (exCount < unhandled.size)
84 error("Too few unhandled exceptions $exCount, expected ${unhandled.size}")
85 errorCatching.close()
86 checkFinishCall()
87 }
88 lastTestPromise = result
89 return result
90 }
91 }
92
93 actual val isNative = false
94
95 actual val isBoundByJsTestTimeout = true
96
97 actual val isJavaAndWindows: Boolean get() = false
98