Updates to Delegated Properties article for Kotlin 1.4 (#10220)
This commit is contained in:
parent
f579e493d3
commit
fa2387c836
|
@ -22,4 +22,57 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk7</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-common</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-reflect</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test-junit</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test-common</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test-annotations-common</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<properties>
|
||||
<kotlin.version>1.4.10</kotlin.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.kotlin.delegates
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.properties.PropertyDelegateProvider
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class DelegateProvider<in T, D>(private val field: String, private val id: Int)
|
||||
: PropertyDelegateProvider<T, DatabaseDelegate<T, D>> {
|
||||
override operator fun provideDelegate(thisRef: T, prop: KProperty<*>): DatabaseDelegate<T, D> {
|
||||
println("Providing delegate for field $field and ID $id")
|
||||
prop.returnType.isMarkedNullable
|
||||
return DatabaseDelegate(field, id)
|
||||
}
|
||||
}
|
||||
|
||||
class DelegateProvidingUser(val id: Int) {
|
||||
var name: String by DelegateProvider("name", id)
|
||||
var age: Int by DelegateProvider("age", id)
|
||||
}
|
||||
|
||||
class DelegateProviderTest {
|
||||
@Test
|
||||
fun testGetKnownFields() {
|
||||
val user = DelegateProvidingUser(1)
|
||||
assertEquals("George", user.name)
|
||||
assertEquals(4, user.age)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetKnownFields() {
|
||||
val user = DelegateProvidingUser(2)
|
||||
user.age = 3
|
||||
assertEquals(3, user.age)
|
||||
}
|
||||
|
||||
@Test(expected = NoRecordFoundException::class)
|
||||
fun testGetKnownField() {
|
||||
val user = DelegateProvidingUser(3)
|
||||
user.name
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue