1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
24 var update = flag.Bool("update", false, "update golden files")
26 var fset = token.NewFileSet()
28 func lineString(text []byte, i int) string {
30 for i < len(text) && text[i] != '\n' {
33 return string(text[i0:i])
39 export checkMode = 1 << iota
43 func runcheck(t *testing.T, source, golden string, mode checkMode) {
45 prog, err := parser.ParseFile(fset, source, nil, parser.ParseComments)
51 // filter exports if necessary
53 ast.FileExports(prog) // ignore result
54 prog.Comments = nil // don't print comments that are not in AST
57 // determine printer configuration
58 cfg := Config{Tabwidth: tabwidth}
59 if mode&rawFormat != 0 {
65 if err := cfg.Fprint(&buf, fset, prog); err != nil {
70 // formatted source must be valid
71 if _, err := parser.ParseFile(fset, "", res, 0); err != nil {
77 // update golden files if necessary
79 if err := ioutil.WriteFile(golden, res, 0644); err != nil {
86 gld, err := ioutil.ReadFile(golden)
93 if len(res) != len(gld) {
94 t.Errorf("len = %d, expected %d (= len(%s))", len(res), len(gld), golden)
98 for i, line, offs := 0, 1, 0; i < len(res) && i < len(gld); i++ {
101 t.Errorf("%s:%d:%d: %s", source, line, i-offs+1, lineString(res, offs))
102 t.Errorf("%s:%d:%d: %s", golden, line, i-offs+1, lineString(gld, offs))
113 func check(t *testing.T, source, golden string, mode checkMode) {
114 // start a timer to produce a time-out signal
117 time.Sleep(10 * time.Second) // plenty of a safety margin, even for very slow machines
124 runcheck(t, source, golden, mode)
128 // wait for the first finisher
131 // test running past time out
132 t.Errorf("%s: running too slowly", source)
134 // test finished within alloted time margin
139 source, golden string
143 // Use go test -update to create/update the respective golden files.
145 {"empty.input", "empty.golden", 0},
146 {"comments.input", "comments.golden", 0},
147 {"comments.input", "comments.x", export},
148 {"linebreaks.input", "linebreaks.golden", 0},
149 {"expressions.input", "expressions.golden", 0},
150 {"expressions.input", "expressions.raw", rawFormat},
151 {"declarations.input", "declarations.golden", 0},
152 {"statements.input", "statements.golden", 0},
153 {"slow.input", "slow.golden", 0},
156 func TestFiles(t *testing.T) {
157 for _, e := range data {
158 source := filepath.Join(dataDir, e.source)
159 golden := filepath.Join(dataDir, e.golden)
160 check(t, source, golden, e.mode)
161 // TODO(gri) check that golden is idempotent
162 //check(t, golden, golden, e.mode)
166 // TestLineComments, using a simple test case, checks that consequtive line
167 // comments are properly terminated with a newline even if the AST position
168 // information is incorrect.
170 func TestLineComments(t *testing.T) {
171 const src = `// comment 1
177 fset := token.NewFileSet()
178 f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
180 panic(err) // error in test
184 fset = token.NewFileSet() // use the wrong file set
185 Fprint(&buf, fset, f)
188 for _, ch := range buf.Bytes() {
195 if nlines < expected {
196 t.Errorf("got %d, expected %d\n", nlines, expected)
197 t.Errorf("result:\n%s", buf.Bytes())
201 // Verify that the printer can be invoked during initialization.
203 const name = "foobar"
205 if err := Fprint(&buf, fset, &ast.Ident{Name: name}); err != nil {
206 panic(err) // error in test
208 // in debug mode, the result contains additional information;
210 if s := buf.String(); !debug && s != name {
211 panic("got " + s + ", want " + name)
215 // Verify that the printer doesn't crash if the AST contains BadXXX nodes.
216 func TestBadNodes(t *testing.T) {
217 const src = "package p\n("
218 const res = "package p\nBadDecl\n"
219 f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
221 t.Error("expected illegal program") // error in test
224 Fprint(&buf, fset, f)
225 if buf.String() != res {
226 t.Errorf("got %q, expected %q", buf.String(), res)
230 // testComment verifies that f can be parsed again after printing it
231 // with its first comment set to comment at any possible source offset.
232 func testComment(t *testing.T, f *ast.File, srclen int, comment *ast.Comment) {
233 f.Comments[0].List[0] = comment
235 for offs := 0; offs <= srclen; offs++ {
237 // Printing f should result in a correct program no
238 // matter what the (incorrect) comment position is.
239 if err := Fprint(&buf, fset, f); err != nil {
242 if _, err := parser.ParseFile(fset, "", buf.Bytes(), 0); err != nil {
243 t.Fatalf("incorrect program for pos = %d:\n%s", comment.Slash, buf.String())
245 // Position information is just an offset.
246 // Move comment one byte down in the source.
251 // Verify that the printer produces always produces a correct program
252 // even if the position information of comments introducing newlines
254 func TestBadComments(t *testing.T) {
256 // first comment - text and position changed by test
259 const pi = 3.14 // rough circle
261 x, y, z int = 1, 2, 3
266 return n /* seed values */
268 return fibo(n-1) + fibo(n-2)
272 f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
274 t.Error(err) // error in test
277 comment := f.Comments[0].List[0]
279 if fset.Position(pos).Offset != 1 {
280 t.Error("expected offset 1") // error in test
283 testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "//-style comment"})
284 testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style comment */"})
285 testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style \n comment */"})
286 testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style comment \n\n\n */"})
289 type visitor chan *ast.Ident
291 func (v visitor) Visit(n ast.Node) (w ast.Visitor) {
292 if ident, ok := n.(*ast.Ident); ok {
298 // idents is an iterator that returns all idents in f via the result channel.
299 func idents(f *ast.File) <-chan *ast.Ident {
308 // identCount returns the number of identifiers found in f.
309 func identCount(f *ast.File) int {
311 for _ = range idents(f) {
317 // Verify that the SourcePos mode emits correct //line comments
318 // by testing that position information for matching identifiers
320 func TestSourcePos(t *testing.T) {
323 import ( "go/printer"; "math" )
324 const pi = 3.14; var x = 0
325 type t struct{ x, y, z int; u, v, w float32 }
326 func (t *t) foo(a, b, c int) int {
327 return a*t.x + b*t.y +
328 // two extra lines here
335 f1, err := parser.ParseFile(fset, "src", src, parser.ParseComments)
340 // pretty-print original
342 err = (&Config{Mode: UseSpaces | SourcePos, Tabwidth: 8}).Fprint(&buf, fset, f1)
347 // parse pretty printed original
348 // (//line comments must be interpreted even w/o parser.ParseComments set)
349 f2, err := parser.ParseFile(fset, "", buf.Bytes(), 0)
351 t.Fatalf("%s\n%s", err, buf.Bytes())
354 // At this point the position information of identifiers in f2 should
355 // match the position information of corresponding identifiers in f1.
357 // number of identifiers must be > 0 (test should run) and must match
361 t.Fatal("got no idents")
364 t.Errorf("got %d idents; want %d", n2, n1)
367 // verify that all identifiers have correct line information
368 i2range := idents(f2)
369 for i1 := range idents(f1) {
372 if i2.Name != i1.Name {
373 t.Errorf("got ident %s; want %s", i2.Name, i1.Name)
376 l1 := fset.Position(i1.Pos()).Line
377 l2 := fset.Position(i2.Pos()).Line
379 t.Errorf("got line %d; want %d for %s", l2, l1, i1.Name)
384 t.Logf("\n%s", buf.Bytes())
388 // TestFuncType tests that an ast.FuncType with a nil Params field
389 // can be printed (per go/ast specification). Test case for issue 3870.
390 func TestFuncType(t *testing.T) {
392 Name: &ast.Ident{Name: "p"},
395 Name: &ast.Ident{Name: "f"},
396 Type: &ast.FuncType{},
402 if err := Fprint(&buf, fset, src); err != nil {
407 const want = `package p
413 t.Fatalf("got:\n%s\nwant:\n%s\n", got, want)
417 // TextX is a skeleton test that can be filled in for debugging one-off cases.
419 func TestX(t *testing.T) {
425 f, err := parser.ParseFile(fset, "src", src, parser.ParseComments)
430 // pretty-print original
432 if err = (&Config{Mode: UseSpaces, Tabwidth: 8}).Fprint(&buf, fset, f); err != nil {
436 // parse pretty printed original
437 if _, err := parser.ParseFile(fset, "", buf.Bytes(), 0); err != nil {
438 t.Fatalf("%s\n%s", err, buf.Bytes())