-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsg_test.go
More file actions
161 lines (151 loc) · 3.32 KB
/
msg_test.go
File metadata and controls
161 lines (151 loc) · 3.32 KB
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
package irc
import (
"bufio"
"io"
"reflect"
"regexp"
"strings"
"testing"
)
// Tests Parse called on messages that do not contain errors.
func TestParseOK(t *testing.T) {
tests := []struct {
raw string
msg Message
}{
{
raw: ":e!foo@bar.com JOIN #test54321",
msg: Message{
Origin: "e",
User: "foo",
Host: "bar.com",
Command: "JOIN",
Arguments: []string{"#test54321"},
},
},
{
raw: ":e JOIN #test54321",
msg: Message{
Origin: "e",
Command: "JOIN",
Arguments: []string{"#test54321"},
},
},
{
raw: "JOIN #test54321",
msg: Message{
Command: "JOIN",
Arguments: []string{"#test54321"},
},
},
{
raw: "JOIN #test54321 :foo bar",
msg: Message{
Command: "JOIN",
Arguments: []string{"#test54321", "foo bar"},
},
},
{
raw: "JOIN #test54321 ::foo bar",
msg: Message{
Command: "JOIN",
Arguments: []string{"#test54321", ":foo bar"},
},
},
{
raw: "JOIN #test54321 foo bar ",
msg: Message{
Command: "JOIN",
Arguments: []string{"#test54321", "foo", "bar"},
},
},
{
raw: "JOIN :",
msg: Message{
Command: "JOIN",
Arguments: []string{""},
},
},
}
for _, test := range tests {
m, err := Parse(test.raw)
if err != nil {
t.Errorf(err.Error())
}
if !reflect.DeepEqual(m, test.msg) {
t.Errorf("failed to correctly parse %#v\nGot: %#v", test, m)
}
}
}
// Tests read (the unexported version that does not call parse) on messages without errors.
func TestReadOk(t *testing.T) {
max := make([]byte, MaxBytes)
for i := range max {
max[i] = 'a'
}
max[len(max)-2] = '\r'
max[len(max)-1] = '\n'
tests := []struct {
s string
ms []string
}{
{"a\r\nb\r\nc\r\n", []string{"a", "b", "c"}},
{"a\r\nb\r\n\r\nc\r\n", []string{"a", "b", "c"}},
{"a \r\nb \r\n\r\nc\r\n", []string{"a ", "b ", "c"}},
{
":e!foo@bar.com JOIN #test54321\r\n",
[]string{":e!foo@bar.com JOIN #test54321"},
},
{string(max), []string{string(max[:len(max)-2])}},
}
for _, test := range tests {
in := bufio.NewReader(strings.NewReader(test.s))
i := 0
for {
m, err := read(in)
if err == io.EOF && i == len(test.ms) {
break
}
if i >= len(test.ms) {
t.Errorf("expected end of messages")
}
if err != nil {
t.Errorf(err.Error())
}
if m != test.ms[i] {
t.Errorf("expected message %s, got %s",
test.ms[i], m)
}
i++
}
}
}
// Tests read (the unexported version that does not call parse) on messages with errors.
func TestReadError(t *testing.T) {
tooLong := make([]byte, MaxBytes)
for i := range tooLong {
tooLong[i] = 'a'
}
tooLong[len(tooLong)-1] = '\r'
tests := []struct {
s string
errStr string
}{
// EOF if there's nothing left.
{"", io.EOF.Error()},
{"a", "unexpected end of file in message stream"},
{"a\r\r\n", "unexpected carrage return in message stream"},
{"hello there\000\r\n", "unexpected null in message stream"},
{string(tooLong), ErrTooLong.Error()},
}
for _, test := range tests {
in := bufio.NewReader(strings.NewReader(test.s))
_, err := read(in)
if err == nil {
t.Errorf("expected error [%s], got none", test.errStr)
} else if matched, _ := regexp.MatchString(test.errStr, err.Error()); !matched {
t.Errorf("unexpected error [%s], expected [%s]",
err, test.errStr)
}
}
}