catbase/plugins/your/your_test.go

76 lines
1.9 KiB
Go
Raw Normal View History

// © 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"
"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 {
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{},
}
}
2021-12-21 19:08:20 +00:00
func setup(t *testing.T) (*YourPlugin, *bot.MockBot, func()) {
mb := bot.NewMockBot()
c := New(mb)
2021-12-21 19:08:20 +00:00
return c, mb, func() {
mb.TearDown()
}
}
func TestReplacement(t *testing.T) {
2021-12-21 19:08:20 +00:00
c, mb, td := setup(t)
defer td()
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"))
assert.True(t, res)
assert.Len(t, mb.Messages, 1)
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()
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"))
assert.Len(t, mb.Messages, 0)
}