From d3b95c693b4a4133a20a3d4fef8453eaf86b0dc9 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Mon, 5 Sep 2022 13:03:47 -0500 Subject: [PATCH] Add test for SplitCommandAndArgs --- lib/common.go | 4 ++++ lib/common_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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) + } + } +}