Add dice tests

This commit is contained in:
Chris Sexton 2016-03-30 19:04:49 -04:00
parent ef40d335eb
commit a38a28636c
2 changed files with 119 additions and 15 deletions

View File

@ -2,14 +2,17 @@
package dice package dice
import "github.com/velour/catbase/bot" import (
"time"
"github.com/velour/catbase/bot"
)
import ( import (
"fmt" "fmt"
"math/rand" "math/rand"
"strconv" "strconv"
"strings" "strings"
"time"
) )
// This is a dice plugin to serve as an example and quick copy/paste for new plugins. // 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 // NewDicePlugin creates a new DicePlugin with the Plugin interface
func New(bot bot.Bot) *DicePlugin { func New(bot bot.Bot) *DicePlugin {
rand.Seed(time.Now().Unix())
return &DicePlugin{ return &DicePlugin{
Bot: bot, Bot: bot,
} }
@ -36,13 +41,9 @@ func (p *DicePlugin) Message(message bot.Message) bool {
channel := message.Channel channel := message.Channel
parts := strings.Fields(message.Body) parts := strings.Fields(message.Body)
if (len(parts) == 2 || len(parts) == 1) && message.Command { if len(parts) == 1 && message.Command {
var dice []string var dice []string
if len(parts) == 1 { dice = strings.Split(parts[0], "d")
dice = strings.Split(parts[0], "d")
} else {
dice = strings.Split(parts[1], "d")
}
if len(dice) == 2 { if len(dice) == 2 {
// We actually have a die roll. // We actually have a die roll.
@ -79,13 +80,6 @@ func (p *DicePlugin) Message(message bot.Message) bool {
return false 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. // Help responds to help requests. Every plugin must implement a help function.
func (p *DicePlugin) Help(channel string, parts []string) { func (p *DicePlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, "Roll dice using notation XdY. Try \"3d20\".") p.Bot.SendMessage(channel, "Roll dice using notation XdY. Try \"3d20\".")

110
plugins/dice/dice_test.go Normal file
View File

@ -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())
}