-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
156 lines (123 loc) · 3.89 KB
/
common_test.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
package astTool
import (
"go/ast"
"go/token"
"testing"
)
func TestNewFieldOfFuncType(t *testing.T) {
var input = `package miclient
type svc interface {
}`
var expect = `package miclient
type svc interface {
RoleGet()
}
`
_, _ = input, expect
h := ParseFromCode(input)
//search
searcher := Searcher{Root: h.Ast}
resultNode := searcher.FindTypeDecl("svc")
if resultNode == nil {
t.Logf("can not find typeDecl(%s)", "svc")
}
typeSpec := resultNode.Specs[0].(*ast.TypeSpec)
interfaceType := typeSpec.Type.(*ast.InterfaceType)
newfunctype := NewFuncType(nil, nil)
newField := NewFieldOfFuncType([]string{"RoleGet"}, newfunctype, nil)
fieldList := NewFieldList(newField)
//replace
interfaceType.Methods = fieldList
got := h.Output(nil)
if got != expect {
t.Errorf("\n got:%q,\n exp:%q", got, expect)
}
}
func TestNewField(t *testing.T) {
var input = `package miclient
type svc interface {
}`
var expect = `package miclient
type svc interface {
RoleGet(ctx context.Context, reqproto *partnerproto.StaffAuthFetchReqProto) (*partnerproto.StaffAuthFetchRespProto, error)
}
`
_, _ = input, expect
h := ParseFromCode(input)
//search
searcher := Searcher{Root: h.Ast}
resultNode := searcher.FindTypeDecl("svc")
if resultNode == nil {
t.Logf("can not find typeDecl(%s)", "svc")
}
typeSpec := resultNode.Specs[0].(*ast.TypeSpec)
interfaceType := typeSpec.Type.(*ast.InterfaceType)
selectExp := NewSelectExp(NewIdent("context"), NewIdent("Context"))
paramFieldCtx := NewField([]string{"ctx"}, selectExp, ExprTypeSelectorExpr, nil)
selectExp1 := NewSelectExp(NewIdent("partnerproto"), NewIdent("StaffAuthFetchReqProto"))
startExp1 := NewStarExp(selectExp1)
paramFieldReqproto := NewField([]string{"reqproto"}, startExp1, ExprTypeStartExpr, nil)
params := NewFieldList(paramFieldCtx, paramFieldReqproto)
selectExp2 := NewSelectExp(NewIdent("partnerproto"), NewIdent("StaffAuthFetchRespProto"))
startExp2 := NewStarExp(selectExp2)
resultField1 := NewField([]string{""}, startExp2, ExprTypeStartExpr, nil)
resultField2 := NewField([]string{""}, NewIdent("error"), ExprTypeIdent, nil)
results := NewFieldList(resultField1, resultField2)
newfunctype := NewFuncType(params, results)
newField := NewFieldOfFuncType([]string{"RoleGet"}, newfunctype, nil)
fieldList := NewFieldList(newField)
//replace
interfaceType.Methods = fieldList
got := h.Output(nil)
if got != expect {
t.Errorf("\n got:%q,\n exp:%q", got, expect)
}
}
// todo
func TestNewEmptyStmt(t *testing.T) {
srcode := `package miclient
var ppx int
func testFoo(){}`
h := ParseFromCode(srcode)
wantedCode := `package miclient
var ppx int
func testFoo() { CALLfoo() }
`
//2, add stmt into func body
searcher := Searcher{Root: h.Ast}
funcDecl := searcher.FindFuncDecl("testFoo")
//fpos := h.FindFuncDeclNode("testFoo")
//fnode := h.FindFuncDeclNode(fpos)
//fbody := (*fnode).(*ast.FuncDecl).Body
stmt := NewExpStmt(NewCallExpr(NewIdent("CALLfoo"), nil))
h.AddStmt(funcDecl.Body, TAIL, stmt)
pos := funcDecl.Pos()
emptystmt := NewEmptyStmt(pos)
h.AddStmt(funcDecl.Body, TAIL, emptystmt)
gotCode := h.Output(nil)
if gotCode != wantedCode {
t.Errorf("\n got: %q \n exp: %q", gotCode, wantedCode)
}
}
func TestNewDeclStmt(t *testing.T) {
srcode := `package miclient
var ppx int
func testFoo(){}`
h := ParseFromCode(srcode)
wantedCode := `package miclient
var ppx int
func testFoo() { var guessCreateEndpoint kitendpoint.Endpoint }
`
//2, add stmt into func body
searcher := Searcher{Root: h.Ast}
funcDecl := searcher.FindFuncDecl("testFoo")
specs := make([]ast.Spec, 0)
specs = append(specs, NewValueSpec("guessCreateEndpoint", NewSelectExp(NewIdent("kitendpoint"), NewIdent("Endpoint"))))
genDecl := NewGenDecl(token.VAR, specs...)
declStmt := NewDeclStmt(genDecl)
h.AddStmt(funcDecl.Body, TAIL, declStmt)
gotCode := h.Output(nil)
if gotCode != wantedCode {
t.Errorf("\n got: %q \n exp: %q", gotCode, wantedCode)
}
}