Rename toDelegateFor to provideDelegate.

This commit is contained in:
Stanislav Erokhin
2016-12-12 14:56:16 +03:00
parent db5250a614
commit 9dc9fb578f
71 changed files with 278 additions and 276 deletions
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -0,0 +1,31 @@
// WITH_RUNTIME
// IGNORE_BACKEND: JS
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
}
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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
@@ -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
}