p.setComment(d.Doc)
p.print(d.Pos(), d.Tok, blank)
- if d.Lparen.IsValid() {
+ if d.Lparen.IsValid() || len(d.Specs) > 1 {
// group of parenthesized declarations
p.print(d.Lparen, token.LPAREN)
if n := len(d.Specs); n > 0 {
t.Errorf("%v\norig: %q\ngot : %q", err, src, got)
}
}
+
+// If a declaration has multiple specifications, a parenthesized
+// declaration must be printed even if Lparen is token.NoPos.
+func TestParenthesizedDecl(t *testing.T) {
+ // a package with multiple specs in a single declaration
+ const src = "package p; var ( a float64; b int )"
+ fset := token.NewFileSet()
+ f, err := parser.ParseFile(fset, "", src, 0)
+
+ // print the original package
+ var buf bytes.Buffer
+ err = Fprint(&buf, fset, f)
+ if err != nil {
+ t.Fatal(err)
+ }
+ original := buf.String()
+
+ // now remove parentheses from the declaration
+ for i := 0; i != len(f.Decls); i++ {
+ f.Decls[i].(*ast.GenDecl).Lparen = token.NoPos
+ }
+ buf.Reset()
+ err = Fprint(&buf, fset, f)
+ if err != nil {
+ t.Fatal(err)
+ }
+ noparen := buf.String()
+
+ if noparen != original {
+ t.Errorf("got %q, want %q", noparen, original)
+ }
+}