From b9e5c15bdbca33d0b70cfa9927aeed533d621f24 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 3 Feb 2021 10:12:54 +0800 Subject: [PATCH] Add polymorphic serialization tests --- .../message/data/MessageSerializationTest.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/mirai-core/src/jvmTest/kotlin/message/data/MessageSerializationTest.kt b/mirai-core/src/jvmTest/kotlin/message/data/MessageSerializationTest.kt index 090e4e700..f65491e0f 100644 --- a/mirai-core/src/jvmTest/kotlin/message/data/MessageSerializationTest.kt +++ b/mirai-core/src/jvmTest/kotlin/message/data/MessageSerializationTest.kt @@ -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()?.content) + assertEquals("content", element["content"]?.cast()?.content) + assertEquals(1, element["serviceId"]?.cast()?.content?.toInt()) } @Test