forked from go-chat-bot/bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.go
68 lines (63 loc) · 1.57 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
package bot
import (
"reflect"
"testing"
)
func TestParser(t *testing.T) {
channel := &ChannelData{Channel: "#go-bot"}
user := &User{Nick: "user123"}
cmdWithoutArgs := CmdPrefix + "cmd"
cmdWithArgs := CmdPrefix + "cmd arg1 arg2 "
cmdWithQuotes := CmdPrefix + "cmd \"arg1 arg2\""
tests := []struct {
msg string
expected *Cmd
}{
{"", nil},
{"!", nil},
{"regular message", nil},
{cmdWithoutArgs, &Cmd{
Raw: cmdWithoutArgs,
Command: "cmd",
Channel: channel.Channel,
ChannelData: channel,
User: user,
Message: "cmd",
}},
{cmdWithArgs, &Cmd{
Raw: cmdWithArgs,
Command: "cmd",
Channel: channel.Channel,
ChannelData: channel,
User: user,
Message: "cmd arg1 arg2",
RawArgs: "arg1 arg2",
Args: []string{"arg1", "arg2"},
}},
{cmdWithQuotes, &Cmd{
Raw: cmdWithQuotes,
Command: "cmd",
Channel: channel.Channel,
ChannelData: channel,
User: user,
Message: "cmd \"arg1 arg2\"",
RawArgs: "\"arg1 arg2\"",
Args: []string{"arg1 arg2"},
}},
}
for _, test := range tests {
cmd, _ := parse(test.msg, channel, user)
if !reflect.DeepEqual(test.expected, cmd) {
t.Errorf("Expected:\n%#v\ngot:\n%#v", test.expected, cmd)
}
}
}
func TestInvalidArguments(t *testing.T) {
cmd, err := parse("!cmd Invalid \"arg", &ChannelData{Channel: "#go-bot"}, &User{Nick: "user123"})
if err == nil {
t.Error("Expected error, got nil")
}
if cmd != nil {
t.Errorf("Expected nil, got %#v", cmd)
}
}