From 1cede9cf458b162a92832af661b53b9b2f1ed221 Mon Sep 17 00:00:00 2001 From: Gleb Sinyavsky Date: Sun, 13 Dec 2015 20:31:59 +0300 Subject: [PATCH] Some helper methods added to Message. Some tests added. Small fixes --- configs.go | 4 +-- types.go | 20 +++++++++++++-- types_test.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 4 deletions(-) diff --git a/configs.go b/configs.go index 0b390f8..00c2830 100644 --- a/configs.go +++ b/configs.go @@ -93,8 +93,8 @@ type BaseFile struct { File interface{} FileID string UseExisting bool - MimeType string - FileSize int + MimeType string + FileSize int } // Params returns map[string]string representation of BaseFile diff --git a/types.go b/types.go index c0d828c..afa1e18 100644 --- a/types.go +++ b/types.go @@ -127,9 +127,25 @@ func (m *Message) IsCommand() bool { return m.Text != "" && m.Text[0] == '/' } -// Command returns first word from message +// Command if message is command returns first word from message(entire command) +// otherwise returns empty string func (m *Message) Command() string { - return strings.Split(m.Text, " ")[0] + if m.IsCommand() { + return strings.SplitN(m.Text, " ", 2)[0] + } + return "" +} + +// CommandArguments if message is command, returns all text after command, excluding the command itself +// otherwise returns empty string +func (m *Message) CommandArguments() string { + if m.IsCommand() { + split := strings.SplitN(m.Text, " ", 2) + if len(split) == 2 { + return strings.SplitN(m.Text, " ", 2)[1] + } + } + return "" } // PhotoSize contains information about photos, including ID and Width and Height. diff --git a/types_test.go b/types_test.go index 055877f..9e5610c 100644 --- a/types_test.go +++ b/types_test.go @@ -31,6 +31,75 @@ func TestMessageTime(t *testing.T) { } } +func TestMessageIsCommandWithCommand(t *testing.T) { + message := tgbotapi.Message{Text: "/command"} + + if message.IsCommand() != true { + t.Fail() + } +} + +func TestIsCommandWithText(t *testing.T) { + message := tgbotapi.Message{Text: "some text"} + + if message.IsCommand() != false { + t.Fail() + } +} + +func TestIsCommandWithEmptyText(t *testing.T) { + message := tgbotapi.Message{Text: ""} + + if message.IsCommand() != false { + t.Fail() + } +} + +func TestCommandWithCommand(t *testing.T) { + message := tgbotapi.Message{Text: "/command"} + + if message.Command() != "/command" { + t.Fail() + } +} + +func TestCommandWithEmptyText(t *testing.T) { + message := tgbotapi.Message{Text: ""} + + if message.Command() != "" { + t.Fail() + } +} + +func TestCommandWithNonCommand(t *testing.T) { + message := tgbotapi.Message{Text: "test text"} + + if message.Command() != "" { + t.Fail() + } +} + +func TestMessageCommandArgumentsWithArguments(t *testing.T) { + message := tgbotapi.Message{Text: "/command with arguments"} + if message.CommandArguments() != "with arguments" { + t.Fail() + } +} + +func TestMessageCommandArgumentsWithoutArguments(t *testing.T) { + message := tgbotapi.Message{Text: "/command"} + if message.CommandArguments() != "" { + t.Fail() + } +} + +func TestMessageCommandArgumentsForNonCommand(t *testing.T) { + message := tgbotapi.Message{Text: "test text"} + if message.CommandArguments() != "" { + t.Fail() + } +} + func TestChatIsPrivate(t *testing.T) { chat := tgbotapi.Chat{ID: 10, Type: "private"}