Source file
src/go/ast/issues_test.go
1
2
3
4
5 package ast_test
6
7 import (
8 "go/ast"
9 "go/parser"
10 "go/token"
11 "testing"
12 )
13
14 func TestIssue33649(t *testing.T) {
15 for _, src := range []string{
16 `package p; func _()`,
17 `package p; func _() {`,
18 `package p; func _() { _ = 0`,
19 `package p; func _() { _ = 0 }`,
20 } {
21 fset := token.NewFileSet()
22 f, _ := parser.ParseFile(fset, "", src, parser.AllErrors)
23 if f == nil {
24 panic("invalid test setup: parser didn't return an AST")
25 }
26
27
28 var tf *token.File
29 fset.Iterate(func(f *token.File) bool {
30 tf = f
31 return true
32 })
33 tfEnd := tf.Base() + tf.Size()
34
35 fd := f.Decls[len(f.Decls)-1].(*ast.FuncDecl)
36 fdEnd := int(fd.End())
37
38 if fdEnd != tfEnd {
39 t.Errorf("%q: got fdEnd = %d; want %d (base = %d, size = %d)", src, fdEnd, tfEnd, tf.Base(), tf.Size())
40 }
41 }
42 }
43
44
45 func TestIssue28089(t *testing.T) {
46 for i, test := range []struct {
47 src string
48 want bool
49 }{
50
51 {`package p`, false},
52
53 {`// Package p doc.
54 package p`, false},
55
56 {`// Package p doc.
57 package p
58 // Code generated by gen. DO NOT EDIT.
59 `, false},
60
61 {`// Package p doc.
62 package p
63 const c = "` + "`" + `
64 // Code generated by gen. DO NOT EDIT.
65 ` + "`" + `
66 `, false},
67
68 {`// Copyright 2019 The Go Authors. All rights reserved.
69 // Use of this source code is governed by a BSD-style
70 // license that can be found in the LICENSE file.
71
72 // Package p doc comment goes here.
73 //
74 // Code generated by gen. DO NOT EDIT.
75 package p
76
77 ... `, true},
78
79
80
81
82
83
84
85
86 {`// Package p doc comment goes here.
87 //
88 // Code generated by gen. DO NOT EDIT.
89 package p
90
91 ... `, true},
92
93 {`// Copyright 2019 The Go Authors. All rights reserved.
94 // Use of this source code is governed by a BSD-style
95 // license that can be found in the LICENSE file.
96
97 // Package p doc comment goes here.
98 //
99 // Code generated by gen. DO NOT EDIT.
100 package p
101
102 ... `, false},
103
104 {`// Code generated by gen. DO NOT EDIT.
105 package p
106 `, false},
107
108 {`// Code generated DO NOT EDIT.
109 package p
110 `, false},
111
112
113 {`/* start of a general comment
114
115 // Code generated by tool; DO NOT EDIT.
116
117 end of a general comment */
118
119 // +build !dev
120
121 // Package comment.
122 package p
123
124 // Does match even though it's inside general comment (/*-style).
125 `, true},
126 } {
127 fset := token.NewFileSet()
128 f, err := parser.ParseFile(fset, "", test.src, parser.PackageClauseOnly|parser.ParseComments)
129 if f == nil {
130 t.Fatalf("parse %d failed to return AST: %v", i, err)
131 }
132
133 got := ast.IsGenerated(f)
134 if got != test.want {
135 t.Errorf("%d: IsGenerated on <<%s>> returned %t", i, test.src, got)
136 }
137 }
138
139 }
140
View as plain text