telegram-bot-api/types_test.go

411 lines
9.8 KiB
Go
Raw Permalink Normal View History

2018-10-09 06:34:34 +02:00
package tgbotapi
2015-11-21 11:17:34 +01:00
import (
"testing"
"time"
)
func TestUserStringWith(t *testing.T) {
2018-10-09 06:34:34 +02:00
user := User{
2017-11-09 19:51:50 +01:00
ID: 0,
FirstName: "Test",
LastName: "Test",
UserName: "",
LanguageCode: "en",
IsBot: false,
}
2015-11-21 11:17:34 +01:00
if user.String() != "Test Test" {
t.Fail()
}
}
func TestUserStringWithUserName(t *testing.T) {
2018-10-09 06:34:34 +02:00
user := User{
2017-11-09 19:51:50 +01:00
ID: 0,
FirstName: "Test",
LastName: "Test",
UserName: "@test",
LanguageCode: "en",
}
2015-11-21 11:17:34 +01:00
if user.String() != "@test" {
t.Fail()
}
}
func TestMessageTime(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Date: 0}
2015-11-21 11:17:34 +01:00
date := time.Unix(0, 0)
if message.Time() != date {
t.Fail()
}
}
func TestMessageIsCommandWithCommand(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "/command"}
message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
if !message.IsCommand() {
t.Fail()
}
}
func TestIsCommandWithText(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "some text"}
if message.IsCommand() {
t.Fail()
}
}
func TestIsCommandWithEmptyText(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: ""}
if message.IsCommand() {
t.Fail()
}
}
func TestCommandWithCommand(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "/command"}
message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
if message.Command() != "command" {
t.Fail()
}
}
func TestCommandWithEmptyText(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: ""}
if message.Command() != "" {
t.Fail()
}
}
func TestCommandWithNonCommand(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "test text"}
if message.Command() != "" {
t.Fail()
}
}
func TestCommandWithBotName(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "/command@testbot"}
message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
if message.Command() != "command" {
t.Fail()
}
}
2017-10-07 18:06:20 +02:00
func TestCommandWithAtWithBotName(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "/command@testbot"}
message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
2017-10-07 18:06:20 +02:00
if message.CommandWithAt() != "command@testbot" {
t.Fail()
}
}
func TestMessageCommandArgumentsWithArguments(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "/command with arguments"}
message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
if message.CommandArguments() != "with arguments" {
t.Fail()
}
}
2017-10-07 18:06:20 +02:00
func TestMessageCommandArgumentsWithMalformedArguments(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "/command-without argument space"}
message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
2017-10-07 18:06:20 +02:00
if message.CommandArguments() != "without argument space" {
t.Fail()
}
}
func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "/command"}
if message.CommandArguments() != "" {
t.Fail()
}
}
func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
2018-10-09 06:34:34 +02:00
message := Message{Text: "test text"}
if message.CommandArguments() != "" {
t.Fail()
}
}
2016-04-14 00:06:18 +02:00
func TestMessageEntityParseURLGood(t *testing.T) {
2018-10-09 06:34:34 +02:00
entity := MessageEntity{URL: "https://www.google.com"}
2016-04-14 00:06:18 +02:00
if _, err := entity.ParseURL(); err != nil {
t.Fail()
}
}
func TestMessageEntityParseURLBad(t *testing.T) {
2018-10-09 06:34:34 +02:00
entity := MessageEntity{URL: ""}
2016-04-14 00:06:18 +02:00
if _, err := entity.ParseURL(); err == nil {
t.Fail()
}
}
2015-11-21 11:17:34 +01:00
func TestChatIsPrivate(t *testing.T) {
2018-10-09 06:34:34 +02:00
chat := Chat{ID: 10, Type: "private"}
2015-11-21 11:17:34 +01:00
if !chat.IsPrivate() {
2015-11-21 11:17:34 +01:00
t.Fail()
}
}
func TestChatIsGroup(t *testing.T) {
2018-10-09 06:34:34 +02:00
chat := Chat{ID: 10, Type: "group"}
2015-11-21 11:17:34 +01:00
if !chat.IsGroup() {
2015-11-21 11:17:34 +01:00
t.Fail()
}
}
func TestChatIsChannel(t *testing.T) {
2018-10-09 06:34:34 +02:00
chat := Chat{ID: 10, Type: "channel"}
2015-11-21 11:17:34 +01:00
if !chat.IsChannel() {
2015-11-21 11:17:34 +01:00
t.Fail()
}
}
func TestChatIsSuperGroup(t *testing.T) {
2018-10-09 06:34:34 +02:00
chat := Chat{ID: 10, Type: "supergroup"}
if !chat.IsSuperGroup() {
t.Fail()
}
}
2019-01-10 12:40:12 +01:00
func TestMessageEntityIsMention(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "mention"}
2019-01-10 12:40:12 +01:00
if !entity.IsMention() {
t.Fail()
}
}
func TestMessageEntityIsHashtag(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "hashtag"}
2019-01-10 12:40:12 +01:00
if !entity.IsHashtag() {
t.Fail()
}
}
func TestMessageEntityIsBotCommand(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "bot_command"}
2019-01-10 12:40:12 +01:00
if !entity.IsCommand() {
t.Fail()
}
}
func TestMessageEntityIsUrl(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "url"}
2019-01-10 12:40:12 +01:00
2020-07-21 21:20:12 +02:00
if !entity.IsURL() {
2019-01-10 12:40:12 +01:00
t.Fail()
}
}
func TestMessageEntityIsEmail(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "email"}
2019-01-10 12:40:12 +01:00
if !entity.IsEmail() {
t.Fail()
}
}
func TestMessageEntityIsBold(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "bold"}
2019-01-10 12:40:12 +01:00
if !entity.IsBold() {
t.Fail()
}
}
func TestMessageEntityIsItalic(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "italic"}
2019-01-10 12:40:12 +01:00
if !entity.IsItalic() {
t.Fail()
}
}
func TestMessageEntityIsCode(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "code"}
2019-01-10 12:40:12 +01:00
if !entity.IsCode() {
t.Fail()
}
}
func TestMessageEntityIsPre(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "pre"}
2019-01-10 12:40:12 +01:00
if !entity.IsPre() {
t.Fail()
}
}
func TestMessageEntityIsTextLink(t *testing.T) {
2020-02-15 14:19:31 +01:00
entity := MessageEntity{Type: "text_link"}
2019-01-10 12:40:12 +01:00
if !entity.IsTextLink() {
t.Fail()
}
}
2015-11-21 11:17:34 +01:00
func TestFileLink(t *testing.T) {
2018-10-09 06:34:34 +02:00
file := File{FilePath: "test/test.txt"}
2015-11-21 11:17:34 +01:00
if file.Link("token") != "https://api.telegram.org/file/bottoken/test/test.txt" {
t.Fail()
}
2015-11-21 12:22:08 +01:00
}
// Ensure all configs are sendable
var (
2018-10-09 06:34:34 +02:00
_ Chattable = AnimationConfig{}
2022-04-16 18:36:59 +02:00
_ Chattable = AnswerWebAppQueryConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = AudioConfig{}
2021-12-04 03:44:02 +01:00
_ Chattable = BanChatMemberConfig{}
2021-12-07 16:08:41 +01:00
_ Chattable = BanChatSenderChatConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = CallbackConfig{}
_ Chattable = ChatActionConfig{}
2021-03-09 18:27:17 +01:00
_ Chattable = ChatAdministratorsConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = ChatInfoConfig{}
_ Chattable = ChatInviteLinkConfig{}
2020-11-06 18:36:00 +01:00
_ Chattable = CloseConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = ContactConfig{}
2021-03-09 18:27:17 +01:00
_ Chattable = CopyMessageConfig{}
_ Chattable = CreateChatInviteLinkConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = DeleteChatPhotoConfig{}
_ Chattable = DeleteChatStickerSetConfig{}
_ Chattable = DeleteMessageConfig{}
2021-06-27 21:15:09 +02:00
_ Chattable = DeleteMyCommandsConfig{}
2021-03-09 18:27:17 +01:00
_ Chattable = DeleteWebhookConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = DocumentConfig{}
2021-03-09 18:27:17 +01:00
_ Chattable = EditChatInviteLinkConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = EditMessageCaptionConfig{}
_ Chattable = EditMessageLiveLocationConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = EditMessageMediaConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = EditMessageReplyMarkupConfig{}
_ Chattable = EditMessageTextConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = FileConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = ForwardConfig{}
_ Chattable = GameConfig{}
2024-05-02 21:49:19 +02:00
_ Chattable = GetBusinessConnectionConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = GetChatMemberConfig{}
2022-04-16 18:36:59 +02:00
_ Chattable = GetChatMenuButtonConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = GetGameHighScoresConfig{}
2022-04-16 18:36:59 +02:00
_ Chattable = GetMyDefaultAdministratorRightsConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = InlineConfig{}
_ Chattable = InvoiceConfig{}
_ Chattable = KickChatMemberConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = LeaveChatConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = LocationConfig{}
2020-11-06 18:36:00 +01:00
_ Chattable = LogOutConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = MediaGroupConfig{}
_ Chattable = MessageConfig{}
_ Chattable = PhotoConfig{}
_ Chattable = PinChatMessageConfig{}
2020-11-06 05:29:48 +01:00
_ Chattable = PreCheckoutConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = PromoteChatMemberConfig{}
2024-05-02 21:49:19 +02:00
_ Chattable = ReplaceStickerInSetConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = RestrictChatMemberConfig{}
2021-03-09 18:27:17 +01:00
_ Chattable = RevokeChatInviteLinkConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = SendPollConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = SetChatDescriptionConfig{}
2022-04-16 18:36:59 +02:00
_ Chattable = SetChatMenuButtonConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = SetChatPhotoConfig{}
_ Chattable = SetChatTitleConfig{}
_ Chattable = SetGameScoreConfig{}
2022-04-16 18:36:59 +02:00
_ Chattable = SetMyDefaultAdministratorRightsConfig{}
2020-11-06 05:29:48 +01:00
_ Chattable = ShippingConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = StickerConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = StopMessageLiveLocationConfig{}
2021-03-09 18:27:17 +01:00
_ Chattable = StopPollConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = UnbanChatMemberConfig{}
2021-12-07 16:08:41 +01:00
_ Chattable = UnbanChatSenderChatConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = UnpinChatMessageConfig{}
_ Chattable = UpdateConfig{}
2024-01-05 21:05:00 +01:00
_ Chattable = SetMessageReactionConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = UserProfilePhotosConfig{}
_ Chattable = VenueConfig{}
_ Chattable = VideoConfig{}
_ Chattable = VideoNoteConfig{}
_ Chattable = VoiceConfig{}
_ Chattable = WebhookConfig{}
2022-12-31 01:15:29 +01:00
_ Chattable = CreateForumTopicConfig{}
_ Chattable = EditForumTopicConfig{}
_ Chattable = CloseForumTopicConfig{}
_ Chattable = ReopenForumTopicConfig{}
_ Chattable = DeleteForumTopicConfig{}
_ Chattable = UnpinAllForumTopicMessagesConfig{}
_ Chattable = GetForumTopicIconStickersConfig{}
_ Chattable = EditGeneralForumTopicConfig{}
_ Chattable = CloseGeneralForumTopicConfig{}
_ Chattable = ReopenGeneralForumTopicConfig{}
_ Chattable = HideGeneralForumTopicConfig{}
_ Chattable = UnhideGeneralForumTopicConfig{}
2024-01-05 21:05:00 +01:00
_ Chattable = UnpinAllGeneralForumTopicMessagesConfig{}
_ Chattable = SetCustomEmojiStickerSetThumbnailConfig{}
_ Chattable = SetStickerSetTitleConfig{}
_ Chattable = DeleteStickerSetConfig{}
_ Chattable = SetStickerEmojiListConfig{}
_ Chattable = SetStickerKeywordsConfig{}
_ Chattable = SetStickerMaskPositionConfig{}
_ Chattable = GetMyDescriptionConfig{}
_ Chattable = SetMyDescriptionConfig{}
_ Chattable = GetMyShortDescriptionConfig{}
_ Chattable = SetMyShortDescriptionConfig{}
2023-07-05 10:23:14 +02:00
_ Chattable = GetMyNameConfig{}
_ Chattable = SetMyNameConfig{}
2024-05-31 18:21:42 +02:00
_ Chattable = RefundStarPaymentConfig{}
)
// Ensure all Fileable types are correct.
var (
_ Fileable = (*PhotoConfig)(nil)
_ Fileable = (*AudioConfig)(nil)
_ Fileable = (*DocumentConfig)(nil)
_ Fileable = (*StickerConfig)(nil)
_ Fileable = (*VideoConfig)(nil)
_ Fileable = (*AnimationConfig)(nil)
_ Fileable = (*VideoNoteConfig)(nil)
_ Fileable = (*VoiceConfig)(nil)
_ Fileable = (*SetChatPhotoConfig)(nil)
_ Fileable = (*EditMessageMediaConfig)(nil)
_ Fileable = (*SetChatPhotoConfig)(nil)
_ Fileable = (*UploadStickerConfig)(nil)
_ Fileable = (*NewStickerSetConfig)(nil)
_ Fileable = (*AddStickerConfig)(nil)
_ Fileable = (*MediaGroupConfig)(nil)
2020-07-26 23:06:22 +02:00
_ Fileable = (*WebhookConfig)(nil)
_ Fileable = (*SetStickerSetThumbConfig)(nil)
)
2021-08-20 21:31:52 +02:00
// Ensure all RequestFileData types are correct.
var (
_ RequestFileData = (*FilePath)(nil)
_ RequestFileData = (*FileBytes)(nil)
_ RequestFileData = (*FileReader)(nil)
_ RequestFileData = (*FileURL)(nil)
_ RequestFileData = (*FileID)(nil)
_ RequestFileData = (*fileAttach)(nil)
)