leftpad: refactor

This commit is contained in:
Chris Sexton 2021-02-07 14:20:54 -05:00 committed by Chris Sexton
parent 353f289cae
commit 0891713523
2 changed files with 56 additions and 59 deletions

View File

@ -5,12 +5,11 @@ package leftpad
import ( import (
"fmt" "fmt"
"regexp"
"strconv" "strconv"
"strings"
"github.com/chrissexton/leftpad" "github.com/chrissexton/leftpad"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/config" "github.com/velour/catbase/config"
) )
@ -25,7 +24,7 @@ func New(b bot.Bot) *LeftpadPlugin {
bot: b, bot: b,
config: b.Config(), config: b.Config(),
} }
b.Register(p, bot.Message, p.message) b.RegisterRegexCmd(p, bot.Message, leftpadRegex, p.leftpadCmd)
return p return p
} }
@ -33,32 +32,25 @@ type leftpadResp struct {
Str string Str string
} }
func (p *LeftpadPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { var leftpadRegex = regexp.MustCompile(`(?i)^leftpad (?P<padstr>\S+) (?P<padding>\d+) (?P<text>.+)$`)
if !message.Command {
return false
}
parts := strings.Fields(message.Body) func (p *LeftpadPlugin) leftpadCmd(r bot.Request) bool {
if len(parts) > 3 && parts[0] == "leftpad" { padchar := r.Values["padstr"]
padchar := parts[1] length, err := strconv.Atoi(r.Values["padding"])
length, err := strconv.Atoi(parts[2]) if err != nil {
if err != nil { p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Invalid padding number")
p.bot.Send(c, bot.Message, message.Channel, "Invalid padding number")
return true
}
maxLen, who := p.config.GetInt("LeftPad.MaxLen", 50), p.config.Get("LeftPad.Who", "Putin")
if length > maxLen && maxLen > 0 {
msg := fmt.Sprintf("%s would kill me if I did that.", who)
p.bot.Send(c, bot.Message, message.Channel, msg)
return true
}
text := strings.Join(parts[3:], " ")
res := leftpad.LeftPad(text, length, padchar)
p.bot.Send(c, bot.Message, message.Channel, res)
return true return true
} }
maxLen, who := p.config.GetInt("LeftPad.MaxLen", 50), p.config.Get("LeftPad.Who", "Putin")
if length > maxLen && maxLen > 0 {
msg := fmt.Sprintf("%s would kill me if I did that.", who)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
return true
}
text := r.Values["text"]
return false res := leftpad.LeftPad(text, length, padchar)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, res)
return true
} }

View File

@ -3,10 +3,10 @@
package leftpad package leftpad
import ( import (
"github.com/velour/catbase/plugins/cli"
"strings"
"testing" "testing"
"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"
@ -14,16 +14,24 @@ import (
"github.com/velour/catbase/plugins/counter" "github.com/velour/catbase/plugins/counter"
) )
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) { func makeMessage(payload string) bot.Request {
isCmd := strings.HasPrefix(payload, "!") values := bot.ParseValues(leftpadRegex, payload)
if isCmd { return bot.Request{
payload = payload[1:] Kind: bot.Message,
Conn: &cli.CliPlugin{},
Values: values,
Msg: msg.Message{
User: &user.User{Name: "tester"},
Channel: "test",
Body: payload,
},
} }
return &cli.CliPlugin{}, bot.Message, msg.Message{
User: &user.User{Name: "tester"}, }
Channel: "test",
Body: payload, func testMessage(p *LeftpadPlugin, body string) {
Command: isCmd, if leftpadRegex.MatchString(body) {
p.leftpadCmd(makeMessage(body))
} }
} }
@ -38,51 +46,48 @@ func makePlugin(t *testing.T) (*LeftpadPlugin, *bot.MockBot) {
func TestLeftpad(t *testing.T) { func TestLeftpad(t *testing.T) {
p, mb := makePlugin(t) p, mb := makePlugin(t)
p.message(makeMessage("!leftpad test 8 test")) testMessage(p, "leftpad test 8 test")
assert.Contains(t, mb.Messages[0], "testtest") if assert.Len(t, mb.Messages, 1) {
assert.Len(t, mb.Messages, 1) assert.Contains(t, mb.Messages[0], "testtest")
} }
func TestBadNumber(t *testing.T) {
p, mb := makePlugin(t)
p.message(makeMessage("!leftpad test fuck test"))
assert.Contains(t, mb.Messages[0], "Invalid")
assert.Len(t, mb.Messages, 1)
} }
func TestNotCommand(t *testing.T) { func TestNotCommand(t *testing.T) {
p, mb := makePlugin(t) p, mb := makePlugin(t)
p.message(makeMessage("leftpad test fuck test")) testMessage(p, "leftpad test fuck test")
assert.Len(t, mb.Messages, 0) assert.Len(t, mb.Messages, 0)
} }
func TestNoMaxLen(t *testing.T) { func TestNoMaxLen(t *testing.T) {
p, mb := makePlugin(t) p, mb := makePlugin(t)
p.config.Set("LeftPad.MaxLen", "0") p.config.Set("LeftPad.MaxLen", "0")
p.message(makeMessage("!leftpad dicks 100 dicks")) testMessage(p, "leftpad dicks 100 dicks")
assert.Len(t, mb.Messages, 1) if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "dicks") assert.Contains(t, mb.Messages[0], "dicks")
}
} }
func Test50Padding(t *testing.T) { func Test50Padding(t *testing.T) {
p, mb := makePlugin(t) p, mb := makePlugin(t)
p.config.Set("LeftPad.MaxLen", "50") p.config.Set("LeftPad.MaxLen", "50")
assert.Equal(t, 50, p.config.GetInt("LeftPad.MaxLen", 100)) assert.Equal(t, 50, p.config.GetInt("LeftPad.MaxLen", 100))
p.message(makeMessage("!leftpad dicks 100 dicks")) testMessage(p, "leftpad dicks 100 dicks")
assert.Len(t, mb.Messages, 1) if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "kill me") assert.Contains(t, mb.Messages[0], "kill me")
}
} }
func TestUnder50Padding(t *testing.T) { func TestUnder50Padding(t *testing.T) {
p, mb := makePlugin(t) p, mb := makePlugin(t)
p.config.Set("LeftPad.MaxLen", "50") p.config.Set("LeftPad.MaxLen", "50")
p.message(makeMessage("!leftpad dicks 49 dicks")) testMessage(p, "leftpad dicks 49 dicks")
assert.Len(t, mb.Messages, 1) if assert.Len(t, mb.Messages, 1) {
assert.Contains(t, mb.Messages[0], "dicks") assert.Contains(t, mb.Messages[0], "dicks")
}
} }
func TestNotPadding(t *testing.T) { func TestNotPadding(t *testing.T) {
p, mb := makePlugin(t) p, mb := makePlugin(t)
p.message(makeMessage("!lololol")) testMessage(p, "lololol")
assert.Len(t, mb.Messages, 0) assert.Len(t, mb.Messages, 0)
} }