Merge pull request #109 from mccoyst/pick-a-choose

Get *slightly* serious and fix the rest of the bugs
This commit is contained in:
Chris Sexton 2018-08-30 16:14:35 -04:00 committed by GitHub
commit ce91370313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 6 deletions

View File

@ -30,14 +30,14 @@ 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
}
n, items, err := p.parse(message.Body)
if err != nil {
p.Bot.SendMessage(message.Channel, err.Error())
return false
return true
}
if n == 1 {
@ -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")

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