-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfunc.go
217 lines (182 loc) · 4.57 KB
/
func.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package gounit
import (
"fmt"
"go/ast"
"go/token"
)
//Func is a wrapper around ast.FuncDecl containing few methods
//to use within a test template
type Func struct {
Signature *ast.FuncDecl
}
//NewFunc returns pointer to the Func struct
func NewFunc(sig *ast.FuncDecl) *Func {
return &Func{Signature: sig}
}
//NumParams returns a number of the function params
func (f *Func) NumParams() int {
return f.Signature.Type.Params.NumFields()
}
//NumResults returns a number of the function results
func (f *Func) NumResults() int {
if f.Signature.Type.Results == nil {
return 0
}
return f.Signature.Type.Results.NumFields()
}
//Params returns a list of the function params with their types
func (f *Func) Params(fs *token.FileSet) []string {
if f.Signature.Type.Params == nil {
return nil
}
params := []string{}
for i, p := range f.Signature.Type.Params.List {
for _, n := range p.Names {
param := n.Name
if i == len(f.Signature.Type.Params.List)-1 && f.IsVariadic() {
param += " []" + nodeToString(fs, p.Type.(*ast.Ellipsis).Elt)
} else {
param += " " + nodeToString(fs, p.Type)
}
params = append(params, param)
}
}
return params
}
//Results returns a list of the function results with their types
//if function's last param is an error it is not included in the result slice
func (f *Func) Results(fs *token.FileSet) []string {
if f.Signature.Type.Results == nil {
return nil
}
var (
results []string
n = 1
)
for _, r := range f.Signature.Type.Results.List {
if len(r.Names) > 0 {
for range r.Names {
results = append(results, fmt.Sprintf("got%d %s", n, nodeToString(fs, r.Type)))
n++
}
} else {
results = append(results, fmt.Sprintf("got%d %s", n, nodeToString(fs, r.Type)))
n++
}
}
if f.ReturnsError() {
results = results[:len(results)-1]
}
return results
}
//ParamsNames returns a list of the function params' names
func (f *Func) ParamsNames() []string {
if f.Signature.Type.Params == nil {
return nil
}
names := []string{}
for i, p := range f.Signature.Type.Params.List {
for k, n := range p.Names {
name := n.Name
if i == len(f.Signature.Type.Params.List)-1 && k == len(p.Names)-1 && f.IsVariadic() {
name += "..."
}
names = append(names, name)
}
}
return names
}
//ResultsNames returns a list of the function results' names
//if function's last result is an error the name of param is "err"
func (f *Func) ResultsNames() []string {
if f.Signature.Type.Results == nil {
return nil
}
var (
names []string
n = 1
)
for _, r := range f.Signature.Type.Results.List {
if len(r.Names) > 0 {
for range r.Names {
names = append(names, fmt.Sprintf("got%d", n))
n++
}
} else {
names = append(names, fmt.Sprintf("got%d", n))
n++
}
}
if f.ReturnsError() {
names[len(names)-1] = "err"
}
return names
}
//Name returns a name of func
func (f *Func) Name() string {
return f.Signature.Name.String()
}
//TestName returns a name of the test
func (f *Func) TestName() string {
name := "Test"
if f.IsMethod() {
recvType := f.ReceiverType()
if star, ok := recvType.(*ast.StarExpr); ok {
name += star.X.(*ast.Ident).String()
} else {
name += recvType.(*ast.Ident).String()
}
name += "_"
} else if !f.Signature.Name.IsExported() {
name += "_"
}
return name + f.Signature.Name.String()
}
//IsMethod returns true if the function is a method
func (f *Func) IsMethod() bool {
return f.Signature.Recv != nil
}
//ReceiverType returns a type of the method receiver
func (f *Func) ReceiverType() ast.Expr {
if f.Signature.Recv == nil {
return nil
}
return f.Signature.Recv.List[0].Type
}
//ReturnsError returns true if the function's last param's type is error
func (f *Func) ReturnsError() bool {
lastResult := f.LastResult()
if lastResult == nil {
return false
}
ident, ok := lastResult.Type.(*ast.Ident)
return ok && ident.Name == "error"
}
//LastParam returns function's last param
func (f *Func) LastParam() *ast.Field {
numFields := len(f.Signature.Type.Params.List)
if numFields == 0 {
return nil
}
return f.Signature.Type.Params.List[numFields-1]
}
//LastResult returns function's last result
func (f *Func) LastResult() *ast.Field {
if f.Signature.Type.Results == nil {
return nil
}
numFields := len(f.Signature.Type.Results.List)
if numFields == 0 {
return nil
}
return f.Signature.Type.Results.List[numFields-1]
}
//IsVariadic returns true if it's the variadic function
func (f *Func) IsVariadic() bool {
lastParam := f.LastParam()
if lastParam == nil {
return false
}
_, isVariadic := lastParam.Type.(*ast.Ellipsis)
return isVariadic
}