-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 29 additions & 28 deletions
57
core/commonTest/src/kotlinx/serialization/MetaSerializableTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,58 @@ | ||
package kotlinx.serialization | ||
|
||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlin.reflect.KClass | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
|
||
// TODO: for this test to work, kotlin dependency should be updated | ||
// to serialization plugin with @MetaSerializable support | ||
class MetaSerializableTest { | ||
|
||
@MetaSerializable | ||
@Target(AnnotationTarget.CLASS) | ||
annotation class MySerializableWithCustomSerializer(@MetaSerializable.Serializer val with: KClass<out KSerializer<*>> = KSerializer::class) | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY) | ||
annotation class MySerializable | ||
|
||
@MetaSerializable | ||
@Target(AnnotationTarget.CLASS) | ||
annotation class MySerializableWithInfo(val value: Int, val klass: KClass<*>) | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.PROPERTY) | ||
annotation class MySerializableWithInfo( | ||
val value: Int, | ||
val kclass: KClass<*> | ||
) | ||
|
||
@MySerializableWithCustomSerializer(MySerializer::class) | ||
@MySerializable | ||
class Project1(val name: String, val language: String) | ||
|
||
@MySerializableWithCustomSerializer | ||
@MySerializableWithInfo(123, String::class) | ||
class Project2(val name: String, val language: String) | ||
|
||
@MySerializableWithInfo(value = 123, String::class) | ||
@MySerializableWithInfo(123, String::class) | ||
@Serializable | ||
class Project3(val name: String, val language: String) | ||
|
||
object MySerializer : KSerializer<Project1> { | ||
override val descriptor: SerialDescriptor | ||
get() = throw NotImplementedError() | ||
|
||
override fun serialize(encoder: Encoder, value: Project1) = throw NotImplementedError() | ||
override fun deserialize(decoder: Decoder): Project1 = throw NotImplementedError() | ||
} | ||
@Serializable | ||
class Wrapper( | ||
@MySerializableWithInfo(234, Int::class) val project: Project3 | ||
) | ||
|
||
@Test | ||
fun testCustomSerializer() { | ||
// val serializer = serializer<Project1>() | ||
// assertEquals(serializer, MySerializer) | ||
fun testMetaSerializable() { | ||
val serializer = serializer<Project1>() | ||
assertNotNull(serializer) | ||
} | ||
|
||
@Test | ||
fun testDefaultSerializer() { | ||
// val serializer = serializer<Project2>() | ||
// assertNotNull(serializer) | ||
fun testMetaSerializableWithInfo() { | ||
val info = serializer<Project2>().descriptor.annotations.filterIsInstance<MySerializableWithInfo>().first() | ||
assertEquals(123, info.value) | ||
assertEquals(String::class, info.kclass) | ||
} | ||
|
||
@Test | ||
fun testDefaultSerialInfo() { | ||
// val descriptor = serializer<Project3>().descriptor | ||
// val annotation = descriptor.annotations.filterIsInstance<MySerializableWithInfo>().first() | ||
// assertEquals(123, annotation.value) | ||
// assertEquals(String::class, annotation.klass) | ||
fun testMetaSerializableOnProperty() { | ||
val info = serializer<Wrapper>().descriptor | ||
.getElementAnnotations(0).filterIsInstance<MySerializableWithInfo>().first() | ||
assertEquals(234, info.value) | ||
assertEquals(Int::class, info.kclass) | ||
} | ||
} |