提交 8f5190cb 编写于 作者: D Derek Parker

Add ability to register commands

上级 84f80fd1
...@@ -19,6 +19,10 @@ func DebugCommands() *Commands { ...@@ -19,6 +19,10 @@ func DebugCommands() *Commands {
return &Commands{cmds} return &Commands{cmds}
} }
func (c *Commands) Register(cmdstr string, cf cmdfunc) {
c.cmds[cmdstr] = cf
}
func (c *Commands) Find(cmdstr string) cmdfunc { func (c *Commands) Find(cmdstr string) cmdfunc {
cmd, ok := c.cmds[cmdstr] cmd, ok := c.cmds[cmdstr]
if !ok { if !ok {
......
package command package command
import "testing" import (
"fmt"
"testing"
)
func TestCommandDefault(t *testing.T) { func TestCommandDefault(t *testing.T) {
var ( var (
...@@ -17,3 +20,20 @@ func TestCommandDefault(t *testing.T) { ...@@ -17,3 +20,20 @@ func TestCommandDefault(t *testing.T) {
t.Fatal("wrong command output") t.Fatal("wrong command output")
} }
} }
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")
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册