Exemple #1
0
func Test_Matcher(t *testing.T) {
	var tests = []struct {
		src     string
		cmd     string
		matched bool
		args    []*command.Argument
	}{
		{src: "image me ", cmd: "image me", matched: true, args: []*command.Argument{}},
		{src: "imge me", cmd: "image me", matched: false, args: []*command.Argument{}},
		{src: "image ", cmd: "image me", matched: false, args: []*command.Argument{}},
		{src: "image", cmd: "image me", matched: false, args: []*command.Argument{}},
		{src: "imageme", cmd: "image me", matched: false, args: []*command.Argument{}},
		{"send hello to you ", "send <message> to <user>", true,
			[]*command.Argument{{"message", "hello"}, {"user", "you"}},
		},
		{"test  send hello to you ", "send <message> to <user>", true,
			[]*command.Argument{{"message", "hello"}, {"user", "you"}},
		},
		{"send message hello to you ", "send message <message> to <user>", true,
			[]*command.Argument{{"message", "hello"}, {"user", "you"}},
		},
		{"send hello world ", "send <message1> <message2>", true,
			[]*command.Argument{{"message1", "hello"}, {"message2", "world"}},
		},
		{"send \"hello world\" ", "send <message1>", true,
			[]*command.Argument{{"message1", "hello world"}},
		},
	}

	for i, tt := range tests {
		items, err := parse.Parse(tt.cmd)
		if err != nil {
			t.Errorf("%q. err:%v", i, err)
		}
		//t.Log(items[0], items[1])

		m := New(tt.src, items, nil)
		ok, _ := m.Match()
		args := m.Arguments()

		if ok != tt.matched {
			t.Errorf("%v. src='%v',cmd='%v' mismatched: want=%v have=%v", i, tt.src, tt.cmd, tt.matched, ok)
		}

		if len(args) != len(tt.args) {
			t.Errorf("%v. args mismatched: want=%v have=%v cmd=\"%v\" text=\"%v\"", i, tt.args, args, tt.cmd, tt.src)
		}

	}

}
Exemple #2
0
func New(cmd string, cmdFunc CmdFunc, matchWords bool) (*BuiltinCommand, error) {
	c := &BuiltinCommand{
		Cmd:        cmd,
		CmdFunc:    cmdFunc,
		matchWords: matchWords,
		logger:     kitlog.NewContext(log.Logger).With("m", "BuiltinCommand"),
	}

	items, err := parse.Parse(c.Cmd)
	if err != nil {
		return nil, err
	}
	c.items = items
	return c, nil
}
Exemple #3
0
func New(method, url, cmd string, matchWords bool) (*HttpCommand, error) {
	c := &HttpCommand{
		Method:     method,
		Url:        url,
		Cmd:        cmd,
		matchWords: matchWords,
		client:     http.DefaultClient,
		logger:     kitlog.NewContext(log.Logger).With("m", "HttpCommand"),
	}

	items, err := parse.Parse(c.Cmd)
	if err != nil {
		return nil, err
	}
	c.items = items
	return c, nil
}