-
Notifications
You must be signed in to change notification settings - Fork 59
/
helpers_test.go
156 lines (128 loc) · 3.88 KB
/
helpers_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
package twitch
import (
"testing"
"time"
)
func assertStringsEqual(t *testing.T, expected, actual string) {
if expected != actual {
t.Errorf("failed asserting that \"%s\" is expected \"%s\"", actual, expected)
}
}
func assertIntsEqual(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("failed asserting that \"%d\" is expected \"%d\"", actual, expected)
}
}
func assertInt32sEqual(t *testing.T, expected, actual int32) {
if expected != actual {
t.Errorf("failed asserting that \"%d\" is expected \"%d\"", actual, expected)
}
}
func assertBoolEqual(t *testing.T, expected, actual bool) {
if expected != actual {
t.Errorf("failed asserting that \"%t\" is expected \"%t\"", actual, expected)
}
}
func assertTrue(t *testing.T, actual bool, errorMessage string) {
if !actual {
t.Error(errorMessage)
}
}
func assertFalse(t *testing.T, actual bool, errorMessage string) {
if actual {
t.Error(errorMessage)
}
}
func assertStringSlicesEqual(t *testing.T, expected, actual []string) {
if actual == nil {
if expected == nil {
return
}
t.Errorf("actual slice was nil")
}
if len(actual) != len(expected) {
t.Errorf("actual slice(%#v)(%d) was not the same length as expected slice(%#v)(%d)", actual, len(actual), expected, len(expected))
}
for i, v := range actual {
if v != expected[i] {
t.Errorf("actual slice value \"%s\" was not equal to expected value \"%s\" at index \"%d\"", v, expected[i], i)
}
}
}
func assertStringMapsEqual(t *testing.T, expected, actual map[string]string) {
if actual == nil {
if expected == nil {
return
}
t.Errorf("actual map was nil")
}
if len(expected) != len(actual) {
t.Errorf("actual map was not the same length as the expected map")
}
for key, want := range expected {
got, ok := actual[key]
if !ok {
t.Errorf("actual map doesn't contain key \"%s\"", key)
continue
}
if want != got {
t.Errorf("actual map value \"%s\" was not equal to expected value \"%s\" in key \"%s\"", got, want, key)
continue
}
}
}
func assertStringIntMapsEqual(t *testing.T, expected, actual map[string]int) {
if actual == nil {
t.Errorf("actual map was nil")
}
if len(expected) != len(actual) {
t.Errorf("actual map was not the same length as the expected map")
}
for k, v := range expected {
got, ok := actual[k]
if !ok {
t.Errorf("actual map doesn't contain key \"%s\"", k)
continue
}
if v != got {
t.Errorf("actual map value \"%d\" was not equal to expected value \"%d\" in key \"%s\"", got, v, k)
continue
}
}
}
func assertErrorsEqual(t *testing.T, expected, actual error) {
if expected != actual {
t.Errorf("failed asserting that error \"%s\" is expected \"%s\"", actual, expected)
}
}
func assertMessageTypesEqual(t *testing.T, expected, actual MessageType) {
if expected != actual {
t.Errorf("failed asserting that MessageType \"%d\" is expected \"%d\"", actual, expected)
}
}
// formats a ping-signature (i.e. go-twitch-irc) into a full-fledged pong response (i.e. ":tmi.twitch.tv PONG tmi.twitch.tv :go-twitch-irc")
func formatPong(signature string) string {
return ":tmi.twitch.tv PONG tmi.twitch.tv :" + signature
}
type timedTestMessage struct {
message string
time time.Time
}
func assertJoinRateLimitRespected(t *testing.T, joinLimit int, joinMessages []timedTestMessage) {
messageBuckets := make(map[string][]timedTestMessage)
startBucketTime := joinMessages[0].time
endBucketTime := startBucketTime.Add(TwitchRateLimitWindow)
for _, msg := range joinMessages {
if !msg.time.Before(endBucketTime) {
startBucketTime = msg.time
endBucketTime = startBucketTime.Add(TwitchRateLimitWindow)
}
key := startBucketTime.Format("15:04:05") + " -> " + endBucketTime.Format("15:04:05")
messageBuckets[key] = append(messageBuckets[key], msg)
}
for key, bucket := range messageBuckets {
if len(bucket) > joinLimit {
t.Errorf("%s has %d joins", key, len(bucket))
}
}
}