KT-4773: added tests
This commit is contained in:
@@ -47,7 +47,7 @@ public class DelegatePropertyTest extends SingleFileTranslationTest {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
public void testDelegateForExtProperty() throws Exception {
|
||||
public void testDelegateByExtensionProperty() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
|
||||
@@ -91,4 +91,8 @@ public class DelegationTest extends SingleFileTranslationTest {
|
||||
public void testDelegationExtProp() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
public void testDelegationExtensionPropertyDelegated() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package foo
|
||||
|
||||
class State(var realValue: Int)
|
||||
|
||||
fun format(event: String, property: String, value: Int): String
|
||||
= "${event}: ${property} = ${value}; "
|
||||
|
||||
object LoggerDelegate {
|
||||
var log = ""
|
||||
|
||||
fun get(state: State, desc: PropertyMetadataImpl): Int {
|
||||
log += format("get", desc.name, state.realValue)
|
||||
return state.realValue
|
||||
}
|
||||
|
||||
fun set(state: State, desc: PropertyMetadataImpl, value: Int) {
|
||||
log += format("set", desc.name, value)
|
||||
state.realValue = value
|
||||
}
|
||||
}
|
||||
|
||||
var State.value by LoggerDelegate
|
||||
|
||||
fun box(): String {
|
||||
val state = State(1)
|
||||
var expectedLog = ""
|
||||
|
||||
assertEquals(1, state.value)
|
||||
expectedLog += format("get", "value", 1)
|
||||
|
||||
state.value = 3
|
||||
expectedLog += format("set", "value", 3)
|
||||
|
||||
assertEquals(3, state.value)
|
||||
expectedLog += format("get", "value", 3)
|
||||
|
||||
assertEquals(expectedLog, LoggerDelegate.log)
|
||||
return "OK"
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package foo
|
||||
|
||||
class Delegate {
|
||||
fun get(t: A, p: PropertyMetadata): Int = 1
|
||||
}
|
||||
|
||||
val A.prop: Int by Delegate()
|
||||
|
||||
class A {
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return if (A().prop == 1) "OK" else "fail"
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package foo
|
||||
|
||||
class State(var value: Int)
|
||||
|
||||
trait Base {
|
||||
var State.multiplied: Int
|
||||
}
|
||||
|
||||
class Delegate(val multiplier: Int) {
|
||||
fun get(state: State, desc: PropertyMetadataImpl): Int = multiplier * state.value
|
||||
|
||||
fun set(state: State, desc: PropertyMetadataImpl, value: Int) {
|
||||
state.value = value / multiplier
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
open class BaseImpl() : Base {
|
||||
override var State.multiplied: Int by Delegate(2)
|
||||
}
|
||||
|
||||
class Derived() : Base by BaseImpl() {
|
||||
fun getValueMultiplied(state: State): Int = state.multiplied
|
||||
|
||||
fun setValueMultiplied(state: State, value: Int) {
|
||||
state.multiplied = value
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val d = Derived()
|
||||
|
||||
val state = State(2)
|
||||
assertEquals(4, d.getValueMultiplied(state))
|
||||
|
||||
d.setValueMultiplied(state, 10)
|
||||
assertEquals(10, d.getValueMultiplied(state))
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user