Add test for SplitCommandAndArgs
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Ryan Cavicchioni 2022-09-05 13:03:47 -05:00
parent 043738668b
commit d3b95c693b
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
2 changed files with 27 additions and 0 deletions

View File

@ -102,6 +102,10 @@ func ContainsCommand(s, prefix, cmd string) bool {
func SplitCommandAndArgs(s, prefix string) (cmd string, args []string) { func SplitCommandAndArgs(s, prefix string) (cmd string, args []string) {
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
if !strings.HasPrefix(s, prefix) {
return
}
x := strings.Split(s, " ") x := strings.Split(s, " ")
if len(x) > 1 { if len(x) > 1 {

View File

@ -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)
}
}
}