Refactor delegation unit tests.
This commit is contained in:
@@ -4,53 +4,30 @@ import org.junit.Test as test
|
||||
import kotlin.test.*
|
||||
import kotlin.properties.*
|
||||
|
||||
interface WithBox {
|
||||
fun box(): String
|
||||
}
|
||||
|
||||
abstract class DelegationTestBase {
|
||||
fun doTest(klass: WithBox) {
|
||||
assertEquals("OK", klass.box())
|
||||
class NotNullVarTest() {
|
||||
test fun doTest() {
|
||||
NotNullVarTestGeneric("a", "b").doTest()
|
||||
}
|
||||
}
|
||||
|
||||
class DelegationTest(): DelegationTestBase() {
|
||||
test fun testNotNullVar() {
|
||||
doTest(TestNotNullVar("a", "b"))
|
||||
}
|
||||
|
||||
test fun testObservablePropertyInChangeSupport() {
|
||||
doTest(TestObservablePropertyInChangeSupport())
|
||||
}
|
||||
|
||||
test fun testObservableProperty() {
|
||||
doTest(TestObservableProperty())
|
||||
}
|
||||
|
||||
test fun testVetoableProperty() {
|
||||
doTest(TestVetoableProperty())
|
||||
}
|
||||
}
|
||||
|
||||
public class TestNotNullVar<T>(val a1: String, val b1: T): WithBox {
|
||||
private class NotNullVarTestGeneric<T>(val a1: String, val b1: T) {
|
||||
var a: String by Delegates.notNull()
|
||||
var b by Delegates.notNull<T>()
|
||||
|
||||
override fun box(): String {
|
||||
public fun doTest() {
|
||||
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"
|
||||
assertTrue(a == "a", "fail: a should be a, but was $a")
|
||||
assertTrue(b == "b", "fail: b should be b, but was $b")
|
||||
}
|
||||
}
|
||||
|
||||
class TestObservablePropertyInChangeSupport: WithBox, ChangeSupport() {
|
||||
class ObservablePropertyInChangeSupportTest: ChangeSupport() {
|
||||
|
||||
var b by property(init = 2)
|
||||
var c by property(3)
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
var result = false
|
||||
addChangeListener("b", object: ChangeListener {
|
||||
public override fun onPropertyChange(event: ChangeEvent) {
|
||||
@@ -63,39 +40,36 @@ class TestObservablePropertyInChangeSupport: WithBox, ChangeSupport() {
|
||||
}
|
||||
})
|
||||
b = 4
|
||||
if (b != 4) return "fail: b != 4"
|
||||
if (!result) return "fail: result should be true"
|
||||
return "OK"
|
||||
assertTrue(b == 4, "fail: b != 4")
|
||||
assertTrue(result, "fail: result should be true")
|
||||
}
|
||||
}
|
||||
|
||||
class TestObservableProperty: WithBox {
|
||||
class ObservablePropertyTest {
|
||||
var result = false
|
||||
|
||||
var b by Delegates.observable(1, { pd, o, n -> result = true})
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
b = 4
|
||||
if (b != 4) return "fail: b != 4"
|
||||
if (!result) return "fail: result should be true"
|
||||
return "OK"
|
||||
assertTrue(b == 4, "fail: b != 4")
|
||||
assertTrue(result, "fail: result should be true")
|
||||
}
|
||||
}
|
||||
|
||||
class A(val p: Boolean)
|
||||
|
||||
class TestVetoableProperty: WithBox {
|
||||
class VetoablePropertyTest {
|
||||
var result = false
|
||||
var b by Delegates.vetoable(A(true), { pd, o, n -> result = n.p == true; result})
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
val firstValue = A(true)
|
||||
b = firstValue
|
||||
if (b != firstValue) return "fail1: b should be firstValue = A(true)"
|
||||
if (!result) return "fail2: result should be true"
|
||||
assertTrue(b == firstValue, "fail1: b should be firstValue = A(true)")
|
||||
assertTrue(result, "fail2: result should be true")
|
||||
b = A(false)
|
||||
if (b != firstValue) return "fail3: b should be firstValue = A(true)"
|
||||
if (result) return "fail4: result should be false"
|
||||
return "OK"
|
||||
assertTrue(b == firstValue, "fail3: b should be firstValue = A(true)")
|
||||
assertFalse(result, "fail4: result should be false")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test.properties.delegation
|
||||
package test.properties.delegation.map
|
||||
|
||||
import java.util.*
|
||||
import kotlin.properties.*
|
||||
|
||||
@@ -1,228 +1,175 @@
|
||||
package test.properties.delegation
|
||||
package test.properties.delegation.map
|
||||
|
||||
import org.junit.Test as test
|
||||
import java.util.HashMap
|
||||
import kotlin.properties.*
|
||||
import kotlin.test.*
|
||||
|
||||
class MapDelegationTest(): DelegationTestBase() {
|
||||
|
||||
test fun testMapPropertyString() {
|
||||
doTest(TestMapPropertyString())
|
||||
}
|
||||
|
||||
test fun testMapValWithDifferentTypes() {
|
||||
doTest(TestMapValWithDifferentTypes())
|
||||
}
|
||||
|
||||
test fun testMapVarWithDifferentTypes() {
|
||||
doTest(TestMapVarWithDifferentTypes())
|
||||
}
|
||||
|
||||
test fun testNullableKey() {
|
||||
doTest(TestNullableKey())
|
||||
}
|
||||
|
||||
test fun testMapPropertyKey() {
|
||||
doTest(TestMapPropertyKey())
|
||||
}
|
||||
|
||||
test fun testMapPropertyFunction() {
|
||||
doTest(TestMapPropertyFunction())
|
||||
}
|
||||
|
||||
test fun testMapPropertyCustom() {
|
||||
doTest(TestMapPropertyCustom())
|
||||
}
|
||||
|
||||
test fun testMapValWithDefault() {
|
||||
doTest(TestMapValWithDefault())
|
||||
}
|
||||
|
||||
test fun testMapVarWithDefault() {
|
||||
doTest(TestMapVarWithDefault())
|
||||
}
|
||||
|
||||
test fun testMapPropertyCustomWithDefault() {
|
||||
doTest(TestMapPropertyCustomWithDefault())
|
||||
}
|
||||
}
|
||||
|
||||
data class B(val a: Int)
|
||||
|
||||
class TestMapValWithDifferentTypes(): WithBox {
|
||||
class MapValWithDifferentTypesTest() {
|
||||
val map = hashMapOf("a" to "a", "b" to 1, "c" to B(1), "d" to null)
|
||||
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'"
|
||||
if (b != 1) return "fail at 'b'"
|
||||
if (c != B(1)) return "fail at 'c'"
|
||||
if (d != null) return "fail at 'd'"
|
||||
return "OK"
|
||||
test fun doTest() {
|
||||
assertTrue(a == "a", "fail at 'a'")
|
||||
assertTrue(b == 1, "fail at 'b'")
|
||||
assertTrue(c == B(1), "fail at 'c'")
|
||||
assertTrue(d == null, "fail at 'd'")
|
||||
}
|
||||
}
|
||||
|
||||
class TestMapVarWithDifferentTypes(): WithBox {
|
||||
class MapVarWithDifferentTypesTest() {
|
||||
val map: HashMap<String, Any?> = hashMapOf("a" to "a", "b" to 1, "c" to B(1), "d" to "d")
|
||||
var a: String by Delegates.mapVar(map)
|
||||
var b: Int by Delegates.mapVar(map)
|
||||
var c by Delegates.mapVar<Any>(map)
|
||||
var d by Delegates.mapVar<String?>(map)
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
a = "aa"
|
||||
b = 11
|
||||
c = B(11)
|
||||
d = null
|
||||
if (a != "aa") return "fail at 'a'"
|
||||
if (b != 11) return "fail at 'b'"
|
||||
if (c != B(11)) return "fail at 'c'"
|
||||
if (d != null) return "fail at 'd'"
|
||||
assertTrue(a == "aa", "fail at 'a'")
|
||||
assertTrue(b == 11, "fail at 'b'")
|
||||
assertTrue(c == B(11), "fail at 'c'")
|
||||
assertTrue(d == null, "fail at 'd'")
|
||||
|
||||
map["a"] = null
|
||||
a // fails { a } // does not fail due to KT-8135
|
||||
return "OK"
|
||||
}
|
||||
}
|
||||
|
||||
class TestNullableKey: WithBox {
|
||||
val map = hashMapOf(null:Any? to "null": Any?)
|
||||
class MapNullableKeyTest {
|
||||
val map = hashMapOf<Any?, Any?>(null to "null")
|
||||
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'"
|
||||
test fun doTest() {
|
||||
assertTrue(a == "null", "fail at 'a'")
|
||||
a = "foo"
|
||||
if (a != "foo") return "fail at 'a' after set"
|
||||
return "OK"
|
||||
assertTrue(a == "foo", "fail at 'a' after set")
|
||||
}
|
||||
}
|
||||
|
||||
class TestMapPropertyString(): WithBox {
|
||||
val map = hashMapOf("a" to "a", "b" to "b", "c" to "c":Any?)
|
||||
class MapPropertyStringTest() {
|
||||
val map = hashMapOf<String, Any?>("a" to "a", "b" to "b", "c" to "c")
|
||||
val a: String by Delegates.mapVal(map)
|
||||
var b by Delegates.mapVar<String>(map)
|
||||
val c by Delegates.mapVal<String>(map)
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
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"
|
||||
assertTrue(a == "a", "fail at 'a'")
|
||||
assertTrue(b == "newB", "fail at 'b'")
|
||||
assertTrue(c == "c", "fail at 'c'")
|
||||
}
|
||||
}
|
||||
|
||||
class TestMapValWithDefault(): WithBox {
|
||||
class MapValWithDefaultTest() {
|
||||
val map = hashMapOf<String, String>()
|
||||
val a: String by Delegates.mapVal(map, default = { ref, desc -> "aDefault" })
|
||||
val b: String by FixedMapVal(map, default = { ref: TestMapValWithDefault, desc: String -> "bDefault" }, key = {"b"})
|
||||
val c: String by FixedMapVal(map, default = { ref: TestMapValWithDefault, desc: String -> "cDefault" }, key = { desc -> desc.name })
|
||||
val b: String by FixedMapVal(map, default = { ref: MapValWithDefaultTest, desc: String -> "bDefault" }, key = {"b"})
|
||||
val c: String by FixedMapVal(map, default = { ref: MapValWithDefaultTest, desc: String -> "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"
|
||||
test fun doTest() {
|
||||
assertTrue(a == "aDefault", "fail at 'a'")
|
||||
assertTrue(b == "bDefault", "fail at 'b'")
|
||||
assertTrue(c == "cDefault", "fail at 'c'")
|
||||
}
|
||||
}
|
||||
|
||||
class TestMapVarWithDefault(): WithBox {
|
||||
class MapVarWithDefaultTest() {
|
||||
val map = hashMapOf<String, Any?>()
|
||||
var a: String by Delegates.mapVar(map, default = {ref, desc -> "aDefault" })
|
||||
var b: String by FixedMapVar(map, default = {ref: Any?, desc: String -> "bDefault" }, key = {"b"})
|
||||
var c: String by FixedMapVar(map, default = {ref: Any?, desc: String -> "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'"
|
||||
test fun doTest() {
|
||||
assertTrue(a == "aDefault", "fail at 'a'")
|
||||
assertTrue(b == "bDefault", "fail at 'b'")
|
||||
assertTrue(c == "cDefault", "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"
|
||||
assertTrue(a == "a", "fail at 'a' after set")
|
||||
assertTrue(b == "b", "fail at 'b' after set")
|
||||
assertTrue(c == "c", "fail at 'c' after set")
|
||||
}
|
||||
}
|
||||
|
||||
class TestMapPropertyKey(): WithBox {
|
||||
val map = hashMapOf("a" to "a", "b" to "b" : Any?)
|
||||
class MapPropertyKeyTest() {
|
||||
val map = hashMapOf<String, Any?>("a" to "a", "b" to "b")
|
||||
val a by FixedMapVal<Any?, String, String>(map, key = {"a"})
|
||||
var b by FixedMapVar<Any?, String, String>(map, key = {"b"})
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
b = "c"
|
||||
if (a != "a") return "fail at 'a'"
|
||||
if (b != "c") return "fail at 'b'"
|
||||
return "OK"
|
||||
assertTrue(a == "a", "fail at 'a'")
|
||||
assertTrue(b == "c", "fail at 'b'")
|
||||
}
|
||||
}
|
||||
|
||||
class TestMapPropertyFunction(): WithBox {
|
||||
val map = hashMapOf("aDesc" to "a", "bDesc" to "b": Any?)
|
||||
class MapPropertyFunctionTest() {
|
||||
val map = hashMapOf<String, Any?>("aDesc" to "a", "bDesc" to "b")
|
||||
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 {
|
||||
test fun doTest() {
|
||||
b = "c"
|
||||
if (a != "a") return "fail at 'a'"
|
||||
if (b != "c") return "fail at 'b' after set"
|
||||
return "OK"
|
||||
assertTrue(a == "a", "fail at 'a'")
|
||||
assertTrue(b == "c", "fail at 'b' after set")
|
||||
}
|
||||
}
|
||||
|
||||
val mapVal = object: MapVal<TestMapPropertyCustom, String, String>() {
|
||||
override fun map(ref: TestMapPropertyCustom) = ref.map
|
||||
val mapVal = object: MapVal<MapPropertyCustomTest, String, String>() {
|
||||
override fun map(ref: MapPropertyCustomTest) = ref.map
|
||||
override fun key(desc: PropertyMetadata) = "${desc.name}Desc"
|
||||
}
|
||||
|
||||
val mapVar = object : MapVar<TestMapPropertyCustom, String, String>() {
|
||||
override fun map(ref: TestMapPropertyCustom) = ref.map
|
||||
val mapVar = object : MapVar<MapPropertyCustomTest, String, String>() {
|
||||
override fun map(ref: MapPropertyCustomTest) = ref.map
|
||||
override fun key(desc: PropertyMetadata) = "${desc.name}Desc"
|
||||
}
|
||||
|
||||
class TestMapPropertyCustom(): WithBox {
|
||||
val map = hashMapOf("aDesc" to "a", "bDesc" to "b":Any?)
|
||||
class MapPropertyCustomTest() {
|
||||
val map = hashMapOf<String, Any?>("aDesc" to "a", "bDesc" to "b")
|
||||
val a by mapVal
|
||||
var b by mapVar
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
b = "newB"
|
||||
if (a != "a") return "fail at 'a'"
|
||||
if (b != "newB") return "fail at 'b' after set"
|
||||
return "OK"
|
||||
assertTrue(a == "a", "fail at 'a'")
|
||||
assertTrue(b == "newB", "fail at 'b' after set")
|
||||
}
|
||||
}
|
||||
|
||||
val mapValWithDefault = object : MapVal<TestMapPropertyCustomWithDefault, String, String>() {
|
||||
override fun map(ref: TestMapPropertyCustomWithDefault) = ref.map
|
||||
val mapValWithDefault = object : MapVal<MapPropertyCustomWithDefaultTest, String, String>() {
|
||||
override fun map(ref: MapPropertyCustomWithDefaultTest) = ref.map
|
||||
override fun key(desc: PropertyMetadata) = desc.name
|
||||
|
||||
override fun default(ref: TestMapPropertyCustomWithDefault, key: PropertyMetadata) = "default"
|
||||
override fun default(ref: MapPropertyCustomWithDefaultTest, key: PropertyMetadata) = "default"
|
||||
}
|
||||
|
||||
val mapVarWithDefault = object : MapVar<TestMapPropertyCustomWithDefault, String, String>() {
|
||||
override fun map(ref: TestMapPropertyCustomWithDefault) = ref.map
|
||||
val mapVarWithDefault = object : MapVar<MapPropertyCustomWithDefaultTest, String, String>() {
|
||||
override fun map(ref: MapPropertyCustomWithDefaultTest) = ref.map
|
||||
override fun key(desc: PropertyMetadata) = desc.name
|
||||
|
||||
override fun default(ref: TestMapPropertyCustomWithDefault, key: PropertyMetadata) = "default"
|
||||
override fun default(ref: MapPropertyCustomWithDefaultTest, key: PropertyMetadata) = "default"
|
||||
}
|
||||
|
||||
class TestMapPropertyCustomWithDefault(): WithBox {
|
||||
class MapPropertyCustomWithDefaultTest() {
|
||||
val map = hashMapOf<String, Any?>()
|
||||
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'"
|
||||
test fun doTest() {
|
||||
assertTrue(a == "default", "fail at 'a'")
|
||||
assertTrue(b == "default", "fail at 'b'")
|
||||
b = "c"
|
||||
if (b != "c") return "fail at 'b' after set"
|
||||
return "OK"
|
||||
assertTrue(b == "c", "fail at 'b' after set")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,93 +1,49 @@
|
||||
package test.properties.delegation.lazy
|
||||
|
||||
import test.properties.delegation.WithBox
|
||||
import test.properties.delegation.DelegationTestBase
|
||||
import org.junit.Test as test
|
||||
import kotlin.properties.*
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class LazyValuesTest(): DelegationTestBase() {
|
||||
|
||||
test fun testLazyVal() {
|
||||
doTest(TestLazyVal())
|
||||
}
|
||||
|
||||
test fun testUnsafeLazyVal() {
|
||||
doTest(TestUnsafeLazyVal())
|
||||
}
|
||||
|
||||
test fun testNullableLazyVal() {
|
||||
doTest(TestNullableLazyVal())
|
||||
}
|
||||
|
||||
test fun testUnsafeNullableLazyVal() {
|
||||
doTest(TestUnsafeNullableLazyVal())
|
||||
}
|
||||
|
||||
// deprecated lazy tests
|
||||
|
||||
test fun testUnsafeLazyValDeprecated() {
|
||||
doTest(TestUnsafeLazyValDeprecated())
|
||||
}
|
||||
|
||||
test fun testBlockingLazyValDeprecated() {
|
||||
doTest(TestBlockingLazyValDeprecated())
|
||||
}
|
||||
|
||||
test fun testUnsafeNullableLazyValDeprecated() {
|
||||
doTest(TestUnsafeNullableLazyValDeprecated())
|
||||
}
|
||||
|
||||
test fun testBlockingNullableLazyValDeprecated() {
|
||||
doTest(TestBlockingNullableLazyValDeprecated())
|
||||
}
|
||||
|
||||
test fun testIdentityEqualsIsUsedToUnescapeLazyVal() {
|
||||
doTest(TestIdentityEqualsIsUsedToUnescapeLazyVal())
|
||||
}
|
||||
}
|
||||
|
||||
class TestLazyVal: WithBox {
|
||||
class LazyValTest {
|
||||
var result = 0
|
||||
val a by lazy {
|
||||
++result
|
||||
}
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
a
|
||||
if (a != 1) return "fail: initializer should be invoked only once"
|
||||
return "OK"
|
||||
assertTrue(a == 1, "fail: initializer should be invoked only once")
|
||||
}
|
||||
}
|
||||
|
||||
class TestUnsafeLazyVal: WithBox {
|
||||
class UnsafeLazyValTest {
|
||||
var result = 0
|
||||
val a by lazy(LazyThreadSafetyMode.NONE) {
|
||||
++result
|
||||
}
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
a
|
||||
if (a != 1) return "fail: initializer should be invoked only once"
|
||||
return "OK"
|
||||
assertTrue(a == 1, "fail: initializer should be invoked only once")
|
||||
}
|
||||
}
|
||||
|
||||
class TestNullableLazyVal: WithBox {
|
||||
class NullableLazyValTest {
|
||||
var resultA = 0
|
||||
var resultB = 0
|
||||
|
||||
val a: Int? by lazy { resultA++; null}
|
||||
val b by lazy { foo() }
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
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"
|
||||
assertTrue(a == null, "fail: a should be null")
|
||||
assertTrue(b == null, "fail: b should be null")
|
||||
assertTrue(resultA == 1, "fail: initializer for a should be invoked only once")
|
||||
assertTrue(resultB == 1, "fail: initializer for b should be invoked only once")
|
||||
}
|
||||
|
||||
fun foo(): String? {
|
||||
@@ -96,22 +52,21 @@ class TestNullableLazyVal: WithBox {
|
||||
}
|
||||
}
|
||||
|
||||
class TestUnsafeNullableLazyVal: WithBox {
|
||||
class UnsafeNullableLazyValTest {
|
||||
var resultA = 0
|
||||
var resultB = 0
|
||||
|
||||
val a: Int? by lazy(LazyThreadSafetyMode.NONE) { resultA++; null}
|
||||
val b by lazy(LazyThreadSafetyMode.NONE) { foo() }
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
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"
|
||||
assertTrue(a == null, "fail: a should be null")
|
||||
assertTrue(b == null, "fail: a should be null")
|
||||
assertTrue(resultA == 1, "fail: initializer for a should be invoked only once")
|
||||
assertTrue(resultB == 1, "fail: initializer for b should be invoked only once")
|
||||
}
|
||||
|
||||
fun foo(): String? {
|
||||
@@ -120,48 +75,45 @@ class TestUnsafeNullableLazyVal: WithBox {
|
||||
}
|
||||
}
|
||||
|
||||
class TestUnsafeLazyValDeprecated: WithBox {
|
||||
class UnsafeLazyValDeprecatedTest {
|
||||
var result = 0
|
||||
val a by Delegates.lazy {
|
||||
++result
|
||||
}
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
a
|
||||
if (a != 1) return "fail: initializer should be invoked only once"
|
||||
return "OK"
|
||||
assertTrue(a == 1, "fail: initializer should be invoked only once")
|
||||
}
|
||||
}
|
||||
|
||||
class TestBlockingLazyValDeprecated: WithBox {
|
||||
class BlockingLazyValDeprecatedTest {
|
||||
var result = 0
|
||||
val a by Delegates.blockingLazy {
|
||||
++result
|
||||
}
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
a
|
||||
if (a != 1) return "fail: initializer should be invoked only once"
|
||||
return "OK"
|
||||
assertTrue(a == 1, "fail: initializer should be invoked only once")
|
||||
}
|
||||
}
|
||||
|
||||
class TestUnsafeNullableLazyValDeprecated : WithBox {
|
||||
class UnsafeNullableLazyValDeprecatedTest {
|
||||
var resultA = 0
|
||||
var resultB = 0
|
||||
|
||||
val a: Int? by Delegates.lazy { resultA++; null}
|
||||
val b by Delegates.lazy { foo() }
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
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"
|
||||
assertTrue(a == null, "fail: a should be null")
|
||||
assertTrue(b == null, "fail: b should be null")
|
||||
assertTrue(resultA == 1, "fail: initializer for a should be invoked only once")
|
||||
assertTrue(resultB == 1, "fail: initializer for b should be invoked only once")
|
||||
}
|
||||
|
||||
fun foo(): String? {
|
||||
@@ -170,22 +122,21 @@ class TestUnsafeNullableLazyValDeprecated : WithBox {
|
||||
}
|
||||
}
|
||||
|
||||
class TestBlockingNullableLazyValDeprecated: WithBox {
|
||||
class BlockingNullableLazyValDeprecatedTest {
|
||||
var resultA = 0
|
||||
var resultB = 0
|
||||
|
||||
val a: Int? by Delegates.blockingLazy { resultA++; null}
|
||||
val b by Delegates.blockingLazy { foo() }
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
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"
|
||||
assertTrue(a == null, "fail: a should be null")
|
||||
assertTrue(b == null, "fail: a should be null")
|
||||
assertTrue(resultA == 1, "fail: initializer for a should be invoked only once")
|
||||
assertTrue(resultB == 1, "fail: initializer for b should be invoked only once")
|
||||
}
|
||||
|
||||
fun foo(): String? {
|
||||
@@ -194,15 +145,14 @@ class TestBlockingNullableLazyValDeprecated: WithBox {
|
||||
}
|
||||
}
|
||||
|
||||
class TestIdentityEqualsIsUsedToUnescapeLazyVal: WithBox {
|
||||
class IdentityEqualsIsUsedToUnescapeLazyValTest {
|
||||
var equalsCalled = 0
|
||||
val a by lazy { ClassWithCustomEquality { equalsCalled++ } }
|
||||
|
||||
override fun box(): String {
|
||||
test fun doTest() {
|
||||
a
|
||||
a
|
||||
if (equalsCalled > 0) return "fail: equals called $equalsCalled times."
|
||||
return "OK"
|
||||
assertTrue(equalsCalled == 0, "fail: equals called $equalsCalled times.")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user