diff options
Diffstat (limited to 'libgo/go/go/printer/printer_test.go')
-rw-r--r-- | libgo/go/go/printer/printer_test.go | 75 |
1 files changed, 68 insertions, 7 deletions
diff --git a/libgo/go/go/printer/printer_test.go b/libgo/go/go/printer/printer_test.go index 525fcc1595f..9adf48cda61 100644 --- a/libgo/go/go/printer/printer_test.go +++ b/libgo/go/go/printer/printer_test.go @@ -171,14 +171,14 @@ func TestLineComments(t *testing.T) { ` fset := token.NewFileSet() - ast1, err1 := parser.ParseFile(fset, "", src, parser.ParseComments) - if err1 != nil { - panic(err1) + f, err := parser.ParseFile(fset, "", src, parser.ParseComments) + if err != nil { + panic(err) // error in test } var buf bytes.Buffer fset = token.NewFileSet() // use the wrong file set - Fprint(&buf, fset, ast1) + Fprint(&buf, fset, f) nlines := 0 for _, ch := range buf.Bytes() { @@ -190,6 +190,7 @@ func TestLineComments(t *testing.T) { const expected = 3 if nlines < expected { t.Errorf("got %d, expected %d\n", nlines, expected) + t.Errorf("result:\n%s", buf.Bytes()) } } @@ -198,9 +199,11 @@ func init() { const name = "foobar" var buf bytes.Buffer if err := Fprint(&buf, fset, &ast.Ident{Name: name}); err != nil { - panic(err) + panic(err) // error in test } - if s := buf.String(); s != name { + // in debug mode, the result contains additional information; + // ignore it + if s := buf.String(); !debug && s != name { panic("got " + s + ", want " + name) } } @@ -211,7 +214,7 @@ func TestBadNodes(t *testing.T) { const res = "package p\nBadDecl\n" f, err := parser.ParseFile(fset, "", src, parser.ParseComments) if err == nil { - t.Errorf("expected illegal program") + t.Error("expected illegal program") // error in test } var buf bytes.Buffer Fprint(&buf, fset, f) @@ -219,3 +222,61 @@ func TestBadNodes(t *testing.T) { t.Errorf("got %q, expected %q", buf.String(), res) } } + +// Print and parse f with +func testComment(t *testing.T, f *ast.File, srclen int, comment *ast.Comment) { + f.Comments[0].List[0] = comment + var buf bytes.Buffer + for offs := 0; offs <= srclen; offs++ { + buf.Reset() + // Printing f should result in a correct program no + // matter what the (incorrect) comment position is. + if err := Fprint(&buf, fset, f); err != nil { + t.Error(err) + } + if _, err := parser.ParseFile(fset, "", buf.Bytes(), 0); err != nil { + t.Fatalf("incorrect program for pos = %d:\n%s", comment.Slash, buf.String()) + } + // Position information is just an offset. + // Move comment one byte down in the source. + comment.Slash++ + } +} + +// Verify that the printer produces always produces a correct program +// even if the position information of comments introducing newlines +// is incorrect. +func TestBadComments(t *testing.T) { + const src = ` +// first comment - text and position changed by test +package p +import "fmt" +const pi = 3.14 // rough circle +var ( + x, y, z int = 1, 2, 3 + u, v float64 +) +func fibo(n int) { + if n < 2 { + return n /* seed values */ + } + return fibo(n-1) + fibo(n-2) +} +` + + f, err := parser.ParseFile(fset, "", src, parser.ParseComments) + if err != nil { + t.Error(err) // error in test + } + + comment := f.Comments[0].List[0] + pos := comment.Pos() + if fset.Position(pos).Offset != 1 { + t.Error("expected offset 1") // error in test + } + + testComment(t, f, len(src), &ast.Comment{pos, "//-style comment"}) + testComment(t, f, len(src), &ast.Comment{pos, "/*-style comment */"}) + testComment(t, f, len(src), &ast.Comment{pos, "/*-style \n comment */"}) + testComment(t, f, len(src), &ast.Comment{pos, "/*-style comment \n\n\n */"}) +} |