diff --git a/build.gradle b/build.gradle index 35c7fdbaae..01f8be4cd9 100644 --- a/build.gradle +++ b/build.gradle @@ -39,6 +39,8 @@ dependencies { api "io.ktor:ktor-server:$ktor_version" api "io.ktor:ktor-server-host-common:$ktor_version" + + testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version" } compileKotlin { diff --git a/src/test/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifierTests.kt b/src/test/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifierTests.kt new file mode 100644 index 0000000000..df568f6a74 --- /dev/null +++ b/src/test/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifierTests.kt @@ -0,0 +1,79 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types + +import kotlinx.serialization.ImplicitReflectionSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json +import kotlin.test.* + +@ImplicitReflectionSerializer +private const val chatIdentifierChatId: Identifier = 123L +@ImplicitReflectionSerializer +private const val chatIdentifierLink = "tg://user?id=$chatIdentifierChatId" +@ImplicitReflectionSerializer +private const val testUsername = "@Example" + +@ImplicitReflectionSerializer +class ChatIdentifierTests { + @Test + fun `Cast from Int to ChatId is working correctly`() { + val chatId = chatIdentifierChatId.toInt().toChatId() + assertEquals(chatIdentifierChatId, chatId.chatId) + } + @Test + fun `Cast from Byte to ChatId is working correctly`() { + val chatId = chatIdentifierChatId.toByte().toChatId() + assertEquals(chatIdentifierChatId, chatId.chatId) + } + @Test + fun `Cast from Identifier to ChatId is working correctly`() { + val chatId = chatIdentifierChatId.toChatId() + assertEquals(chatIdentifierChatId, chatId.chatId) + } + + @Test + fun `Creating link from ChatId is correct`() { + val chatId = chatIdentifierChatId.toChatId() + assertEquals(chatIdentifierLink, chatId.link) + } + + @Test + fun `Cast from String to Username is working correctly`() { + assertEquals(testUsername, testUsername.toUsername().username) + + assertFails("Username creating must fail when trying to create from string which is not starting from @ symbol") { + testUsername.replace("@", "").toUsername().username + } + } + + + @Test + fun `Deserializing from String must work correctly`() { + @Serializable + data class Example( + val identifier: ChatIdentifier + ) + + Example(chatIdentifierChatId.toChatId()).let { withChatId -> + val stringified = Json.plain.stringify(Example.serializer(), withChatId) + assertEquals(stringified, "{\"identifier\":\"$chatIdentifierChatId\"}") + val deserialized = Json.plain.parse(Example.serializer(), stringified) + assertEquals(withChatId, deserialized) + } + + Example(testUsername.toUsername()).let { withUsername -> + val stringified = Json.plain.stringify(Example.serializer(), withUsername) + assertEquals(stringified, "{\"identifier\":\"$testUsername\"}") + val deserialized = Json.plain.parse(Example.serializer(), stringified) + assertEquals(withUsername, deserialized) + } + + // Replace @ by empty string, because from time to time we can retrieve from Telegram system + // username without starting @ symbol + Example(testUsername.toUsername()).let { withUsername -> + val stringified = Json.plain.stringify(Example.serializer(), withUsername).replace("@", "") + assertEquals(stringified, "{\"identifier\":\"${testUsername.replace("@", "")}\"}") + val deserialized = Json.plain.parse(Example.serializer(), stringified) + assertEquals(withUsername, deserialized) + } + } +}