JS backend: tests for reflection support
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
abstract class A {
|
||||
abstract fun foo(): String
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
override fun foo() = "OK"
|
||||
}
|
||||
|
||||
fun box(): String = B().(A::foo)()
|
||||
@@ -0,0 +1,8 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
if (true.(Boolean::not)() != false) return "Fail 1"
|
||||
if (false.(Boolean::not)() != true) return "Fail 2"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package foo
|
||||
|
||||
class A {
|
||||
val s = "sA"
|
||||
fun memBar(other: String): String = s +":memBar:" + other
|
||||
}
|
||||
|
||||
fun A.extBar(other: String):String = s + ":extBar:" + other
|
||||
|
||||
fun box():String {
|
||||
fun A.locExtBar(other: String):String = s + ":locExtBar:" + other
|
||||
|
||||
val a = A()
|
||||
|
||||
var r = a.(A::memBar)("!!")
|
||||
if (r != "sA:memBar:!!") return r
|
||||
|
||||
r = a.(A::extBar)("!!")
|
||||
if (r != "sA:extBar:!!") return r
|
||||
|
||||
r = a.(A::locExtBar)("!!")
|
||||
if (r != "sA:locExtBar:!!") return r
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package foo
|
||||
|
||||
fun run(a: A, arg: String, funRef:A.(String) -> String): String {
|
||||
return a.(funRef)(arg)
|
||||
}
|
||||
|
||||
class A {
|
||||
val s = "sA"
|
||||
fun memBar(other: String): String = s +":memBar:" + other
|
||||
}
|
||||
|
||||
fun A.extBar(other: String):String = s + ":extBar:" + other
|
||||
|
||||
fun box():String {
|
||||
fun A.locExtBar(other: String):String = s + ":locExtBar:" + other
|
||||
|
||||
val a = A()
|
||||
|
||||
var r = run(a, "!!", A::memBar)
|
||||
if (r != "sA:memBar:!!") return r
|
||||
|
||||
r = run(a, "!!", A::extBar)
|
||||
if (r != "sA:extBar:!!") return r
|
||||
|
||||
r = run(a, "!!", A::locExtBar)
|
||||
if (r != "sA:locExtBar:!!") return r
|
||||
|
||||
r = run(a, "!!") {A.(other:String):String -> s + ":literal:" + other }
|
||||
if (r != "sA:literal:!!") return r
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
class A {
|
||||
fun bar(k: Int) = k
|
||||
|
||||
fun result() = this.(::bar)(111)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = A().result()
|
||||
if (result != 111) return "Fail $result"
|
||||
return "OK"
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
class A {
|
||||
fun o() = 111
|
||||
fun k(k: Int) = k
|
||||
}
|
||||
|
||||
fun A.bar() = this.(::o)() + this.(A::k)(222)
|
||||
|
||||
fun box(): String {
|
||||
val result = A().bar()
|
||||
if (result != 333) return "Fail $result"
|
||||
return "OK"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg1: A, funRef:A.() -> String): String {
|
||||
return arg1.funRef()
|
||||
}
|
||||
|
||||
class A {
|
||||
fun foo() = "OK"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val x = A::foo
|
||||
var r = A().x()
|
||||
if (r != "OK") return r
|
||||
|
||||
r = run(A(), A::foo)
|
||||
if (r != "OK") return r
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg1: A, arg2: String, funRef:A.(String) -> String): String {
|
||||
return arg1.funRef(arg2)
|
||||
}
|
||||
|
||||
class A {
|
||||
fun foo(result: String):String = result
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val x = A::foo
|
||||
var r = A().x("OK")
|
||||
|
||||
if (r != "OK") return r
|
||||
|
||||
r = run(A(), "OK", A::foo)
|
||||
if (r != "OK") return r
|
||||
return "OK"
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg1: A, funRef:A.() -> Unit): Unit {
|
||||
return arg1.funRef()
|
||||
}
|
||||
|
||||
class A {
|
||||
var result = "Fail"
|
||||
|
||||
fun foo() {
|
||||
result = "OK"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
val x = A::foo
|
||||
a.x()
|
||||
var r = a.result
|
||||
if (r != "OK") return r
|
||||
|
||||
val a1 = A()
|
||||
run(a1, A::foo)
|
||||
r = a.result
|
||||
if (r != "OK") return r
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg1: A, arg2: String, funRef:A.(String) -> Unit): Unit {
|
||||
return arg1.funRef(arg2)
|
||||
}
|
||||
|
||||
class A {
|
||||
var result = "Fail"
|
||||
|
||||
fun foo(newResult: String) {
|
||||
result = newResult
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
val x = A::foo
|
||||
a.x("OK")
|
||||
var r = a.result
|
||||
if (r != "OK") return r
|
||||
|
||||
val a1 = A()
|
||||
run(a1, "OK", A::foo)
|
||||
r = a1.result
|
||||
if (r != "OK") return r
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package foo
|
||||
|
||||
open class A {
|
||||
open fun foo(a:String,b:String): String = "fooA:" + a + b
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
override fun foo(a:String,b:String): String = "fooB:" + a + b
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val b = B()
|
||||
var ref = A::foo
|
||||
val result = b.(ref)("1", "2")
|
||||
return (if (result == "fooB:12") "OK" else result)
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package foo
|
||||
|
||||
open class A {
|
||||
open fun foo(a:String,b:String): String = "fooA:" + a + b
|
||||
}
|
||||
|
||||
object B : A() {
|
||||
override fun foo(a:String,b:String): String = "fooB:" + a + b
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var ref = B::foo
|
||||
val result = B.(ref)("1", "2")
|
||||
return (if (result == "fooB:12") "OK" else result)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var result = "Fail"
|
||||
|
||||
fun changeToOK() { result = "OK" }
|
||||
|
||||
val ok = ::changeToOK
|
||||
ok()
|
||||
return result
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
class A {
|
||||
var result = "OK"
|
||||
}
|
||||
|
||||
fun box() = (::A)().result
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
class A(val result: String)
|
||||
|
||||
fun box() = (::A)("OK").result
|
||||
@@ -0,0 +1,21 @@
|
||||
package foo
|
||||
|
||||
class A(val x:Int) {
|
||||
var s = "sA"
|
||||
{
|
||||
s += ":init:" + x
|
||||
}
|
||||
}
|
||||
|
||||
class B(val arg1:String, val arg2:String) {
|
||||
var msg = ""
|
||||
{
|
||||
msg = arg1 + arg2
|
||||
}
|
||||
}
|
||||
|
||||
fun box():String {
|
||||
val ref = ::A
|
||||
var result = ref(1).s + (::B)("23", "45").msg
|
||||
return (if (result == "sA:init:12345") "OK" else result)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
class A
|
||||
|
||||
fun box(): String {
|
||||
fun A.foo() = "OK"
|
||||
return A().(A::foo)()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
class A {
|
||||
fun result() = this.(::bar)("OK")
|
||||
}
|
||||
|
||||
fun A.bar(x: String) = x
|
||||
|
||||
fun box() = A().result()
|
||||
@@ -0,0 +1,10 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
class A
|
||||
|
||||
fun A.foo() = this.(A::bar)("OK")
|
||||
|
||||
fun A.bar(x: String) = x
|
||||
|
||||
fun box() = A().foo()
|
||||
@@ -0,0 +1,25 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun <T> run(arg1: A, arg2: T, funRef:A.(T) -> T): T {
|
||||
return arg1.funRef(arg2)
|
||||
}
|
||||
|
||||
class A {
|
||||
var xx: Int = 100
|
||||
}
|
||||
|
||||
fun A.bar(x: Int): Int {
|
||||
this.xx = this.xx * 2
|
||||
return x
|
||||
}
|
||||
|
||||
fun box(): Boolean {
|
||||
val funRef = A::bar
|
||||
val obj = A()
|
||||
var result = obj.(funRef)(25)
|
||||
if (result != 25 || obj.xx != 200) return false
|
||||
|
||||
result = run(A(), 25, funRef)
|
||||
return result == 25 && obj.xx == 200
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg1: A, funRef:A.() -> String): String {
|
||||
return arg1.funRef()
|
||||
}
|
||||
|
||||
class A
|
||||
|
||||
fun A.foo() = "OK"
|
||||
|
||||
fun box(): String {
|
||||
val x = A::foo
|
||||
var r = A().x()
|
||||
if (r != "OK") return r
|
||||
|
||||
r = run(A(), A::foo)
|
||||
if (r != "OK") return r
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg1: A, arg2: String, funRef:A.(String) -> String): String {
|
||||
return arg1.funRef(arg2)
|
||||
}
|
||||
|
||||
class A
|
||||
|
||||
fun A.foo(result: String) = result
|
||||
|
||||
fun box(): String {
|
||||
val x = A::foo
|
||||
var r = A().x("OK")
|
||||
if (r != "OK") return r
|
||||
|
||||
r = run(A(), "OK", A::foo)
|
||||
return "OK"
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg1: A, funRef:A.() -> Unit): Unit {
|
||||
return arg1.funRef()
|
||||
}
|
||||
|
||||
class A {
|
||||
var result = "Fail"
|
||||
}
|
||||
|
||||
fun A.foo() {
|
||||
result = "OK"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
val x = A::foo
|
||||
a.x()
|
||||
|
||||
if (a.result != "OK") return a.result
|
||||
|
||||
val a1 = A()
|
||||
run(a1, A::foo)
|
||||
return a.result
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg1: A, arg2: String, funRef:A.(String) -> Unit): Unit {
|
||||
return arg1.funRef(arg2)
|
||||
}
|
||||
|
||||
class A {
|
||||
var result = "Fail"
|
||||
}
|
||||
|
||||
fun A.foo(newResult: String) {
|
||||
result = newResult
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
val x = A::foo
|
||||
a.x("OK")
|
||||
|
||||
if (a.result != "OK") return a.result
|
||||
|
||||
val a1 = A()
|
||||
run(a1, "OK", A::foo)
|
||||
return a.result
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
fun Int.is42With(that: Int) = this + 2 * that == 42
|
||||
return if (16.(Int::is42With)(13)) "OK" else "Fail"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
class A
|
||||
|
||||
fun box(): String {
|
||||
var result = "Fail"
|
||||
|
||||
fun A.ext() { result = "OK" }
|
||||
|
||||
val f = A::ext
|
||||
A().f()
|
||||
return result
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package foo
|
||||
|
||||
fun Int.sum0(other: Int): Int = this + other;
|
||||
|
||||
fun box(): String {
|
||||
fun Int.sum1(other: Int): Int = this + other
|
||||
|
||||
val sum2 = {Int.(other : Int) : Int -> this + other}
|
||||
|
||||
var x = 10
|
||||
x = x.sum0(5)
|
||||
x = x.sum1(5)
|
||||
x = x.sum2(5)
|
||||
|
||||
var y = 10
|
||||
y = y.(Int::sum0)(5)
|
||||
y = y.(Int::sum1)(5)
|
||||
y = y.sum2(5)
|
||||
|
||||
var result:String = (if (x == y && x == 25) "OK" else "x=${x} y=${y}")
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
fun foo(): String {
|
||||
fun bar() = "OK"
|
||||
val ref = ::bar
|
||||
return ref()
|
||||
}
|
||||
|
||||
val ref = ::foo
|
||||
return ref()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
fun foo(until: Int): String {
|
||||
fun bar(x: Int): String =
|
||||
if (x == until) "OK" else bar(x + 1)
|
||||
return (::bar)(0)
|
||||
}
|
||||
|
||||
fun box() = foo(10)
|
||||
@@ -0,0 +1,7 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
fun foo() = "OK"
|
||||
return (::foo)()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
val result = "OK"
|
||||
|
||||
fun foo() = result
|
||||
|
||||
return (::foo)()
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
fun foo(s: String) = s
|
||||
return (::foo)("OK")
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package foo
|
||||
|
||||
fun box(): String {
|
||||
var s = "abc"
|
||||
assertEquals("ABC", s.(String::toUpperCase)())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun <T> run(arg1: T, arg2: T, funRef:(T,T) -> T): T {
|
||||
return funRef(arg1, arg2)
|
||||
}
|
||||
|
||||
fun tmp(o: Int, k: Int) = o + k
|
||||
|
||||
class A {
|
||||
fun bar() = (::tmp)(111, 222)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val result = A().bar()
|
||||
if (result != 333) return "Fail $result"
|
||||
|
||||
var r = run(111, 222, ::tmp)
|
||||
if (result != 333) return "Fail $result"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun <T> run(arg1: T, arg2: T, funRef:(T,T) -> T): T {
|
||||
return funRef(arg1, arg2)
|
||||
}
|
||||
|
||||
fun tmp(o: Int, k: Int) = o + k
|
||||
|
||||
class A
|
||||
|
||||
fun A.bar() = (::tmp)(111, 222)
|
||||
|
||||
fun box(): String {
|
||||
val result = A().bar()
|
||||
if (result != 333) return "Fail $result"
|
||||
|
||||
var r = run(111, 222, ::tmp)
|
||||
if (result != 333) return "Fail $result"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(funRef:() -> String): String {
|
||||
return funRef()
|
||||
}
|
||||
|
||||
fun bar() = "OK"
|
||||
|
||||
fun box(): String {
|
||||
val x = ::bar
|
||||
|
||||
var r = x()
|
||||
if (r != "OK") return r
|
||||
|
||||
r = run(::bar)
|
||||
if (r != "OK") return r
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg: Int, funRef:(Int) -> Int): Int {
|
||||
return funRef(arg)
|
||||
}
|
||||
|
||||
fun inc(x: Int) = x + 1
|
||||
|
||||
fun tmp():Function1<Int, Int> {
|
||||
return ::inc
|
||||
}
|
||||
|
||||
fun box(): Boolean {
|
||||
if (tmp()(5) != 6) return false
|
||||
|
||||
if (run(5, tmp()) != 6) return false
|
||||
|
||||
return true
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/.
|
||||
package foo
|
||||
|
||||
fun run(arg: Int, funRef:(Int) -> Int): Int {
|
||||
return funRef(arg)
|
||||
}
|
||||
fun inc(x: Int) = x + 1
|
||||
|
||||
fun box(): Boolean {
|
||||
val funRef = ::inc
|
||||
if (funRef(5) != 6) return false
|
||||
|
||||
if (run(5, funRef) != 6) return false
|
||||
|
||||
if (run(5) {x -> x + 1} != 6) return false
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// This test was adapted from compiler/testData/codegen/boxWithStdlib/callableReference/function/local/.
|
||||
package foo
|
||||
|
||||
var state = 23
|
||||
|
||||
fun box(): String {
|
||||
fun incrementState(inc: Int) {
|
||||
state += inc
|
||||
}
|
||||
|
||||
val inc = ::incrementState
|
||||
inc(12)
|
||||
inc(-5)
|
||||
inc(27)
|
||||
inc(-15)
|
||||
|
||||
return if (state == 42) "OK" else "Fail $state"
|
||||
}
|
||||
Reference in New Issue
Block a user