Skip to content

Commit

Permalink
go-chat-bot#135 tests for RegisterMessageRecieveFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
bnfinet committed Jun 9, 2022
1 parent f8f2441 commit 487fa46
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,17 @@ func TestMultiplePeriodicCommands(t *testing.T) {
defer b.Close()

entries := b.cron.Entries()

sort.Slice(entries, func(i, j int) bool {
return entries[i].ID < entries[j].ID
})

t.Logf("%+v", entries)
if len(entries) != 2 {
t.Fatal("Should have 2 cron job entries")
}
if entries[0].Next.Hour() != 8 {

t.Fatal("First cron job should be scheduled for 8am")
}
if entries[1].Next.Hour() != 12 {
Expand Down Expand Up @@ -691,20 +698,28 @@ func TestCmdV3WithoutSpecifyingChannel(t *testing.T) {

func TestMessageReceiveFilter(t *testing.T) {
reset()

// register a command
hello := func(c *Cmd) (string, error) { return "after receive filter: " + c.Message, nil }
RegisterCommand("cmd", "", "", hello)
RegisterCommand("hello", "", "", hello)

modified := func(cd *ChannelData, msg *Message, sender *User, cmd *Cmd) error {
// filter function that modifies the message before command interpretation
var modified filterMessageReceiveFuncV1
modified = func(cd *ChannelData, msg *Message, sender *User, cmd *Cmd) error {
cmd.Message = "modified"
return nil
}
errored := func(cd *ChannelData, msg *Message, sender *User, cmd *Cmd) error { return errors.New("error") }

// filter function that errors before command interpretation
var errored filterMessageReceiveFuncV1
errored = func(cd *ChannelData, msg *Message, sender *User, cmd *Cmd) error { return errors.New("error") }

RegisterMessageReceiveFilter("modified", modified)
RegisterMessageReceiveFilter("errored", errored)

b := newBot()
b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!cmd"}, &User{Nick: "user"})

b.MessageReceived(&ChannelData{Channel: "#go-bot"}, &Message{Text: "!hello"}, &User{Nick: "user"})

waitMessages(t, 1, 1)

Expand Down

0 comments on commit 487fa46

Please sign in to comment.