2014-05-20 21:28:24 +00:00
|
|
|
package command
|
|
|
|
|
2014-05-20 23:09:34 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
2014-05-20 21:28:24 +00:00
|
|
|
|
|
|
|
func TestCommandDefault(t *testing.T) {
|
|
|
|
var (
|
|
|
|
cmds = Commands{make(map[string]cmdfunc)}
|
|
|
|
cmd = cmds.Find("non-existant-command")
|
|
|
|
)
|
|
|
|
|
|
|
|
err := cmd()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("cmd() did not default")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() != "command not available" {
|
|
|
|
t.Fatal("wrong command output")
|
|
|
|
}
|
|
|
|
}
|
2014-05-20 23:09:34 +00:00
|
|
|
|
|
|
|
func TestCommandRegister(t *testing.T) {
|
|
|
|
cmds := Commands{make(map[string]cmdfunc)}
|
|
|
|
|
|
|
|
cmds.Register("foo", func() error { return fmt.Errorf("registered command") })
|
|
|
|
|
|
|
|
cmd := cmds.Find("foo")
|
|
|
|
|
|
|
|
err := cmd()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("cmd was not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() != "registered command" {
|
|
|
|
t.Fatal("wrong command output")
|
|
|
|
}
|
|
|
|
}
|
2014-05-21 15:15:58 +00:00
|
|
|
|
|
|
|
func TestCommandReplay(t *testing.T) {
|
|
|
|
cmds := Commands{make(map[string]cmdfunc)}
|
|
|
|
cmds.Register("foo", func() error { return fmt.Errorf("registered command") })
|
|
|
|
cmd := cmds.Find("foo")
|
|
|
|
|
|
|
|
err := cmd()
|
|
|
|
if err.Error() != "registered command" {
|
|
|
|
t.Fatal("wrong command output")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = cmds.Find("")
|
|
|
|
err = cmd()
|
|
|
|
if err.Error() != "registered command" {
|
|
|
|
t.Fatal("wrong command output")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommandReplayWithoutPreviousCommand(t *testing.T) {
|
|
|
|
var (
|
|
|
|
cmds = Commands{make(map[string]cmdfunc)}
|
|
|
|
cmd = cmds.Find("")
|
|
|
|
err = cmd()
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Null command not returned", err)
|
|
|
|
}
|
|
|
|
}
|