Delegate properties stdlib, take 2.

This commit is contained in:
Maxim Shafirov
2013-05-30 17:54:27 +04:00
parent a954508284
commit 58914cdf33
7 changed files with 236 additions and 256 deletions
@@ -3,8 +3,6 @@ package test.properties.delegation
import junit.framework.TestCase
import kotlin.test.*
import kotlin.properties.*
import kotlin.properties.delegation.*
import kotlin.properties.delegation.lazy.*
trait WithBox {
fun box(): String
@@ -27,8 +25,8 @@ class DelegationTest(): DelegationTestBase() {
}
public class TestNotNullVar<T>(val a1: String, val b1: T): WithBox {
var a: String by NotNullVar<String>()
var b by NotNullVar<T>()
var a: String by Delegates.notNull<String>()
var b by Delegates.notNull<T>()
override fun box(): String {
a = a1
@@ -61,4 +59,4 @@ class TestObservableProperty: WithBox, ChangeSupport() {
if (!result) return "fail: result should be true"
return "OK"
}
}
}
@@ -1,7 +1,7 @@
package test.properties.delegation
import java.util.HashMap
import kotlin.properties.delegation.*
import kotlin.properties.*
class MapDelegationTest(): DelegationTestBase() {
@@ -48,10 +48,10 @@ class MapDelegationTest(): DelegationTestBase() {
class TestMapValWithDifferentTypes(): WithBox {
val map = hashMapOf("a" to "a", "b" to 1, "c" to A(1), "d" to null)
val a by map.readOnlyProperty<String>()
val b by map.readOnlyProperty<Int>()
val c by map.readOnlyProperty<Any>()
val d by map.readOnlyProperty<Int?>()
val a by Delegates.mapVal<String>(map)
val b by Delegates.mapVal<Int>(map)
val c by Delegates.mapVal<Any>(map)
val d by Delegates.mapVal<Int?>(map)
override fun box(): String {
if (a != "a") return "fail at 'a'"
@@ -66,10 +66,10 @@ class TestMapValWithDifferentTypes(): WithBox {
class TestMapVarWithDifferentTypes(): WithBox {
val map: HashMap<String, Any?> = hashMapOf("a" to "a", "b" to 1, "c" to A(1), "d" to "d")
var a by map.property<String>()
var b by map.property<Int>()
var c by map.property<Any>()
var d by map.property<String?>()
var a by Delegates.mapVar<String>(map)
var b by Delegates.mapVar<Int>(map)
var c by Delegates.mapVar<Any>(map)
var d by Delegates.mapVar<String?>(map)
override fun box(): String {
a = "aa"
@@ -87,8 +87,8 @@ class TestMapVarWithDifferentTypes(): WithBox {
}
class TestNullableKey: WithBox {
val map = hashMapOf(null to "null")
var a by map.property<String?> { desc -> null }
val map = hashMapOf(null:Any? to "null": Any?)
var a by FixedMapVar<Any?, Any?, Any?>(map, key = { desc -> null }, default = {ref, desc -> null})
override fun box(): String {
if (a != "null") return "fail at 'a'"
@@ -99,10 +99,10 @@ class TestNullableKey: WithBox {
}
class TestMapPropertyString(): WithBox {
val map = hashMapOf("a" to "a", "b" to "b", "c" to "c")
val a by map.readOnlyProperty<String>()
var b by map.property<String>()
val c by map.property<String>()
val map = hashMapOf("a" to "a", "b" to "b", "c" to "c":Any?)
val a by Delegates.mapVal<String>(map)
var b by Delegates.mapVar<String>(map)
val c by Delegates.mapVal<String>(map)
override fun box(): String {
b = "newB"
@@ -115,9 +115,9 @@ class TestMapPropertyString(): WithBox {
class TestMapValWithDefault(): WithBox {
val map = hashMapOf<String, String>()
val a by map.readOnlyProperty<String>(default = { "aDefault" })
val b by map.readOnlyProperty<String>(default = { "bDefault" }, key = "b")
val c by map.readOnlyProperty<String>(default = { "cDefault" }, key = { desc -> desc.name })
val a by Delegates.mapVal<String>(map, default = { ref, desc -> "aDefault" })
val b by FixedMapVal<TestMapValWithDefault, String, String>(map, default = { ref, desc -> "bDefault" }, key = {"b"})
val c by FixedMapVal<TestMapValWithDefault, String, String>(map, default = { ref, desc -> "cDefault" }, key = { desc -> desc.name })
override fun box(): String {
if (a != "aDefault") return "fail at 'a'"
@@ -128,10 +128,10 @@ class TestMapValWithDefault(): WithBox {
}
class TestMapVarWithDefault(): WithBox {
val map = hashMapOf<String, String>()
var a by map.property<String>(default = { "aDefault" })
var b by map.property<String>(default = { "bDefault" }, key = "b")
var c by map.property<String>(default = { "cDefault" }, key = { desc -> desc.name })
val map = hashMapOf<String, Any?>()
var a: String by Delegates.mapVar(map, default = {ref, desc -> "aDefault" })
var b: String by FixedMapVar<Any?, String, String>(map, default = {ref, desc -> "bDefault" }, key = {"b"})
var c: String by FixedMapVar<Any?, String, String>(map, default = {ref, desc -> "cDefault" }, key = { desc -> desc.name })
override fun box(): String {
if (a != "aDefault") return "fail at 'a'"
@@ -148,9 +148,9 @@ class TestMapVarWithDefault(): WithBox {
}
class TestMapPropertyKey(): WithBox {
val map = hashMapOf("a" to "a", "b" to "b")
val a by map.readOnlyProperty<String>(key = "a")
var b by map.property<String>(key = "b")
val map = hashMapOf("a" to "a", "b" to "b" : Any?)
val a by FixedMapVal<Any?, String, String>(map, key = {"a"})
var b by FixedMapVar<Any?, String, String>(map, key = {"b"})
override fun box(): String {
b = "c"
@@ -161,9 +161,9 @@ class TestMapPropertyKey(): WithBox {
}
class TestMapPropertyFunction(): WithBox {
val map = hashMapOf("aDesc" to "a", "bDesc" to "b")
val a by map.readOnlyProperty<String> { desc -> "${desc.name}Desc" }
var b by map.property<String> { desc -> "${desc.name}Desc" }
val map = hashMapOf("aDesc" to "a", "bDesc" to "b": Any?)
val a by FixedMapVal<Any?, String, String>(map, { desc -> "${desc.name}Desc" })
var b by FixedMapVar<Any?, String, String>(map, { desc -> "${desc.name}Desc" })
override fun box(): String {
b = "c"
@@ -173,17 +173,18 @@ class TestMapPropertyFunction(): WithBox {
}
}
val mapVal = object : MapVal<TestMapPropertyCustom, String>() {
override fun getMap(thisRef: TestMapPropertyCustom) = thisRef.map
override fun getKey(desc: PropertyMetadata) = "${desc.name}Desc"
val mapVal = object: MapVal<TestMapPropertyCustom, String, String>() {
override fun map(ref: TestMapPropertyCustom) = ref.map
override fun key(desc: PropertyMetadata) = "${desc.name}Desc"
}
val mapVar = object : MapVar<TestMapPropertyCustom, String>() {
override fun getMap(thisRef: TestMapPropertyCustom) = thisRef.map
override fun getKey(desc: PropertyMetadata) = "${desc.name}Desc"
val mapVar = object : MapVar<TestMapPropertyCustom, String, String>() {
override fun map(ref: TestMapPropertyCustom) = ref.map
override fun key(desc: PropertyMetadata) = "${desc.name}Desc"
}
class TestMapPropertyCustom(): WithBox {
val map = hashMapOf("aDesc" to "a", "bDesc" to "b")
val map = hashMapOf("aDesc" to "a", "bDesc" to "b":Any?)
val a by mapVal
var b by mapVar
@@ -195,22 +196,22 @@ class TestMapPropertyCustom(): WithBox {
}
}
val mapValWithDefault = object : MapVal<TestMapPropertyCustomWithDefault, String>() {
override fun getMap(thisRef: TestMapPropertyCustomWithDefault) = thisRef.map
override fun getKey(desc: PropertyMetadata) = desc.name
val mapValWithDefault = object : MapVal<TestMapPropertyCustomWithDefault, String, String>() {
override fun map(ref: TestMapPropertyCustomWithDefault) = ref.map
override fun key(desc: PropertyMetadata) = desc.name
override fun getDefaultValue(desc: PropertyMetadata, key: Any?) = "default"
override fun default(ref: TestMapPropertyCustomWithDefault, key: PropertyMetadata) = "default"
}
val mapVarWithDefault = object : MapVar<TestMapPropertyCustomWithDefault, String>() {
override fun getMap(thisRef: TestMapPropertyCustomWithDefault) = thisRef.map
override fun getKey(desc: PropertyMetadata) = desc.name
val mapVarWithDefault = object : MapVar<TestMapPropertyCustomWithDefault, String, String>() {
override fun map(ref: TestMapPropertyCustomWithDefault) = ref.map
override fun key(desc: PropertyMetadata) = desc.name
override fun getDefaultValue(desc: PropertyMetadata, key: Any?) = "default"
override fun default(ref: TestMapPropertyCustomWithDefault, key: PropertyMetadata) = "default"
}
class TestMapPropertyCustomWithDefault(): WithBox {
val map = hashMapOf<String, String>()
val map = hashMapOf<String, Any?>()
val a by mapValWithDefault
var b by mapVarWithDefault
@@ -221,4 +222,4 @@ class TestMapPropertyCustomWithDefault(): WithBox {
if (b != "c") return "fail at 'b' after set"
return "OK"
}
}
}
@@ -1,8 +1,8 @@
package test.properties.delegation.lazy
import kotlin.properties.delegation.lazy.*
import test.properties.delegation.WithBox
import test.properties.delegation.DelegationTestBase
import kotlin.properties.*
class LazyValuesTest(): DelegationTestBase() {
@@ -33,7 +33,7 @@ class LazyValuesTest(): DelegationTestBase() {
class TestLazyVal: WithBox {
var result = 0
val a by LazyVal {
val a by Delegates.lazy {
++result
}
@@ -48,8 +48,8 @@ class TestNullableLazyVal: WithBox {
var resultA = 0
var resultB = 0
val a: Int? by NullableLazyVal { resultA++; null}
val b by NullableLazyVal { foo() }
val a: Int? by Delegates.lazy { resultA++; null}
val b by Delegates.lazy { foo() }
override fun box(): String {
a
@@ -70,7 +70,7 @@ class TestNullableLazyVal: WithBox {
class TestAtomicLazyVal: WithBox {
var result = 0
val a by AtomicLazyVal {
val a by Delegates.blockingLazy {
++result
}
@@ -85,8 +85,8 @@ class TestVolatileNullableLazyVal: WithBox {
var resultA = 0
var resultB = 0
val a: Int? by VolatileNullableLazyVal { resultA++; null}
val b by VolatileNullableLazyVal { foo() }
val a: Int? by Delegates.blockingLazy { resultA++; null}
val b by Delegates.blockingLazy { foo() }
override fun box(): String {
a
@@ -107,7 +107,7 @@ class TestVolatileNullableLazyVal: WithBox {
class TestVolatileLazyVal: WithBox {
var result = 0
val a by VolatileLazyVal {
val a by Delegates.blockingLazy {
++result
}
@@ -122,8 +122,8 @@ class TestAtomicNullableLazyVal: WithBox {
var resultA = 0
var resultB = 0
val a: Int? by AtomicNullableLazyVal { resultA++; null}
val b by AtomicNullableLazyVal { foo() }
val a: Int? by Delegates.blockingLazy { resultA++; null}
val b by Delegates.blockingLazy { foo() }
override fun box(): String {
a