138
func TestUnresolved(t *testing.T) {
139
f, err := ParseFile(fset, "", `
143
func f2a(byte, int, float)
144
func f3a(a, b int, c float)
146
func f5a(a s1a, b ...complex)
149
func f2b([]byte, (int), *float)
150
func f3b(a, b *int, c []float)
151
func f4b(...*complex)
152
func f5b(a s1a, b ...[]complex)
154
type s1a struct { int }
155
type s2a struct { byte; int; s1a }
156
type s3a struct { a, b int; c float }
158
type s1b struct { *int }
159
type s2b struct { byte; int; *float }
160
type s3b struct { a, b *s3b; c []float }
166
want := "int " + // f1a
167
"byte int float " + // f2a
168
"int float " + // f3a
173
"byte int float " + // f2b
174
"int float " + // f3b
180
"int float " + // s3a
183
"byte int float " + // s2a
186
// collect unresolved identifiers
188
for _, u := range f.Unresolved {
189
buf.WriteString(u.Name)
195
t.Errorf("\ngot: %s\nwant: %s", got, want)
136
199
var imports = map[string]bool{
244
func TestCommentGroups(t *testing.T) {
245
f, err := ParseFile(fset, "", `
246
package p /* 1a */ /* 1b */ /* 1c */ // 1d
252
/* 3c */ const e = 2.7182
254
// Example from issue 3139
255
func ExampleCount() {
256
fmt.Println(strings.Count("cheese", "e"))
257
fmt.Println(strings.Count("five", "")) // before & after each rune
266
expected := [][]string{
267
{"/* 1a */", "/* 1b */", "/* 1c */", "// 1d"},
268
{"/* 2a\n*/", "// 2b"},
269
{"/* 3a */", "// 3b", "/* 3c */"},
270
{"// Example from issue 3139"},
271
{"// before & after each rune"},
272
{"// Output:", "// 3", "// 5"},
274
if len(f.Comments) != len(expected) {
275
t.Fatalf("got %d comment groups; expected %d", len(f.Comments), len(expected))
277
for i, exp := range expected {
278
got := f.Comments[i].List
279
if len(got) != len(exp) {
280
t.Errorf("got %d comments in group %d; expected %d", len(got), i, len(exp))
283
for j, exp := range exp {
286
t.Errorf("got %q in group %d; expected %q", got, i, exp)
292
func getField(file *ast.File, fieldname string) *ast.Field {
293
parts := strings.Split(fieldname, ".")
294
for _, d := range file.Decls {
295
if d, ok := d.(*ast.GenDecl); ok && d.Tok == token.TYPE {
296
for _, s := range d.Specs {
297
if s, ok := s.(*ast.TypeSpec); ok && s.Name.Name == parts[0] {
298
if s, ok := s.Type.(*ast.StructType); ok {
299
for _, f := range s.Fields.List {
300
for _, name := range f.Names {
301
if name.Name == parts[1] {
314
// Don't use ast.CommentGroup.Text() - we want to see exact comment text.
315
func commentText(c *ast.CommentGroup) string {
318
for _, c := range c.List {
319
buf.WriteString(c.Text)
325
func checkFieldComments(t *testing.T, file *ast.File, fieldname, lead, line string) {
326
f := getField(file, fieldname)
328
t.Fatalf("field not found: %s", fieldname)
330
if got := commentText(f.Doc); got != lead {
331
t.Errorf("got lead comment %q; expected %q", got, lead)
333
if got := commentText(f.Comment); got != line {
334
t.Errorf("got line comment %q; expected %q", got, line)
338
func TestLeadAndLineComments(t *testing.T) {
339
f, err := ParseFile(fset, "", `
342
/* F1 lead comment */
344
F1 int /* F1 */ // line comment
347
F2 int // F2 line comment
349
f3 int // f3 line comment
355
checkFieldComments(t, f, "T.F1", "/* F1 lead comment *///", "/* F1 */// line comment")
356
checkFieldComments(t, f, "T.F2", "// F2 lead// comment", "// F2 line comment")
357
checkFieldComments(t, f, "T.f3", "// f3 lead comment", "// f3 line comment")
359
checkFieldComments(t, f, "T.F1", "/* F1 lead comment *///", "/* F1 */// line comment")
360
checkFieldComments(t, f, "T.F2", "// F2 lead// comment", "// F2 line comment")
361
if getField(f, "T.f3") != nil {
362
t.Error("not expected to find T.f3")