• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package kotlinx.serialization
2 
3 import kotlinx.serialization.test.*
4 import kotlin.reflect.KClass
5 import kotlin.test.*
6 
7 @MetaSerializable
8 @Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY)
9 annotation class MySerializable
10 
11 @MetaSerializable
12 @Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY)
13 annotation class MySerializableWithInfo(
14     val value: Int,
15     val kclass: KClass<*>
16 )
17 
18 
19 class MetaSerializableTest {
20 
21     @MySerializable
22     class Project1(val name: String, val language: String)
23 
24     @MySerializableWithInfo(123, String::class)
25     class Project2(val name: String, val language: String)
26 
27     @MySerializableWithInfo(123, String::class)
28     @Serializable
29     class Project3(val name: String, val language: String)
30 
31     @Serializable
32     class Wrapper(
33         @MySerializableWithInfo(234, Int::class) val project: Project3
34     )
35 
36     @Test
testMetaSerializablenull37     fun testMetaSerializable() {
38         val serializer = serializer<Project1>()
39         assertNotNull(serializer)
40     }
41 
42     @Test
testMetaSerializableWithInfonull43     fun testMetaSerializableWithInfo() {
44         val info = serializer<Project2>().descriptor.annotations.filterIsInstance<MySerializableWithInfo>().first()
45         assertEquals(123, info.value)
46         assertEquals(String::class, info.kclass)
47     }
48 
49     @Test
testMetaSerializableOnPropertynull50     fun testMetaSerializableOnProperty() {
51         val info = serializer<Wrapper>().descriptor
52             .getElementAnnotations(0).filterIsInstance<MySerializableWithInfo>().first()
53         assertEquals(234, info.value)
54         assertEquals(Int::class, info.kclass)
55     }
56 }
57