1
0
Fork 0
telegram/types_test.go

2051 lines
46 KiB
Go
Raw Permalink Normal View History

2020-01-09 15:27:00 +00:00
package telegram
import (
"io/ioutil"
"os"
"path/filepath"
2020-11-05 08:28:32 +00:00
"strconv"
2020-01-09 15:27:00 +00:00
"testing"
"time"
"github.com/stretchr/testify/assert"
http "github.com/valyala/fasthttp"
"golang.org/x/text/language"
)
func TestUserLanguage(t *testing.T) {
for _, tc := range []struct {
name string
user User
expResult language.Tag
}{{
name: "russian",
user: User{LanguageCode: "ru"},
expResult: language.Russian,
}, {
name: "english",
user: User{LanguageCode: "en"},
expResult: language.English,
}, {
name: "other",
user: User{LanguageCode: "w-t-f"},
expResult: language.Und,
}, {
name: "empty",
expResult: language.Und,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.user.Language(), tc.expResult)
})
}
}
func TestUserFullName(t *testing.T) {
for _, tc := range []struct {
name string
user User
expResult string
}{{
name: "full",
user: User{FirstName: "Maxim", LastName: "Lebedev"},
expResult: "Maxim Lebedev",
}, {
name: "first",
user: User{FirstName: "Maxim"},
expResult: "Maxim",
}, {
name: "last",
user: User{LastName: "Lebedev"},
expResult: "",
}, {
name: "empty",
expResult: "",
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.user.FullName(), tc.expResult)
})
}
}
func TestUserHasLastName(t *testing.T) {
t.Run("true", func(t *testing.T) {
u := User{LastName: "Lebedev"}
assert.True(t, u.HasLastName())
})
t.Run("false", func(t *testing.T) {
u := User{FirstName: "Maxim"}
assert.False(t, u.HasLastName())
})
}
func TestUserHasUsername(t *testing.T) {
t.Run("true", func(t *testing.T) {
u := User{Username: "toby3d", FirstName: "Maxim", LastName: "Lebedev"}
assert.True(t, u.HasUsername())
})
t.Run("false", func(t *testing.T) {
u := User{FirstName: "Maxim", LastName: "Lebedev"}
assert.False(t, u.HasUsername())
})
}
func TestChatIsPrivate(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{Type: ChatPrivate, FirstName: "Maxim", LastName: "Lebedev"}
assert.True(t, c.IsPrivate())
})
t.Run("false", func(t *testing.T) {
c := Chat{Type: ChatSuperGroup, FirstName: "Меня заставили создать эту группу"}
assert.False(t, c.IsPrivate())
})
}
func TestChatIsGroup(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{Type: ChatGroup, FirstName: "Меня заставили создать эту группу"}
assert.True(t, c.IsGroup())
})
t.Run("false", func(t *testing.T) {
c := Chat{Type: ChatPrivate, FirstName: "Maxim", LastName: "Lebedev"}
assert.False(t, c.IsGroup())
})
}
func TestChatIsSuperGroup(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{Type: ChatSuperGroup, FirstName: "Меня заставили создать эту группу"}
assert.True(t, c.IsSuperGroup())
})
t.Run("false", func(t *testing.T) {
c := Chat{Type: ChatPrivate, FirstName: "Maxim", LastName: "Lebedev"}
assert.False(t, c.IsSuperGroup())
})
}
func TestChatIsChannel(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{Type: ChatChannel, FirstName: "toby3d"}
assert.True(t, c.IsChannel())
})
t.Run("false", func(t *testing.T) {
c := Chat{Type: ChatPrivate, FirstName: "Меня заставили создать эту группу"}
assert.False(t, c.IsChannel())
})
}
func TestChatHasPinnedMessage(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{PinnedMessage: &Message{Text: "hello, world!"}}
assert.True(t, c.HasPinnedMessage())
})
t.Run("false", func(t *testing.T) {
c := Chat{}
assert.False(t, c.HasPinnedMessage())
})
}
func TestChatHasStickerSet(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{StickerSetName: "HotCherry"}
assert.True(t, c.HasStickerSet())
})
t.Run("false", func(t *testing.T) {
c := Chat{}
assert.False(t, c.HasStickerSet())
})
}
func TestChatFullName(t *testing.T) {
for _, tc := range []struct {
name string
chat Chat
expResult string
}{{
name: "full",
chat: Chat{FirstName: "Maxim", LastName: "Lebedev"},
expResult: "Maxim Lebedev",
}, {
name: "first",
chat: Chat{FirstName: "Меня заставили создать эту группу"},
expResult: "Меня заставили создать эту группу",
}, {
name: "last",
chat: Chat{LastName: "WTF"},
expResult: "",
}, {
name: "empty",
expResult: "",
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.chat.FullName(), tc.expResult)
})
}
}
func TestChatHasLastName(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{FirstName: "Maxim", LastName: "Lebedev"}
assert.True(t, c.HasLastName())
})
t.Run("false", func(t *testing.T) {
c := Chat{FirstName: "Меня заставили создать эту группу", LastName: ""}
assert.False(t, c.HasLastName())
})
}
func TestChatHasUsername(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{Username: "toby3dRu"}
assert.True(t, c.HasUsername())
})
t.Run("false", func(t *testing.T) {
c := Chat{}
assert.False(t, c.HasUsername())
})
}
func TestChatHasDescription(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{Description: "hello, world!"}
assert.True(t, c.HasDescription())
})
t.Run("false", func(t *testing.T) {
c := Chat{}
assert.False(t, c.HasDescription())
})
}
func TestChatHasInviteLink(t *testing.T) {
t.Run("true", func(t *testing.T) {
c := Chat{InviteLink: "https://t.me/joinchat/AAAAAD84EWpWTRTMh4aahQ"}
assert.True(t, c.HasInviteLink())
})
t.Run("false", func(t *testing.T) {
c := Chat{}
assert.False(t, c.HasInviteLink())
})
}
func TestMessageIsCommand(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Text: "/" + CommandStart, Entities: []*MessageEntity{{Type: EntityBotCommand}}}
assert.True(t, m.IsCommand())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, @toby3d", Entities: []*MessageEntity{{Type: EntityMention}}}
assert.False(t, m.IsCommand())
})
}
func TestMessageIsCommandEqual(t *testing.T) {
for _, tc := range []struct {
name string
message Message
command string
expResult bool
}{{
name: "exact",
message: Message{
Text: "/" + CommandStart,
Entities: []*MessageEntity{{Type: EntityBotCommand, Length: len(CommandStart) + 1}},
},
command: CommandStart,
expResult: true,
}, {
name: "other",
message: Message{
Text: "/" + CommandStart,
Entities: []*MessageEntity{{Type: EntityBotCommand, Length: len(CommandStart) + 1}},
},
command: CommandHelp,
expResult: false,
}, {
name: "empty",
command: CommandHelp,
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.IsCommandEqual(tc.command), tc.expResult)
})
}
}
func TestMessageCommand(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult string
}{{
name: "command",
message: Message{
Text: "/" + CommandStart,
Entities: []*MessageEntity{{Type: EntityBotCommand, Length: len(CommandStart) + 1}},
},
expResult: CommandStart,
}, {
name: "other",
message: Message{
Text: "hello world",
Entities: []*MessageEntity{{Type: EntityBold, Offset: 6, Length: 5}},
},
expResult: "",
}, {
name: "empty",
expResult: "",
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.Command(), tc.expResult)
})
}
}
func TestMessageRawCommand(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult string
}{{
name: "command",
message: Message{
Text: "/" + CommandStart,
Entities: []*MessageEntity{{Type: EntityBotCommand, Length: len(CommandStart) + 1}},
},
expResult: CommandStart,
}, {
name: "other",
message: Message{
Text: "hello world",
Entities: []*MessageEntity{{Type: EntityBold, Offset: 6, Length: 5}},
},
expResult: "",
}, {
name: "empty",
expResult: "",
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.RawCommand(), tc.expResult)
})
}
}
func TestMessageHasCommandArgument(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult bool
}{{
name: "true",
message: Message{
Text: "/start example",
Entities: []*MessageEntity{{Type: EntityBotCommand, Length: len(CommandStart) + 1}},
},
expResult: true,
}, {
name: "other",
message: Message{
Text: "/start",
Entities: []*MessageEntity{{Type: EntityBold, Offset: 1, Length: len(CommandStart)}},
},
expResult: false,
}, {
name: "false",
message: Message{
Text: "/start",
Entities: []*MessageEntity{{Type: EntityBotCommand, Length: len(CommandStart) + 1}},
},
expResult: false,
}, {
name: "empty",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.HasCommandArgument(), tc.expResult)
})
}
}
func TestMessageCommandArgument(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult string
}{{
name: "true",
message: Message{
Text: "/start example",
Entities: []*MessageEntity{{Type: EntityBotCommand, Length: len(CommandStart) + 1}},
},
expResult: "example",
}, {
name: "false",
message: Message{
Text: "/start",
Entities: []*MessageEntity{{Type: EntityBotCommand, Length: len(CommandStart) + 1}},
},
expResult: "",
}, {
name: "empty",
expResult: "",
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.CommandArgument(), tc.expResult)
})
}
}
func TestMessageIsReply(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult bool
}{{
name: "true",
message: Message{Text: "hello!", ReplyToMessage: &Message{Text: "hello, world!"}},
expResult: true,
}, {
name: "false",
message: Message{Text: "hello!"},
expResult: false,
}, {
name: "empty",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.IsReply(), tc.expResult)
})
}
}
func TestMessageIsForward(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Text: "hello, world!", ForwardDate: time.Now().Unix()}
assert.True(t, m.IsForward())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsForward())
})
}
func TestMessageTime(t *testing.T) {
now := time.Now().Round(time.Second)
zero := time.Time{}
for _, tc := range []struct {
name string
message Message
expResult time.Time
}{{
name: "specific date",
message: Message{Date: now.Unix()},
expResult: now,
}, {
name: "zero",
message: Message{Date: 0},
expResult: zero,
}, {
name: "empty",
expResult: zero,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.True(t, tc.message.Time().Equal(tc.expResult))
})
}
}
func TestMessageForwardTime(t *testing.T) {
now := time.Now().Round(time.Second)
zero := time.Time{}
for _, tc := range []struct {
name string
message Message
expResult time.Time
}{{
name: "specific date",
message: Message{ForwardDate: now.Unix()},
expResult: now,
}, {
name: "false",
message: Message{ForwardDate: 0},
expResult: zero,
}, {
name: "empty",
expResult: zero,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.True(t, tc.message.ForwardTime().Equal(tc.expResult))
})
}
}
func TestMessageEditTime(t *testing.T) {
now := time.Now().Round(time.Second)
zero := time.Time{}
for _, tc := range []struct {
name string
message Message
expResult time.Time
}{{
name: "true",
message: Message{EditDate: now.Unix()},
expResult: now,
}, {
name: "false",
message: Message{EditDate: 0},
expResult: zero,
}, {
name: "empty",
expResult: zero,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.True(t, tc.message.EditTime().Equal(tc.expResult))
})
}
}
func TestMessageHasBeenEdited(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Text: "sorry, fixed", EditDate: time.Now().UTC().Unix()}
assert.True(t, m.HasBeenEdited())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "sorry, fixed", EditDate: 0}
assert.False(t, m.HasBeenEdited())
})
}
func TestMessageIsText(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.True(t, m.IsText())
})
t.Run("false", func(t *testing.T) {
m := Message{}
assert.False(t, m.IsText())
})
}
func TestMessageIsAudio(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Audio: &Audio{Performer: "One-Aloner", Title: "Monitor"}}
assert.True(t, m.IsAudio())
})
t.Run("false", func(t *testing.T) {
m := Message{}
assert.False(t, m.IsAudio())
})
}
func TestMessageIsDocument(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Document: &Document{FileName: "readme.txt"}}
assert.True(t, m.IsDocument())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsDocument())
})
}
func TestMessageIsGame(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Game: &Game{Title: "MuSquare"}}
assert.True(t, m.IsGame())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsGame())
})
}
func TestMessageIsPhoto(t *testing.T) {
t.Run("true", func(t *testing.T) {
2020-01-28 08:30:47 +00:00
m := Message{Photo: Photo{{FileID: "abc"}}}
2020-01-09 15:27:00 +00:00
assert.True(t, m.IsPhoto())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsPhoto())
})
}
func TestMessageIsSticker(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Sticker: &Sticker{FileID: "abc"}}
assert.True(t, m.IsSticker())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsSticker())
})
}
func TestMessageIsVideo(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Video: &Video{FileID: "abc"}}
assert.True(t, m.IsVideo())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsVideo())
})
}
func TestMessageIsVoice(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Voice: &Voice{FileID: "abc"}}
assert.True(t, m.IsVoice())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsVoice())
})
}
func TestMessageIsVideoNote(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{VideoNote: &VideoNote{FileID: "abc"}}
assert.True(t, m.IsVideoNote())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsVideoNote())
})
}
func TestMessageIsContact(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Contact: &Contact{FirstName: "Maxim", PhoneNumber: "1234567890"}}
assert.True(t, m.IsContact())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsContact())
})
}
func TestMessageIsLocation(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Location: &Location{Latitude: 56.085180, Longitude: 60.735150}}
assert.True(t, m.IsLocation())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsLocation())
})
}
func TestMessageIsVenue(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Venue: &Venue{
Title: "Памятник В. И. Ленину",
Address: "Россия, Челябинская область, Снежинск, улица Свердлова",
Location: &Location{Latitude: 56.085180, Longitude: 60.735150},
}}
assert.True(t, m.IsVenue())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsVenue())
})
}
func TestMessageIsAnimation(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Animation: &Animation{FileID: "abc"}}
assert.True(t, m.IsAnimation())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsAnimation())
})
}
func TestMessageIsNewChatMembersEvent(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{NewChatMembers: []*User{{ID: 42, FirstName: "Maxim", LastName: "Lebedev"}}}
assert.True(t, m.IsNewChatMembersEvent())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsNewChatMembersEvent())
})
}
func TestMessageIsLeftChatMemberEvent(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{LeftChatMember: &User{ID: 42, FirstName: "Maxim", LastName: "Lebedev"}}
assert.True(t, m.IsLeftChatMemberEvent())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsLeftChatMemberEvent())
})
}
func TestMessageIsNewChatTitleEvent(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{NewChatTitle: "Меня заставили создать эту группу"}
assert.True(t, m.IsNewChatTitleEvent())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsNewChatTitleEvent())
})
}
func TestMessageIsNewChatPhotoEvent(t *testing.T) {
t.Run("true", func(t *testing.T) {
2020-01-28 08:30:47 +00:00
m := Message{NewChatPhoto: Photo{{FileID: "abc"}}}
2020-01-09 15:27:00 +00:00
assert.True(t, m.IsNewChatPhotoEvent())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsNewChatPhotoEvent())
})
}
func TestMessageIsDeleteChatPhotoEvent(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{DeleteChatPhoto: true}
assert.True(t, m.IsDeleteChatPhotoEvent())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsDeleteChatPhotoEvent())
})
}
func TestMessageIsGroupChatCreatedEvent(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{GroupChatCreated: true}
assert.True(t, m.IsGroupChatCreatedEvent())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsGroupChatCreatedEvent())
})
}
func TestMessageIsSupergroupChatCreatedEvent(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{SupergroupChatCreated: true}
assert.True(t, m.IsSupergroupChatCreatedEvent())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsSupergroupChatCreatedEvent())
})
}
func TestMessageIsChannelChatCreatedEvent(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{ChannelChatCreated: true}
assert.True(t, m.IsChannelChatCreatedEvent())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsChannelChatCreatedEvent())
})
}
func TestMessageIsPinnedMessage(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{PinnedMessage: &Message{Text: "hello, world!"}}
assert.True(t, m.IsPinnedMessage())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsPinnedMessage())
})
}
func TestMessageIsInvoice(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Invoice: &Invoice{Title: "Time Machine", TotalAmount: 1}}
assert.True(t, m.IsInvoice())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsInvoice())
})
}
func TestMessageIsSuccessfulPayment(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{SuccessfulPayment: &SuccessfulPayment{
OrderInfo: &OrderInfo{Name: "Maxim Lebedev"}, TotalAmount: 1,
}}
assert.True(t, m.IsSuccessfulPayment())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsSuccessfulPayment())
})
}
func TestMessageIsPoll(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Poll: &Poll{
Question: "What is the answer to life, the universe, and everything?",
Options: []*PollOption{
{Text: "42", VoterCount: 420},
{Text: "24", VoterCount: 140},
},
}}
assert.True(t, m.IsPoll())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.IsPoll())
})
}
func TestMessageHasEntities(t *testing.T) {
t.Run("true", func(t *testing.T) {
m := Message{Text: "hello, world!", Entities: []*MessageEntity{
{Type: EntityBold, Offset: 7, Length: 5},
}}
assert.True(t, m.HasEntities())
})
t.Run("false", func(t *testing.T) {
m := Message{Text: "hello, world!"}
assert.False(t, m.HasEntities())
})
}
func TestMessageHasCaptionEntities(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult bool
}{{
name: "true",
message: Message{
Caption: "hello, world!", CaptionEntities: []*MessageEntity{
{Type: EntityBold, Offset: 7, Length: 5},
},
},
expResult: true,
}, {
name: "false",
message: Message{Text: "hello, world!"},
expResult: false,
}, {
name: "empty",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.HasCaptionEntities(), tc.expResult)
})
}
}
func TestMessageHasMentions(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult bool
}{{
name: "true",
message: Message{Text: "hello, @toby3d!", Entities: []*MessageEntity{
{Type: EntityMention, Offset: 7, Length: 7},
}},
expResult: true,
}, {
name: "other",
message: Message{Text: "hello, world!", Entities: []*MessageEntity{
{Type: EntityBold, Offset: 7, Length: 5},
}},
expResult: false,
}, {
name: "false",
message: Message{Text: "hello, world!"},
expResult: false,
}, {
name: "empty",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.HasMentions(), tc.expResult)
})
}
}
func TestMessageHasCaptionMentions(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult bool
}{{
name: "true",
message: Message{Caption: "hello, @toby3d!", CaptionEntities: []*MessageEntity{
{Type: EntityMention, Offset: 7, Length: 7},
}},
expResult: true,
}, {
name: "other",
message: Message{Text: "hello, world!", CaptionEntities: []*MessageEntity{
{Type: EntityBold, Offset: 7, Length: 5},
}},
expResult: false,
}, {
name: "false",
message: Message{Text: "hello, world!"},
expResult: false,
}, {
name: "empty",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.HasCaptionMentions(), tc.expResult)
})
}
}
func TestMessageHasCaption(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult bool
}{{
name: "true",
message: Message{Caption: "hello, world!"},
expResult: true,
}, {
name: "false",
message: Message{Text: "hello, world!"},
expResult: false,
}, {
name: "empty",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.HasCaption(), tc.expResult)
})
}
}
func TestMessageHasAuthorSignature(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult bool
}{{
name: "true",
message: Message{AuthorSignature: "Editor"},
expResult: true,
}, {
name: "false",
message: Message{Text: "hello, world!"},
expResult: false,
}, {
name: "empty",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.HasAuthorSignature(), tc.expResult)
})
}
}
func TestMessageIsEvent(t *testing.T) {
for _, tc := range []struct {
name string
message Message
expResult bool
}{{
name: "ChannelChatCreated",
message: Message{ChannelChatCreated: true},
expResult: true,
}, {
name: "DeleteChatPhoto",
message: Message{DeleteChatPhoto: true},
expResult: true,
}, {
name: "GroupChatCreated",
message: Message{GroupChatCreated: true},
expResult: true,
}, {
name: "LeftChatMember",
message: Message{LeftChatMember: &User{ID: 42}},
expResult: true,
}, {
name: "NewChatMembers",
message: Message{NewChatMembers: []*User{{ID: 42}}},
expResult: true,
}, {
name: "NewChatTitle",
message: Message{NewChatTitle: "Меня заставили создать эту группу"},
expResult: true,
}, {
name: "SupergroupChatCreated",
message: Message{SupergroupChatCreated: true},
expResult: true,
}, {
name: "NewChatPhoto",
2020-01-28 08:30:47 +00:00
message: Message{NewChatPhoto: Photo{{FileID: "abc"}}},
2020-01-09 15:27:00 +00:00
expResult: true,
}, {
name: "false",
message: Message{Text: "hello, world!"},
expResult: false,
}, {
name: "empty",
expResult: false,
}} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.message.IsEvent(), tc.expResult)
})
}
}
func TestMessageEntityIsBold(t *testing.T) {
for _, tc := range []struct {
name string
entity MessageEntity
expResult bool