forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_test.go
110 lines (97 loc) · 3.08 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
package codegen
import (
"reflect"
"testing"
)
func TestRegisterPlugin(t *testing.T) {
var (
p1 = &plugin{name: "abc"}
p2 = &plugin{name: "def"}
pf1 = &plugin{name: "abc", first: true}
pl1 = &plugin{name: "abc", last: true}
pIns = &plugin{name: "cde"}
)
tests := []struct {
name string
existingPs []*plugin
expectedPs []*plugin
}{
{"no-plugins", []*plugin{}, []*plugin{pIns}},
{"plugins-without-first", []*plugin{p1}, []*plugin{p1, pIns}},
{"plugins-with-first", []*plugin{pf1, p2}, []*plugin{pf1, pIns, p2}},
{"plugins-with-same-name", []*plugin{pf1, pIns, p2}, []*plugin{pf1, pIns, pIns, p2}},
{"plugins-with-last", []*plugin{pf1, pl1}, []*plugin{pf1, pIns, pl1}},
{"mixed", []*plugin{pf1, p1, p2}, []*plugin{pf1, p1, pIns, p2}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
plugins = tc.existingPs
RegisterPlugin(pIns.name, "", nil, nil)
if !reflect.DeepEqual(plugins, tc.expectedPs) {
t.Errorf("invalid plugin registration order")
}
})
}
}
func TestRegisterPluginFirst(t *testing.T) {
var (
p1 = &plugin{name: "abc"}
p2 = &plugin{name: "def"}
pf1 = &plugin{name: "abc", first: true}
pf2 = &plugin{name: "def", first: true}
pl1 = &plugin{name: "abc", last: true}
pIns = &plugin{name: "cde", first: true}
)
tests := []struct {
name string
existingPs []*plugin
expectedPs []*plugin
}{
{"no-plugins", []*plugin{}, []*plugin{pIns}},
{"plugins-without-first", []*plugin{p1, p2}, []*plugin{pIns, p1, p2}},
{"plugins-with-first", []*plugin{pf1, pf2}, []*plugin{pf1, pIns, pf2}},
{"plugins-with-same-name", []*plugin{pf1, pIns}, []*plugin{pf1, pIns, pIns}},
{"plugins-with-last", []*plugin{pf1, pl1}, []*plugin{pf1, pIns, pl1}},
{"mixed", []*plugin{pf1, pf2, p1, p2}, []*plugin{pf1, pIns, pf2, p1, p2}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
plugins = tc.existingPs
RegisterPluginFirst(pIns.name, "", nil, nil)
if !reflect.DeepEqual(plugins, tc.expectedPs) {
t.Errorf("invalid plugin registration order")
}
})
}
}
func TestRegisterPluginLast(t *testing.T) {
var (
p1 = &plugin{name: "abc"}
p2 = &plugin{name: "def"}
pl1 = &plugin{name: "abc", last: true}
pl2 = &plugin{name: "def", last: true}
pf1 = &plugin{name: "abc", first: true}
pIns = &plugin{name: "cde", last: true}
)
tests := []struct {
name string
existingPs []*plugin
expectedPs []*plugin
}{
{"no-plugins", []*plugin{}, []*plugin{pIns}},
{"plugins-without-last", []*plugin{p1, p2}, []*plugin{p1, p2, pIns}},
{"plugins-with-last", []*plugin{pl1, pl2}, []*plugin{pl1, pIns, pl2}},
{"plugins-with-same-name", []*plugin{pl1, pIns}, []*plugin{pl1, pIns, pIns}},
{"plugins-with-first", []*plugin{pf1, pl2}, []*plugin{pf1, pIns, pl2}},
{"mixed", []*plugin{pf1, p1, p2, pl1, pl2}, []*plugin{pf1, p1, p2, pl1, pIns, pl2}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
plugins = tc.existingPs
RegisterPluginLast(pIns.name, "", nil, nil)
if !reflect.DeepEqual(plugins, tc.expectedPs) {
t.Errorf("invalid plugin registration order")
}
})
}
}