diff --git a/plugins/picker/picker.go b/plugins/picker/picker.go index e3e375e..c49c24f 100644 --- a/plugins/picker/picker.go +++ b/plugins/picker/picker.go @@ -30,7 +30,7 @@ func New(bot bot.Bot) *PickerPlugin { // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. func (p *PickerPlugin) Message(message msg.Message) bool { - if !strings.HasPrefix(body, "pick") { + if !strings.HasPrefix(message.Body, "pick") { return false } @@ -63,10 +63,11 @@ func (p *PickerPlugin) Message(message msg.Message) bool { return true } -var pickerListPrologue = regexp.MustCompile(`^pick[ \t]+([0-9]*)[ \t]+\{[ \t]+`) -var pickerListItem = regexp.MustCompile(`^([^,]+),[ \t]+`) +var pickerListPrologue = regexp.MustCompile(`^pick[ \t]+([0-9]*)[ \t]+\{[ \t]*`) +var pickerListItem = regexp.MustCompile(`^([^,]+),[ \t]*`) +var pickerListFinalItem = regexp.MustCompile(`^([^,}]+),?[ \t]*\}[ \t]*`) -func (p * PickerPlugin) parse(body string) (int, []string, error) { +func (p *PickerPlugin) parse(body string) (int, []string, error) { subs := pickerListPrologue.FindStringSubmatch(body) if subs == nil { return 0, nil, errors.New("saddle up for a syntax error") @@ -93,9 +94,11 @@ func (p * PickerPlugin) parse(body string) (int, []string, error) { rest = rest[len(subs[0]):] } - if strings.TrimSpace(rest) != "}" { + subs = pickerListFinalItem.FindStringSubmatch(rest) + if subs == nil { return 0, nil, errors.New("lasso yerself that final curly brace, compadre") } + items = append(items, subs[1]) if n < 1 || n > len(items) { return 0, nil, errors.New("whoah there, bucko, I can't create something out of nothing") diff --git a/plugins/picker/picker_test.go b/plugins/picker/picker_test.go new file mode 100644 index 0000000..661f5a7 --- /dev/null +++ b/plugins/picker/picker_test.go @@ -0,0 +1,37 @@ +// © 2018 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors. + +package picker + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/bot/user" +) + +func makeMessage(payload string) msg.Message { + isCmd := strings.HasPrefix(payload, "!") + if isCmd { + payload = payload[1:] + } + return msg.Message{ + User: &user.User{Name: "tester"}, + Channel: "test", + Body: payload, + Command: isCmd, + } +} + +func TestReplacement(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + res := c.Message(makeMessage("!pick 2 { a, b,c}")) + assert.Len(t, mb.Messages, 1) + if !res { + t.Fatalf("expected a successful choice, got %q", mb.Messages[0]) + } +}