]> Cypherpunks repositories - gostls13.git/commitdiff
fix printer test for new syntax
authorRobert Griesemer <gri@golang.org>
Fri, 11 Dec 2009 23:31:06 +0000 (15:31 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 11 Dec 2009 23:31:06 +0000 (15:31 -0800)
R=rsc
https://golang.org/cl/175048

src/pkg/go/printer/nodes.go
src/pkg/go/printer/printer_test.go
src/pkg/go/printer/testdata/declarations.golden
src/pkg/go/printer/testdata/declarations.input
src/pkg/go/printer/testdata/expressions.golden
src/pkg/go/printer/testdata/expressions.input
src/pkg/go/printer/testdata/expressions.raw
src/pkg/go/printer/testdata/linebreaks.golden
src/pkg/go/printer/testdata/linebreaks.input

index e5ecdd27893b74376490d2f81698855b9ece2d37..545a5f048acd79eef6645ba318d98fe72210d948 100644 (file)
@@ -843,8 +843,20 @@ func (p *printer) stmtList(list []ast.Stmt, _indent int) {
 }
 
 
+func (p *printer) moveCommentsAfter(pos token.Position) {
+       // TODO(gri): Make sure a comment doesn't accidentally introduce
+       //            a newline and thus cause a semicolon to be inserted.
+       //            Remove this after transitioning to new semicolon
+       //            syntax and some reasonable grace period (12/11/09).
+       if p.commentBefore(pos) {
+               p.comment.List[0].Position = pos
+       }
+}
+
+
 // block prints an *ast.BlockStmt; it always spans at least two lines.
 func (p *printer) block(s *ast.BlockStmt, indent int) {
+       p.moveCommentsAfter(s.Pos());
        p.print(s.Pos(), token.LBRACE);
        p.stmtList(s.List, indent);
        p.linebreak(s.Rbrace.Line, 1, maxStmtNewlines, ignore, true);
@@ -1109,12 +1121,18 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo
                        p.expr(s.Name, multiLine);
                        p.print(blank);
                }
+               p.moveCommentsAfter(s.Path[0].Pos());
                p.expr(&ast.StringList{s.Path}, multiLine);
                comment = s.Comment;
 
        case *ast.ValueSpec:
                p.leadComment(s.Doc);
                p.identList(s.Names, multiLine);        // always present
+               if s.Values != nil {
+                       p.moveCommentsAfter(s.Values[0].Pos())
+               } else if s.Type != nil {
+                       p.moveCommentsAfter(s.Type.Pos())
+               }
                if n == 1 {
                        if s.Type != nil {
                                p.print(blank);
@@ -1147,6 +1165,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo
        case *ast.TypeSpec:
                p.leadComment(s.Doc);
                p.expr(s.Name, multiLine);
+               p.moveCommentsAfter(s.Type.Pos());
                if n == 1 {
                        p.print(blank)
                } else {
index c85ddb00f8bfddc47d0225ab0ed4ef2e714c0cd5..823f61740dd2907c756f5568bfb9b8cd410054b1 100644 (file)
@@ -6,10 +6,12 @@ package printer
 
 import (
        "bytes";
+       oldParser "exp/parser";
        "flag";
        "io/ioutil";
        "go/ast";
        "go/parser";
+       "os";
        "path";
        "testing";
 )
@@ -38,12 +40,19 @@ type checkMode uint
 const (
        export  checkMode       = 1 << iota;
        rawFormat;
+       oldSyntax;
 )
 
 
 func check(t *testing.T, source, golden string, mode checkMode) {
        // parse source
-       prog, err := parser.ParseFile(source, nil, parser.ParseComments);
+       var prog *ast.File;
+       var err os.Error;
+       if mode&oldSyntax != 0 {
+               prog, err = oldParser.ParseFile(source, nil, parser.ParseComments)
+       } else {
+               prog, err = parser.ParseFile(source, nil, parser.ParseComments)
+       }
        if err != nil {
                t.Error(err);
                return;
@@ -127,7 +136,7 @@ func Test(t *testing.T) {
        for _, e := range data {
                source := path.Join(dataDir, e.source);
                golden := path.Join(dataDir, e.golden);
-               check(t, source, golden, e.mode);
+               check(t, source, golden, e.mode|oldSyntax);
                // TODO(gri) check that golden is idempotent
                //check(t, golden, golden, e.mode);
        }
index 70be9aa2ea6f90e10180d489fd0cc97281b256c6..089fac448cdfd3832c3a8c20c16a3bd0f5a62dbf 100644 (file)
@@ -23,7 +23,6 @@ import (
        aLongRename "io";
 
        b "io";
-       c "i" "o";
 )
 
 // no newlines between consecutive single imports, but
@@ -41,6 +40,10 @@ import _ "fmt"
 import _ "fmt"
 import _ "fmt"
 
+// make sure a comment doesn't cause semicolons to be inserted
+import _ "foo" // a comment
+import "bar"   // a comment
+
 
 // at least one empty line between declarations of different kind
 import _ "io"
@@ -444,6 +447,13 @@ func _() {
 }
 
 
+func _() {
+       var _ = T{
+               a,      // must introduce trailing comma
+       };
+}
+
+
 // formatting of consecutive single-line functions
 func _()       {}
 func _()       {}
@@ -456,3 +466,67 @@ func _()   {}
 func _()               { f(1, 2, 3) }
 func _(x int) int      { return x + 1 }
 func _() int           { type T struct{} }
+
+
+// making function declarations safe for new semicolon rules
+func _() { /* one-line func */ }
+
+func _() { // opening "{" must move up /* one-line func */ }
+
+func _() {     // opening "{" must move up// multi-line func
+
+       // in the following declarations, a comment must not
+       // introduce a newline and thus cause a semicolon to
+       // be inserted
+       const _ T = x // comment
+       ;
+       const _ = x // comment
+       ;
+
+       type _ T // comment
+       ;
+       type _ struct // comment
+       {
+
+       }
+       type _ interface // comment
+       {
+
+       }
+       type _ * // comment
+       T;
+       type _ [ // comment
+       ]T;
+       type _ [ // comment
+       10]T;
+       type _ chan // comment
+       T;
+       type _ map // comment
+       [T]T;
+
+       var _ T // comment
+       ;
+       var _ T = x // comment
+       ;
+       var _ struct // comment
+       {
+
+       }
+       var _ interface // comment
+       {
+
+       }
+       var _ * // comment
+       T;
+       var _ [ // comment
+       ]T;
+       var _ [ // comment
+       10]T;
+       var _ chan // comment
+       T;
+       var _ map // comment
+       [T]T;
+
+       var _ = x // comment
+       ;
+}
index c54a2ce22251828092ad4c6727b874e06b4708ca..b876815f220f96d604f2c35b2b5665c51ed3e505 100644 (file)
@@ -23,7 +23,6 @@ import (
        aLongRename "io";
 
        b "io";
-       c "i" "o";
 )
 
 // no newlines between consecutive single imports, but
@@ -41,6 +40,12 @@ import _ "fmt"
 import _ "fmt"
 import _ "fmt"
 
+// make sure a comment doesn't cause semicolons to be inserted
+import _ // a comment
+       "foo"
+import // a comment
+       "bar"
+
 
 // at least one empty line between declarations of different kind
 import _ "io"
@@ -266,11 +271,11 @@ func _() {
        )
        // respect original line breaks
        var _ = []T {
-               T{0x20, "Telugu"}
+               T{0x20, "Telugu"},
        };
        var _ = []T {
                // respect original line breaks
-               T{0x20, "Telugu"}
+               T{0x20, "Telugu"},
        };
 }
 
@@ -436,7 +441,14 @@ func _() {
                        "panicln": nil,
                        "print": nil,
                        "println": nil,
-               }
+               },
+       }
+}
+
+
+func _() {
+       var _ = T{
+               a       // must introduce trailing comma
        }
 }
 
@@ -459,3 +471,62 @@ func _(x int) int {
 func _() int {
        type T struct{}
 }
+
+
+// making function declarations safe for new semicolon rules
+func _()
+{ /* one-line func */ }
+
+func _()  // opening "{" must move up
+{ /* one-line func */ }
+
+func _()  // opening "{" must move up
+// multi-line func
+{
+       // in the following declarations, a comment must not
+       // introduce a newline and thus cause a semicolon to
+       // be inserted
+       const _  // comment
+       T = x;
+       const _  // comment
+       = x;
+
+       type _  // comment
+       T;
+       type _  // comment
+       struct {};
+       type _  // comment
+       interface {};
+       type _  // comment
+       *T;
+       type _  // comment
+       []T;
+       type _  // comment
+       [10]T;
+       type _  // comment
+       chan T;
+       type _  // comment
+       map[T]T;
+
+       var _  // comment
+       T;
+       var _  // comment
+       T = x;
+       var _  // comment
+       struct {};
+       var _  // comment
+       interface {};
+       var _  // comment
+       *T;
+       var _  // comment
+       []T;
+       var _  // comment
+       [10]T;
+       var _  // comment
+       chan T;
+       var _  // comment
+       map[T]T;
+
+       var _  // comment
+       = x;
+}
index 0530e81da7efa18b1e5b0d25d61300d8f6efef79..4eab165d6fabb9203ada9fb5d119e83ed3a5e934 100644 (file)
@@ -222,52 +222,52 @@ func _() {
 
 func _() {
        // not not add extra indentation to multi-line string lists
-       _ = "foo" "bar";
-       _ = "foo"
-               "bar"
+       _ = "foo" "bar";
+       _ = "foo" +
+               "bar" +
                "bah";
        _ = []string{
-               "abc"
+               "abc" +
                        "def",
-               "foo"
+               "foo" +
                        "bar",
        };
 }
 
 
 const _ = F1 +
-       `string = "%s";`
-               `ptr = *;`
-               `datafmt.T2 = s ["-" p "-"];`
+       `string = "%s";` +
+       `ptr = *;` +
+       `datafmt.T2 = s ["-" p "-"];`
 
 
-const _ = `datafmt "datafmt";`
-       `default = "%v";`
-       `array = *;`
+const _ = `datafmt "datafmt";` +
+       `default = "%v";` +
+       `array = *;` +
        `datafmt.T3 = s  {" " a a / ","};`
 
 
-const _ = `datafmt "datafmt";`
-       `default = "%v";`
-       `array = *;`
+const _ = `datafmt "datafmt";` +
+       `default = "%v";` +
+       `array = *;` +
        `datafmt.T3 = s  {" " a a / ","};`
 
 
 func _() {
        _ = F1 +
-               `string = "%s";`
-                       `ptr = *;`
-                       `datafmt.T2 = s ["-" p "-"];`;
+               `string = "%s";` +
+               `ptr = *;` +
+               `datafmt.T2 = s ["-" p "-"];`;
 
        _ =
-               `datafmt "datafmt";`
-                       `default = "%v";`
-                       `array = *;`
+               `datafmt "datafmt";` +
+                       `default = "%v";` +
+                       `array = *;` +
                        `datafmt.T3 = s  {" " a a / ","};`;
 
-       _ = `datafmt "datafmt";`
-               `default = "%v";`
-               `array = *;`
+       _ = `datafmt "datafmt";` +
+               `default = "%v";` +
+               `array = *;` +
                `datafmt.T3 = s  {" " a a / ","};`;
 }
 
@@ -279,8 +279,8 @@ func _() {
                c;
        _ = a < b ||
                b < a;
-       _ = "933262154439441526816992388562667004907159682643816214685929"
-               "638952175999932299156089414639761565182862536979208272237582"
+       _ = "933262154439441526816992388562667004907159682643816214685929" +
+               "638952175999932299156089414639761565182862536979208272237582" +
                "51185210916864000000000000000000000000";       // 100!
        _ = "170141183460469231731687303715884105727";  // prime
 }
@@ -290,8 +290,8 @@ func _() {
 const (
        _       = "991";
        _       = "2432902008176640000";        // 20!
-       _       = "933262154439441526816992388562667004907159682643816214685929"
-               "638952175999932299156089414639761565182862536979208272237582"
+       _       = "933262154439441526816992388562667004907159682643816214685929" +
+               "638952175999932299156089414639761565182862536979208272237582" +
                "51185210916864000000000000000000000000";       // 100!
        _       = "170141183460469231731687303715884105727";    // prime
 )
index decb58196dd37f32ce3486b9a5fd39a721d67a04..b271e1c62682ae00abbf168f3a66c455451eb4d9 100644 (file)
@@ -226,53 +226,53 @@ func _() {
 
 func _() {
        // not not add extra indentation to multi-line string lists
-       _ = "foo" "bar";
-       _ = "foo"
-       "bar"
+       _ = "foo" "bar";
+       _ = "foo" +
+       "bar" +
        "bah";
        _ = []string {
-               "abc"
+               "abc" +
                "def",
-               "foo"
-               "bar"
+               "foo" +
+               "bar",
        }
 }
 
 
 const _ = F1 +
-       `string = "%s";`
-       `ptr = *;`
+       `string = "%s";` +
+       `ptr = *;` +
        `datafmt.T2 = s ["-" p "-"];`
 
 
 const _ =
-       `datafmt "datafmt";`
-       `default = "%v";`
-       `array = *;`
+       `datafmt "datafmt";` +
+       `default = "%v";` +
+       `array = *;` +
        `datafmt.T3 = s  {" " a a / ","};`
 
 
-const _ = `datafmt "datafmt";`
-`default = "%v";`
-`array = *;`
+const _ = `datafmt "datafmt";` +
+`default = "%v";` +
+`array = *;` +
 `datafmt.T3 = s  {" " a a / ","};`
 
 
 func _() {
        _ = F1 +
-               `string = "%s";`
-               `ptr = *;`
+               `string = "%s";` +
+               `ptr = *;` +
                `datafmt.T2 = s ["-" p "-"];`;
 
        _ =
-               `datafmt "datafmt";`
-               `default = "%v";`
-               `array = *;`
+               `datafmt "datafmt";` +
+               `default = "%v";` +
+               `array = *;` +
                `datafmt.T3 = s  {" " a a / ","};`;
 
-       _ = `datafmt "datafmt";`
-       `default = "%v";`
-       `array = *;`
+       _ = `datafmt "datafmt";` +
+       `default = "%v";` +
+       `array = *;` +
        `datafmt.T3 = s  {" " a a / ","};`
 }
 
@@ -284,8 +284,8 @@ func _() {
        c;
        _ = a < b ||
                b < a;
-       _ = "933262154439441526816992388562667004907159682643816214685929"
-       "638952175999932299156089414639761565182862536979208272237582"
+       _ = "933262154439441526816992388562667004907159682643816214685929" +
+       "638952175999932299156089414639761565182862536979208272237582" +
        "51185210916864000000000000000000000000";  // 100!
        _ = "170141183460469231731687303715884105727";  // prime
 }
@@ -295,8 +295,8 @@ func _() {
 const (
        _ = "991";
        _ = "2432902008176640000";  // 20!
-       _ = "933262154439441526816992388562667004907159682643816214685929"
-       "638952175999932299156089414639761565182862536979208272237582"
+       _ = "933262154439441526816992388562667004907159682643816214685929" +
+       "638952175999932299156089414639761565182862536979208272237582" +
        "51185210916864000000000000000000000000";  // 100!
        _ = "170141183460469231731687303715884105727";  // prime
 )
@@ -304,15 +304,15 @@ const (
 
 func same(t, u *Time) bool {
        // respect source lines in multi-line expressions
-       return t.Year == u.Year
-               && t.Month == u.Month
-               && t.Day == u.Day
-               && t.Hour == u.Hour
-               && t.Minute == u.Minute
-               && t.Second == u.Second
-               && t.Weekday == u.Weekday
-               && t.ZoneOffset == u.ZoneOffset
-               && t.Zone == u.Zone
+       return t.Year == u.Year &&
+               t.Month == u.Month &&
+               t.Day == u.Day &&
+               t.Hour == u.Hour &&
+               t.Minute == u.Minute &&
+               t.Second == u.Second &&
+               t.Weekday == u.Weekday &&
+               t.ZoneOffset == u.ZoneOffset &&
+               t.Zone == u.Zone
 }
 
 
index 3e4f3264882101dfd88431d776de3898b976d321..2c4bb254f620e1f76ad672393a5f2711573d60bb 100644 (file)
@@ -222,52 +222,52 @@ func _() {
 
 func _() {
        // not not add extra indentation to multi-line string lists
-       _ = "foo" "bar";
-       _ = "foo"
-               "bar"
+       _ = "foo" "bar";
+       _ = "foo" +
+               "bar" +
                "bah";
        _ = []string{
-               "abc"
+               "abc" +
                        "def",
-               "foo"
+               "foo" +
                        "bar",
        };
 }
 
 
 const _ = F1 +
-       `string = "%s";`
-               `ptr = *;`
-               `datafmt.T2 = s ["-" p "-"];`
+       `string = "%s";` +
+       `ptr = *;` +
+       `datafmt.T2 = s ["-" p "-"];`
 
 
-const _ = `datafmt "datafmt";`
-       `default = "%v";`
-       `array = *;`
+const _ = `datafmt "datafmt";` +
+       `default = "%v";` +
+       `array = *;` +
        `datafmt.T3 = s  {" " a a / ","};`
 
 
-const _ = `datafmt "datafmt";`
-       `default = "%v";`
-       `array = *;`
+const _ = `datafmt "datafmt";` +
+       `default = "%v";` +
+       `array = *;` +
        `datafmt.T3 = s  {" " a a / ","};`
 
 
 func _() {
        _ = F1 +
-               `string = "%s";`
-                       `ptr = *;`
-                       `datafmt.T2 = s ["-" p "-"];`;
+               `string = "%s";` +
+               `ptr = *;` +
+               `datafmt.T2 = s ["-" p "-"];`;
 
        _ =
-               `datafmt "datafmt";`
-                       `default = "%v";`
-                       `array = *;`
+               `datafmt "datafmt";` +
+                       `default = "%v";` +
+                       `array = *;` +
                        `datafmt.T3 = s  {" " a a / ","};`;
 
-       _ = `datafmt "datafmt";`
-               `default = "%v";`
-               `array = *;`
+       _ = `datafmt "datafmt";` +
+               `default = "%v";` +
+               `array = *;` +
                `datafmt.T3 = s  {" " a a / ","};`;
 }
 
@@ -279,8 +279,8 @@ func _() {
                c;
        _ = a < b ||
                b < a;
-       _ = "933262154439441526816992388562667004907159682643816214685929"
-               "638952175999932299156089414639761565182862536979208272237582"
+       _ = "933262154439441526816992388562667004907159682643816214685929" +
+               "638952175999932299156089414639761565182862536979208272237582" +
                "51185210916864000000000000000000000000";       // 100!
        _ = "170141183460469231731687303715884105727";  // prime
 }
@@ -290,8 +290,8 @@ func _() {
 const (
        _               = "991";
        _               = "2432902008176640000";        // 20!
-       _               = "933262154439441526816992388562667004907159682643816214685929"
-               "638952175999932299156089414639761565182862536979208272237582"
+       _               = "933262154439441526816992388562667004907159682643816214685929" +
+               "638952175999932299156089414639761565182862536979208272237582" +
                "51185210916864000000000000000000000000";       // 100!
        _               = "170141183460469231731687303715884105727";    // prime
 )
index 22ac8dd3032e1d57cea046d2d8672c5ad275d025..a4602c90ce2f6bc5080819a093d45a853e605ea0 100644 (file)
@@ -177,15 +177,15 @@ var facts = map[int]string{
        2: "2",
        10: "3628800",
        20: "2432902008176640000",
-       100: "933262154439441526816992388562667004907159682643816214685929"
-               "638952175999932299156089414639761565182862536979208272237582"
+       100: "933262154439441526816992388562667004907159682643816214685929" +
+               "638952175999932299156089414639761565182862536979208272237582" +
                "51185210916864000000000000000000000000",
 }
 
 func usage() {
        fmt.Fprintf(os.Stderr,
                // TODO(gri): the 2nd string of this string list should not be indented
-               "usage: godoc package [name ...]\n"
+               "usage: godoc package [name ...]\n"+
                        "       godoc -http=:6060\n");
        flag.PrintDefaults();
        os.Exit(2);
index 6624e55d0b456e08dabede2184b6364a1a54b3b0..9aa7bc075cc57e5b58c9c09a17297626d8be8ea5 100644 (file)
@@ -56,7 +56,7 @@ var writerTests = []*writerTest{
                                },
                                contents: "Google.com\n",
                        },
-               }
+               },
        },
        // The truncated test file was produced using these commands:
        //   dd if=/dev/zero bs=1048576 count=16384 > /tmp/16gig.txt
@@ -177,17 +177,16 @@ var facts = map[int] string {
        2: "2",
        10: "3628800",
        20: "2432902008176640000",
-       100: "933262154439441526816992388562667004907159682643816214685929"
-               "638952175999932299156089414639761565182862536979208272237582"
+       100: "933262154439441526816992388562667004907159682643816214685929" +
+               "638952175999932299156089414639761565182862536979208272237582" +
                "51185210916864000000000000000000000000",
 }
 
 func usage() {
        fmt.Fprintf(os.Stderr,
                // TODO(gri): the 2nd string of this string list should not be indented
-               "usage: godoc package [name ...]\n"
-               "       godoc -http=:6060\n"
-       );
+               "usage: godoc package [name ...]\n" +
+               "       godoc -http=:6060\n");
        flag.PrintDefaults();
        os.Exit(2);
 }