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"
|
|
|
|
|
2021-03-23 17:40:18 +00:00
|
|
|
"github.com/velour/catbase/plugins/cli"
|
|
|
|
|
2017-03-13 17:44:44 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
|
|
|
)
|
|
|
|
|
2021-03-23 17:40:18 +00:00
|
|
|
func makeMessage(payload string) bot.Request {
|
2017-03-13 17:44:44 +00:00
|
|
|
isCmd := strings.HasPrefix(payload, "!")
|
|
|
|
if isCmd {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
2021-03-23 17:40:18 +00:00
|
|
|
return bot.Request{
|
|
|
|
Conn: &cli.CliPlugin{},
|
|
|
|
Kind: bot.Message,
|
|
|
|
Msg: msg.Message{
|
|
|
|
User: &user.User{Name: "tester"},
|
|
|
|
Channel: "test",
|
|
|
|
Body: payload,
|
|
|
|
Command: isCmd,
|
|
|
|
},
|
|
|
|
Values: bot.RegexValues{},
|
2017-03-13 17:44:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 19:08:20 +00:00
|
|
|
func setup(t *testing.T) (*YourPlugin, *bot.MockBot, func()) {
|
2017-03-13 17:44:44 +00:00
|
|
|
mb := bot.NewMockBot()
|
|
|
|
c := New(mb)
|
2021-12-21 19:08:20 +00:00
|
|
|
return c, mb, func() {
|
|
|
|
mb.TearDown()
|
|
|
|
}
|
2019-01-21 17:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplacement(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
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")
|
2019-02-05 20:02:15 +00:00
|
|
|
res := c.message(makeMessage("fuck a duck"))
|
2017-03-13 17:44:44 +00:00
|
|
|
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) {
|
2021-12-21 19:08:20 +00:00
|
|
|
c, mb, td := setup(t)
|
|
|
|
defer td()
|
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")
|
2019-02-05 20:02:15 +00:00
|
|
|
c.message(makeMessage("fuck a duck"))
|
2017-03-13 17:44:44 +00:00
|
|
|
assert.Len(t, mb.Messages, 0)
|
|
|
|
}
|