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