// © 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()) }