Add polymorphic serialization tests

This commit is contained in:
Him188 2021-02-03 10:12:54 +08:00
parent 2e91ae0d7f
commit b9e5c15bdb

View File

@ -9,14 +9,16 @@
package net.mamoe.mirai.internal.message.data
import kotlinx.serialization.KSerializer
import kotlinx.serialization.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import net.mamoe.mirai.Mirai
import net.mamoe.mirai.internal.message.MarketFaceImpl
import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody
import net.mamoe.mirai.message.MessageSerializers
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.cast
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
@ -101,7 +103,8 @@ internal class MessageSerializationTest {
LightApp("lightApp"),
image.flash(),
image.toForwardMessage(1L, "test"),
MusicShare(MusicKind.NeteaseCloudMusic, "123", "123", "123", "123", "123", "123")
MusicShare(MusicKind.NeteaseCloudMusic, "123", "123", "123", "123", "123", "123"),
RichMessageOrigin(SimpleServiceMessage(1, "content"), "resource id", RichMessageKind.LONG)
)
companion object {
@ -114,7 +117,19 @@ internal class MessageSerializationTest {
@Test
fun `test polymorphic serialization`() {
@Serializable
data class RichWrapper(
val richMessage: RichMessage
)
val string = format.encodeToString(RichWrapper.serializer(), RichWrapper(SimpleServiceMessage(1, "content")))
println(string)
var element = format.parseToJsonElement(string)
element as JsonObject
element = element["richMessage"] as JsonObject
assertEquals("SimpleServiceMessage", element["type"]?.cast<JsonPrimitive>()?.content)
assertEquals("content", element["content"]?.cast<JsonPrimitive>()?.content)
assertEquals(1, element["serviceId"]?.cast<JsonPrimitive>()?.content?.toInt())
}
@Test