Skip to content

Commit 1cede9c

Browse files
committed
Some helper methods added to Message. Some tests added. Small fixes
1 parent eb6b784 commit 1cede9c

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

configs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ type BaseFile struct {
9393
File interface{}
9494
FileID string
9595
UseExisting bool
96-
MimeType string
97-
FileSize int
96+
MimeType string
97+
FileSize int
9898
}
9999

100100
// Params returns map[string]string representation of BaseFile

types.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,25 @@ func (m *Message) IsCommand() bool {
127127
return m.Text != "" && m.Text[0] == '/'
128128
}
129129

130-
// Command returns first word from message
130+
// Command if message is command returns first word from message(entire command)
131+
// otherwise returns empty string
131132
func (m *Message) Command() string {
132-
return strings.Split(m.Text, " ")[0]
133+
if m.IsCommand() {
134+
return strings.SplitN(m.Text, " ", 2)[0]
135+
}
136+
return ""
137+
}
138+
139+
// CommandArguments if message is command, returns all text after command, excluding the command itself
140+
// otherwise returns empty string
141+
func (m *Message) CommandArguments() string {
142+
if m.IsCommand() {
143+
split := strings.SplitN(m.Text, " ", 2)
144+
if len(split) == 2 {
145+
return strings.SplitN(m.Text, " ", 2)[1]
146+
}
147+
}
148+
return ""
133149
}
134150

135151
// PhotoSize contains information about photos, including ID and Width and Height.

types_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,75 @@ func TestMessageTime(t *testing.T) {
3131
}
3232
}
3333

34+
func TestMessageIsCommandWithCommand(t *testing.T) {
35+
message := tgbotapi.Message{Text: "/command"}
36+
37+
if message.IsCommand() != true {
38+
t.Fail()
39+
}
40+
}
41+
42+
func TestIsCommandWithText(t *testing.T) {
43+
message := tgbotapi.Message{Text: "some text"}
44+
45+
if message.IsCommand() != false {
46+
t.Fail()
47+
}
48+
}
49+
50+
func TestIsCommandWithEmptyText(t *testing.T) {
51+
message := tgbotapi.Message{Text: ""}
52+
53+
if message.IsCommand() != false {
54+
t.Fail()
55+
}
56+
}
57+
58+
func TestCommandWithCommand(t *testing.T) {
59+
message := tgbotapi.Message{Text: "/command"}
60+
61+
if message.Command() != "/command" {
62+
t.Fail()
63+
}
64+
}
65+
66+
func TestCommandWithEmptyText(t *testing.T) {
67+
message := tgbotapi.Message{Text: ""}
68+
69+
if message.Command() != "" {
70+
t.Fail()
71+
}
72+
}
73+
74+
func TestCommandWithNonCommand(t *testing.T) {
75+
message := tgbotapi.Message{Text: "test text"}
76+
77+
if message.Command() != "" {
78+
t.Fail()
79+
}
80+
}
81+
82+
func TestMessageCommandArgumentsWithArguments(t *testing.T) {
83+
message := tgbotapi.Message{Text: "/command with arguments"}
84+
if message.CommandArguments() != "with arguments" {
85+
t.Fail()
86+
}
87+
}
88+
89+
func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
90+
message := tgbotapi.Message{Text: "/command"}
91+
if message.CommandArguments() != "" {
92+
t.Fail()
93+
}
94+
}
95+
96+
func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
97+
message := tgbotapi.Message{Text: "test text"}
98+
if message.CommandArguments() != "" {
99+
t.Fail()
100+
}
101+
}
102+
34103
func TestChatIsPrivate(t *testing.T) {
35104
chat := tgbotapi.Chat{ID: 10, Type: "private"}
36105

0 commit comments

Comments
 (0)