From 260b1eafc36c1741958cbb886f629050e5774984 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 25 Jun 2019 22:06:29 -0400 Subject: [PATCH] Add property delegate tests --- .../util/properties/ResettableLazyProperty.kt | 3 ++ .../ObservableLateInitPropertyTests.kt | 33 +++++++++++++++++ .../properties/ResettableLazyPropertyTests.kt | 35 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/test/kotlin/net/shadowfacts/cacao/util/properties/ObservableLateInitPropertyTests.kt create mode 100644 src/test/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyPropertyTests.kt diff --git a/src/main/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyProperty.kt b/src/main/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyProperty.kt index dd7e106..753dab2 100644 --- a/src/main/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyProperty.kt +++ b/src/main/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyProperty.kt @@ -8,6 +8,9 @@ import kotlin.reflect.KProperty class ResettableLazyProperty(val initializer: () -> Value) { var value: Value? = null + val isInitialized: Boolean + get() = value != null + operator fun getValue(thisRef: Any, property: KProperty<*>): Value { if (value == null) { value = initializer() diff --git a/src/test/kotlin/net/shadowfacts/cacao/util/properties/ObservableLateInitPropertyTests.kt b/src/test/kotlin/net/shadowfacts/cacao/util/properties/ObservableLateInitPropertyTests.kt new file mode 100644 index 0000000..8ae10b8 --- /dev/null +++ b/src/test/kotlin/net/shadowfacts/cacao/util/properties/ObservableLateInitPropertyTests.kt @@ -0,0 +1,33 @@ +package net.shadowfacts.cacao.util.properties + +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test +import java.util.concurrent.CompletableFuture + +/** + * @author shadowfacts + */ +class ObservableLateInitPropertyTests { + + class MyClass(callback: (String) -> Unit) { + val delegate = ObservableLateInitProperty(callback) + var prop by delegate + } + + @Test + fun testObservation() { + val future = CompletableFuture() + val obj = MyClass { future.complete(it) } + obj.prop = "test" + assertEquals("test", future.getNow(null)) + } + + @Test + fun testIsInitialized() { + val obj = MyClass {} + assertFalse(obj.delegate.isInitialized) + obj.prop = "test" + assertTrue(obj.delegate.isInitialized) + } + +} \ No newline at end of file diff --git a/src/test/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyPropertyTests.kt b/src/test/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyPropertyTests.kt new file mode 100644 index 0000000..7e53cb1 --- /dev/null +++ b/src/test/kotlin/net/shadowfacts/cacao/util/properties/ResettableLazyPropertyTests.kt @@ -0,0 +1,35 @@ +package net.shadowfacts.cacao.util.properties + +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +/** + * @author shadowfacts + */ +class ResettableLazyPropertyTests { + + class MyClass { + var iteration = 1 + val delegate = ResettableLazyProperty { "test ${iteration++}" } + val prop by delegate + } + + @Test + fun testResets() { + val obj = MyClass() + assertEquals("test 1", obj.prop) + obj.delegate.reset() + assertEquals("test 2", obj.prop) + assertEquals("test 2", obj.prop) + } + + @Test + fun testIsInitialized() { + val obj = MyClass() + assertFalse(obj.delegate.isInitialized) + assertEquals("test 1", obj.prop) + assertTrue(obj.delegate.isInitialized) + obj.delegate.reset() + assertFalse(obj.delegate.isInitialized) + } +} \ No newline at end of file