This repository has been archived by the owner on Dec 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
132 lines (101 loc) · 3.21 KB
/
plugin_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
package drouter_test
import (
"github.com/NyanKiyoshi/disgord-plugin-router"
"github.com/stretchr/testify/assert"
"testing"
)
func TestPlugin_Use(t *testing.T) {
// Create a testing plugin
plugin := createTestPlugin()
// Create the dummy callback
callback := func(ctx *drouter.Context) error {
return successError
}
// Ensure there are not wrappers on an empty plugin
assert.Empty(t, plugin.RootCommand.Wrappers)
// Add the callback to the module
plugin.Use(callback)
// Ensure it was added
assert.Len(t, plugin.RootCommand.Wrappers, 1)
assert.Equal(t, successError, plugin.RootCommand.Wrappers[0](nil))
}
func TestPlugin_SetPrefix(t *testing.T) {
// Create a testing plugin
plugin := createTestPlugin()
// Ensure the default prefix is used by default
assert.Equal(t, drouter.DefaultPrefix, plugin.Prefix)
// Set a new prefix
plugin.SetPrefix("??")
// Check if the prefix was correctly set
assert.Equal(t, "??", plugin.Prefix)
}
func TestPlugin_Handler(t *testing.T) {
// Create a testing plugin and a dummy handler
plugin := createTestPlugin()
handler := func(ctx *drouter.Context) error {
return successError
}
// Ensure no handler is registered by default
assert.Nil(t, plugin.RootCommand.HandlerFunc)
// Register an handler to the plugin
plugin.Handler(handler)
// Check it was correctly set
assert.NotNil(t, plugin.RootCommand.HandlerFunc)
assert.Equal(t, successError, plugin.RootCommand.HandlerFunc(nil))
}
func TestPlugin_On(t *testing.T) {
// Create a testing plugin and a dummy handler
plugin := createTestPlugin()
// Dummy handlers
handler1 := struct {
Handler1 bool
}{}
handler2 := struct {
Handler2 bool
}{}
// Add a "hello" event
plugin.On("hello", handler1)
// Ensure it was correctly set
handlers := plugin.Listeners["hello"]
assert.NotNil(t, handlers)
assert.Len(t, handlers, 1)
assert.Equal(t, handler1, handlers[0])
// Append another "hello" event
plugin.On("hello", handler2)
// Ensure it was appended
handlers = plugin.Listeners["hello"]
assert.Len(t, handlers, 2)
assert.Equal(t, handler2, handlers[1])
// Append an inexisting event
plugin.On("notfound", handler1)
// Ensure it was appended
handlers = plugin.Listeners["notfound"]
assert.Len(t, handlers, 1)
assert.Equal(t, handler1, handlers[0])
}
func TestPlugin_Command(t *testing.T) {
// Create a testing plugin
plugin := createTestPlugin()
// Ensure no commands are registered on empty plugin
assert.Len(t, plugin.Commands, 0)
// Register a new command
cmd := plugin.Command("color", "colour")
// Check if the command was correctly registered
assert.EqualValues(t, []*drouter.Command{cmd}, plugin.Commands)
// Check if the registered command is correct
assert.ElementsMatch(t, []string{"color", "colour"}, cmd.Names.Keys())
assert.Nil(t, cmd.HandlerFunc)
assert.Empty(t, cmd.Wrappers)
}
func TestPlugin_Help(t *testing.T) {
// Create a testing plugin
plugin := createTestPlugin()
// Ensure no help is set by default
assert.Empty(t, plugin.RootCommand.ShortHelp)
assert.Empty(t, plugin.RootCommand.LongHelp)
// Set custom help
plugin.Help("hello\nworld")
// Check help is set
assert.Equal(t, "hello", plugin.RootCommand.ShortHelp)
assert.Equal(t, "hello\nworld", plugin.RootCommand.LongHelp)
}