1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package test.properties.delegation
import kotlin.test.*
import kotlin.properties.*
class NotNullVarTest() {
@Test fun doTest() {
NotNullVarTestGeneric("a", "b").doTest()
}
}
private class NotNullVarTestGeneric<T : Any>(val a1: String, val b1: T) {
var a: String by Delegates.notNull()
var b by Delegates.notNull<T>()
public fun doTest() {
a = a1
b = b1
assertTrue(a == "a", "fail: a should be a, but was $a")
assertTrue(b == "b", "fail: b should be b, but was $b")
}
}
class ObservablePropertyTest {
var result = false
var b: Int by Delegates.observable(1, { property, old, new ->
assertEquals("b", property.name)
if (!result) assertEquals(1, old)
result = true
assertEquals(new, b, "New value has already been set")
})
@Test fun doTest() {
b = 4
assertTrue(b == 4, "fail: b != 4")
assertTrue(result, "fail: result should be true")
}
}
class A(val p: Boolean)
class VetoablePropertyTest {
var result = false
var b: A by Delegates.vetoable(A(true), { property, old, new ->
assertEquals("b", property.name)
assertEquals(old, b, "New value hasn't been set yet")
result = new.p == true;
result
})
@Test fun doTest() {
val firstValue = A(true)
b = firstValue
assertTrue(b == firstValue, "fail1: b should be firstValue = A(true)")
assertTrue(result, "fail2: result should be true")
b = A(false)
assertTrue(b == firstValue, "fail3: b should be firstValue = A(true)")
assertFalse(result, "fail4: result should be false")
}
}
|