From a9a2c97c56ad33815739d160ac179dadd640a72d Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 30 Mar 2016 19:25:02 -0400 Subject: [PATCH] Add talker tests --- plugins/talker/talker.go | 26 +++---- plugins/talker/talker_test.go | 143 ++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 17 deletions(-) create mode 100644 plugins/talker/talker_test.go diff --git a/plugins/talker/talker.go b/plugins/talker/talker.go index cfd3b88..33b5557 100644 --- a/plugins/talker/talker.go +++ b/plugins/talker/talker.go @@ -42,12 +42,15 @@ var goatse []string = []string{ type TalkerPlugin struct { Bot bot.Bot enforceNicks bool + sayings []string } func New(bot bot.Bot) *TalkerPlugin { + rand.Seed(time.Now().Unix()) return &TalkerPlugin{ Bot: bot, enforceNicks: bot.Config().EnforceNicks, + sayings: bot.Config().WelcomeMsgs, } } @@ -57,13 +60,13 @@ func (p *TalkerPlugin) Message(message bot.Message) bool { lowermessage := strings.ToLower(body) // 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:]) p.Bot.SendMessage(channel, msg) return true } - if strings.HasPrefix(lowermessage, "goatse") { + if message.Command && strings.HasPrefix(lowermessage, "goatse") { nick := message.User.Name if parts := strings.Split(message.Body, " "); len(parts) > 1 { nick = parts[1] @@ -86,31 +89,20 @@ func (p *TalkerPlugin) Message(message bot.Message) bool { 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 } -func (p *TalkerPlugin) LoadData() { - rand.Seed(time.Now().Unix()) -} - func (p *TalkerPlugin) Help(channel string, parts []string) { 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 func (p *TalkerPlugin) Event(kind string, message bot.Message) bool { - sayings := p.Bot.Config().WelcomeMsgs - if len(sayings) == 0 { - return false - } if kind == "JOIN" && strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Nick) { - msg := fmt.Sprintf(sayings[rand.Intn(len(sayings))], message.User.Name) + if len(p.sayings) == 0 { + return false + } + msg := fmt.Sprintf(p.sayings[rand.Intn(len(p.sayings))], message.User.Name) p.Bot.SendMessage(message.Channel, msg) return true } diff --git a/plugins/talker/talker_test.go b/plugins/talker/talker_test.go new file mode 100644 index 0000000..9f778c1 --- /dev/null +++ b/plugins/talker/talker_test.go @@ -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()) +}