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.
4
5package printer
6
7import (
8	"bytes"
9	"errors"
10	"flag"
11	"fmt"
12	"go/ast"
13	"go/parser"
14	"go/token"
15	"io/ioutil"
16	"path/filepath"
17	"testing"
18	"time"
19)
20
21const (
22	dataDir  = "testdata"
23	tabwidth = 8
24)
25
26var update = flag.Bool("update", false, "update golden files")
27
28var fset = token.NewFileSet()
29
30type checkMode uint
31
32const (
33	export checkMode = 1 << iota
34	rawFormat
35	idempotent
36)
37
38// format parses src, prints the corresponding AST, verifies the resulting
39// src is syntactically correct, and returns the resulting src or an error
40// if any.
41func format(src []byte, mode checkMode) ([]byte, error) {
42	// parse src
43	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
44	if err != nil {
45		return nil, fmt.Errorf("parse: %s\n%s", err, src)
46	}
47
48	// filter exports if necessary
49	if mode&export != 0 {
50		ast.FileExports(f) // ignore result
51		f.Comments = nil   // don't print comments that are not in AST
52	}
53
54	// determine printer configuration
55	cfg := Config{Tabwidth: tabwidth}
56	if mode&rawFormat != 0 {
57		cfg.Mode |= RawFormat
58	}
59
60	// print AST
61	var buf bytes.Buffer
62	if err := cfg.Fprint(&buf, fset, f); err != nil {
63		return nil, fmt.Errorf("print: %s", err)
64	}
65
66	// make sure formated output is syntactically correct
67	res := buf.Bytes()
68	if _, err := parser.ParseFile(fset, "", res, 0); err != nil {
69		return nil, fmt.Errorf("re-parse: %s\n%s", err, buf.Bytes())
70	}
71
72	return res, nil
73}
74
75// lineAt returns the line in text starting at offset offs.
76func lineAt(text []byte, offs int) []byte {
77	i := offs
78	for i < len(text) && text[i] != '\n' {
79		i++
80	}
81	return text[offs:i]
82}
83
84// diff compares a and b.
85func diff(aname, bname string, a, b []byte) error {
86	var buf bytes.Buffer // holding long error message
87
88	// compare lengths
89	if len(a) != len(b) {
90		fmt.Fprintf(&buf, "\nlength changed: len(%s) = %d, len(%s) = %d", aname, len(a), bname, len(b))
91	}
92
93	// compare contents
94	line := 1
95	offs := 1
96	for i := 0; i < len(a) && i < len(b); i++ {
97		ch := a[i]
98		if ch != b[i] {
99			fmt.Fprintf(&buf, "\n%s:%d:%d: %s", aname, line, i-offs+1, lineAt(a, offs))
100			fmt.Fprintf(&buf, "\n%s:%d:%d: %s", bname, line, i-offs+1, lineAt(b, offs))
101			fmt.Fprintf(&buf, "\n\n")
102			break
103		}
104		if ch == '\n' {
105			line++
106			offs = i + 1
107		}
108	}
109
110	if buf.Len() > 0 {
111		return errors.New(buf.String())
112	}
113	return nil
114}
115
116func runcheck(t *testing.T, source, golden string, mode checkMode) {
117	src, err := ioutil.ReadFile(source)
118	if err != nil {
119		t.Error(err)
120		return
121	}
122
123	res, err := format(src, mode)
124	if err != nil {
125		t.Error(err)
126		return
127	}
128
129	// update golden files if necessary
130	if *update {
131		if err := ioutil.WriteFile(golden, res, 0644); err != nil {
132			t.Error(err)
133		}
134		return
135	}
136
137	// get golden
138	gld, err := ioutil.ReadFile(golden)
139	if err != nil {
140		t.Error(err)
141		return
142	}
143
144	// formatted source and golden must be the same
145	if err := diff(source, golden, res, gld); err != nil {
146		t.Error(err)
147		return
148	}
149
150	if mode&idempotent != 0 {
151		// formatting golden must be idempotent
152		// (This is very difficult to achieve in general and for now
153		// it is only checked for files explicitly marked as such.)
154		res, err = format(gld, mode)
155		if err := diff(golden, fmt.Sprintf("format(%s)", golden), gld, res); err != nil {
156			t.Errorf("golden is not idempotent: %s", err)
157		}
158	}
159}
160
161func check(t *testing.T, source, golden string, mode checkMode) {
162	// start a timer to produce a time-out signal
163	tc := make(chan int)
164	go func() {
165		time.Sleep(10 * time.Second) // plenty of a safety margin, even for very slow machines
166		tc <- 0
167	}()
168
169	// run the test
170	cc := make(chan int)
171	go func() {
172		runcheck(t, source, golden, mode)
173		cc <- 0
174	}()
175
176	// wait for the first finisher
177	select {
178	case <-tc:
179		// test running past time out
180		t.Errorf("%s: running too slowly", source)
181	case <-cc:
182		// test finished within alloted time margin
183	}
184}
185
186type entry struct {
187	source, golden string
188	mode           checkMode
189}
190
191// Use go test -update to create/update the respective golden files.
192var data = []entry{
193	{"empty.input", "empty.golden", idempotent},
194	{"comments.input", "comments.golden", 0},
195	{"comments.input", "comments.x", export},
196	{"comments2.input", "comments2.golden", idempotent},
197	{"linebreaks.input", "linebreaks.golden", idempotent},
198	{"expressions.input", "expressions.golden", idempotent},
199	{"expressions.input", "expressions.raw", rawFormat | idempotent},
200	{"declarations.input", "declarations.golden", 0},
201	{"statements.input", "statements.golden", 0},
202	{"slow.input", "slow.golden", idempotent},
203}
204
205func TestFiles(t *testing.T) {
206	for _, e := range data {
207		source := filepath.Join(dataDir, e.source)
208		golden := filepath.Join(dataDir, e.golden)
209		check(t, source, golden, e.mode)
210		// TODO(gri) check that golden is idempotent
211		//check(t, golden, golden, e.mode)
212	}
213}
214
215// TestLineComments, using a simple test case, checks that consequtive line
216// comments are properly terminated with a newline even if the AST position
217// information is incorrect.
218//
219func TestLineComments(t *testing.T) {
220	const src = `// comment 1
221	// comment 2
222	// comment 3
223	package main
224	`
225
226	fset := token.NewFileSet()
227	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
228	if err != nil {
229		panic(err) // error in test
230	}
231
232	var buf bytes.Buffer
233	fset = token.NewFileSet() // use the wrong file set
234	Fprint(&buf, fset, f)
235
236	nlines := 0
237	for _, ch := range buf.Bytes() {
238		if ch == '\n' {
239			nlines++
240		}
241	}
242
243	const expected = 3
244	if nlines < expected {
245		t.Errorf("got %d, expected %d\n", nlines, expected)
246		t.Errorf("result:\n%s", buf.Bytes())
247	}
248}
249
250// Verify that the printer can be invoked during initialization.
251func init() {
252	const name = "foobar"
253	var buf bytes.Buffer
254	if err := Fprint(&buf, fset, &ast.Ident{Name: name}); err != nil {
255		panic(err) // error in test
256	}
257	// in debug mode, the result contains additional information;
258	// ignore it
259	if s := buf.String(); !debug && s != name {
260		panic("got " + s + ", want " + name)
261	}
262}
263
264// Verify that the printer doesn't crash if the AST contains BadXXX nodes.
265func TestBadNodes(t *testing.T) {
266	const src = "package p\n("
267	const res = "package p\nBadDecl\n"
268	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
269	if err == nil {
270		t.Error("expected illegal program") // error in test
271	}
272	var buf bytes.Buffer
273	Fprint(&buf, fset, f)
274	if buf.String() != res {
275		t.Errorf("got %q, expected %q", buf.String(), res)
276	}
277}
278
279// testComment verifies that f can be parsed again after printing it
280// with its first comment set to comment at any possible source offset.
281func testComment(t *testing.T, f *ast.File, srclen int, comment *ast.Comment) {
282	f.Comments[0].List[0] = comment
283	var buf bytes.Buffer
284	for offs := 0; offs <= srclen; offs++ {
285		buf.Reset()
286		// Printing f should result in a correct program no
287		// matter what the (incorrect) comment position is.
288		if err := Fprint(&buf, fset, f); err != nil {
289			t.Error(err)
290		}
291		if _, err := parser.ParseFile(fset, "", buf.Bytes(), 0); err != nil {
292			t.Fatalf("incorrect program for pos = %d:\n%s", comment.Slash, buf.String())
293		}
294		// Position information is just an offset.
295		// Move comment one byte down in the source.
296		comment.Slash++
297	}
298}
299
300// Verify that the printer produces a correct program
301// even if the position information of comments introducing newlines
302// is incorrect.
303func TestBadComments(t *testing.T) {
304	const src = `
305// first comment - text and position changed by test
306package p
307import "fmt"
308const pi = 3.14 // rough circle
309var (
310	x, y, z int = 1, 2, 3
311	u, v float64
312)
313func fibo(n int) {
314	if n < 2 {
315		return n /* seed values */
316	}
317	return fibo(n-1) + fibo(n-2)
318}
319`
320
321	f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
322	if err != nil {
323		t.Error(err) // error in test
324	}
325
326	comment := f.Comments[0].List[0]
327	pos := comment.Pos()
328	if fset.Position(pos).Offset != 1 {
329		t.Error("expected offset 1") // error in test
330	}
331
332	testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "//-style comment"})
333	testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style comment */"})
334	testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style \n comment */"})
335	testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style comment \n\n\n */"})
336}
337
338type visitor chan *ast.Ident
339
340func (v visitor) Visit(n ast.Node) (w ast.Visitor) {
341	if ident, ok := n.(*ast.Ident); ok {
342		v <- ident
343	}
344	return v
345}
346
347// idents is an iterator that returns all idents in f via the result channel.
348func idents(f *ast.File) <-chan *ast.Ident {
349	v := make(visitor)
350	go func() {
351		ast.Walk(v, f)
352		close(v)
353	}()
354	return v
355}
356
357// identCount returns the number of identifiers found in f.
358func identCount(f *ast.File) int {
359	n := 0
360	for _ = range idents(f) {
361		n++
362	}
363	return n
364}
365
366// Verify that the SourcePos mode emits correct //line comments
367// by testing that position information for matching identifiers
368// is maintained.
369func TestSourcePos(t *testing.T) {
370	const src = `
371package p
372import ( "go/printer"; "math" )
373const pi = 3.14; var x = 0
374type t struct{ x, y, z int; u, v, w float32 }
375func (t *t) foo(a, b, c int) int {
376	return a*t.x + b*t.y +
377		// two extra lines here
378		// ...
379		c*t.z
380}
381`
382
383	// parse original
384	f1, err := parser.ParseFile(fset, "src", src, parser.ParseComments)
385	if err != nil {
386		t.Fatal(err)
387	}
388
389	// pretty-print original
390	var buf bytes.Buffer
391	err = (&Config{Mode: UseSpaces | SourcePos, Tabwidth: 8}).Fprint(&buf, fset, f1)
392	if err != nil {
393		t.Fatal(err)
394	}
395
396	// parse pretty printed original
397	// (//line comments must be interpreted even w/o parser.ParseComments set)
398	f2, err := parser.ParseFile(fset, "", buf.Bytes(), 0)
399	if err != nil {
400		t.Fatalf("%s\n%s", err, buf.Bytes())
401	}
402
403	// At this point the position information of identifiers in f2 should
404	// match the position information of corresponding identifiers in f1.
405
406	// number of identifiers must be > 0 (test should run) and must match
407	n1 := identCount(f1)
408	n2 := identCount(f2)
409	if n1 == 0 {
410		t.Fatal("got no idents")
411	}
412	if n2 != n1 {
413		t.Errorf("got %d idents; want %d", n2, n1)
414	}
415
416	// verify that all identifiers have correct line information
417	i2range := idents(f2)
418	for i1 := range idents(f1) {
419		i2 := <-i2range
420
421		if i2.Name != i1.Name {
422			t.Errorf("got ident %s; want %s", i2.Name, i1.Name)
423		}
424
425		l1 := fset.Position(i1.Pos()).Line
426		l2 := fset.Position(i2.Pos()).Line
427		if l2 != l1 {
428			t.Errorf("got line %d; want %d for %s", l2, l1, i1.Name)
429		}
430	}
431
432	if t.Failed() {
433		t.Logf("\n%s", buf.Bytes())
434	}
435}
436
437var decls = []string{
438	`import "fmt"`,
439	"const pi = 3.1415\nconst e = 2.71828\n\nvar x = pi",
440	"func sum(x, y int) int\t{ return x + y }",
441}
442
443func TestDeclLists(t *testing.T) {
444	for _, src := range decls {
445		file, err := parser.ParseFile(fset, "", "package p;"+src, parser.ParseComments)
446		if err != nil {
447			panic(err) // error in test
448		}
449
450		var buf bytes.Buffer
451		err = Fprint(&buf, fset, file.Decls) // only print declarations
452		if err != nil {
453			panic(err) // error in test
454		}
455
456		out := buf.String()
457		if out != src {
458			t.Errorf("\ngot : %q\nwant: %q\n", out, src)
459		}
460	}
461}
462
463var stmts = []string{
464	"i := 0",
465	"select {}\nvar a, b = 1, 2\nreturn a + b",
466	"go f()\ndefer func() {}()",
467}
468
469func TestStmtLists(t *testing.T) {
470	for _, src := range stmts {
471		file, err := parser.ParseFile(fset, "", "package p; func _() {"+src+"}", parser.ParseComments)
472		if err != nil {
473			panic(err) // error in test
474		}
475
476		var buf bytes.Buffer
477		err = Fprint(&buf, fset, file.Decls[0].(*ast.FuncDecl).Body.List) // only print statements
478		if err != nil {
479			panic(err) // error in test
480		}
481
482		out := buf.String()
483		if out != src {
484			t.Errorf("\ngot : %q\nwant: %q\n", out, src)
485		}
486	}
487}
488
489func TestBaseIndent(t *testing.T) {
490	// The testfile must not contain multi-line raw strings since those
491	// are not indented (because their values must not change) and make
492	// this test fail.
493	const filename = "printer.go"
494	src, err := ioutil.ReadFile(filename)
495	if err != nil {
496		panic(err) // error in test
497	}
498
499	file, err := parser.ParseFile(fset, filename, src, 0)
500	if err != nil {
501		panic(err) // error in test
502	}
503
504	var buf bytes.Buffer
505	for indent := 0; indent < 4; indent++ {
506		buf.Reset()
507		(&Config{Tabwidth: tabwidth, Indent: indent}).Fprint(&buf, fset, file)
508		// all code must be indented by at least 'indent' tabs
509		lines := bytes.Split(buf.Bytes(), []byte{'\n'})
510		for i, line := range lines {
511			if len(line) == 0 {
512				continue // empty lines don't have indentation
513			}
514			n := 0
515			for j, b := range line {
516				if b != '\t' {
517					// end of indentation
518					n = j
519					break
520				}
521			}
522			if n < indent {
523				t.Errorf("line %d: got only %d tabs; want at least %d: %q", i, n, indent, line)
524			}
525		}
526	}
527}
528
529// TestFuncType tests that an ast.FuncType with a nil Params field
530// can be printed (per go/ast specification). Test case for issue 3870.
531func TestFuncType(t *testing.T) {
532	src := &ast.File{
533		Name: &ast.Ident{Name: "p"},
534		Decls: []ast.Decl{
535			&ast.FuncDecl{
536				Name: &ast.Ident{Name: "f"},
537				Type: &ast.FuncType{},
538			},
539		},
540	}
541
542	var buf bytes.Buffer
543	if err := Fprint(&buf, fset, src); err != nil {
544		t.Fatal(err)
545	}
546	got := buf.String()
547
548	const want = `package p
549
550func f()
551`
552
553	if got != want {
554		t.Fatalf("got:\n%s\nwant:\n%s\n", got, want)
555	}
556}
557
558// TextX is a skeleton test that can be filled in for debugging one-off cases.
559// Do not remove.
560func TestX(t *testing.T) {
561	const src = `
562package p
563func _() {}
564`
565	_, err := format([]byte(src), 0)
566	if err != nil {
567		t.Error(err)
568	}
569}
570