-
Notifications
You must be signed in to change notification settings - Fork 11
/
helpers_test.go
212 lines (181 loc) · 4.42 KB
/
helpers_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
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
package gounit
import (
"go/ast"
"go/parser"
"go/token"
"reflect"
"testing"
)
func Test_nodeToString(t *testing.T) {
type args struct {
fs *token.FileSet
n ast.Node
}
tests := []struct {
name string
args func(t *testing.T) args
want1 string
}{
{
name: "success",
args: func(*testing.T) args {
return args{
fs: token.NewFileSet(),
n: &ast.Ident{Name: "node"},
}
},
want1: "node",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tArgs := tt.args(t)
got1 := nodeToString(tArgs.fs, tArgs.n)
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("nodeToString got1 = %v, want1: %v", got1, tt.want1)
}
})
}
}
func Test_findMissingTests(t *testing.T) {
const gofile = `package gofile
func Test_function() error {
return nil
}`
type args struct {
file *ast.File
funcs []*Func
}
tests := []struct {
name string
args func(t *testing.T) args
want1 []*Func
}{
{
name: "no functions",
args: func(*testing.T) args {
return args{
file: &ast.File{},
}
},
want1: []*Func{},
},
{
name: "tests not found",
args: func(t *testing.T) args {
fs := token.NewFileSet()
file, err := parser.ParseFile(fs, "file.go", []byte(gofile), 0)
if err != nil {
t.Fatalf("failed to parse file: %v", err)
}
return args{
file: file,
funcs: []*Func{{Signature: &ast.FuncDecl{Name: &ast.Ident{Name: "missing_function"}}}},
}
},
want1: []*Func{{Signature: &ast.FuncDecl{Name: &ast.Ident{Name: "missing_function"}}}},
},
{
name: "tests found",
args: func(t *testing.T) args {
fs := token.NewFileSet()
file, err := parser.ParseFile(fs, "file.go", []byte(gofile), 0)
if err != nil {
t.Fatalf("failed to parse file: %v", err)
}
return args{
file: file,
funcs: []*Func{{Signature: &ast.FuncDecl{Name: &ast.Ident{Name: "function"}}}},
}
},
want1: []*Func{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tArgs := tt.args(t)
got1 := findMissingTests(tArgs.file, tArgs.funcs)
if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("findMissingTests got1 = %v, want1: %v", got1, tt.want1)
}
})
}
}
func Test_templateHelpers(t *testing.T) {
helpers := templateHelpers(token.NewFileSet())
astHelper, ok := helpers["ast"].(func(ast.Node) string)
if !ok {
t.Fatalf("unexpected ast helper type")
}
s := astHelper(nil)
if s != "" {
t.Errorf("unexpected ast helper result: %s", s)
}
paramsHelper, ok := helpers["params"].(func(*Func) []string)
if !ok {
t.Fatalf("unexpected params helper type")
}
params := paramsHelper(&Func{Signature: &ast.FuncDecl{Type: &ast.FuncType{Params: &ast.FieldList{}}}})
if len(params) > 0 {
t.Errorf("unexpected params len: %d", len(params))
}
resultsHelper, ok := helpers["results"].(func(*Func) []string)
if !ok {
t.Fatalf("unexpected results helper type")
}
results := resultsHelper(&Func{Signature: &ast.FuncDecl{Type: &ast.FuncType{}}})
if len(results) > 0 {
t.Errorf("unexpected params len: %d", len(results))
}
receiverHelper, ok := helpers["receiver"].(func(*Func) string)
if !ok {
t.Fatalf("unexpected receiver helper type")
}
noReceiver := receiverHelper(&Func{Signature: &ast.FuncDecl{}})
if noReceiver != "" {
t.Errorf("unexpected receiver: %s", noReceiver)
}
receiver := receiverHelper(&Func{Signature: &ast.FuncDecl{Recv: &ast.FieldList{List: []*ast.Field{{Type: &ast.Ident{Name: "Receiver"}}}}}})
if receiver != "Receiver." {
t.Errorf("unexpected receiver: %s", receiver)
}
wantHelper, ok := helpers["want"].(func(string) string)
if !ok {
t.Fatalf("unexpected want helper type")
}
want := wantHelper("got1")
if want != "want1" {
t.Errorf("unexpected want helper result: %s", want)
}
}
func Test_findFunctions(t *testing.T) {
type args struct {
decls []ast.Decl
match matchFunc
}
tests := []struct {
name string
args func(t *testing.T) args
want1 []*Func
}{
{
name: "success",
args: func(t *testing.T) args {
return args{
decls: []ast.Decl{&ast.FuncDecl{}, &ast.FuncDecl{}},
match: func(*ast.FuncDecl) bool { return true },
}
},
want1: []*Func{&Func{}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tArgs := tt.args(t)
got1 := findFunctions(tArgs.decls, tArgs.match)
if len(got1) != 2 {
t.Errorf("unexpected result slice size: got1 = %d, want1: 2", len(got1))
}
})
}
}