forked from go-chat-bot/bot
/
parser_test.go
73 lines (58 loc) · 1.89 KB
/
parser_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
package bot
import (
"fmt"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var (
DefaultChannel = "#go-bot"
DefaultUser = &User{Nick: "user123"}
DefaultCommand = "command"
DefaultRawArgs = "arg1 arg2"
DefaultArgs = []string{
"arg1",
"arg2",
}
)
func TestPaser(t *testing.T) {
Convey("When given a message", t, func() {
Convey("When the message is empty", func() {
cmd := parse("", DefaultChannel, DefaultUser)
So(cmd, ShouldBeNil)
})
Convey("When the message doesn't have the prefix", func() {
Message := "regular message"
cmd := parse(Message, DefaultChannel, DefaultUser)
So(cmd, ShouldBeNil)
})
Convey("When the message is only the prefix", func() {
cmd := parse(CmdPrefix, DefaultChannel, DefaultUser)
So(cmd, ShouldBeNil)
})
Convey("When the message is valid command", func() {
msg := fmt.Sprintf("%v%v", CmdPrefix, DefaultCommand)
cmd := parse(msg, DefaultChannel, DefaultUser)
So(cmd, ShouldNotBeNil)
So(cmd.Command, ShouldEqual, DefaultCommand)
So(cmd.Channel, ShouldEqual, DefaultChannel)
})
Convey("When the message is a command with args", func() {
msg := fmt.Sprintf("%v%v %v", CmdPrefix, DefaultCommand, DefaultRawArgs)
cmd := parse(msg, DefaultChannel, DefaultUser)
So(cmd, ShouldNotBeNil)
So(cmd.Command, ShouldEqual, DefaultCommand)
So(cmd.Channel, ShouldEqual, DefaultChannel)
So(cmd.Args, ShouldResemble, DefaultArgs)
So(cmd.RawArgs, ShouldEqual, DefaultRawArgs)
})
Convey("When the message has extra spaces", func() {
msg := fmt.Sprintf(" %v %v %v %v ", CmdPrefix, DefaultCommand, DefaultArgs[0], DefaultArgs[1])
cmd := parse(msg, DefaultChannel, DefaultUser)
So(cmd, ShouldNotBeNil)
So(cmd.Command, ShouldEqual, DefaultCommand)
So(cmd.Channel, ShouldEqual, DefaultChannel)
So(cmd.Args, ShouldResemble, DefaultArgs)
So(cmd.RawArgs, ShouldEqual, DefaultRawArgs)
})
})
}