mirror of https://github.com/velour/catbase.git
leftpad: refactor
This commit is contained in:
parent
353f289cae
commit
0891713523
|
@ -5,12 +5,11 @@ package leftpad
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/chrissexton/leftpad"
|
||||
"github.com/velour/catbase/bot"
|
||||
"github.com/velour/catbase/bot/msg"
|
||||
"github.com/velour/catbase/config"
|
||||
)
|
||||
|
||||
|
@ -25,7 +24,7 @@ func New(b bot.Bot) *LeftpadPlugin {
|
|||
bot: b,
|
||||
config: b.Config(),
|
||||
}
|
||||
b.Register(p, bot.Message, p.message)
|
||||
b.RegisterRegexCmd(p, bot.Message, leftpadRegex, p.leftpadCmd)
|
||||
return p
|
||||
}
|
||||
|
||||
|
@ -33,32 +32,25 @@ type leftpadResp struct {
|
|||
Str string
|
||||
}
|
||||
|
||||
func (p *LeftpadPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||
if !message.Command {
|
||||
return false
|
||||
}
|
||||
var leftpadRegex = regexp.MustCompile(`(?i)^leftpad (?P<padstr>\S+) (?P<padding>\d+) (?P<text>.+)$`)
|
||||
|
||||
parts := strings.Fields(message.Body)
|
||||
if len(parts) > 3 && parts[0] == "leftpad" {
|
||||
padchar := parts[1]
|
||||
length, err := strconv.Atoi(parts[2])
|
||||
if err != nil {
|
||||
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)
|
||||
func (p *LeftpadPlugin) leftpadCmd(r bot.Request) bool {
|
||||
padchar := r.Values["padstr"]
|
||||
length, err := strconv.Atoi(r.Values["padding"])
|
||||
if err != nil {
|
||||
p.bot.Send(r.Conn, bot.Message, r.Msg.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(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
|
||||
}
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
package leftpad
|
||||
|
||||
import (
|
||||
"github.com/velour/catbase/plugins/cli"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/velour/catbase/plugins/cli"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/velour/catbase/bot"
|
||||
"github.com/velour/catbase/bot/msg"
|
||||
|
@ -14,16 +14,24 @@ import (
|
|||
"github.com/velour/catbase/plugins/counter"
|
||||
)
|
||||
|
||||
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
|
||||
isCmd := strings.HasPrefix(payload, "!")
|
||||
if isCmd {
|
||||
payload = payload[1:]
|
||||
func makeMessage(payload string) bot.Request {
|
||||
values := bot.ParseValues(leftpadRegex, payload)
|
||||
return bot.Request{
|
||||
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,
|
||||
Command: isCmd,
|
||||
|
||||
}
|
||||
|
||||
func testMessage(p *LeftpadPlugin, body string) {
|
||||
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) {
|
||||
p, mb := makePlugin(t)
|
||||
p.message(makeMessage("!leftpad test 8 test"))
|
||||
assert.Contains(t, mb.Messages[0], "testtest")
|
||||
assert.Len(t, mb.Messages, 1)
|
||||
}
|
||||
|
||||
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)
|
||||
testMessage(p, "leftpad test 8 test")
|
||||
if assert.Len(t, mb.Messages, 1) {
|
||||
assert.Contains(t, mb.Messages[0], "testtest")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotCommand(t *testing.T) {
|
||||
p, mb := makePlugin(t)
|
||||
p.message(makeMessage("leftpad test fuck test"))
|
||||
testMessage(p, "leftpad test fuck test")
|
||||
assert.Len(t, mb.Messages, 0)
|
||||
}
|
||||
|
||||
func TestNoMaxLen(t *testing.T) {
|
||||
p, mb := makePlugin(t)
|
||||
p.config.Set("LeftPad.MaxLen", "0")
|
||||
p.message(makeMessage("!leftpad dicks 100 dicks"))
|
||||
assert.Len(t, mb.Messages, 1)
|
||||
assert.Contains(t, mb.Messages[0], "dicks")
|
||||
testMessage(p, "leftpad dicks 100 dicks")
|
||||
if assert.Len(t, mb.Messages, 1) {
|
||||
assert.Contains(t, mb.Messages[0], "dicks")
|
||||
}
|
||||
}
|
||||
|
||||
func Test50Padding(t *testing.T) {
|
||||
p, mb := makePlugin(t)
|
||||
p.config.Set("LeftPad.MaxLen", "50")
|
||||
assert.Equal(t, 50, p.config.GetInt("LeftPad.MaxLen", 100))
|
||||
p.message(makeMessage("!leftpad dicks 100 dicks"))
|
||||
assert.Len(t, mb.Messages, 1)
|
||||
assert.Contains(t, mb.Messages[0], "kill me")
|
||||
testMessage(p, "leftpad dicks 100 dicks")
|
||||
if assert.Len(t, mb.Messages, 1) {
|
||||
assert.Contains(t, mb.Messages[0], "kill me")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnder50Padding(t *testing.T) {
|
||||
p, mb := makePlugin(t)
|
||||
p.config.Set("LeftPad.MaxLen", "50")
|
||||
p.message(makeMessage("!leftpad dicks 49 dicks"))
|
||||
assert.Len(t, mb.Messages, 1)
|
||||
assert.Contains(t, mb.Messages[0], "dicks")
|
||||
testMessage(p, "leftpad dicks 49 dicks")
|
||||
if assert.Len(t, mb.Messages, 1) {
|
||||
assert.Contains(t, mb.Messages[0], "dicks")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotPadding(t *testing.T) {
|
||||
p, mb := makePlugin(t)
|
||||
p.message(makeMessage("!lololol"))
|
||||
testMessage(p, "lololol")
|
||||
assert.Len(t, mb.Messages, 0)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue