-
Notifications
You must be signed in to change notification settings - Fork 0
/
astutil.go
145 lines (130 loc) · 2.94 KB
/
astutil.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
package gocc
import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"unicode"
)
type Suite struct {
File string
Pkg string
PkgPath string
Units []string
}
func NewSuite(file string) (*Suite, error) {
path, err := filepath.Abs(file)
if err != nil {
return nil, err
}
gopath := filepath.Join(os.Getenv("GOPATH"), "src") + string(filepath.Separator)
pkgpath := strings.TrimPrefix(filepath.Dir(path), gopath)
fset := token.NewFileSet()
code, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
return nil, err
}
return &Suite{
File: file,
Pkg: code.Name.Name,
PkgPath: pkgpath,
Units: astLookupUnitTest(code),
}, nil
}
//
// Parser Golang source
//
func astLookupUnitTest(node *ast.File) (units []string) {
ast.Inspect(node, func(n ast.Node) bool {
if f := astUnitTestFunc(n); f != nil {
units = append(units, f.Name.Name)
}
return true
})
return
}
func astUnitTestFunc(node ast.Node) *ast.FuncDecl {
switch f := node.(type) {
case *ast.FuncDecl:
if !unicode.IsUpper([]rune(f.Name.Name)[0]) || f.Type.Results == nil || len(f.Type.Params.List) != 0 || len(f.Type.Results.List) != 1 {
return nil
}
if !strings.HasPrefix(f.Name.Name, "Test") {
return nil
}
switch t := f.Type.Results.List[0].Type.(type) {
case *ast.SelectorExpr:
switch l := t.X.(type) {
case *ast.Ident:
if l.Name == "http" && t.Sel.Name == "Arrow" {
return f
}
}
}
}
return nil
}
//
// Modify golang source
//
func astReWrite(node *ast.File, tests []*Suite) {
ast.Inspect(node, func(node ast.Node) bool {
switch f := node.(type) {
case *ast.FuncDecl:
if f.Name.Name == "main" {
f.Body = astMain(tests)
}
}
return true
})
}
func astMain(tests []*Suite) *ast.BlockStmt {
args := []ast.Expr{
&ast.SelectorExpr{
X: ast.NewIdent("os"),
Sel: ast.NewIdent("Stdout"),
},
&ast.CallExpr{
Fun: &ast.SelectorExpr{X: ast.NewIdent("http"), Sel: ast.NewIdent("New")},
Args: []ast.Expr{
&ast.CallExpr{
Fun: &ast.SelectorExpr{X: ast.NewIdent("http"), Sel: ast.NewIdent("WithMemento")},
Args: []ast.Expr{},
},
&ast.CallExpr{
Fun: &ast.SelectorExpr{X: ast.NewIdent("http"), Sel: ast.NewIdent("WithDefaultHost")},
Args: []ast.Expr{
&ast.IndexExpr{
X: &ast.SelectorExpr{X: ast.NewIdent("os"), Sel: ast.NewIdent("Args")},
Index: &ast.BasicLit{
Kind: token.INT,
Value: "1",
},
},
},
},
},
},
}
for _, test := range tests {
for _, unit := range test.Units {
if test.Pkg == "main" {
args = append(args, ast.NewIdent(unit))
} else {
args = append(args, &ast.SelectorExpr{X: ast.NewIdent(test.Pkg), Sel: ast.NewIdent(unit)})
}
}
}
return &ast.BlockStmt{
List: []ast.Stmt{
&ast.ExprStmt{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{X: ast.NewIdent("http"), Sel: ast.NewIdent("WriteOnce")},
Args: args,
},
},
},
}
}