Move some tests from boxWithStdlib/ to box/
Move those tests which do not require neither stdlib nor reflect
This commit is contained in:
committed by
Alexander Udalov
parent
54a615fcd3
commit
20e36438e2
@@ -0,0 +1,9 @@
|
||||
abstract class Base {
|
||||
val result = "OK"
|
||||
}
|
||||
|
||||
class Derived : Base()
|
||||
|
||||
fun box(): String {
|
||||
return (Base::result).get(Derived())
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
val four: Int by NumberDecrypter
|
||||
|
||||
class A {
|
||||
val two: Int by NumberDecrypter
|
||||
}
|
||||
|
||||
object NumberDecrypter {
|
||||
operator fun getValue(instance: Any?, data: KProperty<*>) = when (data.name) {
|
||||
"four" -> 4
|
||||
"two" -> 2
|
||||
else -> throw AssertionError()
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val x = ::four.get()
|
||||
if (x != 4) return "Fail x: $x"
|
||||
val a = A()
|
||||
val y = A::two.get(a)
|
||||
if (y != 2) return "Fail y: $y"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
var result: String by Delegate
|
||||
|
||||
object Delegate {
|
||||
var value = "lol"
|
||||
|
||||
operator fun getValue(instance: Any?, data: KProperty<*>): String {
|
||||
return value
|
||||
}
|
||||
|
||||
operator fun setValue(instance: Any?, data: KProperty<*>, newValue: String) {
|
||||
value = newValue
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val f = ::result
|
||||
if (f.get() != "lol") return "Fail 1: {$f.get()}"
|
||||
Delegate.value = "rofl"
|
||||
if (f.get() != "rofl") return "Fail 2: {$f.get()}"
|
||||
f.set("OK")
|
||||
return f.get()
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
enum class E {
|
||||
I
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val i = (E::name).get(E.I)
|
||||
if (i != "I") return "Fail $i"
|
||||
val n = (E::ordinal).get(E.I)
|
||||
if (n != 0) return "Fail $n"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
val Array<String>.firstElement: String get() = get(0)
|
||||
|
||||
fun box(): String {
|
||||
val p = Array<String>::firstElement
|
||||
return p.get(arrayOf("OK", "Fail"))
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
//For KT-6020
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.KMutableProperty1
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class Value<T>(var value: T = null as T, var text: String? = null)
|
||||
|
||||
val <T> Value<T>.additionalText by DVal(Value<T>::text) //works
|
||||
|
||||
val <T> Value<T>.additionalValue by DVal(Value<T>::value) //not work
|
||||
|
||||
class DVal<T, R, P: KProperty1<T, R>>(val kmember: P) {
|
||||
operator fun getValue(t: T, p: KProperty<*>): R {
|
||||
return kmember.get(t)
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val p = Value("O", "K")
|
||||
return p.additionalValue + p.additionalText
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
var state = ""
|
||||
|
||||
var topLevel: Int
|
||||
get() {
|
||||
state += "1"
|
||||
return 42
|
||||
}
|
||||
set(value) {
|
||||
throw AssertionError("Nooo")
|
||||
}
|
||||
|
||||
class A {
|
||||
val member: String
|
||||
get() {
|
||||
state += "2"
|
||||
return "42"
|
||||
}
|
||||
}
|
||||
|
||||
val A.ext: Any
|
||||
get() {
|
||||
state += "3"
|
||||
return this
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
(::topLevel)()
|
||||
(A::member)(A())
|
||||
(A::ext)(A())
|
||||
return if (state == "123") "OK" else "Fail $state"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// Name of the getter should be 'getaBcde' according to JavaBean conventions
|
||||
var aBcde: Int = 239
|
||||
|
||||
fun box(): String {
|
||||
val x = (::aBcde).get()
|
||||
if (x != 239) return "Fail x: $x"
|
||||
|
||||
(::aBcde).set(42)
|
||||
|
||||
val y = (::aBcde).get()
|
||||
if (y != 42) return "Fail y: $y"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
import kotlin.reflect.KProperty1
|
||||
|
||||
class A {
|
||||
companion object {
|
||||
val ref: KProperty1<A, String> = A::foo
|
||||
}
|
||||
|
||||
val foo: String = "OK"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
return A.ref.get(A())
|
||||
}
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
class Test {
|
||||
private var iv = 1
|
||||
|
||||
public fun exec() {
|
||||
val t = object : Thread() {
|
||||
override fun run() {
|
||||
Test::iv.get(this@Test)
|
||||
Test::iv.set(this@Test, 2)
|
||||
}
|
||||
}
|
||||
t.start()
|
||||
t.join(1000)
|
||||
}
|
||||
|
||||
fun result() = if (iv == 2) "OK" else "Fail $iv"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val t = Test()
|
||||
t.exec()
|
||||
return t.result()
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun box(): String {
|
||||
class Local {
|
||||
var result = "Fail"
|
||||
}
|
||||
|
||||
val l = Local()
|
||||
(Local::result).set(l, "OK")
|
||||
return (Local::result).get(l)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
open class Base {
|
||||
open val foo = "Base"
|
||||
}
|
||||
|
||||
class Derived : Base() {
|
||||
override val foo = "OK"
|
||||
}
|
||||
|
||||
fun box() = (Base::foo).get(Derived())
|
||||
@@ -0,0 +1,12 @@
|
||||
val String.id: String
|
||||
get() = this
|
||||
|
||||
fun box(): String {
|
||||
val pr = String::id
|
||||
|
||||
if (pr.get("123") != "123") return "Fail value: ${pr.get("123")}"
|
||||
|
||||
if (pr.name != "id") return "Fail name: ${pr.name}"
|
||||
|
||||
return pr.get("OK")
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class A(val x: Int)
|
||||
|
||||
fun box(): String {
|
||||
val p = A::x
|
||||
if (p.get(A(42)) != 42) return "Fail 1"
|
||||
if (p.get(A(-1)) != -1) return "Fail 2"
|
||||
if (p.name != "x") return "Fail 3"
|
||||
return "OK"
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
var storage = 0
|
||||
|
||||
var Int.foo: Int
|
||||
get() {
|
||||
return this + storage
|
||||
}
|
||||
set(value) {
|
||||
storage = this + value
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val pr = Int::foo
|
||||
if (pr.get(42) != 42) return "Fail 1: ${pr.get(42)}"
|
||||
pr.set(200, 39)
|
||||
if (pr.get(-239) != 0) return "Fail 2: ${pr.get(-239)}"
|
||||
if (storage != 239) return "Fail 3: $storage"
|
||||
return "OK"
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
data class Box(var value: String)
|
||||
|
||||
fun box(): String {
|
||||
val o = Box("lorem")
|
||||
val prop = Box::value
|
||||
|
||||
if (prop.get(o) != "lorem") return "Fail 1: ${prop.get(o)}"
|
||||
prop.set(o, "ipsum")
|
||||
if (prop.get(o) != "ipsum") return "Fail 2: ${prop.get(o)}"
|
||||
if (o.value != "ipsum") return "Fail 3: ${o.value}"
|
||||
o.value = "dolor"
|
||||
if (prop.get(o) != "dolor") return "Fail 4: ${prop.get(o)}"
|
||||
if ("$o" != "Box(value=dolor)") return "Fail 5: $o"
|
||||
|
||||
return "OK"
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
data class Box(val value: String)
|
||||
|
||||
var pr = Box("first")
|
||||
|
||||
fun box(): String {
|
||||
val property = ::pr
|
||||
if (property.get() != Box("first")) return "Fail value: ${property.get()}"
|
||||
if (property.name != "pr") return "Fail name: ${property.name}"
|
||||
property.set(Box("second"))
|
||||
if (property.get().value != "second") return "Fail value 2: ${property.get()}"
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
data class Box(val value: String)
|
||||
|
||||
val foo = Box("lol")
|
||||
|
||||
fun box(): String {
|
||||
val property = ::foo
|
||||
if (property.get() != Box("lol")) return "Fail value: ${property.get()}"
|
||||
if (property.name != "foo") return "Fail name: ${property.name}"
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user