This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
parser_test.go
170 lines (165 loc) · 5.85 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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\""
cmdMixedCaseWithoutArgs := CmdPrefix + "Cmd"
cmdMixedCaseWithArgs := CmdPrefix + "Cmd arg1 arg2 "
cmdMixedCaseWithQuotes := CmdPrefix + "Cmd \"arg1 arg2\""
cmdUnicode := CmdPrefix + "Çmd"
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)},
}},
{cmdMixedCaseWithoutArgs, &Cmd{
Raw: cmdMixedCaseWithoutArgs,
Command: "cmd",
Channel: channel.Channel,
ChannelData: channel,
User: user,
Message: "Cmd",
MessageData: &Message{Text: strings.TrimLeft("Cmd", CmdPrefix)},
}},
{cmdMixedCaseWithArgs, &Cmd{
Raw: cmdMixedCaseWithArgs,
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)},
}},
{cmdMixedCaseWithQuotes, &Cmd{
Raw: cmdMixedCaseWithQuotes,
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)},
}},
{cmdUnicode, &Cmd{
Raw: cmdUnicode,
Command: "cmd",
Channel: channel.Channel,
ChannelData: channel,
User: user,
Message: "Çmd",
MessageData: &Message{Text: strings.TrimLeft("Çmd", CmdPrefix)},
}},
}
for _, test := range tests {
t.Run(test.msg, func(t *testing.T) {
cmd, _ := parse(&Message{Text: 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(
&Message{Text: "!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)
}
}