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"}
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 TestMessageCommandArgumentsWithArguments(t *testing.T) {
95 message := tgbotapi.Message{Text: "/command with arguments"}
96 message.Entities = &[]tgbotapi.MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
97 if message.CommandArguments() != "with arguments" {
98 t.Fail()
99 }
100}
101
102func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
103 message := tgbotapi.Message{Text: "/command"}
104 if message.CommandArguments() != "" {
105 t.Fail()
106 }
107}
108
109func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
110 message := tgbotapi.Message{Text: "test text"}
111 if message.CommandArguments() != "" {
112 t.Fail()
113 }
114}
115
116func TestMessageEntityParseURLGood(t *testing.T) {
117 entity := tgbotapi.MessageEntity{URL: "https://www.google.com"}
118
119 if _, err := entity.ParseURL(); err != nil {
120 t.Fail()
121 }
122}
123
124func TestMessageEntityParseURLBad(t *testing.T) {
125 entity := tgbotapi.MessageEntity{URL: ""}
126
127 if _, err := entity.ParseURL(); err == nil {
128 t.Fail()
129 }
130}
131
132func TestChatIsPrivate(t *testing.T) {
133 chat := tgbotapi.Chat{ID: 10, Type: "private"}
134
135 if chat.IsPrivate() != true {
136 t.Fail()
137 }
138}
139
140func TestChatIsGroup(t *testing.T) {
141 chat := tgbotapi.Chat{ID: 10, Type: "group"}
142
143 if chat.IsGroup() != true {
144 t.Fail()
145 }
146}
147
148func TestChatIsChannel(t *testing.T) {
149 chat := tgbotapi.Chat{ID: 10, Type: "channel"}
150
151 if chat.IsChannel() != true {
152 t.Fail()
153 }
154}
155
156func TestChatIsSuperGroup(t *testing.T) {
157 chat := tgbotapi.Chat{ID: 10, Type: "supergroup"}
158
159 if !chat.IsSuperGroup() {
160 t.Fail()
161 }
162}
163
164func TestFileLink(t *testing.T) {
165 file := tgbotapi.File{FilePath: "test/test.txt"}
166
167 if file.Link("token") != "https://api.telegram.org/file/bottoken/test/test.txt" {
168 t.Fail()
169 }
170}