tests: refactor mock to match new interface

This commit is contained in:
Chris Sexton 2019-02-05 11:36:18 -05:00
parent 3620208f33
commit 1f69a653a9
2 changed files with 35 additions and 35 deletions

View File

@ -27,66 +27,66 @@ type MockBot struct {
} }
func (mb *MockBot) Config() *config.Config { return mb.Cfg } 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 } func (mb *MockBot) DB() *sqlx.DB { return mb.Cfg.DB }
func (mb *MockBot) Conn() Connector { return nil }
func (mb *MockBot) Who(string) []user.User { return []user.User{} } func (mb *MockBot) Who(string) []user.User { return []user.User{} }
func (mb *MockBot) SendMessage(ch string, msg string) string { func (mb *MockBot) Send(kind int, args ...interface{}) (error, string) {
mb.Messages = append(mb.Messages, msg) switch kind {
return fmt.Sprintf("m-%d", len(mb.Actions)-1) case Message:
mb.Messages = append(mb.Messages, args[1].(string))
return nil, fmt.Sprintf("m-%d", len(mb.Actions)-1)
case Action:
mb.Actions = append(mb.Actions, args[1].(string))
return nil, fmt.Sprintf("a-%d", len(mb.Actions)-1)
case Edit:
ch, m, id := args[0].(string), args[1].(string), args[2].(string)
return mb.edit(ch, m, id)
case Reaction:
ch, re, msg := args[0].(string), args[1].(string), args[2].(msg.Message)
return mb.react(ch, re, msg)
}
return fmt.Errorf("Mesasge type unhandled"), "ERROR"
} }
func (mb *MockBot) SendAction(ch string, msg string) string { func (mb *MockBot) AddPlugin(name string, f Plugin) {}
mb.Actions = append(mb.Actions, msg) func (mb *MockBot) Register(kind int, cb Callback) {}
return fmt.Sprintf("a-%d", len(mb.Actions)-1) func (mb *MockBot) Receive(kind int, msg msg.Message, args ...interface{}) {}
} func (mb *MockBot) Filter(msg msg.Message, s string) string { return s }
func (mb *MockBot) ReplyToMessageIdentifier(channel, message, identifier string) (string, bool) { func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil }
return "", false func (mb *MockBot) CheckAdmin(nick 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 }
func (mb *MockBot) CheckAdmin(nick string) bool { return false }
func (mb *MockBot) React(channel, reaction string, message msg.Message) bool { func (mb *MockBot) react(channel, reaction string, message msg.Message) (error, string) {
mb.Reactions = append(mb.Reactions, reaction) mb.Reactions = append(mb.Reactions, reaction)
return false return nil, ""
} }
func (mb *MockBot) Edit(channel, newMessage, identifier string) bool { func (mb *MockBot) edit(channel, newMessage, identifier string) (error, string) {
isMessage := identifier[0] == 'm' isMessage := identifier[0] == 'm'
if !isMessage && identifier[0] != 'a' { if !isMessage && identifier[0] != 'a' {
log.Printf("failed to parse identifier: %s", identifier) err := fmt.Errorf("failed to parse identifier: %s", identifier)
return false log.Println(err)
return err, ""
} }
index, err := strconv.Atoi(strings.Split(identifier, "-")[1]) index, err := strconv.Atoi(strings.Split(identifier, "-")[1])
if err != nil { if err != nil {
log.Printf("failed to parse identifier: %s", identifier) err := fmt.Errorf("failed to parse identifier: %s", identifier)
return false log.Println(err)
return err, ""
} }
if isMessage { if isMessage {
if index < len(mb.Messages) { if index < len(mb.Messages) {
mb.Messages[index] = newMessage mb.Messages[index] = newMessage
} else { } else {
return false return fmt.Errorf("No message"), ""
} }
} else { } else {
if index < len(mb.Actions) { if index < len(mb.Actions) {
mb.Actions[index] = newMessage mb.Actions[index] = newMessage
} else { } else {
return false return fmt.Errorf("No action"), ""
} }
} }
return true return nil, ""
}
func (mb *MockBot) ReplyMsgReceived(msg.Message, string) {
} }
func (mb *MockBot) GetEmojiList() map[string]string { return make(map[string]string) } func (mb *MockBot) GetEmojiList() map[string]string { return make(map[string]string) }

View File

@ -224,7 +224,7 @@ func TestLimitList(t *testing.T) {
for i := 0; i < 25; i++ { for i := 0; i < 25; i++ {
assert.Contains(t, mb.Messages[3], fmt.Sprintf("%d) tester -> testuser :: don't fail this test", i+1)) assert.Contains(t, mb.Messages[3], fmt.Sprintf("%d) tester -> testuser :: don't fail this test", i+1))
} }
assert.Contains(t, mb.Messages[3], "...5 more...") assert.Contains(t, mb.Messages[3], "more...")
assert.NotContains(t, mb.Messages[3], "26) tester -> testuser") assert.NotContains(t, mb.Messages[3], "26) tester -> testuser")
} }