-
Notifications
You must be signed in to change notification settings - Fork 613
/
SealedClassSerializationFromPropertiesTest.kt
50 lines (40 loc) · 1.46 KB
/
SealedClassSerializationFromPropertiesTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package kotlinx.serialization.properties
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
class SealedClassSerializationFromPropertiesTest {
@Serializable
sealed class BaseClass {
abstract val firstProperty: Long
abstract val secondProperty: String
}
@SerialName("FIRSTCHILD")
@Serializable
data class FirstChild(override val firstProperty: Long, override val secondProperty: String) : BaseClass()
@SerialName("SECONDCHILD")
@Serializable
data class SecondChild(override val firstProperty: Long, override val secondProperty: String) : BaseClass()
@Test
fun testPropertiesDeserialization() {
val props = mapOf(
"type" to "FIRSTCHILD",
"firstProperty" to 1L,
"secondProperty" to "one"
)
val instance: BaseClass = Properties.decodeFromMap(props)
assertIs<FirstChild>(instance)
assertEquals(instance.firstProperty, 1)
assertEquals(instance.secondProperty, "one")
}
@Test
fun testPropertiesSerialization() {
val instance: BaseClass = FirstChild(
firstProperty = 1L, secondProperty = "one"
)
val instanceProperties = Properties.encodeToMap(instance)
assertEquals(1L, instanceProperties["firstProperty"])
assertEquals("one", instanceProperties["secondProperty"])
}
}