mirror of https://github.com/velour/catbase.git
pick: fix broken pick
* simplified picking regex * added test case for previously broken message
This commit is contained in:
parent
905da629b9
commit
3ad86f2308
|
@ -68,11 +68,11 @@ func (p *PickerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messa
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
var pickerListPrologue = regexp.MustCompile(`^pick([^ \t]*)[ \t]+([0-9]*)[ \t]*\{[ \t]*`)
|
var pickRegex = regexp.MustCompile(`^pick([^\s]+)?(\s\d)?\s+{(.*)}$`)
|
||||||
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 := pickRegex.FindStringSubmatch(body)
|
||||||
|
log.Debug().Str("body", body).Strs("subs", subs).Msg("parse")
|
||||||
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")
|
||||||
}
|
}
|
||||||
|
@ -82,39 +82,21 @@ func (p *PickerPlugin) parse(body string) (int, []string, error) {
|
||||||
Interface("subs", subs).
|
Interface("subs", subs).
|
||||||
Msg("subs")
|
Msg("subs")
|
||||||
|
|
||||||
n := 1
|
|
||||||
delim := ","
|
delim := ","
|
||||||
var err error
|
|
||||||
|
|
||||||
if subs[1] != "" {
|
if subs[1] != "" {
|
||||||
delim = subs[1]
|
delim = subs[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
if subs[2] != "" {
|
n := 1
|
||||||
n, err = strconv.Atoi(subs[2])
|
var err error
|
||||||
|
if num := strings.TrimSpace(subs[2]); num != "" {
|
||||||
|
n, err = strconv.Atoi(num)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pickerListItem := regexp.MustCompile(`^([^` + delim + `]+)` + delim + `[ \t]*`)
|
|
||||||
|
|
||||||
var items []string
|
items := strings.Split(subs[3], delim)
|
||||||
rest := body[len(subs[0]):]
|
|
||||||
for {
|
|
||||||
subs = pickerListItem.FindStringSubmatch(rest)
|
|
||||||
if subs == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
items = append(items, subs[1])
|
|
||||||
rest = rest[len(subs[0]):]
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
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")
|
||||||
|
|
|
@ -88,3 +88,13 @@ func TestPickDelimiterString(t *testing.T) {
|
||||||
assert.NotContains(t, mb.Messages[0], "hot picks")
|
assert.NotContains(t, mb.Messages[0], "hot picks")
|
||||||
log.Debug().Str("resp", mb.Messages[0]).Msg("choose")
|
log.Debug().Str("resp", mb.Messages[0]).Msg("choose")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestKnownBrokenPick(t *testing.T) {
|
||||||
|
mb := bot.NewMockBot()
|
||||||
|
c := New(mb)
|
||||||
|
_ = c.message(makeMessage("!pick⌘ { bagel/egg/smoked turkey/butte/cheese ⌘ fuck all that, just have a bagel and cream cheese }"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
|
assert.Contains(t, mb.Messages[0], "I've chosen")
|
||||||
|
assert.NotContains(t, mb.Messages[0], "hot picks")
|
||||||
|
log.Debug().Str("resp", mb.Messages[0]).Msg("choose")
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue