2016-03-31 02:47:21 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package beers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/velour/catbase/bot"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/bot/user"
|
2016-03-31 02:47:21 +00:00
|
|
|
"github.com/velour/catbase/plugins/counter"
|
|
|
|
)
|
|
|
|
|
2019-02-05 20:02:15 +00:00
|
|
|
func makeMessage(payload string) (bot.Kind, msg.Message) {
|
2016-03-31 02:47:21 +00:00
|
|
|
isCmd := strings.HasPrefix(payload, "!")
|
|
|
|
if isCmd {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
2019-02-05 20:02:15 +00:00
|
|
|
return bot.Message, msg.Message{
|
2016-04-01 14:20:03 +00:00
|
|
|
User: &user.User{Name: "tester"},
|
2016-03-31 02:47:21 +00:00
|
|
|
Channel: "test",
|
|
|
|
Body: payload,
|
|
|
|
Command: isCmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeBeersPlugin(t *testing.T) (*BeersPlugin, *bot.MockBot) {
|
|
|
|
mb := bot.NewMockBot()
|
|
|
|
counter.New(mb)
|
2019-01-21 17:36:02 +00:00
|
|
|
mb.DB().MustExec(`delete from counter; delete from counter_alias;`)
|
2016-03-31 02:47:21 +00:00
|
|
|
b := New(mb)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("!mkalias beer :beer:"))
|
|
|
|
b.message(makeMessage("!mkalias beers :beer:"))
|
2016-03-31 02:47:21 +00:00
|
|
|
return b, mb
|
|
|
|
}
|
|
|
|
|
2019-01-21 17:36:02 +00:00
|
|
|
func TestCounter(t *testing.T) {
|
|
|
|
_, mb := makeBeersPlugin(t)
|
|
|
|
i, err := counter.GetItem(mb.DB(), "tester", "test")
|
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
t.Log(err)
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
err = i.Update(5)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2016-03-31 02:47:21 +00:00
|
|
|
func TestImbibe(t *testing.T) {
|
|
|
|
b, mb := makeBeersPlugin(t)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("!imbibe"))
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("!imbibe"))
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 2)
|
2019-01-19 14:59:53 +00:00
|
|
|
it, err := counter.GetItem(mb.DB(), "tester", itemName)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 2, it.Count)
|
|
|
|
}
|
|
|
|
func TestEq(t *testing.T) {
|
|
|
|
b, mb := makeBeersPlugin(t)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("!beers = 3"))
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
2019-01-19 14:59:53 +00:00
|
|
|
it, err := counter.GetItem(mb.DB(), "tester", itemName)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 3, it.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEqNeg(t *testing.T) {
|
|
|
|
b, mb := makeBeersPlugin(t)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("!beers = -3"))
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
2019-01-19 14:59:53 +00:00
|
|
|
it, err := counter.GetItem(mb.DB(), "tester", itemName)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 0, it.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEqZero(t *testing.T) {
|
|
|
|
b, mb := makeBeersPlugin(t)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("beers += 5"))
|
|
|
|
b.message(makeMessage("!beers = 0"))
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 2)
|
|
|
|
assert.Contains(t, mb.Messages[1], "reversal of fortune")
|
2019-01-19 14:59:53 +00:00
|
|
|
it, err := counter.GetItem(mb.DB(), "tester", itemName)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 0, it.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeersPlusEq(t *testing.T) {
|
|
|
|
b, mb := makeBeersPlugin(t)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("beers += 5"))
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("beers += 5"))
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 2)
|
2019-01-19 14:59:53 +00:00
|
|
|
it, err := counter.GetItem(mb.DB(), "tester", itemName)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 10, it.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPuke(t *testing.T) {
|
|
|
|
b, mb := makeBeersPlugin(t)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("beers += 5"))
|
2019-01-19 14:59:53 +00:00
|
|
|
it, err := counter.GetItem(mb.DB(), "tester", itemName)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 5, it.Count)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("puke"))
|
2019-01-19 14:59:53 +00:00
|
|
|
it, err = counter.GetItem(mb.DB(), "tester", itemName)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 0, it.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeersReport(t *testing.T) {
|
|
|
|
b, mb := makeBeersPlugin(t)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("beers += 5"))
|
2019-01-19 14:59:53 +00:00
|
|
|
it, err := counter.GetItem(mb.DB(), "tester", itemName)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 5, it.Count)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.message(makeMessage("beers"))
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Contains(t, mb.Messages[1], "5 beers")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHelp(t *testing.T) {
|
|
|
|
b, mb := makeBeersPlugin(t)
|
2019-02-05 20:02:15 +00:00
|
|
|
b.help(bot.Help, msg.Message{Channel: "channel"}, []string{})
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegisterWeb(t *testing.T) {
|
|
|
|
b, _ := makeBeersPlugin(t)
|
|
|
|
assert.Nil(t, b.RegisterWeb())
|
|
|
|
}
|