Introduce KProperty{0,1,2}.getDelegate

#KT-8384 In Progress
This commit is contained in:
Alexander Udalov
2016-12-22 18:30:38 +03:00
parent f1cd2ee6fd
commit 78f2515e95
47 changed files with 1398 additions and 0 deletions
@@ -0,0 +1,37 @@
// IGNORE_BACKEND: JS
// WITH_REFLECT
import kotlin.reflect.KProperty
import kotlin.reflect.jvm.isAccessible
import kotlin.test.*
class Delegate(val value: String) {
operator fun getValue(instance: Any?, property: KProperty<*>) = value
}
open class Base {
open val x: String by Delegate("Base")
}
class Derived : Base() {
override val x: String by Delegate("Derived")
}
fun check(expected: String, delegate: Any?) {
if (delegate == null) throw AssertionError("getDelegate returned null")
assertEquals(expected, (delegate as Delegate).value)
}
fun box(): String {
val base = Base()
val derived = Derived()
check("Base", (Base::x).apply { isAccessible = true }.getDelegate(base))
check("Base", (base::x).apply { isAccessible = true }.getDelegate())
check("Derived", (Derived::x).apply { isAccessible = true }.getDelegate(derived))
check("Derived", (derived::x).apply { isAccessible = true }.getDelegate())
check("Base", (Base::x).apply { isAccessible = true }.getDelegate(derived))
return "OK"
}