-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
85 lines (78 loc) · 1.67 KB
/
message_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
package irc
import (
"reflect"
"testing"
)
// messageTest defines the structure for a test case
type messageTest struct {
name string
raw string
msg *Message
err bool
}
// messageTests defines all test cases
var messageTests = []messageTest{
{
name: "join",
raw: ":[email protected] JOIN :#foo\r\n",
msg: &Message{
Command: "JOIN",
Params: ":#foo",
ParamsArray: []string{":#foo"},
Name: "foo",
User: "~bar",
Host: "127.0.0.1",
},
},
{
name: "motd",
raw: ":irc.foo.com 372 foo :- * foo\r\n",
msg: &Message{
Command: "372",
Params: "foo :- * foo",
ParamsArray: []string{"foo", ":-", "*", "foo"},
Name: "irc.foo.com",
},
},
{
name: "ping",
raw: "PING :irc.foo.com\r\n",
msg: &Message{
Command: "PING",
Params: ":irc.foo.com",
ParamsArray: []string{":irc.foo.com"},
},
},
{
name: "malformed",
raw: "foo:\r\n",
err: true,
},
{
name: "empty",
raw: "\r\n",
},
}
// Run all tests
func TestMessages(t *testing.T) {
for _, mt := range messageTests {
t.Run(mt.name, func(t *testing.T) {
// Parse the raw message
m, err := parse(mt.raw)
// Make sure the error is set if the test expects it to be
if mt.err && err == nil {
t.Errorf("%s: should return an error but didn't", mt.name)
}
// If not the msg is nil, assign the raw value to it
if mt.msg != nil {
mt.msg.Raw = mt.raw
}
// Compare the parsed message with what we expect it to be
if !reflect.DeepEqual(mt.msg, m) {
t.Errorf("%s: failed to parse message %s", mt.name, mt.raw)
t.Logf("output: %#v", m)
t.Logf("expected: %#v", mt.msg)
}
})
}
}