Files
kotlin-fork/compiler/testData/codegen/box/reflection/properties/getDelegate/overrideDelegatedByDelegated.kt
T
Alexander Udalov 6cc293a0c5 JVM IR: record FIELD_FOR_PROPERTY for property delegates
Delegated properties now have their $delegate fields recorded in the
metadata (in `ClassCodegen.generateField`). This part of metadata is
used by `KPropertyN.getDelegate` functions, so almost all tests on
getDelegate are now unmuted
2019-04-30 13:15:29 +02:00

38 lines
1.0 KiB
Kotlin
Vendored

// TARGET_BACKEND: JVM
// 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"
}