forked from go-chat-bot/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
120 lines (115 loc) · 4.26 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
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
112
113
114
115
116
117
118
119
120
package bot
import (
"strings"
"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",
MessageData: &Message{Text: strings.TrimLeft("cmd", CmdPrefix)},
}},
{cmdWithArgs, &Cmd{
Raw: cmdWithArgs,
Command: "cmd",
Channel: channel.Channel,
ChannelData: channel,
User: user,
Message: "cmd arg1 arg2",
RawArgs: "arg1 arg2",
Args: []string{"arg1", "arg2"},
MessageData: &Message{Text: strings.TrimLeft("cmd arg1 arg2", CmdPrefix)},
}},
{cmdWithQuotes, &Cmd{
Raw: cmdWithQuotes,
Command: "cmd",
Channel: channel.Channel,
ChannelData: channel,
User: user,
Message: "cmd \"arg1 arg2\"",
RawArgs: "\"arg1 arg2\"",
Args: []string{"arg1 arg2"},
MessageData: &Message{Text: strings.TrimLeft("cmd \"arg1 arg2\"", CmdPrefix)},
}},
}
for _, test := range tests {
cmd, _ := parse(test.msg, channel, user)
if test.expected != nil && cmd != nil {
if test.expected.Raw != cmd.Raw {
t.Errorf("Expected Raw:\n%#v\ngot:\n%#v", test.expected.Raw, cmd.Raw)
}
if test.expected.Channel != cmd.Channel {
t.Errorf("Expected Channel:\n%#v\ngot:\n%#v", test.expected.Channel, cmd.Channel)
}
if test.expected.Message != cmd.Message {
t.Errorf("Expected Message:\n%#v\ngot:\n%#v", test.expected.Message, cmd.Message)
}
if test.expected.Command != cmd.Command {
t.Errorf("Expected Command:\n%#v\ngot:\n%#v", test.expected.Command, cmd.Command)
}
if test.expected.RawArgs != cmd.RawArgs {
t.Errorf("Expected RawArgs:\n%#v\ngot:\n%#v", test.expected.RawArgs, cmd.RawArgs)
}
if test.expected.ChannelData.Protocol != cmd.ChannelData.Protocol {
t.Errorf("Expected ChannelData.Protocol:\n%#v\ngot:\n%#v", test.expected.ChannelData.Protocol, cmd.ChannelData.Protocol)
}
if test.expected.ChannelData.Server != cmd.ChannelData.Server {
t.Errorf("Expected ChannelData.Server:\n%#v\ngot:\n%#v", test.expected.ChannelData.Server, cmd.ChannelData.Server)
}
if test.expected.ChannelData.Channel != cmd.ChannelData.Channel {
t.Errorf("Expected ChannelData.Channel:\n%#v\ngot:\n%#v", test.expected.ChannelData.Channel, cmd.ChannelData.Channel)
}
if test.expected.ChannelData.IsPrivate != cmd.ChannelData.IsPrivate {
t.Errorf("Expected ChannelData.IsPrivate:\n%#v\ngot:\n%#v", test.expected.ChannelData.IsPrivate, cmd.ChannelData.IsPrivate)
}
if test.expected.User.ID != cmd.User.ID {
t.Errorf("Expected User.ID:\n%#v\ngot:\n%#v", test.expected.User.ID, cmd.User.ID)
}
if test.expected.User.Nick != cmd.User.Nick {
t.Errorf("Expected User.Nick:\n%#v\ngot:\n%#v", test.expected.User.Nick, cmd.User.Nick)
}
if test.expected.User.RealName != cmd.User.RealName {
t.Errorf("Expected User.RealName:\n%#v\ngot:\n%#v", test.expected.User.RealName, cmd.User.RealName)
}
if test.expected.User.IsBot != cmd.User.IsBot {
t.Errorf("Expected User.IsBot:\n%#v\ngot:\n%#v", test.expected.User.IsBot, cmd.User.IsBot)
}
if test.expected.MessageData.Text != cmd.MessageData.Text {
t.Errorf("Expected MessageData.Text:\n%#v\ngot:\n%#v", test.expected.MessageData.Text, cmd.MessageData.Text)
}
if test.expected.MessageData.IsAction != cmd.MessageData.IsAction {
t.Errorf("Expected MessageData.IsAction:\n%#v\ngot:\n%#v", test.expected.MessageData.IsAction, cmd.MessageData.IsAction)
}
for i, arg := range test.expected.Args {
if arg != cmd.Args[i] {
t.Errorf("Expected cmd.Args[]:\n%#v\ngot:\n%#v", arg, cmd.Args[i])
}
}
}
}
}
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)
}
}