summaryrefslogtreecommitdiff
path: root/core/commonTest/src/kotlinx/serialization/MetaSerializableTest.kt
blob: 071045a855d117aaefb18e255c8cffdd4abf09b5 (plain)
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
51
52
53
54
55
56
package kotlinx.serialization

import kotlinx.serialization.test.*
import kotlin.reflect.KClass
import kotlin.test.*

@MetaSerializable
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY)
annotation class MySerializable

@MetaSerializable
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY)
annotation class MySerializableWithInfo(
    val value: Int,
    val kclass: KClass<*>
)


class MetaSerializableTest {

    @MySerializable
    class Project1(val name: String, val language: String)

    @MySerializableWithInfo(123, String::class)
    class Project2(val name: String, val language: String)

    @MySerializableWithInfo(123, String::class)
    @Serializable
    class Project3(val name: String, val language: String)

    @Serializable
    class Wrapper(
        @MySerializableWithInfo(234, Int::class) val project: Project3
    )

    @Test
    fun testMetaSerializable() {
        val serializer = serializer<Project1>()
        assertNotNull(serializer)
    }

    @Test
    fun testMetaSerializableWithInfo() {
        val info = serializer<Project2>().descriptor.annotations.filterIsInstance<MySerializableWithInfo>().first()
        assertEquals(123, info.value)
        assertEquals(String::class, info.kclass)
    }

    @Test
    fun testMetaSerializableOnProperty() {
        val info = serializer<Wrapper>().descriptor
            .getElementAnnotations(0).filterIsInstance<MySerializableWithInfo>().first()
        assertEquals(234, info.value)
        assertEquals(Int::class, info.kclass)
    }
}