70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
|
package commands
|
||
|
|
||
|
import (
|
||
|
"regexp"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestParseCommandArguments(t *testing.T) {
|
||
|
args := ParseCommandArguments("normal \"testing quoted\" and normal \"end closed\"")
|
||
|
expected := []string{"normal", "testing quoted", "and", "normal", "end closed"}
|
||
|
|
||
|
for i := 0; i < len(expected); i++ {
|
||
|
if args[i] != expected[i] {
|
||
|
t.Errorf("Expected %s, got %s at index %d", expected[i], args[i], i)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestDispatching(t *testing.T) {
|
||
|
Register("testing", func(ctx *CommandContext) {
|
||
|
// Something
|
||
|
})
|
||
|
|
||
|
if handler := Find("!", "!testing", "testing"); handler == nil {
|
||
|
t.Error("Expected handler for testing")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestRegexpDispatching(t *testing.T) {
|
||
|
RegisterMatch(regexp.MustCompile("^compliment"), func(ctx *CommandContext) {
|
||
|
// Something
|
||
|
})
|
||
|
|
||
|
if handler := Find("", "compliment", "compliment me"); handler == nil {
|
||
|
t.Error("Expected handler for compliment")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestArgumentParsing(t *testing.T) {
|
||
|
holder := parseCommandUsage("test <arg1> <arg2> [arg3]")
|
||
|
|
||
|
if holder.Name != "test" {
|
||
|
t.Error("Unexpected command name")
|
||
|
}
|
||
|
|
||
|
if len(holder.Arguments) != 3 {
|
||
|
t.Error("Invalid number of command arguments:", len(holder.Arguments))
|
||
|
}
|
||
|
|
||
|
if holder.RequiredArgumentCount != 2 {
|
||
|
t.Error("Invalid required argument count")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestArgumentDispatching(t *testing.T) {
|
||
|
holder := parseCommandUsage("test <arg1> <arg2> [arg3]")
|
||
|
|
||
|
if holder.Name != "test" {
|
||
|
t.Error("Unexpected command name")
|
||
|
}
|
||
|
|
||
|
if len(holder.Arguments) != 3 {
|
||
|
t.Error("Invalid number of command arguments:", len(holder.Arguments))
|
||
|
}
|
||
|
|
||
|
if holder.RequiredArgumentCount != 2 {
|
||
|
t.Error("Invalid required argument count")
|
||
|
}
|
||
|
}
|