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 TestMessageEntityIsMention(t *testing.T) {
195 entity := tgbotapi.MessageEntity{Type: "mention"}
196
197 if !entity.IsMention() {
198 t.Fail()
199 }
200}
201
202func TestMessageEntityIsHashtag(t *testing.T) {
203 entity := tgbotapi.MessageEntity{Type: "hashtag"}
204
205 if !entity.IsHashtag() {
206 t.Fail()
207 }
208}
209
210func TestMessageEntityIsBotCommand(t *testing.T) {
211 entity := tgbotapi.MessageEntity{Type: "bot_command"}
212
213 if !entity.IsCommand() {
214 t.Fail()
215 }
216}
217
218func TestMessageEntityIsUrl(t *testing.T) {
219 entity := tgbotapi.MessageEntity{Type: "url"}
220
221 if !entity.IsUrl() {
222 t.Fail()
223 }
224}
225
226func TestMessageEntityIsEmail(t *testing.T) {
227 entity := tgbotapi.MessageEntity{Type: "email"}
228
229 if !entity.IsEmail() {
230 t.Fail()
231 }
232}
233
234func TestMessageEntityIsBold(t *testing.T) {
235 entity := tgbotapi.MessageEntity{Type: "bold"}
236
237 if !entity.IsBold() {
238 t.Fail()
239 }
240}
241
242func TestMessageEntityIsItalic(t *testing.T) {
243 entity := tgbotapi.MessageEntity{Type: "italic"}
244
245 if !entity.IsItalic() {
246 t.Fail()
247 }
248}
249
250func TestMessageEntityIsCode(t *testing.T) {
251 entity := tgbotapi.MessageEntity{Type: "code"}
252
253 if !entity.IsCode() {
254 t.Fail()
255 }
256}
257
258func TestMessageEntityIsPre(t *testing.T) {
259 entity := tgbotapi.MessageEntity{Type: "pre"}
260
261 if !entity.IsPre() {
262 t.Fail()
263 }
264}
265
266func TestMessageEntityIsTextLink(t *testing.T) {
267 entity := tgbotapi.MessageEntity{Type: "text_link"}
268
269 if !entity.IsTextLink() {
270 t.Fail()
271 }
272}
273
274func TestFileLink(t *testing.T) {
275 file := tgbotapi.File{FilePath: "test/test.txt"}
276
277 if file.Link("token") != "https://api.telegram.org/file/bottoken/test/test.txt" {
278 t.Fail()
279 }
280}