1 /*
<lambda>null2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5 @file:Suppress("unused")
6
7 package kotlinx.coroutines
8
9 import kotlinx.coroutines.flow.*
10 import kotlinx.coroutines.internal.*
11 import kotlin.coroutines.*
12 import kotlin.test.*
13
14 public expect val isStressTest: Boolean
15 public expect val stressTestMultiplier: Int
16 public expect val stressTestMultiplierSqrt: Int
17
18 /**
19 * The result of a multiplatform asynchronous test.
20 * Aliases into Unit on K/JVM and K/N, and into Promise on K/JS.
21 */
22 @Suppress("NO_ACTUAL_FOR_EXPECT")
23 public expect class TestResult
24
25 public expect val isNative: Boolean
26
27 public expect open class TestBase constructor() {
28 /*
29 * In common tests we emulate parameterized tests
30 * by iterating over parameters space in the single @Test method.
31 * This kind of tests is too slow for JS and does not fit into
32 * the default Mocha timeout, so we're using this flag to bail-out
33 * and run such tests only on JVM and K/N.
34 */
35 public val isBoundByJsTestTimeout: Boolean
36 public fun error(message: Any, cause: Throwable? = null): Nothing
37 public fun expect(index: Int)
38 public fun expectUnreached()
39 public fun finish(index: Int)
40 public fun ensureFinished() // Ensures that 'finish' was invoked
41 public fun reset() // Resets counter and finish flag. Workaround for parametrized tests absence in common
42
43 public fun runTest(
44 expected: ((Throwable) -> Boolean)? = null,
45 unhandled: List<(Throwable) -> Boolean> = emptyList(),
46 block: suspend CoroutineScope.() -> Unit
47 ): TestResult
48 }
49
hangnull50 public suspend inline fun hang(onCancellation: () -> Unit) {
51 try {
52 suspendCancellableCoroutine<Unit> { }
53 } finally {
54 onCancellation()
55 }
56 }
57
assertFailsWithnull58 public inline fun <reified T : Throwable> assertFailsWith(block: () -> Unit) {
59 try {
60 block()
61 error("Should not be reached")
62 } catch (e: Throwable) {
63 assertTrue(e is T)
64 }
65 }
66
assertFailsWithnull67 public suspend inline fun <reified T : Throwable> assertFailsWith(flow: Flow<*>) {
68 try {
69 flow.collect()
70 fail("Should be unreached")
71 } catch (e: Throwable) {
72 assertTrue(e is T, "Expected exception ${T::class}, but had $e instead")
73 }
74 }
75
accnull76 public suspend fun Flow<Int>.sum() = fold(0) { acc, value -> acc + value }
accnull77 public suspend fun Flow<Long>.longSum() = fold(0L) { acc, value -> acc + value }
78
79
80 // data is added to avoid stacktrace recovery because CopyableThrowable is not accessible from common modules
81 public class TestException(message: String? = null, private val data: Any? = null) : Throwable(message)
82 public class TestException1(message: String? = null, private val data: Any? = null) : Throwable(message)
83 public class TestException2(message: String? = null, private val data: Any? = null) : Throwable(message)
84 public class TestException3(message: String? = null, private val data: Any? = null) : Throwable(message)
85 public class TestCancellationException(message: String? = null, private val data: Any? = null) : CancellationException(message)
86 public class TestRuntimeException(message: String? = null, private val data: Any? = null) : RuntimeException(message)
87 public class RecoverableTestException(message: String? = null) : RuntimeException(message)
88 public class RecoverableTestCancellationException(message: String? = null) : CancellationException(message)
89
wrapperDispatchernull90 public fun wrapperDispatcher(context: CoroutineContext): CoroutineContext {
91 val dispatcher = context[ContinuationInterceptor] as CoroutineDispatcher
92 return object : CoroutineDispatcher() {
93 override fun isDispatchNeeded(context: CoroutineContext): Boolean =
94 dispatcher.isDispatchNeeded(context)
95 override fun dispatch(context: CoroutineContext, block: Runnable) =
96 dispatcher.dispatch(context, block)
97 }
98 }
99
wrapperDispatchernull100 public suspend fun wrapperDispatcher(): CoroutineContext = wrapperDispatcher(coroutineContext)
101
102 class BadClass {
103 override fun equals(other: Any?): Boolean = error("equals")
104 override fun hashCode(): Int = error("hashCode")
105 override fun toString(): String = error("toString")
106 }
107
108 public expect val isJavaAndWindows: Boolean
109