Some new tests for 'toDelegateFor'

This commit is contained in:
Mikhael Bogdanov
2016-12-08 14:39:06 +01:00
committed by Stanislav Erokhin
parent 97d5bbf1c2
commit c5e14a2059
12 changed files with 330 additions and 0 deletions
@@ -0,0 +1,28 @@
// WITH_RUNTIME
// IGNORE_BACKEND: JS
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.toDelegateFor(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,39 @@
// WITH_RUNTIME
// IGNORE_BACKEND: JS
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.toDelegateFor(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,32 @@
// WITH_RUNTIME
// IGNORE_BACKEND: JS
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.toDelegateFor(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,27 @@
// WITH_RUNTIME
// IGNORE_BACKEND: JS
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.toDelegateFor(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
}