-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloc.go
293 lines (265 loc) · 7.4 KB
/
dataloc.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Package dataloc provides functionality to find the source code location of
// table-driven test cases.
package dataloc
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
)
// L returns the source code location of the test case identified by its name.
// It attempts runtime source code analysis to find the location
// by using the expression passed to dataloc.L().
// So some restrictions apply:
// - The function must be invoked as "dataloc.L".
// - The argument must be an expression of the form "dataloc.L(testcase.key)"
// , where "testcase" is a variable declared as "for _, testcase := range testcases"
// , and "testcases" is a slice of a struct type
// , whose "key" field is a string which is passsed to L().
// - or "dataloc.L(key)"
// , where key is a variable declared as "for key, value := range testcases"
// , and "testcases" is a map of string to any type
// , and "key" is the string which is passed to L().
//
// See Example.
func L(name string) string {
s, _ := loc(name)
return s
}
func loc(value string) (string, error) {
_, file, line, _ := runtime.Caller(2)
cwd, err := os.Getwd()
if err != nil {
return "", err
}
file, err = filepath.Rel(cwd, file)
if err != nil {
return "", err
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, file, nil, 0)
if err != nil {
return "", err
}
// [ t ↦ expr ] for "type t struct{ ... }"
objToTypeDecl := make(map[*ast.Object]ast.Expr)
// [ v ↦ expr ] for "v := ..."
objToVarInit := make(map[*ast.Object]ast.Expr)
// [ v ↦ expr ] for "for k, v := range expr"
objToRangeExprForValue := make(map[*ast.Object]ast.Expr)
// [ k ↦ expr ] for "for k, v := range expr"
objToRangeExprForKey := make(map[*ast.Object]ast.Expr)
ast.Inspect(f, func(n ast.Node) bool {
if rangeStmt, ok := n.(*ast.RangeStmt); ok {
if ident, ok := rangeStmt.Value.(*ast.Ident); ok {
objToRangeExprForValue[ident.Obj] = rangeStmt.X
}
if ident, ok := rangeStmt.Key.(*ast.Ident); ok {
objToRangeExprForKey[ident.Obj] = rangeStmt.X
}
} else if decl, ok := n.(ast.Decl); ok {
if genDecl, ok := decl.(*ast.GenDecl); ok {
if genDecl.Tok == token.VAR {
for _, spec := range genDecl.Specs {
if valueSpec, ok := spec.(*ast.ValueSpec); ok {
for i, name := range valueSpec.Names {
if i < len(valueSpec.Values)-1 {
objToVarInit[name.Obj] = valueSpec.Values[i]
}
}
}
}
} else if genDecl.Tok == token.TYPE {
for _, spec := range genDecl.Specs {
if typeSpec, ok := spec.(*ast.TypeSpec); ok {
objToTypeDecl[typeSpec.Name.Obj] = typeSpec.Type
}
}
}
}
} else if assignStmt, ok := n.(*ast.AssignStmt); ok {
for i, expr := range assignStmt.Lhs {
if ident, ok := expr.(*ast.Ident); ok {
if len(assignStmt.Lhs) == len(assignStmt.Rhs) {
objToVarInit[ident.Obj] = assignStmt.Rhs[i]
} else if len(assignStmt.Rhs) == 1 {
objToVarInit[ident.Obj] = assignStmt.Rhs[0]
} else {
debugf("unreachable: len(assignStmt.Lhs)=%d, len(assignStmt.Rhs)=%d", len(assignStmt.Lhs), len(assignStmt.Rhs))
}
}
}
}
return true
})
loc := "(unknown)"
ast.Inspect(f, func(n ast.Node) bool {
if n == nil {
return false
}
pos := fset.Position(n.Pos())
if pos.Line != line {
return true
}
// for example:
// testcases := []struct{}{...}
// for _, testdata := range testcases {
// dataloc.L(testdata.name)
// }
if call, ok := isMethodCall(n, "dataloc", "L"); ok {
arg := call.Args[0]
// ident = testdata, key = name
if ident, key, ok := isSelector(arg); ok {
// expr = testcases
if expr, ok := objToRangeExprForValue[ident.Obj]; ok {
if testcasesIdent, ok := expr.(*ast.Ident); ok {
// testcasesExpr = []struct{}{...}
testcasesExpr := objToVarInit[testcasesIdent.Obj]
node := findTestCaseItem(testcasesExpr, key, value, objToTypeDecl)
if node != nil {
pos := fset.Position(node.Pos())
loc = fmt.Sprintf("%s:%d", pos.Filename, pos.Line)
return false
}
}
}
} else if ident, ok := arg.(*ast.Ident); ok {
// for k, v := range testcases {
// dataloc.L(k)
// }
if expr, ok := objToRangeExprForKey[ident.Obj]; ok {
if testcasesIdent, ok := expr.(*ast.Ident); ok {
testcasesExpr := objToVarInit[testcasesIdent.Obj]
node := findTestCaseItem(testcasesExpr, ident.Name, value, objToTypeDecl)
if node != nil {
pos := fset.Position(node.Pos())
loc = fmt.Sprintf("%s:%d", pos.Filename, pos.Line)
return false
}
}
}
}
}
return true
})
return loc, nil
}
func isMethodCall(n ast.Node, obj, fun string) (*ast.CallExpr, bool) {
if call, ok := n.(*ast.CallExpr); ok {
if ident, name, ok := isSelector(call.Fun); ok {
if ident.Name == obj && name == fun {
return call, true
}
}
}
return nil, false
}
func isSelector(n ast.Node) (*ast.Ident, string, bool) {
if sel, ok := n.(*ast.SelectorExpr); ok {
if ident, ok := sel.X.(*ast.Ident); ok {
return ident, sel.Sel.Name, true
}
}
return nil, "", false
}
func findTestCaseItem(init ast.Expr, key, value string, objToTypeDecl map[*ast.Object]ast.Expr) ast.Node {
testcases, ok := init.(*ast.CompositeLit)
if !ok {
return nil
}
var testcaseType ast.Expr
if t, ok := testcases.Type.(*ast.ArrayType); ok {
testcaseType = t.Elt
if ident, ok := testcaseType.(*ast.Ident); ok {
testcaseType = objToTypeDecl[ident.Obj]
if testcaseType == nil {
logf("could not resolve type of %s", ident.Name)
return nil
}
}
} else if m, ok := testcases.Type.(*ast.MapType); ok {
testcaseType = m
} else {
// testcases should be an array eg.
// testcases := []testcase{ ... }
// or a map eg.
// testcases := map[string]testcase{ ... }
debugf("unexpected testcase type: %#v", testcases.Type)
return nil
}
for _, testcase := range testcases.Elts {
if kv, ok := testcase.(*ast.KeyValueExpr); ok {
if basic, ok := kv.Key.(*ast.BasicLit); ok {
if isStringLiteral(basic, value) {
return kv
}
}
}
testcase, ok := testcase.(*ast.CompositeLit)
if !ok {
// testcase should be a struct literal eg.
// { name: "foo", ... }
// or
// { "foo", ... }
continue
}
for i, field := range testcase.Elts {
if kv, ok := field.(*ast.KeyValueExpr); ok {
// { <key>: <value>, ... }
if ident, ok := kv.Key.(*ast.Ident); ok {
if ident.Name == key {
if isStringLiteral(kv.Value, value) {
return testcase
}
}
}
} else if basic, ok := field.(*ast.BasicLit); ok {
// { <value>, ...}
if findStructFieldIndex(testcaseType, key) == i {
if isStringLiteral(basic, value) {
return testcase
}
}
}
}
}
return nil
}
func isStringLiteral(n ast.Expr, s string) bool {
lit, ok := n.(*ast.BasicLit)
if !ok {
return false
}
if lit.Kind != token.STRING {
return false
}
return lit.Value == strconv.Quote(s)
}
func findStructFieldIndex(t ast.Expr, name string) int {
typ, ok := t.(*ast.StructType)
if !ok {
return -1
}
for i, field := range typ.Fields.List {
for _, ident := range field.Names {
if ident.Name == name {
return i
}
}
}
return -1
}
func logf(format string, args ...interface{}) {
log.Printf(format, args...)
}
const debug = false
func debugf(format string, args ...interface{}) {
if debug {
log.Printf("debug: "+format, args...)
}
}