telegram-bot-api/types_test.go

334 lines
6.9 KiB
Go
Raw 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
if !entity.IsUrl() {
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{}
_ Chattable = AudioConfig{}
_ Chattable = CallbackConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = ChatAdministratorsConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = ChatActionConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = ChatInfoConfig{}
_ Chattable = ChatInviteLinkConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = ContactConfig{}
_ Chattable = DeleteChatPhotoConfig{}
_ Chattable = DeleteChatStickerSetConfig{}
_ Chattable = DeleteMessageConfig{}
_ Chattable = DocumentConfig{}
_ 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{}
2020-01-06 08:44:13 +01:00
_ Chattable = GetChatMemberConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = GetGameHighScoresConfig{}
_ 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{}
_ Chattable = MediaGroupConfig{}
_ Chattable = MessageConfig{}
_ Chattable = PhotoConfig{}
_ Chattable = PinChatMessageConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = PromoteChatMemberConfig{}
_ Chattable = RemoveWebhookConfig{}
_ Chattable = RestrictChatMemberConfig{}
_ Chattable = SendPollConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = SetChatDescriptionConfig{}
_ Chattable = SetChatPhotoConfig{}
_ Chattable = SetChatTitleConfig{}
_ Chattable = SetGameScoreConfig{}
_ Chattable = StickerConfig{}
2020-01-06 08:44:13 +01:00
_ Chattable = StopPollConfig{}
_ Chattable = StopMessageLiveLocationConfig{}
_ Chattable = UnbanChatMemberConfig{}
2018-10-09 06:34:34 +02:00
_ Chattable = UnpinChatMessageConfig{}
_ Chattable = UpdateConfig{}
_ Chattable = UserProfilePhotosConfig{}
_ Chattable = VenueConfig{}
_ Chattable = VideoConfig{}
_ Chattable = VideoNoteConfig{}
_ Chattable = VoiceConfig{}
_ Chattable = WebhookConfig{}
)