mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-23 02:28:45 +00:00
added TelegramDate tests and fix in chat identifier functionality
This commit is contained in:
parent
91ab60d1f5
commit
8b36767435
@ -51,7 +51,7 @@ internal object ChatIdentifierSerializer : KSerializer<ChatIdentifier> {
|
|||||||
|
|
||||||
override fun serialize(encoder: Encoder, obj: ChatIdentifier) {
|
override fun serialize(encoder: Encoder, obj: ChatIdentifier) {
|
||||||
when (obj) {
|
when (obj) {
|
||||||
is ChatId -> encoder.encodeString(obj.chatId.toString())
|
is ChatId -> encoder.encodeLong(obj.chatId)
|
||||||
is Username -> encoder.encodeString(obj.username)
|
is Username -> encoder.encodeString(obj.username)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,10 @@ import org.joda.time.DateTime
|
|||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
@Serializable(TelegramDateSerializer::class)
|
@Serializable(TelegramDateSerializer::class)
|
||||||
class TelegramDate(
|
data class TelegramDate(
|
||||||
|
/**
|
||||||
|
* Contains UNIX time (seconds)
|
||||||
|
*/
|
||||||
private val date: Long
|
private val date: Long
|
||||||
) {
|
) {
|
||||||
constructor(dateTime: DateTime) : this(
|
constructor(dateTime: DateTime) : this(
|
||||||
|
@ -55,7 +55,7 @@ class ChatIdentifierTests {
|
|||||||
|
|
||||||
Example(chatIdentifierChatId.toChatId()).let { withChatId ->
|
Example(chatIdentifierChatId.toChatId()).let { withChatId ->
|
||||||
val stringified = Json.plain.stringify(Example.serializer(), withChatId)
|
val stringified = Json.plain.stringify(Example.serializer(), withChatId)
|
||||||
assertEquals(stringified, "{\"identifier\":\"$chatIdentifierChatId\"}")
|
assertEquals(stringified, "{\"identifier\":$chatIdentifierChatId}")
|
||||||
val deserialized = Json.plain.parse(Example.serializer(), stringified)
|
val deserialized = Json.plain.parse(Example.serializer(), stringified)
|
||||||
assertEquals(withChatId, deserialized)
|
assertEquals(withChatId, deserialized)
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.github.insanusmokrassar.TelegramBotAPI.types
|
||||||
|
|
||||||
|
import kotlinx.serialization.ImplicitReflectionSerializer
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import org.joda.time.DateTime
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
private val dateTimeMillis = System.currentTimeMillis()
|
||||||
|
private val dateTimeUnix = TimeUnit.MILLISECONDS.toSeconds(dateTimeMillis)
|
||||||
|
private val dateTime = DateTime(TimeUnit.SECONDS.toMillis(dateTimeUnix))
|
||||||
|
|
||||||
|
@ImplicitReflectionSerializer
|
||||||
|
class TelegramDateTests {
|
||||||
|
@Serializable
|
||||||
|
data class Example(
|
||||||
|
val dateTime: TelegramDate
|
||||||
|
)
|
||||||
|
@Test
|
||||||
|
fun `Serializtion of TelegramDate is working correctly`() {
|
||||||
|
val example = Example(TelegramDate(dateTimeUnix))
|
||||||
|
|
||||||
|
val stringified = Json.plain.stringify(Example.serializer(), example)
|
||||||
|
assertEquals("{\"dateTime\":$dateTimeUnix}", stringified)
|
||||||
|
|
||||||
|
val deserialized = Json.plain.parse(Example.serializer(), stringified)
|
||||||
|
assertEquals(example, deserialized)
|
||||||
|
|
||||||
|
assertEquals(dateTime, deserialized.dateTime.asDate)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user