Add library classes for delegated properties

This commit is contained in:
Natalia.Ukhorskaya
2013-05-20 13:10:35 +04:00
parent a5f1a8b3f8
commit 97c33b02ab
6 changed files with 629 additions and 2 deletions
@@ -0,0 +1,64 @@
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
}
abstract class DelegationTestBase: TestCase() {
fun doTest(klass: WithBox) {
assertEquals("OK", klass.box())
}
}
class DelegationTest(): DelegationTestBase() {
fun testNotNullVar() {
doTest(TestNotNullVar("a", "b"))
}
fun testObservableProperty() {
doTest(TestObservableProperty())
}
}
public class TestNotNullVar<T>(val a1: String, val b1: T): WithBox {
var a: String by NotNullVar<String>()
var b by NotNullVar<T>()
override fun box(): String {
a = a1
b = b1
if (a != "a") return "fail: a shouuld be a, but was $a"
if (b != "b") return "fail: b should be b, but was $b"
return "OK"
}
}
class TestObservableProperty: WithBox, ChangeSupport() {
var b by property(init = 2)
var c by property(3)
override fun box(): String {
var result = false
addChangeListener("b", object: ChangeListener {
public override fun onPropertyChange(event: ChangeEvent) {
result = true
}
})
addChangeListener("c", object: ChangeListener {
public override fun onPropertyChange(event: ChangeEvent) {
result = false
}
})
b = 4
if (b != 4) return "fail: b != 4"
if (!result) return "fail: result should be true"
return "OK"
}
}
@@ -0,0 +1,224 @@
package test.properties.delegation
import java.util.HashMap
import kotlin.properties.delegation.*
class MapDelegationTest(): DelegationTestBase() {
fun testMapPropertyString() {
doTest(TestMapPropertyString())
}
fun testMapValWithDifferentTypes() {
doTest(TestMapValWithDifferentTypes())
}
fun testMapVarWithDifferentTypes() {
doTest(TestMapVarWithDifferentTypes())
}
fun testNullableKey() {
doTest(TestNullableKey())
}
fun testMapPropertyKey() {
doTest(TestMapPropertyKey())
}
fun testMapPropertyFunction() {
doTest(TestMapPropertyFunction())
}
fun testMapPropertyCustom() {
doTest(TestMapPropertyCustom())
}
fun testMapValWithDefault() {
doTest(TestMapValWithDefault())
}
fun testMapVarWithDefault() {
doTest(TestMapVarWithDefault())
}
fun testMapPropertyCustomWithDefault() {
doTest(TestMapPropertyCustomWithDefault())
}
}
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?>()
override fun box(): String {
if (a != "a") return "fail at 'a'"
if (b != 1) return "fail at 'b'"
if (c != A(1)) return "fail at 'c'"
if (d != null) return "fail at 'd'"
return "OK"
}
data class A(val a: Int)
}
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?>()
override fun box(): String {
a = "aa"
b = 11
c = A(11)
d = null
if (a != "aa") return "fail at 'a'"
if (b != 11) return "fail at 'b'"
if (c != A(11)) return "fail at 'c'"
if (d != null) return "fail at 'd'"
return "OK"
}
data class A(val a: Int)
}
class TestNullableKey: WithBox {
val map = hashMapOf(null to "null")
var a by map.property<String?> { desc -> null }
override fun box(): String {
if (a != "null") return "fail at 'a'"
a = "foo"
if (a != "foo") return "fail at 'a' after set"
return "OK"
}
}
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>()
override fun box(): String {
b = "newB"
if (a != "a") return "fail at 'a'"
if (b != "newB") return "fail at 'b'"
if (c != "c") return "fail at 'c'"
return "OK"
}
}
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 })
override fun box(): String {
if (a != "aDefault") return "fail at 'a'"
if (b != "bDefault") return "fail at 'b'"
if (c != "cDefault") return "fail at 'c'"
return "OK"
}
}
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 })
override fun box(): String {
if (a != "aDefault") return "fail at 'a'"
if (b != "bDefault") return "fail at 'b'"
if (c != "cDefault") return "fail at 'c'"
a = "a"
b = "b"
c = "c"
if (a != "a") return "fail at 'a' after set"
if (b != "b") return "fail at 'b' after set"
if (c != "c") return "fail at 'c' after set"
return "OK"
}
}
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")
override fun box(): String {
b = "c"
if (a != "a") return "fail at 'a'"
if (b != "c") return "fail at 'b'"
return "OK"
}
}
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" }
override fun box(): String {
b = "c"
if (a != "a") return "fail at 'a'"
if (b != "c") return "fail at 'b' after set"
return "OK"
}
}
val mapVal = object : MapVal<TestMapPropertyCustom, String>() {
override fun getMap(thisRef: TestMapPropertyCustom) = thisRef.map
override fun getKey(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"
}
class TestMapPropertyCustom(): WithBox {
val map = hashMapOf("aDesc" to "a", "bDesc" to "b")
val a by mapVal
var b by mapVar
override fun box(): String {
b = "newB"
if (a != "a") return "fail at 'a'"
if (b != "newB") return "fail at 'b' after set"
return "OK"
}
}
val mapValWithDefault = object : MapVal<TestMapPropertyCustomWithDefault, String>() {
override fun getMap(thisRef: TestMapPropertyCustomWithDefault) = thisRef.map
override fun getKey(desc: PropertyMetadata) = desc.name
override fun getDefaultValue(desc: PropertyMetadata, key: Any?) = "default"
}
val mapVarWithDefault = object : MapVar<TestMapPropertyCustomWithDefault, String>() {
override fun getMap(thisRef: TestMapPropertyCustomWithDefault) = thisRef.map
override fun getKey(desc: PropertyMetadata) = desc.name
override fun getDefaultValue(desc: PropertyMetadata, key: Any?) = "default"
}
class TestMapPropertyCustomWithDefault(): WithBox {
val map = hashMapOf<String, String>()
val a by mapValWithDefault
var b by mapVarWithDefault
override fun box(): String {
if (a != "default") return "fail at 'a'"
if (b != "default") return "fail at 'b'"
b = "c"
if (b != "c") return "fail at 'b' after set"
return "OK"
}
}
@@ -0,0 +1,143 @@
package test.properties.delegation.lazy
import kotlin.properties.delegation.lazy.*
import test.properties.delegation.WithBox
import test.properties.delegation.DelegationTestBase
class LazyValuesTest(): DelegationTestBase() {
fun testLazyVal() {
doTest(TestLazyVal())
}
fun testNullableLazyVal() {
doTest(TestNullableLazyVal())
}
fun testAtomicNullableLazyVal() {
doTest(TestAtomicNullableLazyVal())
}
fun testAtomicLazyVal() {
doTest(TestAtomicLazyVal())
}
fun testVolatileNullableLazyVal() {
doTest(TestVolatileNullableLazyVal())
}
fun testVolatileLazyVal() {
doTest(TestVolatileLazyVal())
}
}
class TestLazyVal: WithBox {
var result = 0
val a by LazyVal {
++result
}
override fun box(): String {
a
if (a != 1) return "fail: initializer should be invoked only once"
return "OK"
}
}
class TestNullableLazyVal: WithBox {
var resultA = 0
var resultB = 0
val a: Int? by NullableLazyVal { resultA++; null}
val b by NullableLazyVal { foo() }
override fun box(): String {
a
b
if (a != null) return "fail: a should be null"
if (b != null) return "fail: a should be null"
if (resultA != 1) return "fail: initializer for a should be invoked only once"
if (resultB != 1) return "fail: initializer for b should be invoked only once"
return "OK"
}
fun foo(): String? {
resultB++
return null
}
}
class TestAtomicLazyVal: WithBox {
var result = 0
val a by AtomicLazyVal {
++result
}
override fun box(): String {
a
if (a != 1) return "fail: initializer should be invoked only once"
return "OK"
}
}
class TestVolatileNullableLazyVal: WithBox {
var resultA = 0
var resultB = 0
val a: Int? by VolatileNullableLazyVal { resultA++; null}
val b by VolatileNullableLazyVal { foo() }
override fun box(): String {
a
b
if (a != null) return "fail: a should be null"
if (b != null) return "fail: a should be null"
if (resultA != 1) return "fail: initializer for a should be invoked only once"
if (resultB != 1) return "fail: initializer for b should be invoked only once"
return "OK"
}
fun foo(): String? {
resultB++
return null
}
}
class TestVolatileLazyVal: WithBox {
var result = 0
val a by VolatileLazyVal {
++result
}
override fun box(): String {
a
if (a != 1) return "fail: initializer should be invoked only once"
return "OK"
}
}
class TestAtomicNullableLazyVal: WithBox {
var resultA = 0
var resultB = 0
val a: Int? by AtomicNullableLazyVal { resultA++; null}
val b by AtomicNullableLazyVal { foo() }
override fun box(): String {
a
b
if (a != null) return "fail: a should be null"
if (b != null) return "fail: a should be null"
if (resultA != 1) return "fail: initializer for a should be invoked only once"
if (resultB != 1) return "fail: initializer for b should be invoked only once"
return "OK"
}
fun foo(): String? {
resultB++
return null
}
}