From a38a28636c16b782a0015c42261e24387afc72ca Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Wed, 30 Mar 2016 19:04:49 -0400 Subject: [PATCH] Add dice tests --- plugins/dice/dice.go | 24 ++++----- plugins/dice/dice_test.go | 110 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 plugins/dice/dice_test.go diff --git a/plugins/dice/dice.go b/plugins/dice/dice.go index 5291b15..0a04fdb 100644 --- a/plugins/dice/dice.go +++ b/plugins/dice/dice.go @@ -2,14 +2,17 @@ package dice -import "github.com/velour/catbase/bot" +import ( + "time" + + "github.com/velour/catbase/bot" +) import ( "fmt" "math/rand" "strconv" "strings" - "time" ) // This is a dice plugin to serve as an example and quick copy/paste for new plugins. @@ -20,6 +23,8 @@ type DicePlugin struct { // NewDicePlugin creates a new DicePlugin with the Plugin interface func New(bot bot.Bot) *DicePlugin { + rand.Seed(time.Now().Unix()) + return &DicePlugin{ Bot: bot, } @@ -36,13 +41,9 @@ func (p *DicePlugin) Message(message bot.Message) bool { channel := message.Channel parts := strings.Fields(message.Body) - if (len(parts) == 2 || len(parts) == 1) && message.Command { + if len(parts) == 1 && message.Command { var dice []string - if len(parts) == 1 { - dice = strings.Split(parts[0], "d") - } else { - dice = strings.Split(parts[1], "d") - } + dice = strings.Split(parts[0], "d") if len(dice) == 2 { // We actually have a die roll. @@ -79,13 +80,6 @@ func (p *DicePlugin) Message(message bot.Message) bool { return false } -// LoadData imports any configuration data into the plugin. This is not strictly necessary other -// than the fact that the Plugin interface demands it exist. This may be deprecated at a later -// date. -func (p *DicePlugin) LoadData() { - rand.Seed(time.Now().Unix()) -} - // Help responds to help requests. Every plugin must implement a help function. func (p *DicePlugin) Help(channel string, parts []string) { p.Bot.SendMessage(channel, "Roll dice using notation XdY. Try \"3d20\".") diff --git a/plugins/dice/dice_test.go b/plugins/dice/dice_test.go new file mode 100644 index 0000000..7cfc265 --- /dev/null +++ b/plugins/dice/dice_test.go @@ -0,0 +1,110 @@ +// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors. + +package dice + +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 TestDie(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + res := c.Message(makeMessage("!1d6")) + assert.Len(t, mb.Messages, 1) + assert.True(t, res) + assert.Contains(t, mb.Messages[0], "tester, you rolled:") +} + +func TestDice(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + res := c.Message(makeMessage("!5d6")) + assert.Len(t, mb.Messages, 1) + assert.True(t, res) + assert.Contains(t, mb.Messages[0], "tester, you rolled:") +} + +func TestNotCommand(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + res := c.Message(makeMessage("1d6")) + assert.False(t, res) + assert.Len(t, mb.Messages, 0) +} + +func TestBadDice(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + res := c.Message(makeMessage("!aued6")) + assert.False(t, res) + assert.Len(t, mb.Messages, 0) +} + +func TestBadSides(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + res := c.Message(makeMessage("!1daoeu")) + assert.False(t, res) + assert.Len(t, mb.Messages, 0) +} + +func TestLotsOfDice(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + res := c.Message(makeMessage("!100d100")) + assert.True(t, res) + assert.Len(t, mb.Messages, 1) + assert.Contains(t, mb.Messages[0], "You're a dick.") +} + +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()) +}