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
/
router_test.go
187 lines (150 loc) · 5.16 KB
/
router_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
186
187
package drouter_test
import (
"fmt"
"github.com/NyanKiyoshi/disgord-plugin-router"
"github.com/NyanKiyoshi/disgord-plugin-router/mocks/mocked_disgord"
"github.com/andersfylling/disgord"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"testing"
)
type _myModuleInternalType struct{}
func createTestRouter() *drouter.RouterDefinition {
return &drouter.RouterDefinition{}
}
func createTestPlugin() *drouter.Plugin {
return createTestRouter().Plugin(_myModuleInternalType{}, "my-module")
}
func ExampleNew_newPlugin() {
myPlugin := drouter.Router.Plugin(_myModuleInternalType{}, "my-plugin")
fmt.Printf("ImportName: %s\nNames: %s", myPlugin.ImportName, myPlugin.RootCommand.Names.Keys())
// Output:
// ImportName: github.com/NyanKiyoshi/disgord-plugin-router_test
// Names: [my-plugin]
}
func TestNew(t *testing.T) {
router := createTestRouter()
assert.Len(t, router.Plugins, 0)
}
func TestRouterDefinition_Plugin(t *testing.T) {
router := createTestRouter()
// Ensure there are no plugins by default
assert.Empty(t, router.Plugins)
// Register a new plugin
plugin := router.Plugin(_myModuleInternalType{})
// Ensure the plugin was registerd
assert.NotEmpty(t, router.Plugins)
assert.NotNil(t, router.Plugins[0])
assert.Equal(t, plugin, router.Plugins[0])
}
func TestRouterDefinition_ShouldUse(t *testing.T) {
// Create dummy router
router := createTestRouter()
// Ensure there are not matcher by default
assert.Len(t, router.ShouldEnablePluginFuncs, 0)
// Add dummy matcher
router.ShouldUse(func(plugin *drouter.Plugin) bool {
return true
})
// Ensure the matcher was added as expected
assert.Len(t, router.ShouldEnablePluginFuncs, 1)
assert.True(t, router.ShouldEnablePluginFuncs[0](nil))
}
func TestRouterDefinition_ShouldNotUseRE(t *testing.T) {
// Create dummy router and test plugins
router := createTestRouter()
enabledPlugin := &drouter.Plugin{ImportName: "enabled!!"}
disabledPlugin := &drouter.Plugin{ImportName: "peach"}
// Ensure there are not matcher by default
assert.Len(t, router.ShouldEnablePluginFuncs, 0)
// Add dummy matcher
router.ShouldNotUseRE("^peach$")
// Ensure the matcher was added and is behaving as expected
assert.Len(t, router.ShouldEnablePluginFuncs, 1)
assert.True(t, router.ShouldEnablePluginFuncs[0](enabledPlugin))
assert.False(t, router.ShouldEnablePluginFuncs[0](disabledPlugin))
}
// Test .Configure(...) without registering any plugin to configure.
func TestRouterDefinition_Configure_OnlyInstallInternalEvents(t *testing.T) {
// Backup log.Fatal before mocking it
oldLogFatal := drouter.LogFatalf
defer func() { drouter.LogFatalf = oldLogFatal }()
runTest := func(subT *testing.T, customErr error) {
// Mock log.Fatal
var receivedLog string
drouter.LogFatalf = func(format string, v ...interface{}) {
receivedLog = fmt.Sprintf(format, v...)
}
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedClient := mocked_disgord.NewMockrouterClient(mockCtrl)
mockedClient.
EXPECT().
On(disgord.EventMessageCreate, gomock.Any()).
Return(customErr)
router := createTestRouter()
router.Configure(mockedClient)
// Check if log.Fatal was called if an error was set
if customErr != nil {
assert.Equal(
subT,
"failed to register router's internal MessageCreate event: success",
receivedLog,
)
}
}
t.Run("with proper configuration", func(t *testing.T) {
runTest(t, nil)
})
t.Run("with errored configuration", func(t *testing.T) {
runTest(t, successError)
})
}
func TestRouterDefinition_Configure_WithRegisteredPlugins(t *testing.T) {
// Backup log.Fatal before mocking it
oldLogFatal := drouter.LogFatalf
defer func() { drouter.LogFatalf = oldLogFatal }()
router := createTestRouter().ShouldUse(func(plugin *drouter.Plugin) bool {
return plugin.Prefix != "disabled"
})
plugin := router.Plugin(_myModuleInternalType{}, "ping").
On(disgord.EventMessageCreate, nil) // Register a dummy event
disabledPlugin := router.Plugin(_myModuleInternalType{}).SetPrefix("disabled")
// Ensure it is false by default
assert.False(t, plugin.IsReady)
runTest := func(subT *testing.T, customErr error) {
// Mock log.Fatal
var receivedLog string
drouter.LogFatalf = func(format string, v ...interface{}) {
receivedLog = fmt.Sprintf(format, v...)
}
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedClient := mocked_disgord.NewMockrouterClient(mockCtrl)
mockedClient.
EXPECT().
On(disgord.EventMessageCreate, gomock.Any()).
Return(customErr).
Times(2) // should be called twice: internal event & dummy custom event
// Run configure
router.Configure(mockedClient)
// Ensure the plugin was enabled and the disabled one was ignore
assert.True(t, plugin.IsReady)
assert.False(t, disabledPlugin.IsReady)
// Check if log.Fatal was called if an error was set
if customErr != nil {
assert.Equal(
subT,
"failed to register event MESSAGE_CREATE "+
"for plugin github.com/NyanKiyoshi/disgord-plugin-router_test: success",
receivedLog,
)
}
}
t.Run("with proper event", func(t *testing.T) {
runTest(t, nil)
})
t.Run("with invalid event error", func(t *testing.T) {
runTest(t, successError)
})
}