Skip to content

Commit da0fd7a

Browse files
committed
Added tests for KCallable<*>.callSuspend
1 parent eacc252 commit da0fd7a

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tests.callSuspend.bigArity
2+
3+
import helpers.*
4+
import kotlinx.reflect.lite.impl.*
5+
import kotlin.coroutines.*
6+
7+
8+
fun builder(c: suspend () -> Unit) {
9+
c.startCoroutine(EmptyContinuation)
10+
}
11+
12+
class A {
13+
suspend fun foo(
14+
p00: Long = 0, p01: A = A(), p02: A = A(), p03: A = A(), p04: A = A(), p05: A = A(), p06: A = A(), p07: A = A(), p08: A = A(), p09: A = A(),
15+
p10: A = A(), p11: A = A(), p12: A = A(), p13: A = A(), p14: A = A(), p15: A = A(), p16: A = A(), p17: A = A(), p18: A = A(), p19: A = A(),
16+
p20: A = A(), p21: A = A(), p22: A = A(), p23: A = A(), p24: A = A(), p25: A = A(), p26: A = A(), p27: A = A(), p28: A = A(), p29: String
17+
): String {
18+
return p29 + p00
19+
}
20+
}
21+
22+
fun box(): String {
23+
val a = A()
24+
var res = "FAIL 1"
25+
builder {
26+
res = (A::class.java).kotlin.members.single { it.name == "foo" }.callSuspend(a, 1L, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, "OK") as String
27+
}
28+
if (res != "OK1") return res
29+
return "OK"
30+
}
31+
32+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package tests.callSuspend.callSuspend
2+
3+
// WITH_COROUTINES
4+
// WITH_REFLECT
5+
// TARGET_BACKEND: JVM
6+
7+
import helpers.*
8+
import kotlinx.reflect.lite.*
9+
import kotlinx.reflect.lite.impl.*
10+
import kotlinx.reflect.lite.tests.*
11+
import kotlin.coroutines.*
12+
import kotlin.coroutines.intrinsics.*
13+
14+
fun builder(c: suspend () -> Unit) {
15+
c.startCoroutine(EmptyContinuation)
16+
}
17+
18+
class A {
19+
suspend fun noArgs() = "OK"
20+
21+
suspend fun twoArgs(a: String, b: String) = "$a$b"
22+
}
23+
24+
suspend fun twoArgs(a: String, b: String) = "$a$b"
25+
26+
fun ordinary() = "OK"
27+
28+
var log = ""
29+
30+
var proceed = {}
31+
32+
suspend fun suspendHere() = suspendCoroutineUninterceptedOrReturn<Unit> { cont ->
33+
proceed = {
34+
cont.resumeWith(Result.success(Unit))
35+
}
36+
COROUTINE_SUSPENDED
37+
}
38+
39+
suspend fun suspending() {
40+
log += "before;"
41+
suspendHere()
42+
log += "after;"
43+
}
44+
45+
fun box(): String {
46+
var res: String? = ""
47+
builder {
48+
res = (A::class.java).kotlin.members.find { it.name == "noArgs" }?.callSuspend(A()) as String?
49+
}
50+
if (res != "OK") return res ?: "FAIL 1"
51+
builder {
52+
res = (A::class.java).kotlin.members.find { it.name == "twoArgs" }?.callSuspend(A(), "O", "K") as String?
53+
}
54+
if (res != "OK") return res ?: "FAIL 2"
55+
builder {
56+
res = Class.forName("tests.callSuspend.callSuspend.CallSuspendKt").kotlinPackage.getMemberByName("twoArgs").callSuspend("O", "K") as String?
57+
}
58+
if (res != "OK") return res ?: "FAIL 3"
59+
builder {
60+
res = Class.forName("tests.callSuspend.callSuspend.CallSuspendKt").kotlinPackage.getMemberByName("ordinary").callSuspend() as String?
61+
}
62+
builder {
63+
Class.forName("tests.callSuspend.callSuspend.CallSuspendKt").kotlinPackage.getMemberByName("suspending").callSuspend()
64+
}
65+
log += "suspended;"
66+
proceed()
67+
if (log != "before;suspended;after;") return log
68+
return res ?: "FAIL 4"
69+
}
70+
71+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package tests.callSuspend.primitiveSuspendFunctions
2+
3+
import kotlin.coroutines.startCoroutine
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertFailsWith
6+
import helpers.*
7+
import kotlinx.reflect.lite.impl.*
8+
9+
inline class Z(val value: Int)
10+
11+
class C {
12+
private var value: Z = Z(0)
13+
14+
suspend fun nonNullConsume(z: Z) { value = z }
15+
suspend fun nonNullProduce(): Z = value
16+
suspend fun nullableConsume(z: Z?) { value = z!! }
17+
suspend fun nullableProduce(): Z? = value
18+
suspend fun nonNull_nonNullConsumeAndProduce(z: Z): Z = z
19+
suspend fun nonNull_nullableConsumeAndProduce(z: Z): Z? = z
20+
suspend fun nullable_nonNullConsumeAndProduce(z: Z?): Z = z!!
21+
suspend fun nullable_nullableConsumeAndProduce(z: Z?): Z? = z
22+
}
23+
24+
private fun run0(f: suspend () -> Int): Int {
25+
var result = -1
26+
f.startCoroutine(handleResultContinuation { result = it })
27+
return result
28+
}
29+
30+
fun box(): String {
31+
val c = C()
32+
33+
assertFailsWith<IllegalArgumentException>("Remove assertFailsWith and try again, as this problem may have been fixed.") {
34+
run0 {
35+
val nonNullConsume = (C::class.java).kotlin.members.single { it.name == "nonNullConsume" }
36+
val nonNullProduce = (C::class.java).kotlin.members.single { it.name == "nonNullProduce" }
37+
nonNullConsume.callSuspend(c, Z(1))
38+
(nonNullProduce.callSuspend(c) as Z).value
39+
}.let { assertEquals(1, it) }
40+
}
41+
42+
run0 {
43+
val nullableConsume = (C::class.java).kotlin.members.single { it.name == "nullableConsume" }
44+
nullableConsume.callSuspend(c, Z(2))
45+
val nullableProduce = (C::class.java).kotlin.members.single { it.name == "nullableProduce" }
46+
(nullableProduce.callSuspend(c)!! as Z).value
47+
}.let { assertEquals(2, it) }
48+
49+
assertFailsWith<IllegalArgumentException>("Remove assertFailsWith and try again, as this problem may have been fixed.") {
50+
run0 {
51+
val nonNull_nonNullConsumeAndProduce = (C::class.java).kotlin.members.single { it.name == "nonNull_nonNullConsumeAndProduce" }
52+
(nonNull_nonNullConsumeAndProduce.callSuspend(c, Z(3)) as Z).value
53+
}.let { assertEquals(3, it) }
54+
}
55+
56+
run0 {
57+
val nonNull_nullableConsumeAndProduce = (C::class.java).kotlin.members.single { it.name == "nonNull_nullableConsumeAndProduce" }
58+
(nonNull_nullableConsumeAndProduce.callSuspend(c, Z(4))!! as Z).value
59+
}.let { assertEquals(4, it) }
60+
61+
assertFailsWith<IllegalArgumentException>("Remove assertFailsWith and try again, as this problem may have been fixed.") {
62+
run0 {
63+
val nullable_nonNullConsumeAndProduce = (C::class.java).kotlin.members.single { it.name == "nullable_nonNullConsumeAndProduce" }
64+
(nullable_nonNullConsumeAndProduce.callSuspend(c, Z(5)) as Z).value
65+
}.let { assertEquals(5, it) }
66+
}
67+
68+
run0 {
69+
val nullable_nullableConsumeAndProduce = (C::class.java).kotlin.members.single { it.name == "nullable_nullableConsumeAndProduce" }
70+
(nullable_nullableConsumeAndProduce.callSuspend(c, Z(6))!! as Z).value
71+
}.let { assertEquals(6, it) }
72+
73+
return "OK"
74+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package kotlinx.reflect.lite.tests
2+
3+
import org.junit.*
4+
5+
class CallSuspendTest {
6+
@Test
7+
fun testCallSuspend() = test("callSuspend.callSuspend") { tests.callSuspend.callSuspend.box() }
8+
9+
@Test
10+
fun testCallSuspendBigArity() = test("callSuspend.bigArity") { tests.callSuspend.bigArity.box() }
11+
12+
@Test
13+
fun testPrimitiveSuspendFunctions() = test("callSuspend.primitiveSuspendFunctions") { tests.callSuspend.primitiveSuspendFunctions.box() }
14+
}

0 commit comments

Comments
 (0)