mirror of https://github.com/velour/catbase.git
Merge pull request #209 from velour/pick_delimiter
pick: add delimiter support
This commit is contained in:
commit
bdcafed1f8
|
@ -8,6 +8,8 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
|
@ -66,8 +68,7 @@ func (p *PickerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messa
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
var pickerListPrologue = regexp.MustCompile(`^pick[ \t]+([0-9]*)[ \t]*\{[ \t]*`)
|
var pickerListPrologue = regexp.MustCompile(`^pick([^ \t]*)[ \t]+([0-9]*)[ \t]*\{[ \t]*`)
|
||||||
var pickerListItem = regexp.MustCompile(`^([^,]+),[ \t]*`)
|
|
||||||
var pickerListFinalItem = regexp.MustCompile(`^([^,}]+),?[ \t]*\}[ \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) {
|
||||||
|
@ -76,14 +77,26 @@ func (p *PickerPlugin) parse(body string) (int, []string, error) {
|
||||||
return 0, nil, errors.New("saddle up for a syntax error")
|
return 0, nil, errors.New("saddle up for a syntax error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Debug().
|
||||||
|
Str("body", body).
|
||||||
|
Interface("subs", subs).
|
||||||
|
Msg("subs")
|
||||||
|
|
||||||
n := 1
|
n := 1
|
||||||
|
delim := ","
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if subs[1] != "" {
|
if subs[1] != "" {
|
||||||
n, err = strconv.Atoi(subs[1])
|
delim = subs[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
if subs[2] != "" {
|
||||||
|
n, err = strconv.Atoi(subs[2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pickerListItem := regexp.MustCompile(`^([^` + delim + `]+)` + delim + `[ \t]*`)
|
||||||
|
|
||||||
var items []string
|
var items []string
|
||||||
rest := body[len(subs[0]):]
|
rest := body[len(subs[0]):]
|
||||||
|
|
|
@ -3,10 +3,13 @@
|
||||||
package picker
|
package picker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/velour/catbase/plugins/cli"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/velour/catbase/plugins/cli"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/velour/catbase/bot"
|
"github.com/velour/catbase/bot"
|
||||||
"github.com/velour/catbase/bot/msg"
|
"github.com/velour/catbase/bot/msg"
|
||||||
|
@ -32,6 +35,7 @@ func TestPick2(t *testing.T) {
|
||||||
assert.NotNil(t, c)
|
assert.NotNil(t, c)
|
||||||
res := c.message(makeMessage("!pick 2 { a, b,c}"))
|
res := c.message(makeMessage("!pick 2 { a, b,c}"))
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
|
assert.Contains(t, mb.Messages[0], "hot picks")
|
||||||
if !res {
|
if !res {
|
||||||
t.Fatalf("expected a successful choice, got %q", mb.Messages[0])
|
t.Fatalf("expected a successful choice, got %q", mb.Messages[0])
|
||||||
}
|
}
|
||||||
|
@ -45,3 +49,42 @@ func TestPickDefault(t *testing.T) {
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.Equal(t, `I've chosen "a" for you.`, mb.Messages[0])
|
assert.Equal(t, `I've chosen "a" for you.`, mb.Messages[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPickDefaultWithSeprator(t *testing.T) {
|
||||||
|
mb := bot.NewMockBot()
|
||||||
|
c := New(mb)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
_ = c.message(makeMessage("!pick { a, b, c}"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
|
assert.Contains(t, mb.Messages[0], "I've chosen")
|
||||||
|
assert.NotContains(t, mb.Messages[0], "hot picks")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPickDelimiter(t *testing.T) {
|
||||||
|
mb := bot.NewMockBot()
|
||||||
|
c := New(mb)
|
||||||
|
_ = c.message(makeMessage("!pick; {a; b}"))
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPickDelimiterMulti(t *testing.T) {
|
||||||
|
mb := bot.NewMockBot()
|
||||||
|
c := New(mb)
|
||||||
|
_ = c.message(makeMessage("!pick; 2 {a; b}"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
|
assert.Contains(t, mb.Messages[0], "hot picks")
|
||||||
|
log.Debug().Str("resp", mb.Messages[0]).Msg("choose")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPickDelimiterString(t *testing.T) {
|
||||||
|
mb := bot.NewMockBot()
|
||||||
|
c := New(mb)
|
||||||
|
_ = c.message(makeMessage("!pick123 {a 123 b}"))
|
||||||
|
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