all repos — telegram-bot-api @ 483f21c48bebae648c4464138be96b1344151035

Golang bindings for the Telegram Bot API

types_test.go (view raw)

  1package tgbotapi_test
  2
  3import (
  4	"testing"
  5	"time"
  6
  7	"github.com/go-telegram-bot-api/telegram-bot-api"
  8)
  9
 10func TestUserStringWith(t *testing.T) {
 11	user := tgbotapi.User{
 12		ID:           0,
 13		FirstName:    "Test",
 14		LastName:     "Test",
 15		UserName:     "",
 16		LanguageCode: "en",
 17		IsBot:        false,
 18	}
 19
 20	if user.String() != "Test Test" {
 21		t.Fail()
 22	}
 23}
 24
 25func TestUserStringWithUserName(t *testing.T) {
 26	user := tgbotapi.User{
 27		ID:           0,
 28		FirstName:    "Test",
 29		LastName:     "Test",
 30		UserName:     "@test",
 31		LanguageCode: "en",
 32	}
 33
 34	if user.String() != "@test" {
 35		t.Fail()
 36	}
 37}
 38
 39func TestMessageTime(t *testing.T) {
 40	message := tgbotapi.Message{Date: 0}
 41
 42	date := time.Unix(0, 0)
 43	if message.Time() != date {
 44		t.Fail()
 45	}
 46}
 47
 48func TestMessageIsCommandWithCommand(t *testing.T) {
 49	message := tgbotapi.Message{Text: "/command"}
 50	message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
 51
 52	if message.IsCommand() != true {
 53		t.Fail()
 54	}
 55}
 56
 57func TestIsCommandWithText(t *testing.T) {
 58	message := tgbotapi.Message{Text: "some text"}
 59
 60	if message.IsCommand() != false {
 61		t.Fail()
 62	}
 63}
 64
 65func TestIsCommandWithEmptyText(t *testing.T) {
 66	message := tgbotapi.Message{Text: ""}
 67
 68	if message.IsCommand() != false {
 69		t.Fail()
 70	}
 71}
 72
 73func TestCommandWithCommand(t *testing.T) {
 74	message := tgbotapi.Message{Text: "/command"}
 75	message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
 76
 77	if message.Command() != "command" {
 78		t.Fail()
 79	}
 80}
 81
 82func TestCommandWithEmptyText(t *testing.T) {
 83	message := tgbotapi.Message{Text: ""}
 84
 85	if message.Command() != "" {
 86		t.Fail()
 87	}
 88}
 89
 90func TestCommandWithNonCommand(t *testing.T) {
 91	message := tgbotapi.Message{Text: "test text"}
 92
 93	if message.Command() != "" {
 94		t.Fail()
 95	}
 96}
 97
 98func TestCommandWithBotName(t *testing.T) {
 99	message := tgbotapi.Message{Text: "/command@testbot"}
100	message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
101
102	if message.Command() != "command" {
103		t.Fail()
104	}
105}
106
107func TestCommandWithAtWithBotName(t *testing.T) {
108	message := tgbotapi.Message{Text: "/command@testbot"}
109	message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
110
111	if message.CommandWithAt() != "command@testbot" {
112		t.Fail()
113	}
114}
115
116func TestMessageCommandArgumentsWithArguments(t *testing.T) {
117	message := tgbotapi.Message{Text: "/command with arguments"}
118	message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
119	if message.CommandArguments() != "with arguments" {
120		t.Fail()
121	}
122}
123
124func TestMessageCommandArgumentsWithMalformedArguments(t *testing.T) {
125	message := tgbotapi.Message{Text: "/command-without argument space"}
126	message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
127	if message.CommandArguments() != "without argument space" {
128		t.Fail()
129	}
130}
131
132func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
133	message := tgbotapi.Message{Text: "/command"}
134	if message.CommandArguments() != "" {
135		t.Fail()
136	}
137}
138
139func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
140	message := tgbotapi.Message{Text: "test text"}
141	if message.CommandArguments() != "" {
142		t.Fail()
143	}
144}
145
146func TestMessageEntityParseURLGood(t *testing.T) {
147	entity := tgbotapi.MessageEntity{URL: "https://www.google.com"}
148
149	if _, err := entity.ParseURL(); err != nil {
150		t.Fail()
151	}
152}
153
154func TestMessageEntityParseURLBad(t *testing.T) {
155	entity := tgbotapi.MessageEntity{URL: ""}
156
157	if _, err := entity.ParseURL(); err == nil {
158		t.Fail()
159	}
160}
161
162func TestChatIsPrivate(t *testing.T) {
163	chat := tgbotapi.Chat{ID: 10, Type: "private"}
164
165	if chat.IsPrivate() != true {
166		t.Fail()
167	}
168}
169
170func TestChatIsGroup(t *testing.T) {
171	chat := tgbotapi.Chat{ID: 10, Type: "group"}
172
173	if chat.IsGroup() != true {
174		t.Fail()
175	}
176}
177
178func TestChatIsChannel(t *testing.T) {
179	chat := tgbotapi.Chat{ID: 10, Type: "channel"}
180
181	if chat.IsChannel() != true {
182		t.Fail()
183	}
184}
185
186func TestChatIsSuperGroup(t *testing.T) {
187	chat := tgbotapi.Chat{ID: 10, Type: "supergroup"}
188
189	if !chat.IsSuperGroup() {
190		t.Fail()
191	}
192}
193
194func TestFileLink(t *testing.T) {
195	file := tgbotapi.File{FilePath: "test/test.txt"}
196
197	if file.Link("token") != "https://api.telegram.org/file/bottoken/test/test.txt" {
198		t.Fail()
199	}
200}