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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 18:31:59 +01:00
|
|
|
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}}
|
2015-12-13 18:31:59 +01:00
|
|
|
|
2018-03-26 19:39:07 +02:00
|
|
|
if !message.IsCommand() {
|
2015-12-13 18:31:59 +01:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsCommandWithText(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
message := Message{Text: "some text"}
|
2015-12-13 18:31:59 +01:00
|
|
|
|
2018-03-26 19:39:07 +02:00
|
|
|
if message.IsCommand() {
|
2015-12-13 18:31:59 +01:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsCommandWithEmptyText(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
message := Message{Text: ""}
|
2015-12-13 18:31:59 +01:00
|
|
|
|
2018-03-26 19:39:07 +02:00
|
|
|
if message.IsCommand() {
|
2015-12-13 18:31:59 +01:00
|
|
|
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}}
|
2015-12-13 18:31:59 +01:00
|
|
|
|
2016-01-04 18:45:46 +01:00
|
|
|
if message.Command() != "command" {
|
2015-12-13 18:31:59 +01:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandWithEmptyText(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
message := Message{Text: ""}
|
2015-12-13 18:31:59 +01:00
|
|
|
|
|
|
|
if message.Command() != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandWithNonCommand(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
message := Message{Text: "test text"}
|
2015-12-13 18:31:59 +01:00
|
|
|
|
|
|
|
if message.Command() != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 18:45:46 +01:00
|
|
|
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}}
|
2016-01-04 18:45:46 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 18:31:59 +01:00
|
|
|
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}}
|
2015-12-13 18:31:59 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 18:31:59 +01:00
|
|
|
func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
message := Message{Text: "/command"}
|
2015-12-13 18:31:59 +01:00
|
|
|
if message.CommandArguments() != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
message := Message{Text: "test text"}
|
2015-12-13 18:31:59 +01:00
|
|
|
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
|
|
|
|
2018-03-26 19:39:07 +02: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
|
|
|
|
2018-03-26 19:39:07 +02: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
|
|
|
|
2018-03-26 19:39:07 +02:00
|
|
|
if !chat.IsChannel() {
|
2015-11-21 11:17:34 +01:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 03:13:31 +01:00
|
|
|
func TestChatIsSuperGroup(t *testing.T) {
|
2018-10-09 06:34:34 +02:00
|
|
|
chat := Chat{ID: 10, Type: "supergroup"}
|
2015-12-10 03:13:31 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-10-09 01:21:29 +02:00
|
|
|
|
|
|
|
// Ensure all configs are sendable
|
|
|
|
var (
|
2018-10-09 06:34:34 +02:00
|
|
|
_ Chattable = AnimationConfig{}
|
|
|
|
_ 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{}
|
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{}
|
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{}
|
|
|
|
_ 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{}
|
|
|
|
_ Chattable = SetChatPhotoConfig{}
|
|
|
|
_ Chattable = SetChatTitleConfig{}
|
|
|
|
_ Chattable = SetGameScoreConfig{}
|
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{}
|
|
|
|
_ Chattable = UserProfilePhotosConfig{}
|
|
|
|
_ Chattable = VenueConfig{}
|
|
|
|
_ Chattable = VideoConfig{}
|
|
|
|
_ Chattable = VideoNoteConfig{}
|
|
|
|
_ Chattable = VoiceConfig{}
|
|
|
|
_ Chattable = WebhookConfig{}
|
2018-10-09 01:21:29 +02:00
|
|
|
)
|
2020-07-26 02:29:40 +02:00
|
|
|
|
|
|
|
// 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)
|
2020-07-26 21:40:12 +02:00
|
|
|
_ Fileable = (*MediaGroupConfig)(nil)
|
2020-07-26 23:06:22 +02:00
|
|
|
_ Fileable = (*WebhookConfig)(nil)
|
|
|
|
_ Fileable = (*SetStickerSetThumbConfig)(nil)
|
2020-07-26 02:29:40 +02:00
|
|
|
)
|
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)
|
|
|
|
)
|