diff --git a/lib/common.go b/lib/common.go index 79ca850..6b4c02a 100644 --- a/lib/common.go +++ b/lib/common.go @@ -102,6 +102,10 @@ func ContainsCommand(s, prefix, cmd string) bool { func SplitCommandAndArgs(s, prefix string) (cmd string, args []string) { s = strings.TrimSpace(s) + if !strings.HasPrefix(s, prefix) { + return + } + x := strings.Split(s, " ") if len(x) > 1 { diff --git a/lib/common_test.go b/lib/common_test.go index 9ebede4..2b21f55 100644 --- a/lib/common_test.go +++ b/lib/common_test.go @@ -53,3 +53,26 @@ func TestHasCommandCommand(t *testing.T) { } } } + +func TestSplitComandAndArgs(t *testing.T) { + tables := []struct { + s string + prefix string + wantCmd string + wantArgs []string + }{ + {"!command x y", "!", "command", []string{"x", "y"}}, + {"!command", "!", "command", []string(nil)}, + {"hey man", "!", "", []string(nil)}, + } + + for _, table := range tables { + gotCmd, gotArgs := SplitCommandAndArgs(table.s, table.prefix) + if gotCmd != table.wantCmd { + t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd) + } + if !reflect.DeepEqual(gotArgs, table.wantArgs) { + t.Errorf("got: %+v, want: %+v", gotArgs, table.wantArgs) + } + } +}