/
defs.go
138 lines (117 loc) · 2.52 KB
/
defs.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
package sweet
import (
"fmt"
"reflect"
"testing"
)
func formatName(suiteName string, funcName string) string {
return fmt.Sprintf("%s/%s", suiteName, funcName)
}
type funcDef struct {
Name string
ParamSets []*paramSet
}
func newFuncDef(name string, paramSets ...*paramSet) *funcDef {
fd := &funcDef{
Name: name,
ParamSets: make([]*paramSet, 0),
}
for _, set := range paramSets {
fd.ParamSets = append(fd.ParamSets, set)
}
return fd
}
func (fd *funcDef) Validate(funcVal reflect.Value) (int, error) {
if !funcVal.IsValid() || funcVal.Kind() != reflect.Func {
return 0, errInvalidValue
}
typ := funcVal.Type()
for _, set := range fd.ParamSets {
// Make sure the number of params match
if typ.NumIn() != len(set.Params) {
continue
}
// Make sure the params themselves match
fullMatch := true
for idx := 0; idx < typ.NumIn(); idx++ {
if set.Params[idx].Type != typ.In(idx) {
fullMatch = false
break
}
}
if fullMatch {
if set.Deprecated {
return set.Version, errDeprecated
}
return set.Version, nil
}
}
return 0, errUnsupportedMethod
}
type paramSet struct {
Params []*paramDef
Deprecated bool
Version int
}
func newParamSet(version int, deprecated bool, params ...*paramDef) *paramSet {
ps := ¶mSet{
Params: make([]*paramDef, 0),
Deprecated: deprecated,
Version: version,
}
for _, param := range params {
ps.Params = append(ps.Params, param)
}
return ps
}
type paramDef struct {
Type reflect.Type
}
func newParamDef(typ reflect.Type) *paramDef {
return ¶mDef{Type: typ}
}
var (
defSetUpAllTests = newFuncDef(
"SetUpAllTests",
newParamSet(1, false), // No params
)
defTearDownAllTests = newFuncDef(
"TearDownAllTests",
newParamSet(1, false), // No params
)
defSetUpSuite = newFuncDef(
"SetUpSuite",
newParamSet(1, false), // No params
)
defTearDownSuite = newFuncDef(
"TearDownSuite",
newParamSet(1, false), // No params
)
defSetUpTest = newFuncDef(
"SetUpTest",
newParamSet(1, true,
newParamDef(reflect.TypeOf(&testing.T{})),
),
newParamSet(2, false,
newParamDef(reflect.TypeOf((*T)(nil)).Elem()),
),
)
defTearDownTest = newFuncDef(
"TearDownTest",
newParamSet(1, true,
newParamDef(reflect.TypeOf(&testing.T{})),
),
newParamSet(2, false,
newParamDef(reflect.TypeOf((*T)(nil)).Elem()),
),
)
defTest = newFuncDef(
"Test",
newParamSet(1, true,
newParamDef(reflect.TypeOf(&testing.T{})),
),
newParamSet(2, false,
newParamDef(reflect.TypeOf((*T)(nil)).Elem()),
),
)
)