-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_lit_test.go
89 lines (80 loc) · 2.42 KB
/
func_lit_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
package gorefactor
import (
"github.com/dave/dst"
"github.com/stretchr/testify/assert"
"testing"
)
func TestAddFieldToFuncLitParams(t *testing.T) {
t.Run("case 1", func(t *testing.T) {
var src = `
package main
func rpc(ctx context.Context, hashKey string, timeout time.Duration, fn func(*AccountServiceClient) error) error {
return clientThrift.RpcWithContext(ctx, hashKey, timeout, func(c interface{}) error {
ct, ok := c.(*AccountServiceClient)
if ok {
return fn(ct)
} else {
return fmt.Errorf("reflect client thrift error")
}
})
}
func UpdateUserStrInfo(ctx context.Context, req *UserStrInfoRequest) (r *UpdateUserInfoRes, err error) {
tctx := trace.NewThriftUtilContextFromContext(ctx)
if req == nil {
return nil, fmt.Errorf("req nil")
}
err = rpc(ctx, req.Item, time.Millisecond*3000,
func(c *AccountServiceClient) error {
r, err = c.UpdateUserStrInfo(req, tctx)
return err
},
)
return
}
`
var expectedWithoutScope = `
package main
func rpc(ctx context.Context, hashKey string, timeout time.Duration, fn func(*AccountServiceClient) error) error {
return clientThrift.RpcWithContext(ctx, hashKey, timeout, func(fctx context.Context, c interface{}) error {
ct, ok := c.(*AccountServiceClient)
if ok {
return fn(ct)
} else {
return fmt.Errorf("reflect client thrift error")
}
})
}
func UpdateUserStrInfo(ctx context.Context, req *UserStrInfoRequest) (r *UpdateUserInfoRes, err error) {
tctx := trace.NewThriftUtilContextFromContext(ctx)
if req == nil {
return nil, fmt.Errorf("req nil")
}
err = rpc(ctx, req.Item, time.Millisecond*3000,
func(fctx context.Context, c *AccountServiceClient) error {
r, err = c.UpdateUserStrInfo(req, tctx)
return err
},
)
return
}
`
{
df, _ := ParseSrcFileFromBytes([]byte(src))
assert.True(t, AddFieldToFuncLitParams(df, EmptyScope, &dst.Field{
Names: []*dst.Ident{dst.NewIdent("fctx")},
Type: dst.NewIdent("context.Context"),
}, 0))
buf := printToBuf(df)
assertCodesEqual(t, expectedWithoutScope, buf.String())
}
{
df, _ := ParseSrcFileFromBytes([]byte(src))
assert.False(t, AddFieldToFuncLitParams(df, Scope{FuncName: "NonExist"}, &dst.Field{
Names: []*dst.Ident{dst.NewIdent("fctx")},
Type: dst.NewIdent("context.Context"),
}, 0))
buf := printToBuf(df)
assertCodesEqual(t, src, buf.String())
}
})
}