Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 6 | |
Roman Elizarov | 1bb8c1c | 2018-01-11 10:39:46 +0300 | [diff] [blame] | 7 | import kotlin.js.* |
| 8 | |
Nikita Koval | 8fa07b5 | 2019-09-27 18:26:03 +0300 | [diff] [blame] | 9 | public actual val isStressTest: Boolean = false |
| 10 | public actual val stressTestMultiplier: Int = 1 |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 11 | |
Vsevolod Tolstopyatov | a3429f7 | 2021-07-16 16:02:36 +0300 | [diff] [blame] | 12 | @Suppress("ACTUAL_WITHOUT_EXPECT", "ACTUAL_TYPE_ALIAS_TO_CLASS_WITH_DECLARATION_SITE_VARIANCE") |
| 13 | public actual typealias TestResult = Promise<Unit> |
| 14 | |
Nikita Koval | 8fa07b5 | 2019-09-27 18:26:03 +0300 | [diff] [blame] | 15 | public actual open class TestBase actual constructor() { |
Vsevolod Tolstopyatov | 2571516 | 2021-07-16 14:38:58 +0300 | [diff] [blame] | 16 | public actual val isBoundByJsTestTimeout = true |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 17 | private var actionIndex = 0 |
| 18 | private var finished = false |
| 19 | private var error: Throwable? = null |
Vsevolod Tolstopyatov | 2571516 | 2021-07-16 14:38:58 +0300 | [diff] [blame] | 20 | private var lastTestPromise: Promise<*>? = null |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Throws [IllegalStateException] like `error` in stdlib, but also ensures that the test will not |
| 24 | * complete successfully even if this exception is consumed somewhere in the test. |
| 25 | */ |
Ilya Gorbunov | f0cd180 | 2018-04-17 19:59:31 +0300 | [diff] [blame] | 26 | @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 27 | public actual fun error(message: Any, cause: Throwable? = null): Nothing { |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 28 | if (cause != null) console.log(cause) |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 29 | val exception = IllegalStateException( |
| 30 | if (cause == null) message.toString() else "$message; caused by $cause") |
| 31 | if (error == null) error = exception |
| 32 | throw exception |
| 33 | } |
| 34 | |
Roman Elizarov | 7587eba | 2018-07-25 12:22:46 +0300 | [diff] [blame] | 35 | private fun printError(message: String, cause: Throwable) { |
| 36 | if (error == null) error = cause |
| 37 | println("$message: $cause") |
| 38 | console.log(cause) |
| 39 | } |
| 40 | |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 41 | /** |
| 42 | * Asserts that this invocation is `index`-th in the execution sequence (counting from one). |
| 43 | */ |
| 44 | public actual fun expect(index: Int) { |
| 45 | val wasIndex = ++actionIndex |
| 46 | check(index == wasIndex) { "Expecting action index $index but it is actually $wasIndex" } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Asserts that this line is never executed. |
| 51 | */ |
| 52 | public actual fun expectUnreached() { |
| 53 | error("Should not be reached") |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Asserts that this it the last action in the test. It must be invoked by any test that used [expect]. |
| 58 | */ |
| 59 | public actual fun finish(index: Int) { |
| 60 | expect(index) |
| 61 | check(!finished) { "Should call 'finish(...)' at most once" } |
| 62 | finished = true |
| 63 | } |
| 64 | |
Vsevolod Tolstopyatov | fe820ba | 2019-04-24 17:14:03 +0300 | [diff] [blame] | 65 | /** |
| 66 | * Asserts that [finish] was invoked |
| 67 | */ |
| 68 | public actual fun ensureFinished() { |
| 69 | require(finished) { "finish(...) should be caller prior to this check" } |
| 70 | } |
| 71 | |
Vsevolod Tolstopyatov | 732474f | 2018-07-20 11:36:20 +0300 | [diff] [blame] | 72 | public actual fun reset() { |
| 73 | check(actionIndex == 0 || finished) { "Expecting that 'finish(...)' was invoked, but it was not" } |
| 74 | actionIndex = 0 |
| 75 | finished = false |
| 76 | } |
| 77 | |
Ilya Gorbunov | f0cd180 | 2018-04-17 19:59:31 +0300 | [diff] [blame] | 78 | @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 79 | public actual fun runTest( |
| 80 | expected: ((Throwable) -> Boolean)? = null, |
| 81 | unhandled: List<(Throwable) -> Boolean> = emptyList(), |
| 82 | block: suspend CoroutineScope.() -> Unit |
Vsevolod Tolstopyatov | a3429f7 | 2021-07-16 16:02:36 +0300 | [diff] [blame] | 83 | ): TestResult { |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 84 | var exCount = 0 |
| 85 | var ex: Throwable? = null |
Vsevolod Tolstopyatov | 2571516 | 2021-07-16 14:38:58 +0300 | [diff] [blame] | 86 | /* |
| 87 | * This is an additional sanity check against `runTest` mis-usage on JS. |
| 88 | * The only way to write an async test on JS is to return Promise from the test function. |
| 89 | * _Just_ launching promise and returning `Unit` won't suffice as the underlying test framework |
| 90 | * won't be able to detect an asynchronous failure in a timely manner. |
| 91 | * We cannot detect such situations, but we can detect the most common erroneous pattern |
| 92 | * in our code base, an attempt to use multiple `runTest` in the same `@Test` method, |
| 93 | * which typically is a premise to the same error: |
| 94 | * ``` |
| 95 | * @Test |
| 96 | * fun incorrectTestForJs() { // <- promise is not returned |
| 97 | * for (parameter in parameters) { |
| 98 | * runTest { |
| 99 | * runTestForParameter(parameter) |
| 100 | * } |
| 101 | * } |
| 102 | * } |
| 103 | * ``` |
| 104 | */ |
| 105 | if (lastTestPromise != null) { |
| 106 | error("Attempt to run multiple asynchronous test within one @Test method") |
| 107 | } |
Vsevolod Tolstopyatov | 8baa736 | 2021-08-02 12:55:35 +0300 | [diff] [blame] | 108 | val result = GlobalScope.promise(block = block, context = CoroutineExceptionHandler { _, e -> |
Roman Elizarov | 1bb8c1c | 2018-01-11 10:39:46 +0300 | [diff] [blame] | 109 | if (e is CancellationException) return@CoroutineExceptionHandler // are ignored |
| 110 | exCount++ |
Roman Elizarov | 7587eba | 2018-07-25 12:22:46 +0300 | [diff] [blame] | 111 | when { |
| 112 | exCount > unhandled.size -> |
| 113 | printError("Too many unhandled exceptions $exCount, expected ${unhandled.size}, got: $e", e) |
| 114 | !unhandled[exCount - 1](e) -> |
| 115 | printError("Unhandled exception was unexpected: $e", e) |
| 116 | } |
Roman Elizarov | 1bb8c1c | 2018-01-11 10:39:46 +0300 | [diff] [blame] | 117 | }).catch { e -> |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 118 | ex = e |
| 119 | if (expected != null) { |
| 120 | if (!expected(e)) |
| 121 | error("Unexpected exception", e) |
| 122 | } else |
| 123 | throw e |
Roman Elizarov | 1bb8c1c | 2018-01-11 10:39:46 +0300 | [diff] [blame] | 124 | }.finally { |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 125 | if (ex == null && expected != null) error("Exception was expected but none produced") |
Roman Elizarov | 1bb8c1c | 2018-01-11 10:39:46 +0300 | [diff] [blame] | 126 | if (exCount < unhandled.size) |
| 127 | error("Too few unhandled exceptions $exCount, expected ${unhandled.size}") |
| 128 | error?.let { throw it } |
| 129 | check(actionIndex == 0 || finished) { "Expecting that 'finish(...)' was invoked, but it was not" } |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 130 | } |
Vsevolod Tolstopyatov | 2571516 | 2021-07-16 14:38:58 +0300 | [diff] [blame] | 131 | lastTestPromise = result |
| 132 | return result |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 133 | } |
| 134 | } |
Roman Elizarov | 1bb8c1c | 2018-01-11 10:39:46 +0300 | [diff] [blame] | 135 | |
| 136 | private fun <T> Promise<T>.finally(block: () -> Unit): Promise<T> = |
| 137 | then(onFulfilled = { value -> block(); value }, onRejected = { ex -> block(); throw ex }) |