Add talker tests

This commit is contained in:
Chris Sexton 2016-03-30 19:25:02 -04:00
parent a38a28636c
commit a9a2c97c56
2 changed files with 152 additions and 17 deletions

View File

@ -42,12 +42,15 @@ var goatse []string = []string{
type TalkerPlugin struct { type TalkerPlugin struct {
Bot bot.Bot Bot bot.Bot
enforceNicks bool enforceNicks bool
sayings []string
} }
func New(bot bot.Bot) *TalkerPlugin { func New(bot bot.Bot) *TalkerPlugin {
rand.Seed(time.Now().Unix())
return &TalkerPlugin{ return &TalkerPlugin{
Bot: bot, Bot: bot,
enforceNicks: bot.Config().EnforceNicks, enforceNicks: bot.Config().EnforceNicks,
sayings: bot.Config().WelcomeMsgs,
} }
} }
@ -57,13 +60,13 @@ func (p *TalkerPlugin) Message(message bot.Message) bool {
lowermessage := strings.ToLower(body) lowermessage := strings.ToLower(body)
// TODO: This ought to be space split afterwards to remove any punctuation // TODO: This ought to be space split afterwards to remove any punctuation
if strings.HasPrefix(lowermessage, "say") { if message.Command && strings.HasPrefix(lowermessage, "say") {
msg := strings.TrimSpace(body[3:]) msg := strings.TrimSpace(body[3:])
p.Bot.SendMessage(channel, msg) p.Bot.SendMessage(channel, msg)
return true return true
} }
if strings.HasPrefix(lowermessage, "goatse") { if message.Command && strings.HasPrefix(lowermessage, "goatse") {
nick := message.User.Name nick := message.User.Name
if parts := strings.Split(message.Body, " "); len(parts) > 1 { if parts := strings.Split(message.Body, " "); len(parts) > 1 {
nick = parts[1] nick = parts[1]
@ -86,31 +89,20 @@ func (p *TalkerPlugin) Message(message bot.Message) bool {
return true return true
} }
if strings.Contains(lowermessage, "felps") || strings.Contains(lowermessage, "fredfelps") {
outmsg := p.Bot.Filter(message, "GOD HATES $NICK")
p.Bot.SendMessage(channel, outmsg)
return true
}
return false return false
} }
func (p *TalkerPlugin) LoadData() {
rand.Seed(time.Now().Unix())
}
func (p *TalkerPlugin) Help(channel string, parts []string) { func (p *TalkerPlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, "Hi, this is talker. I like to talk about FredFelps!") p.Bot.SendMessage(channel, "Hi, this is talker. I like to talk about FredFelps!")
} }
// Empty event handler because this plugin does not do anything on event recv // Empty event handler because this plugin does not do anything on event recv
func (p *TalkerPlugin) Event(kind string, message bot.Message) bool { func (p *TalkerPlugin) Event(kind string, message bot.Message) bool {
sayings := p.Bot.Config().WelcomeMsgs if kind == "JOIN" && strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Nick) {
if len(sayings) == 0 { if len(p.sayings) == 0 {
return false return false
} }
if kind == "JOIN" && strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Nick) { msg := fmt.Sprintf(p.sayings[rand.Intn(len(p.sayings))], message.User.Name)
msg := fmt.Sprintf(sayings[rand.Intn(len(sayings))], message.User.Name)
p.Bot.SendMessage(message.Channel, msg) p.Bot.SendMessage(message.Channel, msg)
return true return true
} }

View File

@ -0,0 +1,143 @@
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package talker
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/velour/catbase/bot"
)
func makeMessage(payload string) bot.Message {
isCmd := strings.HasPrefix(payload, "!")
if isCmd {
payload = payload[1:]
}
return bot.Message{
User: &bot.User{Name: "tester"},
Channel: "test",
Body: payload,
Command: isCmd,
}
}
func TestGoatse(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("goatse"))
assert.Len(t, mb.Messages, 0)
assert.False(t, res)
}
func TestGoatseCommand(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("!goatse"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "g o a t s e")
}
func TestGoatseWithNickCommand(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("!goatse seabass"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "g o a t s e")
assert.Contains(t, mb.Messages[0], "seabass")
}
func TestSay(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("say hello"))
assert.Len(t, mb.Messages, 0)
assert.False(t, res)
}
func TestSayCommand(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Message(makeMessage("!say hello"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "hello")
}
func TestNineChars(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.enforceNicks = true
assert.NotNil(t, c)
res := c.Message(makeMessage("hello there"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "OCD")
}
func TestWelcome(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.sayings = []string{"Hi"}
assert.NotNil(t, c)
res := c.Event("JOIN", makeMessage("hello there"))
assert.Len(t, mb.Messages, 1)
assert.True(t, res)
assert.Contains(t, mb.Messages[0], "Hi")
}
func TestNoSayings(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
c.sayings = []string{}
assert.NotNil(t, c)
res := c.Event("JOIN", makeMessage("hello there"))
assert.Len(t, mb.Messages, 0)
assert.False(t, res)
}
func TestNonJoinEvent(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
res := c.Event("SPLURT", makeMessage("hello there"))
assert.Len(t, mb.Messages, 0)
assert.False(t, res)
}
func TestHelp(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
c.Help("channel", []string{})
assert.Len(t, mb.Messages, 1)
}
func TestBotMessage(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
assert.False(t, c.BotMessage(makeMessage("test")))
}
func TestEvent(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
assert.False(t, c.Event("dummy", makeMessage("test")))
}
func TestRegisterWeb(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
assert.Nil(t, c.RegisterWeb())
}