add test for int/float

This commit is contained in:
Vladimir Krivosheev
2017-02-13 15:22:46 +01:00
parent 150770b217
commit 3ebf06dcd1
4 changed files with 80 additions and 13 deletions
@@ -3,20 +3,21 @@ package com.intellij.configurationStore
import com.intellij.openapi.components.BaseState
import com.intellij.testFramework.assertions.Assertions.assertThat
import com.intellij.util.loadElement
import com.intellij.util.xmlb.XmlSerializer
import com.intellij.util.xmlb.annotations.Attribute
import org.junit.Test
private class AState : BaseState() {
internal class AState : BaseState() {
@get:Attribute("customName")
var languageLevel by storedProperty<String?>()
var property2 by storedProperty(0)
var floatProperty by storedProperty(0.3)
var nestedComplex by storedProperty<NestedState?>()
}
private class NestedState : BaseState() {
internal class NestedState : BaseState() {
var childProperty by storedProperty<String?>()
}
@@ -36,7 +37,7 @@ class StoredPropertyStateTest {
assertThat(state).isNotEqualTo(AState())
assertThat(XmlSerializer.serialize(state)).isEqualTo("""<AState customName="foo" />""")
assertThat(state.serialize()).isEqualTo("""<AState customName="foo" />""")
assertThat(loadElement("""<AState customName="foo" />""").deserialize(AState::class.java).languageLevel).isEqualTo("foo")
}
@@ -15,6 +15,7 @@
*/
package com.intellij.configurationStore.xml
import com.intellij.configurationStore.AState
import com.intellij.configurationStore.deserialize
import com.intellij.util.loadElement
import com.intellij.util.xmlb.annotations.MapAnnotation
@@ -38,6 +39,16 @@ class KotlinXmlSerializerTest {
</bean>""", data)
}
@Test fun floatProperty() {
val state = AState()
state.floatProperty = 3.4
doSerializerTest("""
<AState>
<option name="floatProperty" value="3.4" />
</AState>
""", state)
}
@Test fun nullInMap() {
@Tag("bean")
class Foo {
@@ -15,6 +15,7 @@
*/
package com.intellij.configurationStore.xml
import com.intellij.configurationStore.StoredPropertyStateTest
import com.intellij.configurationStore.deserialize
import com.intellij.configurationStore.serialize
import com.intellij.openapi.util.JDOMUtil
@@ -39,6 +40,7 @@ import java.util.*
XmlSerializerTest::class,
XmlSerializerMapTest::class,
XmlSerializerCollectionTest::class,
StoredPropertyStateTest::class,
KotlinXmlSerializerTest::class
)
class XmlSerializerTestSuite
@@ -24,7 +24,7 @@ import kotlin.reflect.KProperty
abstract class BaseState : SerializationFilter, ModificationTracker {
// if property value differs from default
private val properties: MutableList<StoredProperty<*>> = SmartList()
private val properties: MutableList<StoredProperty> = SmartList()
@Volatile internal var modificationCount: Long = 0
@@ -47,25 +47,25 @@ abstract class BaseState : SerializationFilter, ModificationTracker {
}
fun <T> storedProperty(defaultValue: T? = null): ReadWriteProperty<BaseState, T?> {
val result = StoredProperty(defaultValue)
val result = ObjectStoredProperty(defaultValue)
properties.add(result)
return result
}
fun storedProperty(defaultValue: Int = 0): ReadWriteProperty<BaseState, Int> {
val result = StoredProperty(defaultValue)
val result = IntStoredProperty(defaultValue)
properties.add(result)
return result
}
fun storedProperty(defaultValue: Float = 0f): ReadWriteProperty<BaseState, Float> {
val result = StoredProperty(defaultValue)
val result = FloatStoredProperty(defaultValue)
properties.add(result)
return result
}
fun storedProperty(defaultValue: Boolean = false): ReadWriteProperty<BaseState, Boolean> {
val result = StoredProperty(defaultValue)
val result = ObjectStoredProperty(defaultValue)
properties.add(result)
return result
}
@@ -99,9 +99,16 @@ abstract class BaseState : SerializationFilter, ModificationTracker {
}
}
internal class StoredProperty<T>(internal val defaultValue: T) : ReadWriteProperty<BaseState, T> {
internal var value = defaultValue
internal var name: String? = null
internal interface StoredProperty {
val defaultValue: Any?
val value: Any?
val name: String?
}
private class ObjectStoredProperty<T>(override val defaultValue: T) : ReadWriteProperty<BaseState, T>, StoredProperty {
override var value = defaultValue
override var name: String? = null
override operator fun getValue(thisRef: BaseState, property: KProperty<*>) = value
@@ -115,9 +122,55 @@ internal class StoredProperty<T>(internal val defaultValue: T) : ReadWriteProper
}
}
override fun equals(other: Any?) = this === other || (other is StoredProperty<*> && value == other.value)
override fun equals(other: Any?) = this === other || (other is ObjectStoredProperty<*> && value == other.value)
override fun hashCode() = value?.hashCode() ?: 0
override fun toString() = if (value === defaultValue) "" else value?.toString() ?: super.toString()
}
private class IntStoredProperty(override val defaultValue: Int) : ReadWriteProperty<BaseState, Int>, StoredProperty {
override var value = defaultValue
override var name: String? = null
override operator fun getValue(thisRef: BaseState, property: KProperty<*>) = value
@Suppress("UNCHECKED_CAST")
override fun setValue(thisRef: BaseState, property: KProperty<*>, @Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") newValue: Int) {
if (value != newValue) {
thisRef.modificationCount++
name = property.name
value = newValue
}
}
override fun equals(other: Any?) = this === other || (other is IntStoredProperty && value == other.value)
override fun hashCode() = value.hashCode()
override fun toString() = if (value == defaultValue) "" else value.toString()
}
private class FloatStoredProperty(override val defaultValue: Float) : ReadWriteProperty<BaseState, Float>, StoredProperty {
override var value = defaultValue
override var name: String? = null
override operator fun getValue(thisRef: BaseState, property: KProperty<*>) = value
@Suppress("UNCHECKED_CAST")
override fun setValue(thisRef: BaseState, property: KProperty<*>, @Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") newValue: Float) {
if (value != newValue) {
thisRef.modificationCount++
name = property.name
value = newValue
}
}
override fun equals(other: Any?) = this === other || (other is FloatStoredProperty && value == other.value)
override fun hashCode() = value.hashCode()
override fun toString() = if (value == defaultValue) "" else value.toString()
}