Add more tests.
parent
289f7ef6ad
commit
c0eb5db6c3
109
bot_test.go
109
bot_test.go
|
@ -230,6 +230,16 @@ func TestSendWithExistingVoice(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSendWithContact(t *testing.T) {
|
||||
bot, _ := getBot(t)
|
||||
|
||||
contact := tgbotapi.NewContact(ChatID, "5551234567", "Test")
|
||||
|
||||
if _, err := bot.Send(contact); err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendWithLocation(t *testing.T) {
|
||||
bot, _ := getBot(t)
|
||||
|
||||
|
@ -240,6 +250,16 @@ func TestSendWithLocation(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSendWithVenue(t *testing.T) {
|
||||
bot, _ := getBot(t)
|
||||
|
||||
venue := tgbotapi.NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
|
||||
|
||||
if _, err := bot.Send(venue); err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendWithNewVideo(t *testing.T) {
|
||||
bot, _ := getBot(t)
|
||||
|
||||
|
@ -399,6 +419,95 @@ func TestSetWebhookWithoutCert(t *testing.T) {
|
|||
bot.RemoveWebhook()
|
||||
}
|
||||
|
||||
func TestNewInlineQueryResultArticle(t *testing.T) {
|
||||
result := tgbotapi.NewInlineQueryResultArticle("id", "title", "message")
|
||||
|
||||
if result.Type != "article" ||
|
||||
result.ID != "id" ||
|
||||
result.Title != "title" ||
|
||||
result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "message" {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInlineQueryResultGIF(t *testing.T) {
|
||||
result := tgbotapi.NewInlineQueryResultGIF("id", "google.com")
|
||||
|
||||
if result.Type != "gif" ||
|
||||
result.ID != "id" ||
|
||||
result.URL != "google.com" {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInlineQueryResultMPEG4GIF(t *testing.T) {
|
||||
result := tgbotapi.NewInlineQueryResultMPEG4GIF("id", "google.com")
|
||||
|
||||
if result.Type != "mpeg4_gif" ||
|
||||
result.ID != "id" ||
|
||||
result.URL != "google.com" {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInlineQueryResultPhoto(t *testing.T) {
|
||||
result := tgbotapi.NewInlineQueryResultPhoto("id", "google.com")
|
||||
|
||||
if result.Type != "photo" ||
|
||||
result.ID != "id" ||
|
||||
result.URL != "google.com" {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInlineQueryResultVideo(t *testing.T) {
|
||||
result := tgbotapi.NewInlineQueryResultVideo("id", "google.com")
|
||||
|
||||
if result.Type != "video" ||
|
||||
result.ID != "id" ||
|
||||
result.URL != "google.com" {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewEditMessageText(t *testing.T) {
|
||||
edit := tgbotapi.NewEditMessageText(ChatID, ReplyToMessageID, "new text")
|
||||
|
||||
if edit.Text != "new text" ||
|
||||
edit.BaseEdit.ChatID != ChatID ||
|
||||
edit.BaseEdit.MessageID != ReplyToMessageID {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewEditMessageCaption(t *testing.T) {
|
||||
edit := tgbotapi.NewEditMessageCaption(ChatID, ReplyToMessageID, "new caption")
|
||||
|
||||
if edit.Caption != "new caption" ||
|
||||
edit.BaseEdit.ChatID != ChatID ||
|
||||
edit.BaseEdit.MessageID != ReplyToMessageID {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewEditMessageReplyMarkup(t *testing.T) {
|
||||
markup := tgbotapi.InlineKeyboardMarkup{
|
||||
InlineKeyboard: [][]tgbotapi.InlineKeyboardButton{
|
||||
[]tgbotapi.InlineKeyboardButton{
|
||||
tgbotapi.InlineKeyboardButton{Text: "test"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
edit := tgbotapi.NewEditMessageReplyMarkup(ChatID, ReplyToMessageID, markup)
|
||||
|
||||
if edit.ReplyMarkup.InlineKeyboard[0][0].Text != "test" ||
|
||||
edit.BaseEdit.ChatID != ChatID ||
|
||||
edit.BaseEdit.MessageID != ReplyToMessageID {
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUpdatesChan(t *testing.T) {
|
||||
bot, _ := getBot(t)
|
||||
|
||||
|
|
|
@ -44,6 +44,7 @@ const (
|
|||
const (
|
||||
// ErrBadFileType happens when you pass an unknown type
|
||||
ErrBadFileType = "bad file type"
|
||||
ErrBadURL = "bad or empty url"
|
||||
)
|
||||
|
||||
// Chattable is any config type that can be sent.
|
||||
|
|
5
types.go
5
types.go
|
@ -2,6 +2,7 @@ package tgbotapi
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
@ -176,6 +177,10 @@ type MessageEntity struct {
|
|||
|
||||
// ParseURL attempts to parse a URL contained within a MessageEntity.
|
||||
func (entity MessageEntity) ParseURL() (*url.URL, error) {
|
||||
if entity.URL == "" {
|
||||
return nil, errors.New(ErrBadURL)
|
||||
}
|
||||
|
||||
return url.Parse(entity.URL)
|
||||
}
|
||||
|
||||
|
|
|
@ -108,6 +108,22 @@ func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestMessageEntityParseURLGood(t *testing.T) {
|
||||
entity := tgbotapi.MessageEntity{URL: "https://www.google.com"}
|
||||
|
||||
if _, err := entity.ParseURL(); err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageEntityParseURLBad(t *testing.T) {
|
||||
entity := tgbotapi.MessageEntity{URL: ""}
|
||||
|
||||
if _, err := entity.ParseURL(); err == nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatIsPrivate(t *testing.T) {
|
||||
chat := tgbotapi.Chat{ID: 10, Type: "private"}
|
||||
|
||||
|
|
Loading…
Reference in New Issue