forked from belak/go-seabird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux_command_test.go
54 lines (46 loc) · 1.67 KB
/
mux_command_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
package seabird
import (
"testing"
"github.com/go-irc/irc"
"github.com/stretchr/testify/assert"
)
func TestCommandMux(t *testing.T) {
// Empty mux should still have help
mux := NewCommandMux("!")
assert.Equal(t, 1, len(mux.cmdHelp))
mh := &messageHandler{}
// Ensure simple commands can be hit
mux.Event("hello", mh.Handle, nil)
mux.HandleEvent(nil, irc.MustParseMessage(":belak PRIVMSG #hello :!hello"))
assert.Equal(t, 1, mh.count)
mux.HandleEvent(nil, irc.MustParseMessage(":belak PRIVMSG bot :!hello"))
assert.Equal(t, 2, mh.count)
// Ensure private commands don't work publicly
mux = NewCommandMux("!")
mh = &messageHandler{}
mux.Private("hello", mh.Handle, nil)
mux.HandleEvent(nil, irc.MustParseMessage(":belak PRIVMSG #hello :!hello"))
assert.Equal(t, 0, mh.count)
mux.HandleEvent(nil, irc.MustParseMessage(":belak PRIVMSG bot :!hello"))
assert.Equal(t, 1, mh.count)
// Ensure public commands don't work publicly
mux = NewCommandMux("!")
mh = &messageHandler{}
mux.Channel("hello", mh.Handle, nil)
mux.HandleEvent(nil, irc.MustParseMessage(":belak PRIVMSG #hello :!hello"))
assert.Equal(t, 1, mh.count)
mux.HandleEvent(nil, irc.MustParseMessage(":belak PRIVMSG bot :!hello"))
assert.Equal(t, 1, mh.count)
// Ensure commands are separate
mux = NewCommandMux("!")
mh = &messageHandler{}
mh2 := &messageHandler{}
mux.Event("hello1", mh.Handle, nil)
mux.Event("hello2", mh2.Handle, nil)
mux.HandleEvent(nil, irc.MustParseMessage(":belak PRIVMSG #hello :!hello1"))
assert.Equal(t, 1, mh.count)
assert.Equal(t, 0, mh2.count)
mux.HandleEvent(nil, irc.MustParseMessage(":belak PRIVMSG #hello :!hello2"))
assert.Equal(t, 1, mh.count)
assert.Equal(t, 1, mh2.count)
}