forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_test.go
200 lines (167 loc) · 5.06 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
package iris
/*
Contains tests for plugin, no end-to-end, just local-object tests, these are enoguh for now.
CONTRIBUTE & DISCUSSION ABOUT TESTS TO: https://github.com/iris-contrib/tests
*/
import (
"fmt"
"testing"
)
const (
testPluginExDescription = "Description for My test plugin"
testPluginExName = "My test plugin"
)
type testPluginEx struct {
named, activated, descriptioned bool
prelistenran, postlistenran, precloseran bool
}
func (t *testPluginEx) GetName() string {
fmt.Println("GetName Struct")
t.named = true
return testPluginExName
}
func (t *testPluginEx) GetDescription() string {
fmt.Println("GetDescription Struct")
t.descriptioned = true
return testPluginExDescription
}
func (t *testPluginEx) Activate(p PluginContainer) error {
fmt.Println("Activate Struct")
t.activated = true
return nil
}
func (t *testPluginEx) PreListen(*Framework) {
fmt.Println("PreListen Struct")
t.prelistenran = true
}
func (t *testPluginEx) PostListen(*Framework) {
fmt.Println("PostListen Struct")
t.postlistenran = true
}
func (t *testPluginEx) PreClose(*Framework) {
fmt.Println("PreClose Struct")
t.precloseran = true
}
func ExamplePlugins_Add() {
initDefault()
Plugins.Add(PreListenFunc(func(*Framework) {
fmt.Println("PreListen Func")
}))
Plugins.Add(PostListenFunc(func(*Framework) {
fmt.Println("PostListen Func")
}))
Plugins.Add(PreCloseFunc(func(*Framework) {
fmt.Println("PreClose Func")
}))
myplugin := &testPluginEx{}
Plugins.Add(myplugin)
desc := Plugins.GetDescription(myplugin)
fmt.Println(desc)
ListenVirtual()
Close()
// Output:
// GetName Struct
// Activate Struct
// GetDescription Struct
// Description for My test plugin
// PreListen Func
// PreListen Struct
// PostListen Func
// PostListen Struct
// PreClose Func
// PreClose Struct
}
// if a plugin has GetName, then it should be registered only one time, the name exists for that reason, it's like unique ID
func TestPluginDublicateName(t *testing.T) {
var plugins pluginContainer
firstNamedPlugin := &testPluginEx{}
sameNamedPlugin := &testPluginEx{}
// err := plugins.Add(firstNamedPlugin, sameNamedPlugin) or
err := plugins.Add(firstNamedPlugin)
if err != nil {
t.Fatalf("Unexpected error when adding a plugin with name: %s", testPluginExName)
}
err = plugins.Add(sameNamedPlugin)
if err == nil {
t.Fatalf("Expected an error because of dublicate named plugin!")
}
if len(plugins.activatedPlugins) != 1 {
t.Fatalf("Expected: %d activated plugin but we got: %d", 1, len(plugins.activatedPlugins))
}
}
type testPluginActivationType struct {
shouldError bool
}
func (t testPluginActivationType) Activate(p PluginContainer) error {
p.Add(&testPluginEx{})
if t.shouldError {
return fmt.Errorf("An error happens, this plugin and the added plugins by this plugin should not be registered")
}
return nil
}
func TestPluginActivate(t *testing.T) {
var plugins pluginContainer
myplugin := testPluginActivationType{shouldError: false}
plugins.Add(myplugin)
if len(plugins.activatedPlugins) != 2 { // 2 because it registeres a second plugin also
t.Fatalf("Expected activated plugins to be: %d but we got: %d", 0, len(plugins.activatedPlugins))
}
}
// if any error returned from the Activate plugin's method, then this plugin and the plugins it registers should not be registered at all
func TestPluginActivationError(t *testing.T) {
var plugins pluginContainer
myplugin := testPluginActivationType{shouldError: true}
plugins.Add(myplugin)
if len(plugins.activatedPlugins) > 0 {
t.Fatalf("Expected activated plugins to be: %d but we got: %d", 0, len(plugins.activatedPlugins))
}
}
func TestPluginEvents(t *testing.T) {
var plugins pluginContainer
var prelistenran, postlistenran, precloseran bool
plugins.Add(PreListenFunc(func(*Framework) {
prelistenran = true
}))
plugins.Add(PostListenFunc(func(*Framework) {
postlistenran = true
}))
plugins.Add(PreCloseFunc(func(*Framework) {
precloseran = true
}))
myplugin := &testPluginEx{}
plugins.Add(myplugin)
if len(plugins.activatedPlugins) != 4 {
t.Fatalf("Expected: %d plugins to be registed but we got: %d", 4, len(plugins.activatedPlugins))
}
desc := plugins.GetDescription(myplugin)
if desc != testPluginExDescription {
t.Fatalf("Expected: %s as Description of the plugin but got: %s", testPluginExDescription, desc)
}
plugins.DoPreListen(nil)
plugins.DoPostListen(nil)
plugins.DoPreClose(nil)
if !prelistenran {
t.Fatalf("Expected to run PreListen Func but it doesnt!")
}
if !postlistenran {
t.Fatalf("Expected to run PostListen Func but it doesnt!")
}
if !precloseran {
t.Fatalf("Expected to run PostListen Func but it doesnt!")
}
if !myplugin.named {
t.Fatalf("Plugin should be named with: %s!", testPluginExName)
}
if !myplugin.activated {
t.Fatalf("Plugin should be activated but it's not!")
}
if !myplugin.prelistenran {
t.Fatalf("Expected to run PreListen Struct but it doesnt!")
}
if !myplugin.postlistenran {
t.Fatalf("Expected to run PostListen Struct but it doesnt!")
}
if !myplugin.precloseran {
t.Fatalf("Expected to run PostListen Struct but it doesnt!")
}
}