-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathirc_test.go
185 lines (175 loc) · 6.22 KB
/
irc_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package justgrep
import (
"bufio"
"fmt"
"os"
"testing"
"time"
)
func assert(t *testing.T, what string, have interface{}, expect interface{}) {
if have != expect {
t.Errorf("assertion on %s failed: have %q, expected %q", what, have, expect)
}
}
func assertStrSlc(t *testing.T, what string, have []string, expect []string) {
if len(have) != len(expect) {
t.Errorf(
"assertion on %s failed: length doesn't match, have %d, expected %d: %q vs %q",
what,
len(have),
len(expect),
have,
expect,
)
}
for i, elem := range have {
if elem != expect[i] {
t.Errorf("assertion on %s[%d] failed: have %q, expected %q", what, i, elem, expect[i])
}
}
}
func assertStrMap(t *testing.T, what string, have map[string]string, expect map[string]string) {
if len(have) != len(expect) {
t.Errorf(
"assertion on %s failed: length doesn't match, have %d, expected %d: %q vs %q",
what,
len(have),
len(expect),
have,
expect,
)
}
for key, value := range have {
if value != expect[key] {
t.Errorf("assertion on %s[%q] failed: have %q, expected %q", what, key, value, expect[key])
}
}
}
func TestNewMessage(t *testing.T) {
m, err := NewMessage("@badge-info=subscriber/15;badges=subscriber/12,glhf-pledge/1;color=#DAA520;display-name=Mm2PL;emotes=;flags=;id=1d7e0b34-fe74-4895-92ae-dd912046e637;mod=0;room-id=11148817;subscriber=1;tmi-sent-ts=1632058935165;turbo=0;user-id=117691339;user-type= :[email protected] PRIVMSG #pajlada :-tags")
assert(t, "error", err, nil)
assert(t, "Action", m.Action, "PRIVMSG")
assert(t, "Prefix", m.Prefix, "[email protected]")
assertStrSlc(t, "Args", m.Args, []string{"#pajlada", "-tags"})
assertStrMap(
t, "Tags", m.Tags, map[string]string{
"badge-info": "subscriber/15",
"badges": "subscriber/12,glhf-pledge/1",
"color": "#DAA520",
"display-name": "Mm2PL",
"emotes": "",
"flags": "",
"id": "1d7e0b34-fe74-4895-92ae-dd912046e637",
"mod": "0",
"room-id": "11148817",
"subscriber": "1",
"tmi-sent-ts": "1632058935165",
"turbo": "0",
"user-id": "117691339",
"user-type": "",
},
)
m, err = NewMessage("@badge-info=subscriber/15;badges=subscriber/12,glhf-pledge/1;color=#DAA520;display-name=Mm2PL;emotes=;flags=;id=1d7e0b34-fe74-4895-92ae-dd912046e637;mod=0;room-id=11148817;subscriber=1;tmi-sent-ts=1632058935165;turbo=0;user-id=117691339;user-type= :[email protected] PRIVMSG #pajlada :-tags many words asdasd")
assert(t, "error", err, nil)
assert(t, "Action", m.Action, "PRIVMSG")
assert(t, "Prefix", m.Prefix, "[email protected]")
assertStrSlc(t, "Args", m.Args, []string{"#pajlada", "-tags many words asdasd"})
assertStrMap(
t, "Tags", m.Tags, map[string]string{
"badge-info": "subscriber/15",
"badges": "subscriber/12,glhf-pledge/1",
"color": "#DAA520",
"display-name": "Mm2PL",
"emotes": "",
"flags": "",
"id": "1d7e0b34-fe74-4895-92ae-dd912046e637",
"mod": "0",
"room-id": "11148817",
"subscriber": "1",
"tmi-sent-ts": "1632058935165",
"turbo": "0",
"user-id": "117691339",
"user-type": "",
},
)
m, err = NewMessage(`@tag=spaces\sexist\sas\sdo\nnew\rlines\sand\:semicolons TEST`)
assert(t, "error", err, nil)
assert(t, "Action", m.Action, "TEST")
assert(t, "Prefix", m.Prefix, "")
assertStrSlc(t, "Args", m.Args, []string{})
assertStrMap(
t, "Tags", m.Tags, map[string]string{
"tag": "spaces exist as do\nnew\rlines and;semicolons",
},
)
m, err = NewMessage("@emote-only=0;followers-only=-1;r9k=0;room-id=111024753;slow=0;subs-only=0 :tmi.twitch.tv ROOMSTATE #boring_nick")
assert(t, "error", err, nil)
assert(t, "Action", m.Action, "ROOMSTATE")
assert(t, "Prefix", m.Prefix, "tmi.twitch.tv")
assertStrSlc(t, "Args", m.Args, []string{"#boring_nick"})
}
func BenchmarkNewMessage(b *testing.B) {
file, err := os.Open("channel.txt")
if err != nil {
fmt.Println("You need to have a large logs of IRC messages named channel.txt for this benchmark to work.")
fmt.Println("Failed to open file", err)
b.Failed()
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, _, err := reader.ReadLine()
if err != nil {
break
}
_, err = NewMessage(string(line))
if err != nil {
fmt.Printf("Failed at line: %s. Err=%s\n", line, err)
}
}
}
func getTestMessage() *Message {
return &Message{
Raw: "@badge-info=subscriber/15;badges=subscriber/12,glhf-pledge/1;color=#DAA520;display-name=Mm2PL;emotes=;flags=;id=1d7e0b34-fe74-4895-92ae-dd912046e637;mod=0;room-id=11148817;subscriber=1;tmi-sent-ts=1632058935165;turbo=0;user-id=117691339;user-type= :[email protected] PRIVMSG #pajlada :-tags many words asdasd",
Prefix: "[email protected]",
User: "mm2pl",
Args: []string{"#pajlada", "-tags many words asdasd"},
Action: "PRIVMSG",
Tags: map[string]string{
"badge-info": "subscriber/15",
"badges": "subscriber/12,glhf-pledge/1",
"color": "#DAA520",
"display-name": "Mm2PL",
"emotes": "",
"flags": "",
"id": "1d7e0b34-fe74-4895-92ae-dd912046e637",
"mod": "0",
"room-id": "11148817",
"subscriber": "1",
"tmi-sent-ts": "1632058935165",
"turbo": "0",
"user-id": "117691339",
"user-type": "",
},
Timestamp: time.Date(2021, 9, 19, 15, 42, 15, 165, time.UTC),
}
}
func testSerializeMessage(t *testing.T, rawIrc string) {
m, err := NewMessage(rawIrc)
assert(t, "error", err, nil)
assert(t, "serialized output", m.Serialize(), rawIrc+"\r\n")
}
func TestMessage_Serialize(t *testing.T) {
testSerializeMessage(
t,
"@badge-info=subscriber/15;badges=subscriber/12,glhf-pledge/1;color=#DAA520;display-name=Mm2PL;emotes=;flags=;id=1d7e0b34-fe74-4895-92ae-dd912046e637;mod=0;room-id=11148817;subscriber=1;tmi-sent-ts=1632058935165;turbo=0;user-id=117691339;user-type= :[email protected] PRIVMSG #pajlada :-tags many words asdasd",
)
// test escaping tags
testSerializeMessage(t, `@tag=spaces\sexist\sas\sdo\nnew\rlines\sand\:semicolons TEST`)
}
func BenchmarkMessage_Serialize(b *testing.B) {
m := getTestMessage()
for i := 0; i < 1000; i++ {
_ = m.Serialize()
}
}