2017-03-13 17:44:44 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package your
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
|
|
|
)
|
|
|
|
|
|
|
|
func makeMessage(payload string) msg.Message {
|
|
|
|
isCmd := strings.HasPrefix(payload, "!")
|
|
|
|
if isCmd {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
|
|
|
return msg.Message{
|
|
|
|
User: &user.User{Name: "tester"},
|
|
|
|
Channel: "test",
|
|
|
|
Body: payload,
|
|
|
|
Command: isCmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:36:02 +00:00
|
|
|
func setup(t *testing.T) (*YourPlugin, *bot.MockBot) {
|
2017-03-13 17:44:44 +00:00
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
2019-01-21 17:36:02 +00:00
|
|
|
mb.DB().MustExec(`delete from config;`)
|
|
|
|
return c, mb
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplacement(t *testing.T) {
|
|
|
|
c, mb := setup(t)
|
2019-01-20 20:21:26 +00:00
|
|
|
c.config.Set("Your.MaxLength", "1000")
|
|
|
|
c.config.SetArray("your.replacements", []string{"0"})
|
|
|
|
c.config.Set("your.replacements.0.freq", "1.0")
|
|
|
|
c.config.Set("your.replacements.0.this", "fuck")
|
|
|
|
c.config.Set("your.replacements.0.that", "duck")
|
2017-03-13 17:44:44 +00:00
|
|
|
res := c.Message(makeMessage("fuck a duck"))
|
|
|
|
assert.True(t, res)
|
2019-01-20 20:21:26 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
2017-03-13 17:44:44 +00:00
|
|
|
assert.Contains(t, mb.Messages[0], "duck a duck")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoReplacement(t *testing.T) {
|
2019-01-21 17:36:02 +00:00
|
|
|
c, mb := setup(t)
|
2019-01-20 20:21:26 +00:00
|
|
|
c.config.Set("Your.MaxLength", "1000")
|
|
|
|
c.config.SetArray("your.replacements", []string{"0", "1", "2"})
|
|
|
|
c.config.Set("your.replacements.0.freq", "1.0")
|
|
|
|
c.config.Set("your.replacements.0.this", "nope")
|
|
|
|
c.config.Set("your.replacements.0.that", "duck")
|
|
|
|
|
|
|
|
c.config.Set("your.replacements.1.freq", "1.0")
|
|
|
|
c.config.Set("your.replacements.1.this", "nope")
|
|
|
|
c.config.Set("your.replacements.1.that", "duck")
|
|
|
|
|
|
|
|
c.config.Set("your.replacements.2.freq", "1.0")
|
|
|
|
c.config.Set("your.replacements.2.this", "Fuck")
|
|
|
|
c.config.Set("your.replacements.2.that", "duck")
|
2017-03-13 17:44:44 +00:00
|
|
|
c.Message(makeMessage("fuck a duck"))
|
|
|
|
assert.Len(t, mb.Messages, 0)
|
|
|
|
}
|