From 9caf5baf343e2c80811bf277f90bf752ab520df9 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 23 Jun 2015 21:44:29 +0300 Subject: [PATCH] Refactor delegation unit tests. --- .../properties/delegation/DelegationTest.kt | 68 ++----- .../properties/delegation/MapAccessorsTest.kt | 2 +- .../delegation/MapDelegationTest.kt | 191 +++++++----------- .../delegation/lazy/LazyValuesTest.kt | 132 ++++-------- 4 files changed, 132 insertions(+), 261 deletions(-) diff --git a/libraries/stdlib/test/properties/delegation/DelegationTest.kt b/libraries/stdlib/test/properties/delegation/DelegationTest.kt index 4f78f3647c6..1f141f641b8 100644 --- a/libraries/stdlib/test/properties/delegation/DelegationTest.kt +++ b/libraries/stdlib/test/properties/delegation/DelegationTest.kt @@ -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(val a1: String, val b1: T): WithBox { +private class NotNullVarTestGeneric(val a1: String, val b1: T) { var a: String by Delegates.notNull() var b by Delegates.notNull() - 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") } } diff --git a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt index 0254fd984da..ebc1b72111c 100644 --- a/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt +++ b/libraries/stdlib/test/properties/delegation/MapAccessorsTest.kt @@ -1,4 +1,4 @@ -package test.properties.delegation +package test.properties.delegation.map import java.util.* import kotlin.properties.* diff --git a/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt b/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt index 70d229539a7..c770e222fae 100644 --- a/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt +++ b/libraries/stdlib/test/properties/delegation/MapDelegationTest.kt @@ -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(map) val b by Delegates.mapVal(map) val c by Delegates.mapVal(map) val d by Delegates.mapVal(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 = 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(map) var d by Delegates.mapVar(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(null to "null") var a by FixedMapVar(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("a" to "a", "b" to "b", "c" to "c") val a: String by Delegates.mapVal(map) var b by Delegates.mapVar(map) val c by Delegates.mapVal(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() 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() 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("a" to "a", "b" to "b") val a by FixedMapVal(map, key = {"a"}) var b by FixedMapVar(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("aDesc" to "a", "bDesc" to "b") val a by FixedMapVal(map, { desc -> "${desc.name}Desc" }) var b by FixedMapVar(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() { - override fun map(ref: TestMapPropertyCustom) = ref.map +val mapVal = object: MapVal() { + override fun map(ref: MapPropertyCustomTest) = ref.map override fun key(desc: PropertyMetadata) = "${desc.name}Desc" } -val mapVar = object : MapVar() { - override fun map(ref: TestMapPropertyCustom) = ref.map +val mapVar = object : MapVar() { + 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("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() { - override fun map(ref: TestMapPropertyCustomWithDefault) = ref.map +val mapValWithDefault = object : MapVal() { + 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() { - override fun map(ref: TestMapPropertyCustomWithDefault) = ref.map +val mapVarWithDefault = object : MapVar() { + 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() 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") } } diff --git a/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt b/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt index eb931740286..68ecd0f3125 100644 --- a/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt +++ b/libraries/stdlib/test/properties/delegation/lazy/LazyValuesTest.kt @@ -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.") } }