-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottom_test.go
163 lines (137 loc) · 3.47 KB
/
bottom_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
package bottom
import (
"fmt"
"testing"
"time"
"github.com/lrstanley/girc"
)
func TestNew(t *testing.T) {
for _, test := range []struct {
name string
server string
expectError bool
}{
{"happy path", "ircs://irc.example.com:6697", false},
{"empty server", "", true},
{"garbage server", "\b\b////b\b\b\b///:::/", true},
{"missing port", "irc://irc.example.com", true},
} {
t.Run(test.name, func(t *testing.T) {
b, err := New("", "", test.server, true)
if err == nil && test.expectError {
t.Fatalf("expected error")
}
if err != nil && !test.expectError {
t.Fatalf("unexpected error: %+v", err)
}
// There's no point continuing- it's all going to be empty
if test.expectError {
return
}
t.Run("Middlewares", func(t *testing.T) {
if b.Middlewares == nil {
t.Errorf("No middlewares set")
}
})
t.Run("Client", func(t *testing.T) {
if b.Client == nil {
t.Errorf("No middlewares set")
}
})
t.Run("ErrorFunc", func(t *testing.T) {
if b.ErrorFunc == nil {
t.Errorf("No middlewares set")
}
})
})
}
}
func TestBottom_Privmsg(t *testing.T) {
var (
count int
sender string
channel string
msg string
)
b, _ := New("", "", "ircs://irc.example.com:6697", true)
r := NewRouter()
r.AddRoute("(i?)PATTERN", func(s, c string, m []string) error {
sender = s
channel = c
msg = m[0]
count++
return fmt.Errorf("an error")
})
b.Middlewares.Push(r)
if b.Middlewares == nil || len(*b.Middlewares) != 1 {
t.Errorf("middlewares should exist, and contain one thing: %v", b.Middlewares)
}
t.Run("invocation count", func(t *testing.T) {
b.privmsg(nil, girc.Event{
Source: &girc.Source{Name: "#testing"},
Command: "PRIVMSG",
Params: []string{"#testing", "PATTERN"},
Timestamp: time.Now(),
})
if count != 1 {
t.Errorf("expected 1, received %d", count)
}
})
t.Run("sender name", func(t *testing.T) {
b.privmsg(nil, girc.Event{
Source: &girc.Source{Name: "test-user"},
Command: "PRIVMSG",
Params: []string{"#testing", "PATTERN"},
Timestamp: time.Now(),
})
if sender != "test-user" {
t.Errorf("expected %q, received %q", "test-user", sender)
}
})
t.Run("channel name", func(t *testing.T) {
b.privmsg(nil, girc.Event{
Source: &girc.Source{Name: "test-user"},
Command: "PRIVMSG",
Params: []string{"#testing", "PATTERN"},
Timestamp: time.Now(),
})
if channel != "#testing" {
t.Errorf("expected %q, received %q", "#testing", sender)
}
})
t.Run("message content", func(t *testing.T) {
b.privmsg(nil, girc.Event{
Source: &girc.Source{Name: "test-user"},
Command: "PRIVMSG",
Params: []string{"#testing", "PATTERN"},
Timestamp: time.Now(),
})
if msg != "PATTERN" {
t.Errorf("expected %q, received %q", "PATTERN", sender)
}
})
t.Run("too old", func(t *testing.T) {
count = 0
b.privmsg(nil, girc.Event{
Source: &girc.Source{Name: "#testing"},
Command: "PRIVMSG",
Params: []string{"#testing", "PATTERN"},
Timestamp: time.Now().Add(0 - time.Hour),
})
if count > 0 {
t.Errorf("expected 0, received %d", count)
}
})
t.Run("doesn't match pattern", func(t *testing.T) {
count = 0
b.privmsg(nil, girc.Event{
Source: &girc.Source{Name: "#testing"},
Command: "PRIVMSG",
Params: []string{"#testing", "some other message"},
Timestamp: time.Now(),
})
if count > 0 {
t.Errorf("expected 0, received %d", count)
}
})
}