Get *slightly* serious and fix the rest of the bugs

This commit is contained in:
Steve McCoy 2018-08-30 16:05:03 -04:00
parent fa7ed79ff6
commit aa7a09937c
2 changed files with 45 additions and 5 deletions

View File

@ -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,8 +63,9 @@ 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) {
subs := pickerListPrologue.FindStringSubmatch(body)
@ -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")

View File

@ -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])
}
}