-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_test.go
137 lines (101 loc) · 3.06 KB
/
process_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
package tracegen
import (
"os"
"path/filepath"
"reflect"
"testing"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
"github.com/dave/dst/decorator/resolver"
)
const gomod = `module test
go 1.17`
const inputFunc = `package main
func Foo() {}
`
const inputMethod = `package main
type Foo struct{}
func (f *Foo) Foo() {}
`
const inputNonExportedFunc = `package main
func foo() {}
`
const skippedFuncAndMethod = `package main
//trace:skip
func Foo() {}
type Bar struct{}
//trace:skip
func (b *Bar) Foo() {}
`
const explicitIncludeMethod = `package main
//trace:skip
type Foo struct{}
func (f *Foo) A() {}
//trace:enable
func (f *Foo) B() {}
`
const genericTypeReceiver = `package main
type Foo[T any] struct{}
func (f *Foo[T]) Foo() {}
`
const genericFunctionParam = `package main
func Foo[T any]() {}
`
func check(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
func writeModule(t *testing.T, sample string) (path string) {
t.Helper()
dir, err := os.MkdirTemp("", "")
check(t, err)
path = filepath.Join(dir, "sample.go")
err = os.WriteFile(path, []byte(sample), 0644)
check(t, err)
err = os.WriteFile(filepath.Join(dir, "go.mod"), []byte(gomod), 0644)
check(t, err)
return path
}
func TestProcess(t *testing.T) {
tests := map[string]struct {
input string
calls []bool
settings Settings
}{
"default calls funcs": {inputFunc, []bool{false}, Settings{}},
"default calls methods": {inputMethod, []bool{false}, Settings{}},
"default calls non-exported funcs": {inputNonExportedFunc, []bool{false}, Settings{}},
"default skips explicitly skipped funcs and methods": {skippedFuncAndMethod, []bool{true, true}, Settings{}},
"methods skips funcs": {inputFunc, []bool{true}, Settings{Methods: true}},
"exported skips non-exported funcs": {inputNonExportedFunc, []bool{true}, Settings{Exported: true}},
"explicit include preempts exclude": {explicitIncludeMethod, []bool{true, false}, Settings{}},
"explicit include preempts untagged parent": {explicitIncludeMethod, []bool{true, false}, Settings{Tagged: true}},
"default calls funcs with generic type receiver": {genericTypeReceiver, []bool{false}, Settings{}},
"default calls funcs with generic function param": {genericFunctionParam, []bool{false}, Settings{}},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
path := writeModule(t, test.input)
err := os.Chdir(filepath.Dir(path))
check(t, err)
var calls []bool
err = Process(
test.settings,
[]string{"."},
func(fn *dst.FuncDecl, shouldSkip bool) (imports []string) {
calls = append(calls, shouldSkip)
return nil
},
func(pkg *decorator.Package, file *dst.File) resolver.RestorerResolver {
return NewSimpleResolver(pkg, file, nil)
},
)
check(t, err)
if !reflect.DeepEqual(calls, test.calls) {
t.Fatalf("mismatched calls, got %v, expected %v", calls, test.calls)
}
})
}
}