1
0
mirror of https://github.com/golang/go synced 2024-11-25 11:48:04 -07:00

fix printer test for new syntax

R=rsc
https://golang.org/cl/175048
This commit is contained in:
Robert Griesemer 2009-12-11 15:31:06 -08:00
parent 857d4cf1a9
commit b9b89f56ad
9 changed files with 277 additions and 105 deletions

View 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. // block prints an *ast.BlockStmt; it always spans at least two lines.
func (p *printer) block(s *ast.BlockStmt, indent int) { func (p *printer) block(s *ast.BlockStmt, indent int) {
p.moveCommentsAfter(s.Pos());
p.print(s.Pos(), token.LBRACE); p.print(s.Pos(), token.LBRACE);
p.stmtList(s.List, indent); p.stmtList(s.List, indent);
p.linebreak(s.Rbrace.Line, 1, maxStmtNewlines, ignore, true); 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.expr(s.Name, multiLine);
p.print(blank); p.print(blank);
} }
p.moveCommentsAfter(s.Path[0].Pos());
p.expr(&ast.StringList{s.Path}, multiLine); p.expr(&ast.StringList{s.Path}, multiLine);
comment = s.Comment; comment = s.Comment;
case *ast.ValueSpec: case *ast.ValueSpec:
p.leadComment(s.Doc); p.leadComment(s.Doc);
p.identList(s.Names, multiLine); // always present 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 n == 1 {
if s.Type != nil { if s.Type != nil {
p.print(blank); p.print(blank);
@ -1147,6 +1165,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo
case *ast.TypeSpec: case *ast.TypeSpec:
p.leadComment(s.Doc); p.leadComment(s.Doc);
p.expr(s.Name, multiLine); p.expr(s.Name, multiLine);
p.moveCommentsAfter(s.Type.Pos());
if n == 1 { if n == 1 {
p.print(blank) p.print(blank)
} else { } else {

View File

@ -6,10 +6,12 @@ package printer
import ( import (
"bytes"; "bytes";
oldParser "exp/parser";
"flag"; "flag";
"io/ioutil"; "io/ioutil";
"go/ast"; "go/ast";
"go/parser"; "go/parser";
"os";
"path"; "path";
"testing"; "testing";
) )
@ -38,12 +40,19 @@ type checkMode uint
const ( const (
export checkMode = 1 << iota; export checkMode = 1 << iota;
rawFormat; rawFormat;
oldSyntax;
) )
func check(t *testing.T, source, golden string, mode checkMode) { func check(t *testing.T, source, golden string, mode checkMode) {
// parse source // 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 { if err != nil {
t.Error(err); t.Error(err);
return; return;
@ -127,7 +136,7 @@ func Test(t *testing.T) {
for _, e := range data { for _, e := range data {
source := path.Join(dataDir, e.source); source := path.Join(dataDir, e.source);
golden := path.Join(dataDir, e.golden); 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 // TODO(gri) check that golden is idempotent
//check(t, golden, golden, e.mode); //check(t, golden, golden, e.mode);
} }

View File

@ -23,7 +23,6 @@ import (
aLongRename "io"; aLongRename "io";
b "io"; b "io";
c "i" "o";
) )
// no newlines between consecutive single imports, but // no newlines between consecutive single imports, but
@ -41,6 +40,10 @@ import _ "fmt"
import _ "fmt" 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 // at least one empty line between declarations of different kind
import _ "io" import _ "io"
@ -444,6 +447,13 @@ func _() {
} }
func _() {
var _ = T{
a, // must introduce trailing comma
};
}
// formatting of consecutive single-line functions // formatting of consecutive single-line functions
func _() {} func _() {}
func _() {} func _() {}
@ -456,3 +466,67 @@ func _() {}
func _() { f(1, 2, 3) } func _() { f(1, 2, 3) }
func _(x int) int { return x + 1 } func _(x int) int { return x + 1 }
func _() int { type T struct{} } 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
;
}

View File

@ -23,7 +23,6 @@ import (
aLongRename "io"; aLongRename "io";
b "io"; b "io";
c "i" "o";
) )
// no newlines between consecutive single imports, but // no newlines between consecutive single imports, but
@ -41,6 +40,12 @@ import _ "fmt"
import _ "fmt" 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 // at least one empty line between declarations of different kind
import _ "io" import _ "io"
@ -266,11 +271,11 @@ func _() {
) )
// respect original line breaks // respect original line breaks
var _ = []T { var _ = []T {
T{0x20, "Telugu"} T{0x20, "Telugu"},
}; };
var _ = []T { var _ = []T {
// respect original line breaks // respect original line breaks
T{0x20, "Telugu"} T{0x20, "Telugu"},
}; };
} }
@ -436,8 +441,15 @@ func _() {
"panicln": nil, "panicln": nil,
"print": nil, "print": nil,
"println": nil, "println": nil,
},
} }
} }
func _() {
var _ = T{
a // must introduce trailing comma
}
} }
@ -459,3 +471,62 @@ func _(x int) int {
func _() int { func _() int {
type T struct{} 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;
}

View File

@ -222,52 +222,52 @@ func _() {
func _() { func _() {
// not not add extra indentation to multi-line string lists // not not add extra indentation to multi-line string lists
_ = "foo" "bar"; _ = "foo" + "bar";
_ = "foo" _ = "foo" +
"bar" "bar" +
"bah"; "bah";
_ = []string{ _ = []string{
"abc" "abc" +
"def", "def",
"foo" "foo" +
"bar", "bar",
}; };
} }
const _ = F1 + const _ = F1 +
`string = "%s";` `string = "%s";` +
`ptr = *;` `ptr = *;` +
`datafmt.T2 = s ["-" p "-"];` `datafmt.T2 = s ["-" p "-"];`
const _ = `datafmt "datafmt";` const _ = `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};` `datafmt.T3 = s {" " a a / ","};`
const _ = `datafmt "datafmt";` const _ = `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};` `datafmt.T3 = s {" " a a / ","};`
func _() { func _() {
_ = F1 + _ = F1 +
`string = "%s";` `string = "%s";` +
`ptr = *;` `ptr = *;` +
`datafmt.T2 = s ["-" p "-"];`; `datafmt.T2 = s ["-" p "-"];`;
_ = _ =
`datafmt "datafmt";` `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};`; `datafmt.T3 = s {" " a a / ","};`;
_ = `datafmt "datafmt";` _ = `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};`; `datafmt.T3 = s {" " a a / ","};`;
} }
@ -279,8 +279,8 @@ func _() {
c; c;
_ = a < b || _ = a < b ||
b < a; b < a;
_ = "933262154439441526816992388562667004907159682643816214685929" _ = "933262154439441526816992388562667004907159682643816214685929" +
"638952175999932299156089414639761565182862536979208272237582" "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100! "51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime _ = "170141183460469231731687303715884105727"; // prime
} }
@ -290,8 +290,8 @@ func _() {
const ( const (
_ = "991"; _ = "991";
_ = "2432902008176640000"; // 20! _ = "2432902008176640000"; // 20!
_ = "933262154439441526816992388562667004907159682643816214685929" _ = "933262154439441526816992388562667004907159682643816214685929" +
"638952175999932299156089414639761565182862536979208272237582" "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100! "51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime _ = "170141183460469231731687303715884105727"; // prime
) )

View File

@ -226,53 +226,53 @@ func _() {
func _() { func _() {
// not not add extra indentation to multi-line string lists // not not add extra indentation to multi-line string lists
_ = "foo" "bar"; _ = "foo" + "bar";
_ = "foo" _ = "foo" +
"bar" "bar" +
"bah"; "bah";
_ = []string { _ = []string {
"abc" "abc" +
"def", "def",
"foo" "foo" +
"bar" "bar",
} }
} }
const _ = F1 + const _ = F1 +
`string = "%s";` `string = "%s";` +
`ptr = *;` `ptr = *;` +
`datafmt.T2 = s ["-" p "-"];` `datafmt.T2 = s ["-" p "-"];`
const _ = const _ =
`datafmt "datafmt";` `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};` `datafmt.T3 = s {" " a a / ","};`
const _ = `datafmt "datafmt";` const _ = `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};` `datafmt.T3 = s {" " a a / ","};`
func _() { func _() {
_ = F1 + _ = F1 +
`string = "%s";` `string = "%s";` +
`ptr = *;` `ptr = *;` +
`datafmt.T2 = s ["-" p "-"];`; `datafmt.T2 = s ["-" p "-"];`;
_ = _ =
`datafmt "datafmt";` `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};`; `datafmt.T3 = s {" " a a / ","};`;
_ = `datafmt "datafmt";` _ = `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};` `datafmt.T3 = s {" " a a / ","};`
} }
@ -284,8 +284,8 @@ func _() {
c; c;
_ = a < b || _ = a < b ||
b < a; b < a;
_ = "933262154439441526816992388562667004907159682643816214685929" _ = "933262154439441526816992388562667004907159682643816214685929" +
"638952175999932299156089414639761565182862536979208272237582" "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100! "51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime _ = "170141183460469231731687303715884105727"; // prime
} }
@ -295,8 +295,8 @@ func _() {
const ( const (
_ = "991"; _ = "991";
_ = "2432902008176640000"; // 20! _ = "2432902008176640000"; // 20!
_ = "933262154439441526816992388562667004907159682643816214685929" _ = "933262154439441526816992388562667004907159682643816214685929" +
"638952175999932299156089414639761565182862536979208272237582" "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100! "51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime _ = "170141183460469231731687303715884105727"; // prime
) )
@ -304,15 +304,15 @@ const (
func same(t, u *Time) bool { func same(t, u *Time) bool {
// respect source lines in multi-line expressions // respect source lines in multi-line expressions
return t.Year == u.Year return t.Year == u.Year &&
&& t.Month == u.Month t.Month == u.Month &&
&& t.Day == u.Day t.Day == u.Day &&
&& t.Hour == u.Hour t.Hour == u.Hour &&
&& t.Minute == u.Minute t.Minute == u.Minute &&
&& t.Second == u.Second t.Second == u.Second &&
&& t.Weekday == u.Weekday t.Weekday == u.Weekday &&
&& t.ZoneOffset == u.ZoneOffset t.ZoneOffset == u.ZoneOffset &&
&& t.Zone == u.Zone t.Zone == u.Zone
} }

View File

@ -222,52 +222,52 @@ func _() {
func _() { func _() {
// not not add extra indentation to multi-line string lists // not not add extra indentation to multi-line string lists
_ = "foo" "bar"; _ = "foo" + "bar";
_ = "foo" _ = "foo" +
"bar" "bar" +
"bah"; "bah";
_ = []string{ _ = []string{
"abc" "abc" +
"def", "def",
"foo" "foo" +
"bar", "bar",
}; };
} }
const _ = F1 + const _ = F1 +
`string = "%s";` `string = "%s";` +
`ptr = *;` `ptr = *;` +
`datafmt.T2 = s ["-" p "-"];` `datafmt.T2 = s ["-" p "-"];`
const _ = `datafmt "datafmt";` const _ = `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};` `datafmt.T3 = s {" " a a / ","};`
const _ = `datafmt "datafmt";` const _ = `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};` `datafmt.T3 = s {" " a a / ","};`
func _() { func _() {
_ = F1 + _ = F1 +
`string = "%s";` `string = "%s";` +
`ptr = *;` `ptr = *;` +
`datafmt.T2 = s ["-" p "-"];`; `datafmt.T2 = s ["-" p "-"];`;
_ = _ =
`datafmt "datafmt";` `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};`; `datafmt.T3 = s {" " a a / ","};`;
_ = `datafmt "datafmt";` _ = `datafmt "datafmt";` +
`default = "%v";` `default = "%v";` +
`array = *;` `array = *;` +
`datafmt.T3 = s {" " a a / ","};`; `datafmt.T3 = s {" " a a / ","};`;
} }
@ -279,8 +279,8 @@ func _() {
c; c;
_ = a < b || _ = a < b ||
b < a; b < a;
_ = "933262154439441526816992388562667004907159682643816214685929" _ = "933262154439441526816992388562667004907159682643816214685929" +
"638952175999932299156089414639761565182862536979208272237582" "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100! "51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime _ = "170141183460469231731687303715884105727"; // prime
} }
@ -290,8 +290,8 @@ func _() {
const ( const (
_ = "991"; _ = "991";
_ = "2432902008176640000"; // 20! _ = "2432902008176640000"; // 20!
_ = "933262154439441526816992388562667004907159682643816214685929" _ = "933262154439441526816992388562667004907159682643816214685929" +
"638952175999932299156089414639761565182862536979208272237582" "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000"; // 100! "51185210916864000000000000000000000000"; // 100!
_ = "170141183460469231731687303715884105727"; // prime _ = "170141183460469231731687303715884105727"; // prime
) )

View File

@ -177,15 +177,15 @@ var facts = map[int]string{
2: "2", 2: "2",
10: "3628800", 10: "3628800",
20: "2432902008176640000", 20: "2432902008176640000",
100: "933262154439441526816992388562667004907159682643816214685929" 100: "933262154439441526816992388562667004907159682643816214685929" +
"638952175999932299156089414639761565182862536979208272237582" "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000", "51185210916864000000000000000000000000",
} }
func usage() { func usage() {
fmt.Fprintf(os.Stderr, fmt.Fprintf(os.Stderr,
// TODO(gri): the 2nd string of this string list should not be indented // 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"); " godoc -http=:6060\n");
flag.PrintDefaults(); flag.PrintDefaults();
os.Exit(2); os.Exit(2);

View File

@ -56,7 +56,7 @@ var writerTests = []*writerTest{
}, },
contents: "Google.com\n", contents: "Google.com\n",
}, },
} },
}, },
// The truncated test file was produced using these commands: // The truncated test file was produced using these commands:
// dd if=/dev/zero bs=1048576 count=16384 > /tmp/16gig.txt // dd if=/dev/zero bs=1048576 count=16384 > /tmp/16gig.txt
@ -177,17 +177,16 @@ var facts = map[int] string {
2: "2", 2: "2",
10: "3628800", 10: "3628800",
20: "2432902008176640000", 20: "2432902008176640000",
100: "933262154439441526816992388562667004907159682643816214685929" 100: "933262154439441526816992388562667004907159682643816214685929" +
"638952175999932299156089414639761565182862536979208272237582" "638952175999932299156089414639761565182862536979208272237582" +
"51185210916864000000000000000000000000", "51185210916864000000000000000000000000",
} }
func usage() { func usage() {
fmt.Fprintf(os.Stderr, fmt.Fprintf(os.Stderr,
// TODO(gri): the 2nd string of this string list should not be indented // 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" " godoc -http=:6060\n");
);
flag.PrintDefaults(); flag.PrintDefaults();
os.Exit(2); os.Exit(2);
} }