-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_go_test.go
More file actions
394 lines (338 loc) · 9.09 KB
/
Copy pathparser_go_test.go
File metadata and controls
394 lines (338 loc) · 9.09 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package repomap
import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFirstSentence(t *testing.T) {
t.Parallel()
cases := []struct {
name string
comment string // empty means nil CommentGroup
ident string
want string
}{
{
name: "strip prefix and take first sentence",
comment: "Foo returns the widget count.",
ident: "Foo",
want: "returns the widget count",
},
{
name: "too short after prefix strip",
comment: "Foo",
ident: "Foo",
want: "",
},
{
name: "empty comment",
comment: "",
ident: "Foo",
want: "",
},
{
name: "nil comment group",
comment: "", // handled via nilCG flag below
ident: "Foo",
want: "",
},
{
name: "truncate at 60 runes",
comment: "Foo " + strings.Repeat("x", 80),
ident: "Foo",
want: strings.Repeat("x", 60) + "…",
},
{
name: "unicode rune boundary",
comment: "Foo returns 你好世界 from the server.",
ident: "Foo",
want: "returns 你好世界 from the server",
},
{
name: "multi-line takes first line only",
comment: "Foo does X.\nIt also does Y.",
ident: "Foo",
want: "does X",
},
{
name: "no prefix match uses full first sentence",
comment: "Returns widgets.",
ident: "Foo",
want: "Returns widgets",
},
{
name: "newline separator before period",
comment: "Foo does X\nIt also does Y.",
ident: "Foo",
want: "does X",
},
}
// nil CommentGroup case
t.Run("nil comment group", func(t *testing.T) {
t.Parallel()
got := firstSentence(nil, "Foo")
assert.Equal(t, "", got)
})
for _, tc := range cases {
if tc.name == "nil comment group" {
continue // handled above
}
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var cg *ast.CommentGroup
if tc.comment != "" {
// Build a synthetic CommentGroup from the raw text.
// We parse a Go file snippet so the AST comment is real.
src := "package p\n\n// " + strings.ReplaceAll(tc.comment, "\n", "\n// ") + "\nfunc " + tc.ident + "() {}\n"
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
require.NoError(t, err)
for _, d := range f.Decls {
if fn, ok := d.(*ast.FuncDecl); ok && fn.Doc != nil {
cg = fn.Doc
break
}
}
}
got := firstSentence(cg, tc.ident)
assert.Equal(t, tc.want, got)
})
}
}
// writeGoFixture is a helper that writes a Go source file and a minimal go.mod
// to a temp directory, returning the temp dir and absolute file path.
func writeGoFixture(t *testing.T, filename, src string) (dir, goFile string) {
t.Helper()
dir = t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module example.com/test\n\ngo 1.21\n"), 0o644))
goFile = filepath.Join(dir, filename)
require.NoError(t, os.WriteFile(goFile, []byte(src), 0o644))
return dir, goFile
}
func TestParseGoFile_UnexportedFallback_Triggers(t *testing.T) {
t.Parallel()
// Three unexported functions: one short (3-line body), two long (8-line bodies).
src := `package internal
func tinyHelper() {
_ = 1
_ = 2
}
func runServer(addr string) error {
_ = addr
_ = addr
_ = addr
_ = addr
_ = addr
_ = addr
return nil
}
func handleRequest(req string) string {
_ = req
_ = req
_ = req
_ = req
_ = req
_ = req
return req
}
`
dir, goFile := writeGoFixture(t, "internal.go", src)
fs, err := ParseGoFile(goFile, dir)
require.NoError(t, err)
require.NotNil(t, fs)
// Only the two >=5-line functions should appear; tinyHelper (3-line body) is excluded.
require.Len(t, fs.Symbols, 2)
names := map[string]bool{}
for _, s := range fs.Symbols {
names[s.Name] = true
assert.False(t, s.Exported, "fallback symbol must have Exported=false")
}
assert.True(t, names["runServer"], "runServer should be present")
assert.True(t, names["handleRequest"], "handleRequest should be present")
}
func TestParseGoFile_UnexportedFallback_SuppressedWhenExportedExists(t *testing.T) {
t.Parallel()
// One exported function plus two unexported large helpers.
src := `package mypkg
func Foo() {}
func helperA() error {
_ = 1
_ = 2
_ = 3
_ = 4
_ = 5
return nil
}
func helperB() string {
_ = ""
_ = ""
_ = ""
_ = ""
_ = ""
return ""
}
`
dir, goFile := writeGoFixture(t, "mypkg.go", src)
fs, err := ParseGoFile(goFile, dir)
require.NoError(t, err)
require.NotNil(t, fs)
// Fallback must NOT fire because the exported pass already found Foo.
require.Len(t, fs.Symbols, 1)
assert.Equal(t, "Foo", fs.Symbols[0].Name)
assert.True(t, fs.Symbols[0].Exported)
}
func TestParseGoFile_UnexportedFallback_SkipsTrivialHelpers(t *testing.T) {
t.Parallel()
// Only 1-4 line unexported functions — all below threshold.
src := `package internal
func a() { _ = 1 }
func b() error {
_ = 1
return nil
}
func c() string {
_ = ""
_ = ""
return ""
}
`
dir, goFile := writeGoFixture(t, "trivial.go", src)
fs, err := ParseGoFile(goFile, dir)
require.NoError(t, err)
require.NotNil(t, fs)
assert.Empty(t, fs.Symbols, "all functions below threshold: no symbols expected")
}
func TestParseGoFile_UnexportedFallback_IncludesMethods(t *testing.T) {
t.Parallel()
// Unexported method on an unexported type with a >=5-line body.
src := `package internal
type server struct{ addr string }
func (s *server) run() error {
_ = s.addr
_ = s.addr
_ = s.addr
_ = s.addr
_ = s.addr
return nil
}
`
dir, goFile := writeGoFixture(t, "server.go", src)
fs, err := ParseGoFile(goFile, dir)
require.NoError(t, err)
require.NotNil(t, fs)
require.Len(t, fs.Symbols, 1)
sym := fs.Symbols[0]
assert.Equal(t, "run", sym.Name)
assert.Equal(t, "method", sym.Kind)
assert.False(t, sym.Exported)
assert.NotEmpty(t, sym.Receiver, "Receiver must be set for methods")
}
func TestParseGoFile_UnexportedFallback_PopulatesDoc(t *testing.T) {
t.Parallel()
// Unexported function with a doc comment.
src := `package internal
// runServer starts the HTTP server on the given address and blocks.
func runServer(addr string) error {
_ = addr
_ = addr
_ = addr
_ = addr
_ = addr
return nil
}
`
dir, goFile := writeGoFixture(t, "server.go", src)
fs, err := ParseGoFile(goFile, dir)
require.NoError(t, err)
require.NotNil(t, fs)
require.Len(t, fs.Symbols, 1)
assert.NotEmpty(t, fs.Symbols[0].Doc, "Doc must be populated from the doc comment")
}
func TestParseGoFile_UnexportedFallback_MainInitPath(t *testing.T) {
t.Parallel()
// package main: main() and init() are captured by the hardcoded path;
// the fallback must NOT double-add them.
src := `package main
func main() {
_ = 1
_ = 2
_ = 3
_ = 4
_ = 5
}
func init() {
_ = 1
_ = 2
_ = 3
_ = 4
_ = 5
}
func helper() {
_ = 1
_ = 2
_ = 3
_ = 4
_ = 5
}
`
dir, goFile := writeGoFixture(t, "main.go", src)
fs, err := ParseGoFile(goFile, dir)
require.NoError(t, err)
require.NotNil(t, fs)
// main() and init() should be captured by the hardcoded path (Symbols non-empty),
// so the fallback must NOT fire and helper must NOT appear.
names := map[string]bool{}
for _, s := range fs.Symbols {
names[s.Name] = true
}
assert.True(t, names["main"], "main must be present via hardcoded path")
assert.True(t, names["init"], "init must be present via hardcoded path")
assert.False(t, names["helper"], "helper must NOT appear — fallback suppressed by main/init")
// Total: exactly main + init (helper filtered by fallback suppression).
assert.Len(t, fs.Symbols, 2)
}
func TestParseGoFile_DocExtraction(t *testing.T) {
t.Parallel()
src := `package mypkg
// ProcessBatch applies the batch rules to items and returns an error if any fail.
func ProcessBatch(items []string) error { return nil }
// Config holds the application configuration.
type Config struct {
Name string
}
// DefaultTimeout is the default timeout duration.
const DefaultTimeout = 30
`
dir := t.TempDir()
// Need a git repo for ScanFiles; for ParseGoFile we just need a go.mod.
require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module example.com/test\n\ngo 1.21\n"), 0o644))
goFile := filepath.Join(dir, "myfile.go")
require.NoError(t, os.WriteFile(goFile, []byte(src), 0o644))
fs, err := ParseGoFile(goFile, dir)
require.NoError(t, err)
require.NotNil(t, fs)
byName := make(map[string]Symbol)
for _, s := range fs.Symbols {
byName[s.Name] = s
}
// Function doc: strip prefix "ProcessBatch ", truncated at 60 runes.
// Full sentence: "applies the batch rules to items and returns an error if any fail"
// = 65 runes → truncated at the last word boundary within 60, then "…".
fn, ok := byName["ProcessBatch"]
require.True(t, ok, "ProcessBatch not found")
assert.Equal(t, "applies the batch rules to items and returns an error if…", fn.Doc)
// Type doc: strip prefix "Config "
cfg, ok := byName["Config"]
require.True(t, ok, "Config not found")
assert.Equal(t, "holds the application configuration", cfg.Doc)
// Const doc: strip prefix "DefaultTimeout "
dt, ok := byName["DefaultTimeout"]
require.True(t, ok, "DefaultTimeout not found")
assert.Equal(t, "is the default timeout duration", dt.Doc)
}