2016-03-31 03:09:35 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package leftpad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-02-07 19:20:54 +00:00
|
|
|
"github.com/velour/catbase/plugins/cli"
|
|
|
|
|
2016-03-31 03:09:35 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/velour/catbase/bot"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
2016-03-31 03:09:35 +00:00
|
|
|
"github.com/velour/catbase/plugins/counter"
|
|
|
|
)
|
|
|
|
|
2021-02-07 19:20:54 +00:00
|
|
|
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,
|
|
|
|
},
|
2016-03-31 03:09:35 +00:00
|
|
|
}
|
2021-02-07 19:20:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMessage(p *LeftpadPlugin, body string) {
|
|
|
|
if leftpadRegex.MatchString(body) {
|
|
|
|
p.leftpadCmd(makeMessage(body))
|
2016-03-31 03:09:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePlugin(t *testing.T) (*LeftpadPlugin, *bot.MockBot) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
counter.New(mb)
|
|
|
|
p := New(mb)
|
|
|
|
assert.NotNil(t, p)
|
2019-01-21 19:24:03 +00:00
|
|
|
p.config.Set("LeftPad.MaxLen", "0")
|
2016-03-31 03:09:35 +00:00
|
|
|
return p, mb
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLeftpad(t *testing.T) {
|
|
|
|
p, mb := makePlugin(t)
|
2021-02-07 19:20:54 +00:00
|
|
|
testMessage(p, "leftpad test 8 test")
|
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "testtest")
|
|
|
|
}
|
2016-03-31 03:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotCommand(t *testing.T) {
|
|
|
|
p, mb := makePlugin(t)
|
2021-02-07 19:20:54 +00:00
|
|
|
testMessage(p, "leftpad test fuck test")
|
2016-03-31 03:09:35 +00:00
|
|
|
assert.Len(t, mb.Messages, 0)
|
|
|
|
}
|
|
|
|
|
2016-04-01 14:37:44 +00:00
|
|
|
func TestNoMaxLen(t *testing.T) {
|
|
|
|
p, mb := makePlugin(t)
|
2019-01-21 19:24:03 +00:00
|
|
|
p.config.Set("LeftPad.MaxLen", "0")
|
2021-02-07 19:20:54 +00:00
|
|
|
testMessage(p, "leftpad dicks 100 dicks")
|
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "dicks")
|
|
|
|
}
|
2016-04-01 14:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test50Padding(t *testing.T) {
|
|
|
|
p, mb := makePlugin(t)
|
2019-01-20 20:21:26 +00:00
|
|
|
p.config.Set("LeftPad.MaxLen", "50")
|
2019-01-22 00:16:57 +00:00
|
|
|
assert.Equal(t, 50, p.config.GetInt("LeftPad.MaxLen", 100))
|
2021-02-07 19:20:54 +00:00
|
|
|
testMessage(p, "leftpad dicks 100 dicks")
|
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "kill me")
|
|
|
|
}
|
2016-04-01 14:37:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnder50Padding(t *testing.T) {
|
|
|
|
p, mb := makePlugin(t)
|
2019-01-20 20:21:26 +00:00
|
|
|
p.config.Set("LeftPad.MaxLen", "50")
|
2021-02-07 19:20:54 +00:00
|
|
|
testMessage(p, "leftpad dicks 49 dicks")
|
|
|
|
if assert.Len(t, mb.Messages, 1) {
|
|
|
|
assert.Contains(t, mb.Messages[0], "dicks")
|
|
|
|
}
|
2016-04-01 14:37:44 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 03:09:35 +00:00
|
|
|
func TestNotPadding(t *testing.T) {
|
|
|
|
p, mb := makePlugin(t)
|
2021-02-07 19:20:54 +00:00
|
|
|
testMessage(p, "lololol")
|
2016-03-31 03:09:35 +00:00
|
|
|
assert.Len(t, mb.Messages, 0)
|
|
|
|
}
|