1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-23 02:28:45 +00:00

added tests for ParseMode

This commit is contained in:
InsanusMokrassar 2019-12-02 11:05:15 +06:00
parent 88208016d4
commit 91ab60d1f5
3 changed files with 40 additions and 3 deletions

View File

@ -29,8 +29,7 @@ typealias HTML = HTMLParseMode
@Serializer(ParseMode::class) @Serializer(ParseMode::class)
internal object ParseModeSerializerObject : KSerializer<ParseMode> { internal object ParseModeSerializerObject : KSerializer<ParseMode> {
override fun deserialize(decoder: Decoder): ParseMode { override fun deserialize(decoder: Decoder): ParseMode {
val mode = decoder.decodeString() return when (decoder.decodeString()) {
return when (mode) {
MarkdownParseMode.parseModeName -> MarkdownParseMode MarkdownParseMode.parseModeName -> MarkdownParseMode
HTMLParseMode.parseModeName -> HTMLParseMode HTMLParseMode.parseModeName -> HTMLParseMode
else -> throw IllegalArgumentException("Unknown parse mode") else -> throw IllegalArgumentException("Unknown parse mode")

View File

@ -71,7 +71,7 @@ class ChatIdentifierTests {
// username without starting @ symbol // username without starting @ symbol
Example(testUsername.toUsername()).let { withUsername -> Example(testUsername.toUsername()).let { withUsername ->
val stringified = Json.plain.stringify(Example.serializer(), withUsername).replace("@", "") val stringified = Json.plain.stringify(Example.serializer(), withUsername).replace("@", "")
assertEquals(stringified, "{\"identifier\":\"${testUsername.replace("@", "")}\"}") assertEquals("{\"identifier\":\"${testUsername.replace("@", "")}\"}", stringified)
val deserialized = Json.plain.parse(Example.serializer(), stringified) val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(withUsername, deserialized) assertEquals(withUsername, deserialized)
} }

View File

@ -0,0 +1,38 @@
package com.github.insanusmokrassar.TelegramBotAPI.types
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.*
import kotlinx.serialization.ImplicitReflectionSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals
@ImplicitReflectionSerializer
class ParseModeTests {
@Serializable
data class Example(
val mode: ParseMode
)
@Test
fun `Markdown parse mode correctly serializing and deserializing`() {
val example = Example(Markdown)
val stringified = Json.plain.stringify(Example.serializer(), example)
assertEquals("{\"mode\":\"Markdown\"}", stringified)
val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(example, deserialized)
}
@Test
fun `HTML parse mode correctly serializing and deserializing`() {
val example = Example(HTML)
val stringified = Json.plain.stringify(Example.serializer(), example)
assertEquals("{\"mode\":\"HTML\"}", stringified)
val deserialized = Json.plain.parse(Example.serializer(), stringified)
assertEquals(example, deserialized)
}
}