bb/lib/common_test.go

160 lines
3.6 KiB
Go
Raw Permalink Normal View History

2022-09-04 13:50:43 +00:00
package lib
2022-09-05 20:14:42 +00:00
import (
"reflect"
"testing"
)
2022-09-04 13:50:43 +00:00
2022-09-05 17:54:56 +00:00
func TestContainsCommand(t *testing.T) {
2022-09-04 13:50:43 +00:00
tables := []struct {
s string
prefix string
cmd string
r bool
}{
{"!command x y", "!", "command", true},
{"#command x y", "#", "command", true},
{"command x y", "!", "comamnd", false},
{"", "", "", false},
{"!", "!", "", false},
{"! x y", "!", "", false},
}
for _, table := range tables {
2022-09-05 17:54:56 +00:00
r := ContainsCommand(table.s, table.prefix, table.cmd)
2022-09-04 13:50:43 +00:00
if r != table.r {
2022-09-05 17:54:56 +00:00
t.Errorf("ContainsCommand(%q, %q, %q), got: %t, want: %t",
2022-09-04 13:50:43 +00:00
table.s, table.prefix, table.cmd, r, table.r,
)
}
}
}
2022-09-05 18:03:02 +00:00
func TestHasCommandCommand(t *testing.T) {
tables := []struct {
s string
prefix string
want bool
}{
{"!command", "!", true},
{"!command x y", "!", true},
{"!c x y", "!", true},
{"! x y", "!", false},
{"hey guy", "!", false},
{"hey", "!", false},
{"hey", "", false},
{"", "!", false},
{"", "", false},
}
for _, table := range tables {
if got, want := HasCommand(table.s, table.prefix), table.want; got != want {
t.Errorf(
"s: %s, prefix: %s, got: %t, want: %t",
table.s, table.prefix, got, want,
)
}
}
}
2022-09-05 18:03:47 +00:00
func TestSplitCommandAndArg(t *testing.T) {
tables := []struct {
s string
prefix string
wantCmd string
wantArg string
}{
{"!command x y", "!", "command", "x y"},
{"!command", "!", "command", ""},
{"hey man", "!", "", ""},
}
for _, table := range tables {
gotCmd, gotArg := SplitCommandAndArg(table.s, table.prefix)
if gotCmd != table.wantCmd {
t.Errorf("got: %s, want: %s", gotCmd, table.wantCmd)
}
if gotArg != table.wantArg {
t.Errorf("got: %+v, want: %+v", gotArg, table.wantArg)
}
}
}
func TestSplitCommandAndArgs(t *testing.T) {
2022-09-05 18:03:47 +00:00
tables := []struct {
s string
prefix string
n int
2022-09-05 18:03:47 +00:00
wantCmd string
wantArgs []string
}{
{"!command x y", "!", 2, "command", []string{"x", "y"}},
{"!command x y z", "!", 2, "command", []string{"x", "y z"}},
{"!command", "!", 1, "command", []string{}},
{"hey man", "!", 1, "", []string{}},
2022-09-05 18:03:47 +00:00
}
for _, table := range tables {
gotCmd, gotArgs := SplitCommandAndArgs(table.s, table.prefix, table.n)
2022-09-05 18:03:47 +00:00
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)
2022-09-05 18:03:47 +00:00
}
}
}
func TestSplitArgs(t *testing.T) {
tables := []struct {
s string
n int
want []string
}{
{"a b c", 0, []string{"a", "b", "c"}},
{"a b c", 1, []string{"a b c"}},
{"a b c", 2, []string{"a", "b c"}},
{"a b c", 3, []string{"a", "b", "c"}},
{"a b c", 4, []string{"a", "b", "c"}},
{"", 0, []string{}},
}
for _, table := range tables {
if got, want := SplitArgs(table.s, table.n), table.want; !reflect.DeepEqual(got, want) {
t.Errorf("got: %#v, want: %#v", got, want)
}
}
}
2022-09-14 13:34:51 +00:00
func TestMapKeys(t *testing.T) {
tables := []struct {
m map[string]int
want []string
}{
{map[string]int{"a": 0, "b": 1, "c": 3}, []string{"a", "b", "c"}},
}
for _, table := range tables {
if got, want := MapKeys(table.m), table.want; !reflect.DeepEqual(got, want) {
t.Errorf("got: %#v, want: %#v", got, want)
}
}
}
func TestMapKey(t *testing.T) {
tables := []struct {
m map[string]int
n int
want string
}{
{map[string]int{"a": 0, "b": 1, "c": 2}, 0, "a"},
{map[string]int{"a": 0, "b": 1, "c": 2}, 1, "b"},
{map[string]int{"a": 0, "b": 1, "c": 2}, 2, "c"},
{map[string]int{"a": 0, "b": 1, "c": 2, "d": 0}, 0, "a"},
}
for _, table := range tables {
if got, want := MapKey(table.m, table.n), table.want; got != want {
t.Errorf("got: %#v, want: %#v", got, want)
}
}
}