[kotlin-tooling-core] Implement HasExtras utils
^KT-55112 Verification Pending
This commit is contained in:
committed by
Space Team
parent
55cfe52e7d
commit
4c270c7f6e
+30
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.tooling.core
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
|
||||
interface HasExtras {
|
||||
val extras: Extras
|
||||
}
|
||||
|
||||
interface HasMutableExtras : HasExtras {
|
||||
override val extras: MutableExtras
|
||||
}
|
||||
|
||||
operator fun <T : Any> Extras.Key<T>.getValue(receiver: HasExtras, property: KProperty<*>): T? {
|
||||
return receiver.extras[this]
|
||||
}
|
||||
|
||||
|
||||
operator fun <T : Any> Extras.Key<T>.setValue(receiver: HasMutableExtras, property: KProperty<*>, value: T?) {
|
||||
if (value == null) {
|
||||
receiver.extras.remove(this)
|
||||
} else {
|
||||
receiver.extras[this] = value
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.tooling.core
|
||||
|
||||
import org.jetbrains.kotlin.tooling.core.HasExtrasTest.Subject.Companion.value1
|
||||
import org.jetbrains.kotlin.tooling.core.HasExtrasTest.Subject.Companion.value2
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class HasExtrasTest {
|
||||
|
||||
class Subject : HasMutableExtras {
|
||||
override val extras: MutableExtras = mutableExtrasOf()
|
||||
|
||||
companion object {
|
||||
var Subject.value1: Int? by extrasKeyOf()
|
||||
var Subject.value2: Int? by extrasKeyOf("2")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test - non-null value`() {
|
||||
val subject = Subject()
|
||||
assertNull(subject.value1)
|
||||
assertNull(subject.value2)
|
||||
|
||||
subject.value1 = 1
|
||||
assertEquals(1, subject.value1)
|
||||
assertNull(subject.value2)
|
||||
|
||||
subject.value2 = 2
|
||||
assertEquals(1, subject.value1)
|
||||
assertEquals(2, subject.value2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test - set null`() {
|
||||
val subject = Subject()
|
||||
subject.value1 = 1
|
||||
assertEquals(1, subject.value1)
|
||||
|
||||
subject.value1 = null
|
||||
assertNull(subject.value1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user