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 (
|
2021-02-01 20:14:18 +00:00
|
|
|
"regexp"
|
2016-03-31 02:47:21 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-05-26 15:38:55 +00:00
|
|
|
"github.com/velour/catbase/plugins/cli"
|
|
|
|
|
2016-03-31 02:47:21 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
func makeMessage(payload string, r *regexp.Regexp) bot.Request {
|
2016-03-31 02:47:21 +00:00
|
|
|
isCmd := strings.HasPrefix(payload, "!")
|
|
|
|
if isCmd {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
2019-05-27 23:21:53 +00:00
|
|
|
c := &cli.CliPlugin{}
|
2021-02-01 20:14:18 +00:00
|
|
|
values := bot.ParseValues(r, payload)
|
|
|
|
return bot.Request{
|
|
|
|
Conn: c,
|
|
|
|
Kind: bot.Message,
|
|
|
|
Values: values,
|
|
|
|
Msg: msg.Message{
|
2021-02-14 21:57:22 +00:00
|
|
|
User: &user.User{Name: "tester", ID: "id"},
|
2021-02-01 20:14:18 +00:00
|
|
|
Channel: "test",
|
|
|
|
Body: payload,
|
|
|
|
Command: isCmd,
|
|
|
|
},
|
2016-03-31 02:47:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
func testMessage(p *BeersPlugin, msg string) bool {
|
|
|
|
for _, h := range p.handlers {
|
|
|
|
if h.Regex.MatchString(msg) {
|
|
|
|
req := makeMessage(msg, h.Regex)
|
|
|
|
if h.Handler(req) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-12-21 19:08:20 +00:00
|
|
|
func makeBeersPlugin(t *testing.T) (*BeersPlugin, *bot.MockBot, func()) {
|
2016-03-31 02:47:21 +00:00
|
|
|
mb := bot.NewMockBot()
|
|
|
|
counter.New(mb)
|
|
|
|
b := New(mb)
|
2021-12-21 19:08:20 +00:00
|
|
|
counter.MkAlias(mb.Store(), "beer", DEFAULT_ITEM)
|
|
|
|
counter.MkAlias(mb.Store(), "beers", DEFAULT_ITEM)
|
|
|
|
return b, mb, func() {
|
|
|
|
mb.TearDown()
|
|
|
|
}
|
2016-03-31 02:47:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 17:36:02 +00:00
|
|
|
func TestCounter(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
_, mb, td := makeBeersPlugin(t)
|
|
|
|
defer td()
|
|
|
|
i, err := counter.GetUserItem(mb.Store(), "tester", "id", "test")
|
2019-01-21 17:36:02 +00:00
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
t.Log(err)
|
|
|
|
t.Fatal()
|
|
|
|
}
|
2021-06-17 17:59:29 +00:00
|
|
|
err = i.Update(nil, 5)
|
2019-01-21 17:36:02 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2016-03-31 02:47:21 +00:00
|
|
|
func TestImbibe(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
b, mb, td := makeBeersPlugin(t)
|
|
|
|
defer td()
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "imbibe")
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "imbibe")
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 2)
|
2021-12-21 19:08:20 +00:00
|
|
|
it, err := counter.GetUserItem(mb.Store(), "tester", "id", DEFAULT_ITEM)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 2, it.Count)
|
|
|
|
}
|
|
|
|
func TestEq(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
b, mb, td := makeBeersPlugin(t)
|
|
|
|
defer td()
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "beers = 3")
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
2021-12-21 19:08:20 +00:00
|
|
|
it, err := counter.GetUserItem(mb.Store(), "tester", "id", DEFAULT_ITEM)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 3, it.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEqZero(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
b, mb, td := makeBeersPlugin(t)
|
|
|
|
defer td()
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "beers += 5")
|
|
|
|
testMessage(b, "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")
|
2021-12-21 19:08:20 +00:00
|
|
|
it, err := counter.GetUserItem(mb.Store(), "tester", "id", DEFAULT_ITEM)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 0, it.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeersPlusEq(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
b, mb, td := makeBeersPlugin(t)
|
|
|
|
defer td()
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "beers += 5")
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "beers += 5")
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 2)
|
2021-12-21 19:08:20 +00:00
|
|
|
it, err := counter.GetUserItem(mb.Store(), "tester", "id", DEFAULT_ITEM)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 10, it.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPuke(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
b, mb, td := makeBeersPlugin(t)
|
|
|
|
defer td()
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "beers += 5")
|
2021-12-21 19:08:20 +00:00
|
|
|
it, err := counter.GetUserItem(mb.Store(), "tester", "id", DEFAULT_ITEM)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 5, it.Count)
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "puke")
|
2021-12-21 19:08:20 +00:00
|
|
|
it, err = counter.GetUserItem(mb.Store(), "tester", "id", DEFAULT_ITEM)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 0, it.Count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBeersReport(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
b, mb, td := makeBeersPlugin(t)
|
|
|
|
defer td()
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "beers += 5")
|
2021-12-21 19:08:20 +00:00
|
|
|
it, err := counter.GetUserItem(mb.Store(), "tester", "id", DEFAULT_ITEM)
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 5, it.Count)
|
2021-02-01 20:14:18 +00:00
|
|
|
testMessage(b, "beers")
|
|
|
|
if assert.Len(t, mb.Messages, 2) {
|
|
|
|
assert.Contains(t, mb.Messages[1], "5 beers")
|
|
|
|
}
|
2016-03-31 02:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHelp(t *testing.T) {
|
2021-12-21 19:08:20 +00:00
|
|
|
b, mb, td := makeBeersPlugin(t)
|
|
|
|
defer td()
|
|
|
|
b.help(&cli.CliPlugin{}, bot.Help, msg.Message{Channel: "Channel"}, []string{})
|
2016-03-31 02:47:21 +00:00
|
|
|
assert.Len(t, mb.Messages, 1)
|
|
|
|
}
|