tests: Update external tests (1.1.2-dev-393)
This commit is contained in:
+15
@@ -0,0 +1,15 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
// KT-5612
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(thisRef: Any?, prop: KProperty<*>): String {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
|
||||
val prop by Delegate()
|
||||
|
||||
val a = prop
|
||||
|
||||
fun box() = a
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
}
|
||||
|
||||
class B {
|
||||
private var value: Int by Delegate()
|
||||
|
||||
public fun test() {
|
||||
fun foo() {
|
||||
value = 1
|
||||
}
|
||||
foo()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
B().test()
|
||||
return "OK"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
class AImpl {
|
||||
val prop: Number by Delegate()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if(AImpl().prop == 1) "OK" else "fail"
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = Derived()
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Derived {
|
||||
inner = Derived(inner.a + "-get")
|
||||
return inner
|
||||
}
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Base) { inner = Derived(inner.a + "-" + i.a + "-set") }
|
||||
}
|
||||
|
||||
class A {
|
||||
var prop: Derived by Delegate()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A()
|
||||
if(c.prop.a != "derived-get") return "fail get ${c.prop.a}"
|
||||
c.prop = Derived()
|
||||
if (c.prop.a != "derived-get-derived-set-get") return "fail set ${c.prop.a}"
|
||||
return "OK"
|
||||
}
|
||||
|
||||
open class Base(open val a: String = "base")
|
||||
|
||||
class Derived(override val a: String = "derived"): Base()
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class A {
|
||||
var prop: Int by Delegate()
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
}
|
||||
|
||||
class A {
|
||||
val p = Delegate()
|
||||
var prop: Int by p
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
}
|
||||
|
||||
fun foo() = Delegate()
|
||||
|
||||
class A {
|
||||
var prop: Int by foo()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
}
|
||||
|
||||
val p = Delegate()
|
||||
|
||||
class A {
|
||||
var prop: Int by p
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: A, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
val A.prop: Int by Delegate()
|
||||
|
||||
class A {
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if(A().prop == 1) "OK" else "fail"
|
||||
}
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: F.A, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
class F {
|
||||
val A.prop: Int by Delegate()
|
||||
|
||||
class A {
|
||||
}
|
||||
|
||||
fun foo(): Int {
|
||||
return A().prop
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if(F().foo() == 1) "OK" else "fail"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// WITH_RUNTIME
|
||||
// See KT-10107: 'Variable must be initialized' for delegate with private set
|
||||
|
||||
class My {
|
||||
var delegate: String by kotlin.properties.Delegates.notNull()
|
||||
private set
|
||||
|
||||
init {
|
||||
delegate = "OK"
|
||||
}
|
||||
}
|
||||
|
||||
fun box() = My().delegate
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
open class C
|
||||
|
||||
object O : C()
|
||||
|
||||
object K : C()
|
||||
|
||||
class D(val value: String) {
|
||||
operator fun getValue(thisRef: C, property: Any): String = value
|
||||
}
|
||||
|
||||
val O.prop by D("O")
|
||||
val K.prop by D("K")
|
||||
|
||||
fun box() = O.prop + K.prop
|
||||
backend.native/tests/external/codegen/box/delegatedProperty/extensionPropertyAndExtensionGetValue.kt
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
class A(val o: String)
|
||||
|
||||
interface I {
|
||||
val k: String
|
||||
}
|
||||
|
||||
inline operator fun A.getValue(thisRef: I, property: Any): String = o + thisRef.k
|
||||
|
||||
class B(override val k: String) : I
|
||||
|
||||
val B.prop by A("O")
|
||||
|
||||
fun box() = B("K").prop
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate<T>(var inner: T) {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): T = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: T) { inner = i }
|
||||
}
|
||||
|
||||
class A {
|
||||
inner class B {
|
||||
var prop: Int by Delegate(1)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A().B()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
}
|
||||
|
||||
operator fun Delegate.getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
|
||||
class A {
|
||||
val prop: Int by Delegate()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if(A().prop == 1) "OK" else "fail"
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
}
|
||||
|
||||
class A {
|
||||
operator fun Delegate.getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
val prop: Int by Delegate()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if(A().prop == 1) "OK" else "fail"
|
||||
}
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
// IGNORE_BACKEND: JS
|
||||
|
||||
// No kotlin-reflect.jar in this test
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
object Delegate {
|
||||
operator fun getValue(instance: Any?, property: KProperty<*>) = ""
|
||||
}
|
||||
|
||||
val foo: String by Delegate
|
||||
|
||||
fun box(): String {
|
||||
try {
|
||||
::foo.getDelegate()
|
||||
return "Fail: error should have been thrown"
|
||||
}
|
||||
catch (e: KotlinReflectionNotSupportedError) {
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
class A {
|
||||
val prop: Int by Delegate()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if(A().prop == 1) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
}
|
||||
|
||||
class A {
|
||||
var prop: Int by Delegate()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
interface A {
|
||||
val prop: Int
|
||||
}
|
||||
|
||||
class AImpl: A {
|
||||
override val prop: Int by Delegate()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if(AImpl().prop == 1) "OK" else "fail"
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate<T>(var inner: T) {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): T = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: T) { inner = i }
|
||||
}
|
||||
|
||||
class A {
|
||||
inner class B {
|
||||
var prop by Delegate(1)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A().B()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate<T>(var inner: T) {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): T = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: T) { inner = i }
|
||||
}
|
||||
|
||||
|
||||
class Foo (val f: Int) {
|
||||
companion object {
|
||||
val A: Foo by Delegate(Foo(11))
|
||||
var B: Foo by Delegate(Foo(11))
|
||||
}
|
||||
}
|
||||
|
||||
interface FooTrait {
|
||||
companion object {
|
||||
val A: Foo by Delegate(Foo(11))
|
||||
var B: Foo by Delegate(Foo(11))
|
||||
}
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
if (Foo.A.f != 11) return "fail 1"
|
||||
if (Foo.B.f != 11) return "fail 2"
|
||||
|
||||
Foo.B = Foo(12)
|
||||
if (Foo.B.f != 12) return "fail 3"
|
||||
|
||||
if (FooTrait.A.f != 11) return "fail 4"
|
||||
if (FooTrait.B.f != 11) return "fail 5"
|
||||
|
||||
FooTrait.B = Foo(12)
|
||||
if (FooTrait.B.f != 12) return "fail 6"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
interface T {
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = "OK"
|
||||
val t = object : T {
|
||||
val foo by lazy {
|
||||
a
|
||||
}
|
||||
}
|
||||
return t.foo
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
object X {
|
||||
public var O: String
|
||||
by Delegates.observable("O") { prop, old, new -> }
|
||||
private set
|
||||
}
|
||||
|
||||
open class A {
|
||||
public var K: String
|
||||
by Delegates.observable("") { prop, old, new -> }
|
||||
protected set
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
init {
|
||||
K = "K"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String =
|
||||
X.O + B().K
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
inline fun <T> run(f: () -> T) = f()
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val prop: Int by Delegate()
|
||||
return run { if (prop == 1) "OK" else "fail" }
|
||||
}
|
||||
Vendored
+14
@@ -0,0 +1,14 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
fun <T> run(f: () -> T) = f()
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val prop: Int by Delegate()
|
||||
return run { if (prop == 1) "OK" else "fail" }
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
inline fun <T> run(f: () -> T) = f()
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) {
|
||||
inner = i
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var prop: Int by Delegate()
|
||||
run { prop = 2 }
|
||||
if (prop != 2) return "fail get"
|
||||
return run { if (prop != 2) "fail set" else "OK" }
|
||||
}
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
fun <T> run(f: () -> T) = f()
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) {
|
||||
inner = i
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var prop: Int by Delegate()
|
||||
run { prop = 2 }
|
||||
if (prop != 2) return "fail get"
|
||||
return run { if (prop != 2) "fail set" else "OK" }
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
inline operator fun getValue(t: Any?, p: KProperty<*>): String = p.name
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val OK: String by Delegate()
|
||||
return OK
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
inline operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
inline operator fun setValue(t: Any?, p: KProperty<*>, i: Int) {
|
||||
inner = i
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var prop: Int by Delegate()
|
||||
if (prop != 1) return "fail get"
|
||||
prop = 2
|
||||
if (prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
//WITH_RUNTIME
|
||||
fun box(): String {
|
||||
val x by lazy { "OK" }
|
||||
return x
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
//WITH_REFLECT
|
||||
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
fun box(): String {
|
||||
var foo: String by Delegates.notNull();
|
||||
|
||||
object {
|
||||
fun baz() {
|
||||
foo = "OK"
|
||||
}
|
||||
}.baz()
|
||||
return foo
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val prop: Int by Delegate()
|
||||
return if (prop == 1) "OK" else "fail"
|
||||
}
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val prop by Delegate()
|
||||
return if (prop == 1) "OK" else "fail"
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) {
|
||||
inner = i
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var prop: Int by Delegate()
|
||||
if (prop != 1) return "fail get"
|
||||
prop = 2
|
||||
if (prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
package foo
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) {
|
||||
inner = i
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var prop by Delegate()
|
||||
if (prop != 1) return "fail get"
|
||||
prop = 2
|
||||
if (prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+37
@@ -0,0 +1,37 @@
|
||||
// TODO: muted automatically, investigate should it be ran for JS or not
|
||||
// IGNORE_BACKEND: JS, NATIVE
|
||||
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.jvm.*
|
||||
|
||||
object Delegate {
|
||||
operator fun getValue(thiz: My, property: KProperty<*>): String {
|
||||
if (property !is KMutableProperty<*>) return "Fail: property is not a KMutableProperty"
|
||||
property as KMutableProperty1<My, String>
|
||||
|
||||
try {
|
||||
property.set(thiz, "")
|
||||
return "Fail: property.set should cause IllegalCallableAccessException"
|
||||
}
|
||||
catch (e: IllegalCallableAccessException) {
|
||||
// OK
|
||||
}
|
||||
|
||||
property.isAccessible = true
|
||||
property.set(thiz, "")
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
operator fun setValue(thiz: My, property: KProperty<*>, value: String) {
|
||||
}
|
||||
}
|
||||
|
||||
class My {
|
||||
var delegate: String by Delegate
|
||||
private set
|
||||
}
|
||||
|
||||
fun box() = My().delegate
|
||||
@@ -0,0 +1,22 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
}
|
||||
|
||||
class A {
|
||||
private var prop: Int by Delegate()
|
||||
|
||||
fun test(): String {
|
||||
if(prop != 1) return "fail get"
|
||||
prop = 2
|
||||
if (prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return A().test()
|
||||
}
|
||||
Vendored
+50
@@ -0,0 +1,50 @@
|
||||
// TODO: muted automatically, investigate should it be ran for JS or not
|
||||
// IGNORE_BACKEND: JS, NATIVE
|
||||
|
||||
import java.util.IdentityHashMap
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class A {
|
||||
var foo: Int by IntHandler
|
||||
|
||||
companion object {
|
||||
var bar: Any? by AnyHandler
|
||||
}
|
||||
}
|
||||
|
||||
val baz: String by StringHandler
|
||||
|
||||
|
||||
|
||||
val metadatas = IdentityHashMap<KProperty<*>, Unit>()
|
||||
|
||||
fun record(p: KProperty<*>) = metadatas.put(p, Unit)
|
||||
|
||||
object IntHandler {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int { record(p); return 42 }
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, value: Int) { record(p) }
|
||||
}
|
||||
|
||||
object AnyHandler {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Any? { record(p); return 3.14 }
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, value: Any?) { record(p) }
|
||||
}
|
||||
|
||||
object StringHandler {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): String { record(p); return p.name }
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, value: String) { record(p) }
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val a = A()
|
||||
a.foo = 42
|
||||
a.foo = a.foo + baz.length
|
||||
a.foo = 239
|
||||
A.bar = baz + a.foo
|
||||
baz + A.bar
|
||||
|
||||
if (metadatas.keys.size != 3)
|
||||
return "Fail: only three instances of KProperty should have been created\n${metadatas.keys}"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
open class A<T : Any> {
|
||||
protected var value: T by Delegates.notNull()
|
||||
private set
|
||||
}
|
||||
|
||||
class B : A<Int>()
|
||||
|
||||
fun box(): String {
|
||||
B()
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
class MyClass(val value: String)
|
||||
|
||||
inline fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun MyClass.provideDelegate(host: Any?, p: Any): String =
|
||||
runLogged("tdf(${this.value});") { this.value }
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: Any): String =
|
||||
runLogged("get($this);") { this }
|
||||
|
||||
val testO by runLogged("O;") { MyClass("O") }
|
||||
val testK by runLogged("K;") { "K" }
|
||||
val testOK = runLogged("OK;") { testO + testK }
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("O;tdf(O);K;OK;get(O);get(K);", log)
|
||||
return testOK
|
||||
}
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
inline fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun String.provideDelegate(host: Any?, p: Any): String =
|
||||
runLogged("tdf($this);") { this }
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: Any): String =
|
||||
runLogged("get($this);") { this }
|
||||
|
||||
val testO by runLogged("O;") { "O" }
|
||||
val testK by runLogged("K;") { "K" }
|
||||
val testOK = runLogged("OK;") { testO + testK }
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("O;tdf(O);K;tdf(K);OK;get(O);get(K);", log)
|
||||
return testOK
|
||||
}
|
||||
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
val dispatcher = hashMapOf<String, String>()
|
||||
|
||||
inline fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun String.provideDelegate(host: Any?, p: Any): String {
|
||||
dispatcher[this] = this
|
||||
return runLogged("tdf($this);") { this }
|
||||
}
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: Any): String =
|
||||
runLogged("get(${dispatcher[this]});") { dispatcher[this]!! }
|
||||
|
||||
operator fun String.setValue(receiver: Any?, p: Any, newValue: String) {
|
||||
dispatcher[this] = newValue
|
||||
runLogged("set(${dispatcher[this]});") { dispatcher[this]!! }
|
||||
}
|
||||
|
||||
var testO by runLogged("K;") { "K" }
|
||||
var testK by runLogged("O;") { "O" }
|
||||
val testOK = runLogged("OK;") {
|
||||
testO = "O"
|
||||
testK = "K"
|
||||
testO + testK
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("K;tdf(K);O;tdf(O);OK;set(O);set(K);get(O);get(K);", log)
|
||||
return testOK
|
||||
}
|
||||
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
var log = ""
|
||||
|
||||
class UserDataProperty<in R>(val key: String) {
|
||||
operator fun getValue(thisRef: R, desc: KProperty<*>) = thisRef.toString() + key
|
||||
|
||||
operator fun setValue(thisRef: R, desc: KProperty<*>, value: String?) { log += "set"}
|
||||
}
|
||||
|
||||
|
||||
var String.calc: String by UserDataProperty("K")
|
||||
|
||||
fun box(): String {
|
||||
return "O".calc
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
open class MyClass(val value: String) {
|
||||
override fun toString(): String {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun <T: MyClass> T.provideDelegate(host: Any?, p: Any): T =
|
||||
runLogged("tdf(${this.value});") { this }
|
||||
|
||||
operator fun <T> T.getValue(receiver: Any?, p: Any): T =
|
||||
runLogged("get($this);") { this }
|
||||
|
||||
val testO by runLogged("O;") { MyClass("O") }
|
||||
val testK by runLogged("K;") { "K" }
|
||||
val testOK = runLogged("OK;") { testO.value + testK }
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("O;tdf(O);K;OK;get(O);get(K);", log)
|
||||
return testOK
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate(val value: String) {
|
||||
operator fun provideDelegate(instance: A, property: KProperty<*>): Delegate = Delegate(instance.value)
|
||||
operator fun getValue(instance: Any?, property: KProperty<*>) = value
|
||||
}
|
||||
|
||||
class A(val value: String) {
|
||||
val result: String by Delegate("Fail")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return A("OK").result
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
inline fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun String.provideDelegate(host: Any?, p: Any): String =
|
||||
runLogged("tdf($this);") { this }
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: Any): String =
|
||||
runLogged("get($this);") { this }
|
||||
|
||||
class Test {
|
||||
val testO by runLogged("O;") { "O" }
|
||||
val testK by runLogged("K;") { "K" }
|
||||
val testOK = runLogged("OK;") { testO + testK }
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("", log)
|
||||
val test = Test()
|
||||
assertEquals("O;tdf(O);K;tdf(K);OK;get(O);get(K);", log)
|
||||
return test.testOK
|
||||
}
|
||||
backend.native/tests/external/codegen/box/delegatedProperty/provideDelegate/inlineProvideDelegate.kt
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
inline fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
inline operator fun String.provideDelegate(host: Any?, p: Any): String =
|
||||
runLogged("tdf($this);") { this }
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: Any): String =
|
||||
runLogged("get($this);") { this }
|
||||
|
||||
val testO by runLogged("O;") { "O" }
|
||||
val testK by runLogged("K;") { "K" }
|
||||
val testOK = runLogged("OK;") { testO + testK }
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("O;tdf(O);K;tdf(K);OK;get(O);get(K);", log)
|
||||
return testOK
|
||||
}
|
||||
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
// WITH_RUNTIME
|
||||
// IGNORE_BACKEND: JS, NATIVE
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
inline fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun String.provideDelegate(host: Any?, p: Any): String =
|
||||
runLogged("tdf($this);") { this }
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: Any): String =
|
||||
runLogged("get($this);") { this }
|
||||
|
||||
object Test {
|
||||
fun foo() {}
|
||||
@JvmStatic val testO by runLogged("O;") { "O" }
|
||||
@JvmStatic val testK by runLogged("K;") { "K" }
|
||||
@JvmStatic val testOK = runLogged("OK;") { testO + testK }
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("", log)
|
||||
Test.foo()
|
||||
assertEquals("O;tdf(O);K;tdf(K);OK;get(O);get(K);", log)
|
||||
return Test.testOK
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun provideDelegate(instance: Any?, property: KProperty<*>): Delegate = this
|
||||
operator fun getValue(instance: Any?, property: KProperty<*>) = "OK"
|
||||
}
|
||||
|
||||
val result: String by Delegate()
|
||||
|
||||
fun box(): String {
|
||||
return result
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun provideDelegate(thisRef: Any?, property: KProperty<*>) = this
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>) = "OK"
|
||||
}
|
||||
|
||||
class TestClass {
|
||||
companion object {
|
||||
val test by Delegate()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return TestClass.test
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
inline fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun String.provideDelegate(host: Any?, p: Any): String =
|
||||
runLogged("tdf($this);") { this }
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: Any): String =
|
||||
runLogged("get($this);") { this }
|
||||
|
||||
fun box(): String {
|
||||
val testO by runLogged("O;") { "O" }
|
||||
val testK by runLogged("K;") { "K" }
|
||||
val testOK = runLogged("OK;") { testO + testK }
|
||||
|
||||
assertEquals("O;tdf(O);K;tdf(K);OK;get(O);get(K);", log)
|
||||
return testOK
|
||||
}
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun String.provideDelegate(host: Any?, p: Any): String =
|
||||
runLogged("tdf($this);") { this }
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: Any): String =
|
||||
runLogged("get($this);") { this }
|
||||
|
||||
fun box(): String {
|
||||
val testO by runLogged("O;") { "O" }
|
||||
val testK by runLogged("K;") { "K" }
|
||||
val testOK = runLogged("OK;") { testO + testK }
|
||||
|
||||
assertEquals("O;tdf(O);K;tdf(K);OK;get(O);get(K);", log)
|
||||
return testOK
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
var log: String = ""
|
||||
|
||||
class MyClass(val value: String)
|
||||
|
||||
fun runLogged(entry: String, action: () -> String): String {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
fun runLogged2(entry: String, action: () -> MyClass): MyClass {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun MyClass.provideDelegate(host: Any?, p: Any): String =
|
||||
runLogged("tdf(${this.value});") { this.value }
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: Any): String =
|
||||
runLogged("get($this);") { this }
|
||||
|
||||
|
||||
fun box(): String {
|
||||
val testO by runLogged2("O;") { MyClass("O") }
|
||||
val testK by runLogged("K;") { "K" }
|
||||
val testOK = runLogged("OK;") { testO + testK }
|
||||
|
||||
assertEquals("O;tdf(O);K;OK;get(O);get(K);", log)
|
||||
return testOK
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
object Host {
|
||||
class StringDelegate(val s: String) {
|
||||
operator fun getValue(receiver: String, p: Any) = receiver + s
|
||||
}
|
||||
|
||||
operator fun String.provideDelegate(host: Any?, p: Any) = StringDelegate(this)
|
||||
|
||||
val String.plusK by "K"
|
||||
|
||||
val ok = "O".plusK
|
||||
}
|
||||
|
||||
fun box(): String = Host.ok
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.*
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
var log: String = ""
|
||||
|
||||
inline fun <T> runLogged(entry: String, action: () -> T): T {
|
||||
log += entry
|
||||
return action()
|
||||
}
|
||||
|
||||
operator fun String.provideDelegate(host: Any?, p: KProperty<*>): String =
|
||||
if (p.name == this) runLogged("tdf($this);") { this } else "fail 1"
|
||||
|
||||
operator fun String.getValue(receiver: Any?, p: KProperty<*>): String =
|
||||
if (p.name == this) runLogged("get($this);") { this } else "fail 2"
|
||||
|
||||
val O by runLogged("O;") { "O" }
|
||||
val K by runLogged("K;") { "K" }
|
||||
val OK = runLogged("OK;") { O + K }
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("O;tdf(O);K;tdf(K);OK;get(O);get(K);", log)
|
||||
return OK
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
}
|
||||
|
||||
operator fun Delegate.setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
|
||||
class A {
|
||||
var prop: Int by Delegate()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
}
|
||||
|
||||
class A {
|
||||
operator fun Delegate.setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
|
||||
var prop: Int by Delegate()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+57
@@ -0,0 +1,57 @@
|
||||
// TODO: muted automatically, investigate should it be ran for JS or not
|
||||
// IGNORE_BACKEND: JS, NATIVE
|
||||
|
||||
// WITH_REFLECT
|
||||
// FULL_JDK
|
||||
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import kotlin.reflect.*
|
||||
|
||||
object Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): String {
|
||||
(p as? KProperty0<String>)?.get()
|
||||
(p as? KProperty1<O, String>)?.get(O)
|
||||
(p as? KProperty2<O, O, String>)?.get(O, O)
|
||||
return "Fail"
|
||||
}
|
||||
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, v: String) {
|
||||
(p as? KMutableProperty0<String>)?.set(v)
|
||||
(p as? KMutableProperty1<O, String>)?.set(O, v)
|
||||
(p as? KMutableProperty2<O, O, String>)?.set(O, O, v)
|
||||
}
|
||||
}
|
||||
|
||||
var topLevel: String by Delegate
|
||||
object O {
|
||||
var member: String by Delegate
|
||||
var O.memExt: String by Delegate
|
||||
}
|
||||
|
||||
fun check(lambda: () -> Unit) {
|
||||
try {
|
||||
lambda()
|
||||
} catch (e: Throwable) {
|
||||
if (e !is InvocationTargetException && e !is StackOverflowError) {
|
||||
throw RuntimeException("The current implementation uses reflection to get the value of the property," +
|
||||
"so either InvocationTargetException or StackOverflowError should have happened",
|
||||
e)
|
||||
}
|
||||
return
|
||||
}
|
||||
throw AssertionError("Getting the property value with .get() from getValue() or setting it with .set() in setValue() " +
|
||||
"is effectively an endless recursion and should fail")
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
check { topLevel }
|
||||
check { topLevel = "" }
|
||||
check { O.member }
|
||||
check { O.member = "" }
|
||||
with (O) {
|
||||
check { O.memExt }
|
||||
check { O.memExt = "" }
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
val prop: Int by Delegate()
|
||||
|
||||
fun box(): String {
|
||||
return if(prop == 1) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
}
|
||||
|
||||
var prop: Int by Delegate()
|
||||
|
||||
fun box(): String {
|
||||
if(prop != 1) return "fail get"
|
||||
prop = 2
|
||||
if (prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate<T>(val f: (T) -> Int) {
|
||||
operator fun getValue(t: T, p: KProperty<*>): Int = f(t)
|
||||
}
|
||||
|
||||
val p = Delegate<A> { t -> t.foo() }
|
||||
|
||||
class A(val i: Int) {
|
||||
val prop: Int by p
|
||||
|
||||
fun foo(): Int {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
if(A(1).prop != 1) return "fail get1"
|
||||
if(A(10).prop != 10) return "fail get2"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.*
|
||||
|
||||
val properties = HashSet<KProperty<*>>()
|
||||
|
||||
object Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): String {
|
||||
properties.add(p)
|
||||
return ""
|
||||
}
|
||||
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, v: String) {
|
||||
properties.add(p)
|
||||
}
|
||||
}
|
||||
|
||||
var topLevel: String by Delegate
|
||||
object O {
|
||||
var member: String by Delegate
|
||||
var O.memExt: String by Delegate
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
topLevel = ""
|
||||
O.member = ""
|
||||
with (O) {
|
||||
O.memExt = ""
|
||||
}
|
||||
|
||||
for (p in HashSet(properties)) {
|
||||
// None of these should fail
|
||||
|
||||
(p as? KProperty0<String>)?.get()
|
||||
(p as? KProperty1<O, String>)?.get(O)
|
||||
(p as? KProperty2<O, O, String>)?.get(O, O)
|
||||
|
||||
(p as? KMutableProperty0<String>)?.set("")
|
||||
(p as? KMutableProperty1<O, String>)?.set(O, "")
|
||||
(p as? KMutableProperty2<O, O, String>)?.set(O, O, "")
|
||||
}
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// TODO: muted automatically, investigate should it be ran for JS or not
|
||||
// IGNORE_BACKEND: JS, NATIVE
|
||||
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): String {
|
||||
p.parameters
|
||||
p.returnType
|
||||
p.annotations
|
||||
return p.toString()
|
||||
}
|
||||
}
|
||||
|
||||
val prop: String by Delegate()
|
||||
|
||||
fun box() = if (prop == "val prop: kotlin.String") "OK" else "Fail: $prop"
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = 1
|
||||
}
|
||||
|
||||
class A {
|
||||
inner class B {
|
||||
val prop: Int by Delegate()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if(A().B().prop == 1) "OK" else "fail"
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Delegate {
|
||||
var inner = 1
|
||||
operator fun getValue(t: Any?, p: KProperty<*>): Int = inner
|
||||
operator fun setValue(t: Any?, p: KProperty<*>, i: Int) { inner = i }
|
||||
}
|
||||
|
||||
class A {
|
||||
inner class B {
|
||||
var prop: Int by Delegate()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val c = A().B()
|
||||
if(c.prop != 1) return "fail get"
|
||||
c.prop = 2
|
||||
if (c.prop != 2) return "fail set"
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user