497d671f240bd06fe71c0ddc7fa6288d47087cc0
[platform/upstream/gcc48.git] / libgo / go / go / printer / printer_test.go
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
5 package printer
6
7 import (
8         "bytes"
9         "flag"
10         "go/ast"
11         "go/parser"
12         "go/token"
13         "io/ioutil"
14         "path/filepath"
15         "testing"
16         "time"
17 )
18
19 const (
20         dataDir  = "testdata"
21         tabwidth = 8
22 )
23
24 var update = flag.Bool("update", false, "update golden files")
25
26 var fset = token.NewFileSet()
27
28 func lineString(text []byte, i int) string {
29         i0 := i
30         for i < len(text) && text[i] != '\n' {
31                 i++
32         }
33         return string(text[i0:i])
34 }
35
36 type checkMode uint
37
38 const (
39         export checkMode = 1 << iota
40         rawFormat
41 )
42
43 func runcheck(t *testing.T, source, golden string, mode checkMode) {
44         // parse source
45         prog, err := parser.ParseFile(fset, source, nil, parser.ParseComments)
46         if err != nil {
47                 t.Error(err)
48                 return
49         }
50
51         // filter exports if necessary
52         if mode&export != 0 {
53                 ast.FileExports(prog) // ignore result
54                 prog.Comments = nil   // don't print comments that are not in AST
55         }
56
57         // determine printer configuration
58         cfg := Config{Tabwidth: tabwidth}
59         if mode&rawFormat != 0 {
60                 cfg.Mode |= RawFormat
61         }
62
63         // format source
64         var buf bytes.Buffer
65         if err := cfg.Fprint(&buf, fset, prog); err != nil {
66                 t.Error(err)
67         }
68         res := buf.Bytes()
69
70         // formatted source must be valid
71         if _, err := parser.ParseFile(fset, "", res, 0); err != nil {
72                 t.Error(err)
73                 t.Logf("\n%s", res)
74                 return
75         }
76
77         // update golden files if necessary
78         if *update {
79                 if err := ioutil.WriteFile(golden, res, 0644); err != nil {
80                         t.Error(err)
81                 }
82                 return
83         }
84
85         // get golden
86         gld, err := ioutil.ReadFile(golden)
87         if err != nil {
88                 t.Error(err)
89                 return
90         }
91
92         // compare lengths
93         if len(res) != len(gld) {
94                 t.Errorf("len = %d, expected %d (= len(%s))", len(res), len(gld), golden)
95         }
96
97         // compare contents
98         for i, line, offs := 0, 1, 0; i < len(res) && i < len(gld); i++ {
99                 ch := res[i]
100                 if ch != 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))
103                         t.Error()
104                         return
105                 }
106                 if ch == '\n' {
107                         line++
108                         offs = i + 1
109                 }
110         }
111 }
112
113 func check(t *testing.T, source, golden string, mode checkMode) {
114         // start a timer to produce a time-out signal
115         tc := make(chan int)
116         go func() {
117                 time.Sleep(10 * time.Second) // plenty of a safety margin, even for very slow machines
118                 tc <- 0
119         }()
120
121         // run the test
122         cc := make(chan int)
123         go func() {
124                 runcheck(t, source, golden, mode)
125                 cc <- 0
126         }()
127
128         // wait for the first finisher
129         select {
130         case <-tc:
131                 // test running past time out
132                 t.Errorf("%s: running too slowly", source)
133         case <-cc:
134                 // test finished within alloted time margin
135         }
136 }
137
138 type entry struct {
139         source, golden string
140         mode           checkMode
141 }
142
143 // Use go test -update to create/update the respective golden files.
144 var data = []entry{
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},
154 }
155
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)
163         }
164 }
165
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.
169 //
170 func TestLineComments(t *testing.T) {
171         const src = `// comment 1
172         // comment 2
173         // comment 3
174         package main
175         `
176
177         fset := token.NewFileSet()
178         f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
179         if err != nil {
180                 panic(err) // error in test
181         }
182
183         var buf bytes.Buffer
184         fset = token.NewFileSet() // use the wrong file set
185         Fprint(&buf, fset, f)
186
187         nlines := 0
188         for _, ch := range buf.Bytes() {
189                 if ch == '\n' {
190                         nlines++
191                 }
192         }
193
194         const expected = 3
195         if nlines < expected {
196                 t.Errorf("got %d, expected %d\n", nlines, expected)
197                 t.Errorf("result:\n%s", buf.Bytes())
198         }
199 }
200
201 // Verify that the printer can be invoked during initialization.
202 func init() {
203         const name = "foobar"
204         var buf bytes.Buffer
205         if err := Fprint(&buf, fset, &ast.Ident{Name: name}); err != nil {
206                 panic(err) // error in test
207         }
208         // in debug mode, the result contains additional information;
209         // ignore it
210         if s := buf.String(); !debug && s != name {
211                 panic("got " + s + ", want " + name)
212         }
213 }
214
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)
220         if err == nil {
221                 t.Error("expected illegal program") // error in test
222         }
223         var buf bytes.Buffer
224         Fprint(&buf, fset, f)
225         if buf.String() != res {
226                 t.Errorf("got %q, expected %q", buf.String(), res)
227         }
228 }
229
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
234         var buf bytes.Buffer
235         for offs := 0; offs <= srclen; offs++ {
236                 buf.Reset()
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 {
240                         t.Error(err)
241                 }
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())
244                 }
245                 // Position information is just an offset.
246                 // Move comment one byte down in the source.
247                 comment.Slash++
248         }
249 }
250
251 // Verify that the printer produces always produces a correct program
252 // even if the position information of comments introducing newlines
253 // is incorrect.
254 func TestBadComments(t *testing.T) {
255         const src = `
256 // first comment - text and position changed by test
257 package p
258 import "fmt"
259 const pi = 3.14 // rough circle
260 var (
261         x, y, z int = 1, 2, 3
262         u, v float64
263 )
264 func fibo(n int) {
265         if n < 2 {
266                 return n /* seed values */
267         }
268         return fibo(n-1) + fibo(n-2)
269 }
270 `
271
272         f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
273         if err != nil {
274                 t.Error(err) // error in test
275         }
276
277         comment := f.Comments[0].List[0]
278         pos := comment.Pos()
279         if fset.Position(pos).Offset != 1 {
280                 t.Error("expected offset 1") // error in test
281         }
282
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 */"})
287 }
288
289 type visitor chan *ast.Ident
290
291 func (v visitor) Visit(n ast.Node) (w ast.Visitor) {
292         if ident, ok := n.(*ast.Ident); ok {
293                 v <- ident
294         }
295         return v
296 }
297
298 // idents is an iterator that returns all idents in f via the result channel.
299 func idents(f *ast.File) <-chan *ast.Ident {
300         v := make(visitor)
301         go func() {
302                 ast.Walk(v, f)
303                 close(v)
304         }()
305         return v
306 }
307
308 // identCount returns the number of identifiers found in f.
309 func identCount(f *ast.File) int {
310         n := 0
311         for _ = range idents(f) {
312                 n++
313         }
314         return n
315 }
316
317 // Verify that the SourcePos mode emits correct //line comments
318 // by testing that position information for matching identifiers
319 // is maintained.
320 func TestSourcePos(t *testing.T) {
321         const src = `
322 package p
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
329                 // ...
330                 c*t.z
331 }
332 `
333
334         // parse original
335         f1, err := parser.ParseFile(fset, "src", src, parser.ParseComments)
336         if err != nil {
337                 t.Fatal(err)
338         }
339
340         // pretty-print original
341         var buf bytes.Buffer
342         err = (&Config{Mode: UseSpaces | SourcePos, Tabwidth: 8}).Fprint(&buf, fset, f1)
343         if err != nil {
344                 t.Fatal(err)
345         }
346
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)
350         if err != nil {
351                 t.Fatalf("%s\n%s", err, buf.Bytes())
352         }
353
354         // At this point the position information of identifiers in f2 should
355         // match the position information of corresponding identifiers in f1.
356
357         // number of identifiers must be > 0 (test should run) and must match
358         n1 := identCount(f1)
359         n2 := identCount(f2)
360         if n1 == 0 {
361                 t.Fatal("got no idents")
362         }
363         if n2 != n1 {
364                 t.Errorf("got %d idents; want %d", n2, n1)
365         }
366
367         // verify that all identifiers have correct line information
368         i2range := idents(f2)
369         for i1 := range idents(f1) {
370                 i2 := <-i2range
371
372                 if i2.Name != i1.Name {
373                         t.Errorf("got ident %s; want %s", i2.Name, i1.Name)
374                 }
375
376                 l1 := fset.Position(i1.Pos()).Line
377                 l2 := fset.Position(i2.Pos()).Line
378                 if l2 != l1 {
379                         t.Errorf("got line %d; want %d for %s", l2, l1, i1.Name)
380                 }
381         }
382
383         if t.Failed() {
384                 t.Logf("\n%s", buf.Bytes())
385         }
386 }
387
388 // TextX is a skeleton test that can be filled in for debugging one-off cases.
389 // Do not remove.
390 func TestX(t *testing.T) {
391         const src = `
392 package p
393 func _() {}
394 `
395         // parse original
396         f, err := parser.ParseFile(fset, "src", src, parser.ParseComments)
397         if err != nil {
398                 t.Fatal(err)
399         }
400
401         // pretty-print original
402         var buf bytes.Buffer
403         if err = (&Config{Mode: UseSpaces, Tabwidth: 8}).Fprint(&buf, fset, f); err != nil {
404                 t.Fatal(err)
405         }
406
407         // parse pretty printed original
408         if _, err := parser.ParseFile(fset, "", buf.Bytes(), 0); err != nil {
409                 t.Fatalf("%s\n%s", err, buf.Bytes())
410         }
411
412 }