-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry_test.go
More file actions
117 lines (94 loc) · 3.75 KB
/
registry_test.go
File metadata and controls
117 lines (94 loc) · 3.75 KB
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
package markers
import (
"github.com/stretchr/testify/assert"
"testing"
)
type testMarker struct {
}
type testTypeLevelMarker struct {
}
type testFunctionLevelMarker struct {
}
type testMethodFunctionLevelMarker struct {
}
type testStructInterfaceTypeLevelMarker struct {
}
func TestRegistry_Register(t *testing.T) {
testCases := []struct {
MarkerName string
TargetLevel TargetLevel
Output interface{}
}{
{"marker:type-level", TypeLevel, &testTypeLevelMarker{}},
{"marker:function-level", FunctionLevel, &testFunctionLevelMarker{}},
{"marker:method-function-level", MethodLevel | FunctionLevel, &testMethodFunctionLevelMarker{}},
{"marker:struct-interface-level", StructTypeLevel | InterfaceTypeLevel, &testStructInterfaceTypeLevelMarker{}},
}
registry := NewRegistry()
for _, testCase := range testCases {
err := registry.Register(testCase.MarkerName, "anyPkg", testCase.TargetLevel, testCase.Output)
assert.Nil(t, err)
definition, ok := registry.packageMap["anyPkg"][testCase.MarkerName]
if !ok {
t.Error("marker has not been registered successfully")
}
if definition.Name != testCase.MarkerName {
t.Errorf("marker name is not equal to expected, got %q; want %q", definition.Name, testCase.MarkerName)
}
if definition.TargetLevel != testCase.TargetLevel {
t.Errorf("target level is not equal to expected, got %q; want %q", definition.TargetLevel, testCase.TargetLevel)
}
}
assert.Len(t, registry.packageMap["anyPkg"], len(testCases))
}
func TestRegistry_RegisterWithDefinition(t *testing.T) {
testCases := []struct {
MarkerName string
TargetLevel TargetLevel
Output interface{}
}{
{"marker:type-level", TypeLevel, &testTypeLevelMarker{}},
{"marker:function-level", FunctionLevel, &testFunctionLevelMarker{}},
{"marker:method-function-level", MethodLevel | FunctionLevel, &testMethodFunctionLevelMarker{}},
{"marker:struct-interface-level", StructTypeLevel | InterfaceTypeLevel, &testStructInterfaceTypeLevelMarker{}},
}
registry := NewRegistry()
for _, testCase := range testCases {
newDefinition, _ := MakeDefinition(testCase.MarkerName, "anyPkg", testCase.TargetLevel, testCase.Output)
err := registry.RegisterWithDefinition(newDefinition)
assert.Nil(t, err)
definition, ok := registry.packageMap["anyPkg"][testCase.MarkerName]
if !ok {
t.Error("marker has not been registered successfully")
}
if definition.Name != testCase.MarkerName {
t.Errorf("marker name is not equal to expected, got %q; want %q", definition.Name, testCase.MarkerName)
}
if definition.TargetLevel != testCase.TargetLevel {
t.Errorf("target level is not equal to expected, got %q; want %q", definition.TargetLevel, testCase.TargetLevel)
}
}
assert.Len(t, registry.packageMap["anyPkg"], len(testCases))
}
func TestRegistry_RegisterMarkerAlreadyRegistered(t *testing.T) {
registry := NewRegistry()
registry.Register("marker:test", "anyPkg", TypeLevel, &testTypeLevelMarker{})
err := registry.Register("marker:test", "anyPkg", MethodLevel, &testTypeLevelMarker{})
assert.Len(t, registry.packageMap["anyPkg"], 1)
assert.NotNil(t, err)
assert.Equal(t, "there is already registered definition : marker:test", err.Error())
}
func TestRegistry_RegisterMarkerWithEmptyName(t *testing.T) {
registry := NewRegistry()
err := registry.Register("", "anyPkg", MethodLevel, &testMarker{})
assert.Len(t, registry.packageMap["anyPkg"], 0)
assert.NotNil(t, err)
assert.Equal(t, "marker name cannot be empty", err.Error())
}
func TestRegistry_RegisterMarkerWithoutLevel(t *testing.T) {
registry := NewRegistry()
err := registry.Register("marker:test", "anyPkg", 0, &testMarker{})
assert.Len(t, registry.packageMap["anyPkg"], 0)
assert.NotNil(t, err)
assert.Equal(t, "specify target levels for the definition: marker:test", err.Error())
}