all repos — telegram-bot-api @ master

Golang bindings for the Telegram Bot API

types_test.go (view raw)

  1package tgbotapi
  2
  3import (
  4	"testing"
  5	"time"
  6)
  7
  8func TestUserStringWith(t *testing.T) {
  9	user := User{
 10		ID:           0,
 11		FirstName:    "Test",
 12		LastName:     "Test",
 13		UserName:     "",
 14		LanguageCode: "en",
 15		IsBot:        false,
 16	}
 17
 18	if user.String() != "Test Test" {
 19		t.Fail()
 20	}
 21}
 22
 23func TestUserStringWithUserName(t *testing.T) {
 24	user := User{
 25		ID:           0,
 26		FirstName:    "Test",
 27		LastName:     "Test",
 28		UserName:     "@test",
 29		LanguageCode: "en",
 30	}
 31
 32	if user.String() != "@test" {
 33		t.Fail()
 34	}
 35}
 36
 37func TestMessageTime(t *testing.T) {
 38	message := Message{Date: 0}
 39
 40	date := time.Unix(0, 0)
 41	if message.Time() != date {
 42		t.Fail()
 43	}
 44}
 45
 46func TestMessageIsCommandWithCommand(t *testing.T) {
 47	message := Message{Text: "/command"}
 48	message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
 49
 50	if !message.IsCommand() {
 51		t.Fail()
 52	}
 53}
 54
 55func TestIsCommandWithText(t *testing.T) {
 56	message := Message{Text: "some text"}
 57
 58	if message.IsCommand() {
 59		t.Fail()
 60	}
 61}
 62
 63func TestIsCommandWithEmptyText(t *testing.T) {
 64	message := Message{Text: ""}
 65
 66	if message.IsCommand() {
 67		t.Fail()
 68	}
 69}
 70
 71func TestCommandWithCommand(t *testing.T) {
 72	message := Message{Text: "/command"}
 73	message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
 74
 75	if message.Command() != "command" {
 76		t.Fail()
 77	}
 78}
 79
 80func TestCommandWithEmptyText(t *testing.T) {
 81	message := Message{Text: ""}
 82
 83	if message.Command() != "" {
 84		t.Fail()
 85	}
 86}
 87
 88func TestCommandWithNonCommand(t *testing.T) {
 89	message := Message{Text: "test text"}
 90
 91	if message.Command() != "" {
 92		t.Fail()
 93	}
 94}
 95
 96func TestCommandWithBotName(t *testing.T) {
 97	message := Message{Text: "/command@testbot"}
 98	message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
 99
100	if message.Command() != "command" {
101		t.Fail()
102	}
103}
104
105func TestCommandWithAtWithBotName(t *testing.T) {
106	message := Message{Text: "/command@testbot"}
107	message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
108
109	if message.CommandWithAt() != "command@testbot" {
110		t.Fail()
111	}
112}
113
114func TestMessageCommandArgumentsWithArguments(t *testing.T) {
115	message := Message{Text: "/command with arguments"}
116	message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
117	if message.CommandArguments() != "with arguments" {
118		t.Fail()
119	}
120}
121
122func TestMessageCommandArgumentsWithMalformedArguments(t *testing.T) {
123	message := Message{Text: "/command-without argument space"}
124	message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
125	if message.CommandArguments() != "without argument space" {
126		t.Fail()
127	}
128}
129
130func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
131	message := Message{Text: "/command"}
132	if message.CommandArguments() != "" {
133		t.Fail()
134	}
135}
136
137func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
138	message := Message{Text: "test text"}
139	if message.CommandArguments() != "" {
140		t.Fail()
141	}
142}
143
144func TestMessageEntityParseURLGood(t *testing.T) {
145	entity := MessageEntity{URL: "https://www.google.com"}
146
147	if _, err := entity.ParseURL(); err != nil {
148		t.Fail()
149	}
150}
151
152func TestMessageEntityParseURLBad(t *testing.T) {
153	entity := MessageEntity{URL: ""}
154
155	if _, err := entity.ParseURL(); err == nil {
156		t.Fail()
157	}
158}
159
160func TestChatIsPrivate(t *testing.T) {
161	chat := Chat{ID: 10, Type: "private"}
162
163	if !chat.IsPrivate() {
164		t.Fail()
165	}
166}
167
168func TestChatIsGroup(t *testing.T) {
169	chat := Chat{ID: 10, Type: "group"}
170
171	if !chat.IsGroup() {
172		t.Fail()
173	}
174}
175
176func TestChatIsChannel(t *testing.T) {
177	chat := Chat{ID: 10, Type: "channel"}
178
179	if !chat.IsChannel() {
180		t.Fail()
181	}
182}
183
184func TestChatIsSuperGroup(t *testing.T) {
185	chat := Chat{ID: 10, Type: "supergroup"}
186
187	if !chat.IsSuperGroup() {
188		t.Fail()
189	}
190}
191
192func TestMessageEntityIsMention(t *testing.T) {
193	entity := MessageEntity{Type: "mention"}
194
195	if !entity.IsMention() {
196		t.Fail()
197	}
198}
199
200func TestMessageEntityIsHashtag(t *testing.T) {
201	entity := MessageEntity{Type: "hashtag"}
202
203	if !entity.IsHashtag() {
204		t.Fail()
205	}
206}
207
208func TestMessageEntityIsBotCommand(t *testing.T) {
209	entity := MessageEntity{Type: "bot_command"}
210
211	if !entity.IsCommand() {
212		t.Fail()
213	}
214}
215
216func TestMessageEntityIsUrl(t *testing.T) {
217	entity := MessageEntity{Type: "url"}
218
219	if !entity.IsURL() {
220		t.Fail()
221	}
222}
223
224func TestMessageEntityIsEmail(t *testing.T) {
225	entity := MessageEntity{Type: "email"}
226
227	if !entity.IsEmail() {
228		t.Fail()
229	}
230}
231
232func TestMessageEntityIsBold(t *testing.T) {
233	entity := MessageEntity{Type: "bold"}
234
235	if !entity.IsBold() {
236		t.Fail()
237	}
238}
239
240func TestMessageEntityIsItalic(t *testing.T) {
241	entity := MessageEntity{Type: "italic"}
242
243	if !entity.IsItalic() {
244		t.Fail()
245	}
246}
247
248func TestMessageEntityIsCode(t *testing.T) {
249	entity := MessageEntity{Type: "code"}
250
251	if !entity.IsCode() {
252		t.Fail()
253	}
254}
255
256func TestMessageEntityIsPre(t *testing.T) {
257	entity := MessageEntity{Type: "pre"}
258
259	if !entity.IsPre() {
260		t.Fail()
261	}
262}
263
264func TestMessageEntityIsTextLink(t *testing.T) {
265	entity := MessageEntity{Type: "text_link"}
266
267	if !entity.IsTextLink() {
268		t.Fail()
269	}
270}
271
272func TestFileLink(t *testing.T) {
273	file := File{FilePath: "test/test.txt"}
274
275	if file.Link("token") != "https://api.telegram.org/file/bottoken/test/test.txt" {
276		t.Fail()
277	}
278}
279
280// Ensure all configs are sendable
281var (
282	_ Chattable = AnimationConfig{}
283	_ Chattable = AnswerWebAppQueryConfig{}
284	_ Chattable = AudioConfig{}
285	_ Chattable = BanChatMemberConfig{}
286	_ Chattable = BanChatSenderChatConfig{}
287	_ Chattable = CallbackConfig{}
288	_ Chattable = ChatActionConfig{}
289	_ Chattable = ChatAdministratorsConfig{}
290	_ Chattable = ChatInfoConfig{}
291	_ Chattable = ChatInviteLinkConfig{}
292	_ Chattable = CloseConfig{}
293	_ Chattable = ContactConfig{}
294	_ Chattable = CopyMessageConfig{}
295	_ Chattable = CreateChatInviteLinkConfig{}
296	_ Chattable = DeleteChatPhotoConfig{}
297	_ Chattable = DeleteChatStickerSetConfig{}
298	_ Chattable = DeleteMessageConfig{}
299	_ Chattable = DeleteMyCommandsConfig{}
300	_ Chattable = DeleteWebhookConfig{}
301	_ Chattable = DocumentConfig{}
302	_ Chattable = EditChatInviteLinkConfig{}
303	_ Chattable = EditMessageCaptionConfig{}
304	_ Chattable = EditMessageLiveLocationConfig{}
305	_ Chattable = EditMessageMediaConfig{}
306	_ Chattable = EditMessageReplyMarkupConfig{}
307	_ Chattable = EditMessageTextConfig{}
308	_ Chattable = FileConfig{}
309	_ Chattable = ForwardConfig{}
310	_ Chattable = GameConfig{}
311	_ Chattable = GetBusinessConnectionConfig{}
312	_ Chattable = GetChatMemberConfig{}
313	_ Chattable = GetChatMenuButtonConfig{}
314	_ Chattable = GetGameHighScoresConfig{}
315	_ Chattable = GetMyDefaultAdministratorRightsConfig{}
316	_ Chattable = InlineConfig{}
317	_ Chattable = InvoiceConfig{}
318	_ Chattable = KickChatMemberConfig{}
319	_ Chattable = LeaveChatConfig{}
320	_ Chattable = LocationConfig{}
321	_ Chattable = LogOutConfig{}
322	_ Chattable = MediaGroupConfig{}
323	_ Chattable = MessageConfig{}
324	_ Chattable = PhotoConfig{}
325	_ Chattable = PinChatMessageConfig{}
326	_ Chattable = PreCheckoutConfig{}
327	_ Chattable = PromoteChatMemberConfig{}
328	_ Chattable = ReplaceStickerInSetConfig{}
329	_ Chattable = RestrictChatMemberConfig{}
330	_ Chattable = RevokeChatInviteLinkConfig{}
331	_ Chattable = SendPollConfig{}
332	_ Chattable = SetChatDescriptionConfig{}
333	_ Chattable = SetChatMenuButtonConfig{}
334	_ Chattable = SetChatPhotoConfig{}
335	_ Chattable = SetChatTitleConfig{}
336	_ Chattable = SetGameScoreConfig{}
337	_ Chattable = SetMyDefaultAdministratorRightsConfig{}
338	_ Chattable = ShippingConfig{}
339	_ Chattable = StickerConfig{}
340	_ Chattable = StopMessageLiveLocationConfig{}
341	_ Chattable = StopPollConfig{}
342	_ Chattable = UnbanChatMemberConfig{}
343	_ Chattable = UnbanChatSenderChatConfig{}
344	_ Chattable = UnpinChatMessageConfig{}
345	_ Chattable = UpdateConfig{}
346	_ Chattable = SetMessageReactionConfig{}
347	_ Chattable = UserProfilePhotosConfig{}
348	_ Chattable = VenueConfig{}
349	_ Chattable = VideoConfig{}
350	_ Chattable = VideoNoteConfig{}
351	_ Chattable = VoiceConfig{}
352	_ Chattable = WebhookConfig{}
353	_ Chattable = CreateForumTopicConfig{}
354	_ Chattable = EditForumTopicConfig{}
355	_ Chattable = CloseForumTopicConfig{}
356	_ Chattable = ReopenForumTopicConfig{}
357	_ Chattable = DeleteForumTopicConfig{}
358	_ Chattable = UnpinAllForumTopicMessagesConfig{}
359	_ Chattable = GetForumTopicIconStickersConfig{}
360	_ Chattable = EditGeneralForumTopicConfig{}
361	_ Chattable = CloseGeneralForumTopicConfig{}
362	_ Chattable = ReopenGeneralForumTopicConfig{}
363	_ Chattable = HideGeneralForumTopicConfig{}
364	_ Chattable = UnhideGeneralForumTopicConfig{}
365	_ Chattable = UnpinAllGeneralForumTopicMessagesConfig{}
366	_ Chattable = SetCustomEmojiStickerSetThumbnailConfig{}
367	_ Chattable = SetStickerSetTitleConfig{}
368	_ Chattable = DeleteStickerSetConfig{}
369	_ Chattable = SetStickerEmojiListConfig{}
370	_ Chattable = SetStickerKeywordsConfig{}
371	_ Chattable = SetStickerMaskPositionConfig{}
372	_ Chattable = GetMyDescriptionConfig{}
373	_ Chattable = SetMyDescriptionConfig{}
374	_ Chattable = GetMyShortDescriptionConfig{}
375	_ Chattable = SetMyShortDescriptionConfig{}
376	_ Chattable = GetMyNameConfig{}
377	_ Chattable = SetMyNameConfig{}
378	_ Chattable = RefundStarPaymentConfig{}
379	_ Chattable = GetStarTransactionsConfig{}
380	_ Chattable = PaidMediaConfig{}
381)
382
383// Ensure all Fileable types are correct.
384var (
385	_ Fileable = (*PhotoConfig)(nil)
386	_ Fileable = (*AudioConfig)(nil)
387	_ Fileable = (*DocumentConfig)(nil)
388	_ Fileable = (*StickerConfig)(nil)
389	_ Fileable = (*VideoConfig)(nil)
390	_ Fileable = (*AnimationConfig)(nil)
391	_ Fileable = (*VideoNoteConfig)(nil)
392	_ Fileable = (*VoiceConfig)(nil)
393	_ Fileable = (*SetChatPhotoConfig)(nil)
394	_ Fileable = (*EditMessageMediaConfig)(nil)
395	_ Fileable = (*SetChatPhotoConfig)(nil)
396	_ Fileable = (*UploadStickerConfig)(nil)
397	_ Fileable = (*NewStickerSetConfig)(nil)
398	_ Fileable = (*AddStickerConfig)(nil)
399	_ Fileable = (*MediaGroupConfig)(nil)
400	_ Fileable = (*WebhookConfig)(nil)
401	_ Fileable = (*SetStickerSetThumbConfig)(nil)
402	_ Fileable = (*PaidMediaConfig)(nil)
403)
404
405// Ensure all RequestFileData types are correct.
406var (
407	_ RequestFileData = (*FilePath)(nil)
408	_ RequestFileData = (*FileBytes)(nil)
409	_ RequestFileData = (*FileReader)(nil)
410	_ RequestFileData = (*FileURL)(nil)
411	_ RequestFileData = (*FileID)(nil)
412	_ RequestFileData = (*fileAttach)(nil)
413)