catbase/bot/mock.go

105 lines
2.9 KiB
Go
Raw Normal View History

// © 2016 the CatBase Authors under the WTFPL license. See AUTHORS for the list of authors.
package bot
import (
2017-10-31 13:40:03 +00:00
"fmt"
"log"
2017-10-31 13:40:03 +00:00
"strconv"
"strings"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/mock"
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/bot/user"
"github.com/velour/catbase/config"
)
type MockBot struct {
mock.Mock
db *sqlx.DB
Cfg *config.Config
Messages []string
Actions []string
Reactions []string
}
func (mb *MockBot) Config() *config.Config { return mb.Cfg }
func (mb *MockBot) DBVersion() int64 { return 1 }
func (mb *MockBot) DB() *sqlx.DB { return mb.Cfg.DB }
2017-10-31 13:40:03 +00:00
func (mb *MockBot) Conn() Connector { return nil }
func (mb *MockBot) Who(string) []user.User { return []user.User{} }
func (mb *MockBot) AddHandler(name string, f Handler) {}
2017-10-31 13:40:03 +00:00
func (mb *MockBot) SendMessage(ch string, msg string) string {
mb.Messages = append(mb.Messages, msg)
2017-10-31 13:40:03 +00:00
return fmt.Sprintf("m-%d", len(mb.Actions)-1)
}
2017-10-31 13:40:03 +00:00
func (mb *MockBot) SendAction(ch string, msg string) string {
mb.Actions = append(mb.Actions, msg)
2017-10-31 13:40:03 +00:00
return fmt.Sprintf("a-%d", len(mb.Actions)-1)
}
2017-11-02 20:32:02 +00:00
func (mb *MockBot) ReplyToMessageIdentifier(channel, message, identifier string) (string, bool) {
return "", false
}
func (mb *MockBot) ReplyToMessage(channel, message string, replyTo msg.Message) (string, bool) {
return "", false
}
func (mb *MockBot) MsgReceived(msg msg.Message) {}
func (mb *MockBot) EventReceived(msg msg.Message) {}
func (mb *MockBot) Filter(msg msg.Message, s string) string { return s }
func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil }
2017-09-29 04:58:21 +00:00
func (mb *MockBot) CheckAdmin(nick string) bool { return false }
func (mb *MockBot) React(channel, reaction string, message msg.Message) bool {
mb.Reactions = append(mb.Reactions, reaction)
return false
}
2017-10-31 13:40:03 +00:00
func (mb *MockBot) Edit(channel, newMessage, identifier string) bool {
isMessage := identifier[0] == 'm'
if !isMessage && identifier[0] != 'a' {
log.Printf("failed to parse identifier: %s", identifier)
return false
}
index, err := strconv.Atoi(strings.Split(identifier, "-")[1])
if err != nil {
log.Printf("failed to parse identifier: %s", identifier)
return false
}
if isMessage {
if index < len(mb.Messages) {
mb.Messages[index] = newMessage
} else {
return false
}
} else {
if index < len(mb.Actions) {
mb.Actions[index] = newMessage
} else {
return false
}
}
return true
}
2017-11-02 20:32:02 +00:00
func (mb *MockBot) ReplyMsgReceived(msg.Message, string) {
}
2017-10-31 13:40:03 +00:00
func (mb *MockBot) GetEmojiList() map[string]string { return make(map[string]string) }
func (mb *MockBot) RegisterFilter(s string, f func(string) string) {}
func NewMockBot() *MockBot {
cfg := config.ReadConfig(":memory:")
b := MockBot{
Cfg: cfg,
Messages: make([]string, 0),
Actions: make([]string, 0),
}
return &b
}