mirror of https://github.com/velour/catbase.git
Get *slightly* serious and fix the rest of the bugs
This commit is contained in:
parent
fa7ed79ff6
commit
aa7a09937c
|
@ -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.
|
// 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.
|
// Otherwise, the function returns false and the bot continues execution of other plugins.
|
||||||
func (p *PickerPlugin) Message(message msg.Message) bool {
|
func (p *PickerPlugin) Message(message msg.Message) bool {
|
||||||
if !strings.HasPrefix(body, "pick") {
|
if !strings.HasPrefix(message.Body, "pick") {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,10 +63,11 @@ func (p *PickerPlugin) Message(message msg.Message) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
var pickerListPrologue = regexp.MustCompile(`^pick[ \t]+([0-9]*)[ \t]+\{[ \t]+`)
|
var pickerListPrologue = regexp.MustCompile(`^pick[ \t]+([0-9]*)[ \t]+\{[ \t]*`)
|
||||||
var pickerListItem = regexp.MustCompile(`^([^,]+),[ \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)
|
subs := pickerListPrologue.FindStringSubmatch(body)
|
||||||
if subs == nil {
|
if subs == nil {
|
||||||
return 0, nil, errors.New("saddle up for a syntax error")
|
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]):]
|
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")
|
return 0, nil, errors.New("lasso yerself that final curly brace, compadre")
|
||||||
}
|
}
|
||||||
|
items = append(items, subs[1])
|
||||||
|
|
||||||
if n < 1 || n > len(items) {
|
if n < 1 || n > len(items) {
|
||||||
return 0, nil, errors.New("whoah there, bucko, I can't create something out of nothing")
|
return 0, nil, errors.New("whoah there, bucko, I can't create something out of nothing")
|
||||||
|
|
|
@ -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])
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue