forked from fabioxgn/go-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_test.go
111 lines (82 loc) · 2.94 KB
/
cmd_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package bot
import (
"errors"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestMessageReceived(t *testing.T) {
Convey("Given a new message in the channel", t, func() {
commands = make(map[string]*customCommand)
conn := &ircConnectionMock{}
Convey("When the command is not registered", func() {
conn = &ircConnectionMock{}
Convey("It should not post to the channel", func() {
messageReceived("#go-bot", "!not_a_cmd", "user", conn)
So(conn.Messages, ShouldBeEmpty)
})
})
Convey("The command can return an error", func() {
conn = &ircConnectionMock{}
Convey("it sould send the message with the error to the channel", func() {
cmdError := errors.New("error")
RegisterCommand("cmd", "", "",
func(c *Cmd) (string, error) {
return "", cmdError
})
messageReceived("#go-bot", "!cmd", "user", conn)
So(conn.Channel, ShouldEqual, "#go-bot")
So(conn.Messages, ShouldResemble,
[]string{fmt.Sprintf(errorExecutingCommand, "cmd", cmdError.Error())})
})
})
Convey("When the command is valid and registered", func() {
conn = &ircConnectionMock{}
commands = make(map[string]*customCommand)
expectedMsg := "msg"
cmd := "cmd"
cmdDescription := "Command description"
cmdExampleArgs := "arg1 arg2"
RegisterCommand(cmd, cmdDescription, cmdExampleArgs,
func(c *Cmd) (string, error) {
return expectedMsg, nil
})
Convey("If it is called in the channel, reply on the channel", func() {
messageReceived("#go-bot", "!cmd", "user", conn)
So(conn.Channel, ShouldEqual, "#go-bot")
So(conn.Messages, ShouldResemble, []string{expectedMsg})
})
Convey("If it is a private message, reply to the user", func() {
conn.Nick = "go-bot"
messageReceived("go-bot", "!cmd", "sender-nick", conn)
So(conn.Channel, ShouldEqual, "sender-nick")
})
Convey("When the command is help", func() {
Convey("Display the available commands in the channel", func() {
messageReceived("#go-bot", "!help", "user", conn)
So(conn.Channel, ShouldEqual, "#go-bot")
So(conn.Messages, ShouldResemble, []string{
fmt.Sprintf(helpAboutCommand, CmdPrefix),
fmt.Sprintf(availableCommands, "cmd"),
})
})
Convey("If the command exists send a message to the channel", func() {
messageReceived("#go-bot", "!help cmd", "user", conn)
So(conn.Channel, ShouldEqual, "#go-bot")
So(conn.Messages, ShouldResemble, []string{
fmt.Sprintf(helpDescripton, cmdDescription),
fmt.Sprintf(helpUsage, CmdPrefix, cmd, cmdExampleArgs),
})
})
Convey("If the command does not exists, display the generic help", func() {
messageReceived("#go-bot", "!help not_a_command", "user", conn)
So(conn.Channel, ShouldEqual, "#go-bot")
So(conn.Messages, ShouldResemble, []string{
fmt.Sprintf(helpAboutCommand, CmdPrefix),
fmt.Sprintf(availableCommands, "cmd"),
})
})
})
})
})
}