Skip to content

Commit c0eb5db

Browse files
author
Syfaro
committed
Add more tests.
1 parent 289f7ef commit c0eb5db

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

bot_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ func TestSendWithExistingVoice(t *testing.T) {
230230
}
231231
}
232232

233+
func TestSendWithContact(t *testing.T) {
234+
bot, _ := getBot(t)
235+
236+
contact := tgbotapi.NewContact(ChatID, "5551234567", "Test")
237+
238+
if _, err := bot.Send(contact); err != nil {
239+
t.Fail()
240+
}
241+
}
242+
233243
func TestSendWithLocation(t *testing.T) {
234244
bot, _ := getBot(t)
235245

@@ -240,6 +250,16 @@ func TestSendWithLocation(t *testing.T) {
240250
}
241251
}
242252

253+
func TestSendWithVenue(t *testing.T) {
254+
bot, _ := getBot(t)
255+
256+
venue := tgbotapi.NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
257+
258+
if _, err := bot.Send(venue); err != nil {
259+
t.Fail()
260+
}
261+
}
262+
243263
func TestSendWithNewVideo(t *testing.T) {
244264
bot, _ := getBot(t)
245265

@@ -399,6 +419,95 @@ func TestSetWebhookWithoutCert(t *testing.T) {
399419
bot.RemoveWebhook()
400420
}
401421

422+
func TestNewInlineQueryResultArticle(t *testing.T) {
423+
result := tgbotapi.NewInlineQueryResultArticle("id", "title", "message")
424+
425+
if result.Type != "article" ||
426+
result.ID != "id" ||
427+
result.Title != "title" ||
428+
result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "message" {
429+
t.Fail()
430+
}
431+
}
432+
433+
func TestNewInlineQueryResultGIF(t *testing.T) {
434+
result := tgbotapi.NewInlineQueryResultGIF("id", "google.com")
435+
436+
if result.Type != "gif" ||
437+
result.ID != "id" ||
438+
result.URL != "google.com" {
439+
t.Fail()
440+
}
441+
}
442+
443+
func TestNewInlineQueryResultMPEG4GIF(t *testing.T) {
444+
result := tgbotapi.NewInlineQueryResultMPEG4GIF("id", "google.com")
445+
446+
if result.Type != "mpeg4_gif" ||
447+
result.ID != "id" ||
448+
result.URL != "google.com" {
449+
t.Fail()
450+
}
451+
}
452+
453+
func TestNewInlineQueryResultPhoto(t *testing.T) {
454+
result := tgbotapi.NewInlineQueryResultPhoto("id", "google.com")
455+
456+
if result.Type != "photo" ||
457+
result.ID != "id" ||
458+
result.URL != "google.com" {
459+
t.Fail()
460+
}
461+
}
462+
463+
func TestNewInlineQueryResultVideo(t *testing.T) {
464+
result := tgbotapi.NewInlineQueryResultVideo("id", "google.com")
465+
466+
if result.Type != "video" ||
467+
result.ID != "id" ||
468+
result.URL != "google.com" {
469+
}
470+
}
471+
472+
func TestNewEditMessageText(t *testing.T) {
473+
edit := tgbotapi.NewEditMessageText(ChatID, ReplyToMessageID, "new text")
474+
475+
if edit.Text != "new text" ||
476+
edit.BaseEdit.ChatID != ChatID ||
477+
edit.BaseEdit.MessageID != ReplyToMessageID {
478+
t.Fail()
479+
}
480+
}
481+
482+
func TestNewEditMessageCaption(t *testing.T) {
483+
edit := tgbotapi.NewEditMessageCaption(ChatID, ReplyToMessageID, "new caption")
484+
485+
if edit.Caption != "new caption" ||
486+
edit.BaseEdit.ChatID != ChatID ||
487+
edit.BaseEdit.MessageID != ReplyToMessageID {
488+
t.Fail()
489+
}
490+
}
491+
492+
func TestNewEditMessageReplyMarkup(t *testing.T) {
493+
markup := tgbotapi.InlineKeyboardMarkup{
494+
InlineKeyboard: [][]tgbotapi.InlineKeyboardButton{
495+
[]tgbotapi.InlineKeyboardButton{
496+
tgbotapi.InlineKeyboardButton{Text: "test"},
497+
},
498+
},
499+
}
500+
501+
edit := tgbotapi.NewEditMessageReplyMarkup(ChatID, ReplyToMessageID, markup)
502+
503+
if edit.ReplyMarkup.InlineKeyboard[0][0].Text != "test" ||
504+
edit.BaseEdit.ChatID != ChatID ||
505+
edit.BaseEdit.MessageID != ReplyToMessageID {
506+
t.Fail()
507+
}
508+
509+
}
510+
402511
func TestUpdatesChan(t *testing.T) {
403512
bot, _ := getBot(t)
404513

configs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
const (
4545
// ErrBadFileType happens when you pass an unknown type
4646
ErrBadFileType = "bad file type"
47+
ErrBadURL = "bad or empty url"
4748
)
4849

4950
// Chattable is any config type that can be sent.

types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tgbotapi
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"net/url"
78
"strings"
@@ -176,6 +177,10 @@ type MessageEntity struct {
176177

177178
// ParseURL attempts to parse a URL contained within a MessageEntity.
178179
func (entity MessageEntity) ParseURL() (*url.URL, error) {
180+
if entity.URL == "" {
181+
return nil, errors.New(ErrBadURL)
182+
}
183+
179184
return url.Parse(entity.URL)
180185
}
181186

types_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
108108
}
109109
}
110110

111+
func TestMessageEntityParseURLGood(t *testing.T) {
112+
entity := tgbotapi.MessageEntity{URL: "https://www.google.com"}
113+
114+
if _, err := entity.ParseURL(); err != nil {
115+
t.Fail()
116+
}
117+
}
118+
119+
func TestMessageEntityParseURLBad(t *testing.T) {
120+
entity := tgbotapi.MessageEntity{URL: ""}
121+
122+
if _, err := entity.ParseURL(); err == nil {
123+
t.Fail()
124+
}
125+
}
126+
111127
func TestChatIsPrivate(t *testing.T) {
112128
chat := tgbotapi.Chat{ID: 10, Type: "private"}
113129

0 commit comments

Comments
 (0)