Some tests on callable references on suspend functions

This commit is contained in:
Igor Chevdar
2019-07-18 16:50:27 +03:00
parent 11e2fd5074
commit 26c664f3b1
5 changed files with 138 additions and 0 deletions
+20
View File
@@ -1695,6 +1695,26 @@ task coroutines_anonymousObject(type: KonanLocalTest) {
source = "codegen/coroutines/anonymousObject.kt"
}
task coroutines_functionReference_simple(type: KonanLocalTest) {
goldValue = "42\n"
source = "codegen/coroutines/functionReference_simple.kt"
}
task coroutines_functionReference_eqeq_name(type: KonanLocalTest) {
goldValue = ""
source = "codegen/coroutines/functionReference_eqeq_name.kt"
}
task coroutines_functionReference_invokeAsFunction(type: KonanLocalTest) {
goldValue = "159\n"
source = "codegen/coroutines/functionReference_invokeAsFunction.kt"
}
task coroutines_functionReference_lambdaAsSuspendLambda(type: KonanLocalTest) {
goldValue = ""
source = "codegen/coroutines/functionReference_lambdaAsSuspendLambda.kt"
}
task AbstractMutableCollection(type: KonanLocalTest) {
expectedExitStatus = 0
source = "runtime/collections/AbstractMutableCollection.kt"
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package codegen.coroutines.functionReference_eqeq_name
import kotlin.test.*
suspend fun foo(x: Int) = x
class Foo(val x: Int) {
suspend fun bar() = x
}
@Test fun runTest() {
val ref1 = ::foo
val rec = Foo(42)
val ref2 = rec::bar
val ref3 = ::foo
val ref4 = Foo(42)::bar
val ref5 = rec::bar
val ref6 = Foo::bar
assertEquals("foo", ref1.name)
assertEquals("bar", ref2.name)
assertEquals("bar", ref6.name)
assertFalse(ref1 == ref2)
assertTrue(ref1 == ref3)
assertFalse(ref2 == ref4)
assertTrue(ref2 == ref5)
assertFalse(ref6 == ref2)
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package codegen.coroutines.functionReference_invokeAsFunction
import kotlin.test.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
companion object : EmptyContinuation()
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
}
class Foo(val x: Int) {
suspend fun bar(y: Int) = foo(y) + x
}
suspend fun foo(x: Int) = x
@Test fun runTest() {
val ref = Foo(42)::bar
println((ref as Function2<Int, Continuation<Int>, Any?>)(117, EmptyContinuation))
}
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package codegen.coroutines.functionReference_lambdaAsSuspendLambda
import kotlin.test.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
fun foo(block: (Continuation<Unit>) -> Any?) {
block as (suspend () -> Unit)
}
@Test fun runTest() {
foo {}
}
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package codegen.coroutines.functionReference_simple
import kotlin.test.*
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
companion object : EmptyContinuation()
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
}
suspend fun suspendHere(): Int = suspendCoroutineUninterceptedOrReturn { x ->
x.resume(42)
COROUTINE_SUSPENDED
}
suspend fun foo(x: suspend () -> Int) = x()
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
@Test fun runTest() {
var result = 0
val ref = ::suspendHere
builder {
result = foo(ref)
}
println(result)
}