github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/go/printer/printer_test.go (about) 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 "errors" 10 "flag" 11 "fmt" 12 "go/ast" 13 "go/parser" 14 "go/token" 15 "io" 16 "io/ioutil" 17 "path/filepath" 18 "testing" 19 "time" 20 ) 21 22 const ( 23 dataDir = "testdata" 24 tabwidth = 8 25 ) 26 27 var update = flag.Bool("update", false, "update golden files") 28 29 var fset = token.NewFileSet() 30 31 type checkMode uint 32 33 const ( 34 export checkMode = 1 << iota 35 rawFormat 36 idempotent 37 ) 38 39 // format parses src, prints the corresponding AST, verifies the resulting 40 // src is syntactically correct, and returns the resulting src or an error 41 // if any. 42 func format(src []byte, mode checkMode) ([]byte, error) { 43 // parse src 44 f, err := parser.ParseFile(fset, "", src, parser.ParseComments) 45 if err != nil { 46 return nil, fmt.Errorf("parse: %s\n%s", err, src) 47 } 48 49 // filter exports if necessary 50 if mode&export != 0 { 51 ast.FileExports(f) // ignore result 52 f.Comments = nil // don't print comments that are not in AST 53 } 54 55 // determine printer configuration 56 cfg := Config{Tabwidth: tabwidth} 57 if mode&rawFormat != 0 { 58 cfg.Mode |= RawFormat 59 } 60 61 // print AST 62 var buf bytes.Buffer 63 if err := cfg.Fprint(&buf, fset, f); err != nil { 64 return nil, fmt.Errorf("print: %s", err) 65 } 66 67 // make sure formatted output is syntactically correct 68 res := buf.Bytes() 69 if _, err := parser.ParseFile(fset, "", res, 0); err != nil { 70 return nil, fmt.Errorf("re-parse: %s\n%s", err, buf.Bytes()) 71 } 72 73 return res, nil 74 } 75 76 // lineAt returns the line in text starting at offset offs. 77 func lineAt(text []byte, offs int) []byte { 78 i := offs 79 for i < len(text) && text[i] != '\n' { 80 i++ 81 } 82 return text[offs:i] 83 } 84 85 // diff compares a and b. 86 func diff(aname, bname string, a, b []byte) error { 87 var buf bytes.Buffer // holding long error message 88 89 // compare lengths 90 if len(a) != len(b) { 91 fmt.Fprintf(&buf, "\nlength changed: len(%s) = %d, len(%s) = %d", aname, len(a), bname, len(b)) 92 } 93 94 // compare contents 95 line := 1 96 offs := 1 97 for i := 0; i < len(a) && i < len(b); i++ { 98 ch := a[i] 99 if ch != b[i] { 100 fmt.Fprintf(&buf, "\n%s:%d:%d: %s", aname, line, i-offs+1, lineAt(a, offs)) 101 fmt.Fprintf(&buf, "\n%s:%d:%d: %s", bname, line, i-offs+1, lineAt(b, offs)) 102 fmt.Fprintf(&buf, "\n\n") 103 break 104 } 105 if ch == '\n' { 106 line++ 107 offs = i + 1 108 } 109 } 110 111 if buf.Len() > 0 { 112 return errors.New(buf.String()) 113 } 114 return nil 115 } 116 117 func runcheck(t *testing.T, source, golden string, mode checkMode) { 118 src, err := ioutil.ReadFile(source) 119 if err != nil { 120 t.Error(err) 121 return 122 } 123 124 res, err := format(src, mode) 125 if err != nil { 126 t.Error(err) 127 return 128 } 129 130 // update golden files if necessary 131 if *update { 132 if err := ioutil.WriteFile(golden, res, 0644); err != nil { 133 t.Error(err) 134 } 135 return 136 } 137 138 // get golden 139 gld, err := ioutil.ReadFile(golden) 140 if err != nil { 141 t.Error(err) 142 return 143 } 144 145 // formatted source and golden must be the same 146 if err := diff(source, golden, res, gld); err != nil { 147 t.Error(err) 148 return 149 } 150 151 if mode&idempotent != 0 { 152 // formatting golden must be idempotent 153 // (This is very difficult to achieve in general and for now 154 // it is only checked for files explicitly marked as such.) 155 res, err = format(gld, mode) 156 if err := diff(golden, fmt.Sprintf("format(%s)", golden), gld, res); err != nil { 157 t.Errorf("golden is not idempotent: %s", err) 158 } 159 } 160 } 161 162 func check(t *testing.T, source, golden string, mode checkMode) { 163 // run the test 164 cc := make(chan int) 165 go func() { 166 runcheck(t, source, golden, mode) 167 cc <- 0 168 }() 169 170 // wait with timeout 171 select { 172 case <-time.After(10 * time.Second): // plenty of a safety margin, even for very slow machines 173 // test running past time out 174 t.Errorf("%s: running too slowly", source) 175 case <-cc: 176 // test finished within allotted time margin 177 } 178 } 179 180 type entry struct { 181 source, golden string 182 mode checkMode 183 } 184 185 // Use go test -update to create/update the respective golden files. 186 var data = []entry{ 187 {"empty.input", "empty.golden", idempotent}, 188 {"comments.input", "comments.golden", 0}, 189 {"comments.input", "comments.x", export}, 190 {"comments2.input", "comments2.golden", idempotent}, 191 {"alignment.input", "alignment.golden", idempotent}, 192 {"linebreaks.input", "linebreaks.golden", idempotent}, 193 {"expressions.input", "expressions.golden", idempotent}, 194 {"expressions.input", "expressions.raw", rawFormat | idempotent}, 195 {"declarations.input", "declarations.golden", 0}, 196 {"statements.input", "statements.golden", 0}, 197 {"slow.input", "slow.golden", idempotent}, 198 {"complit.input", "complit.x", export}, 199 } 200 201 func TestFiles(t *testing.T) { 202 t.Parallel() 203 for _, e := range data { 204 source := filepath.Join(dataDir, e.source) 205 golden := filepath.Join(dataDir, e.golden) 206 mode := e.mode 207 t.Run(e.source, func(t *testing.T) { 208 t.Parallel() 209 check(t, source, golden, mode) 210 // TODO(gri) check that golden is idempotent 211 //check(t, golden, golden, e.mode) 212 }) 213 } 214 } 215 216 // TestLineComments, using a simple test case, checks that consecutive line 217 // comments are properly terminated with a newline even if the AST position 218 // information is incorrect. 219 // 220 func TestLineComments(t *testing.T) { 221 const src = `// comment 1 222 // comment 2 223 // comment 3 224 package main 225 ` 226 227 fset := token.NewFileSet() 228 f, err := parser.ParseFile(fset, "", src, parser.ParseComments) 229 if err != nil { 230 panic(err) // error in test 231 } 232 233 var buf bytes.Buffer 234 fset = token.NewFileSet() // use the wrong file set 235 Fprint(&buf, fset, f) 236 237 nlines := 0 238 for _, ch := range buf.Bytes() { 239 if ch == '\n' { 240 nlines++ 241 } 242 } 243 244 const expected = 3 245 if nlines < expected { 246 t.Errorf("got %d, expected %d\n", nlines, expected) 247 t.Errorf("result:\n%s", buf.Bytes()) 248 } 249 } 250 251 // Verify that the printer can be invoked during initialization. 252 func init() { 253 const name = "foobar" 254 var buf bytes.Buffer 255 if err := Fprint(&buf, fset, &ast.Ident{Name: name}); err != nil { 256 panic(err) // error in test 257 } 258 // in debug mode, the result contains additional information; 259 // ignore it 260 if s := buf.String(); !debug && s != name { 261 panic("got " + s + ", want " + name) 262 } 263 } 264 265 // Verify that the printer doesn't crash if the AST contains BadXXX nodes. 266 func TestBadNodes(t *testing.T) { 267 const src = "package p\n(" 268 const res = "package p\nBadDecl\n" 269 f, err := parser.ParseFile(fset, "", src, parser.ParseComments) 270 if err == nil { 271 t.Error("expected illegal program") // error in test 272 } 273 var buf bytes.Buffer 274 Fprint(&buf, fset, f) 275 if buf.String() != res { 276 t.Errorf("got %q, expected %q", buf.String(), res) 277 } 278 } 279 280 // testComment verifies that f can be parsed again after printing it 281 // with its first comment set to comment at any possible source offset. 282 func testComment(t *testing.T, f *ast.File, srclen int, comment *ast.Comment) { 283 f.Comments[0].List[0] = comment 284 var buf bytes.Buffer 285 for offs := 0; offs <= srclen; offs++ { 286 buf.Reset() 287 // Printing f should result in a correct program no 288 // matter what the (incorrect) comment position is. 289 if err := Fprint(&buf, fset, f); err != nil { 290 t.Error(err) 291 } 292 if _, err := parser.ParseFile(fset, "", buf.Bytes(), 0); err != nil { 293 t.Fatalf("incorrect program for pos = %d:\n%s", comment.Slash, buf.String()) 294 } 295 // Position information is just an offset. 296 // Move comment one byte down in the source. 297 comment.Slash++ 298 } 299 } 300 301 // Verify that the printer produces a correct program 302 // even if the position information of comments introducing newlines 303 // is incorrect. 304 func TestBadComments(t *testing.T) { 305 t.Parallel() 306 const src = ` 307 // first comment - text and position changed by test 308 package p 309 import "fmt" 310 const pi = 3.14 // rough circle 311 var ( 312 x, y, z int = 1, 2, 3 313 u, v float64 314 ) 315 func fibo(n int) { 316 if n < 2 { 317 return n /* seed values */ 318 } 319 return fibo(n-1) + fibo(n-2) 320 } 321 ` 322 323 f, err := parser.ParseFile(fset, "", src, parser.ParseComments) 324 if err != nil { 325 t.Error(err) // error in test 326 } 327 328 comment := f.Comments[0].List[0] 329 pos := comment.Pos() 330 if fset.PositionFor(pos, false /* absolute position */).Offset != 1 { 331 t.Error("expected offset 1") // error in test 332 } 333 334 testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "//-style comment"}) 335 testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style comment */"}) 336 testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style \n comment */"}) 337 testComment(t, f, len(src), &ast.Comment{Slash: pos, Text: "/*-style comment \n\n\n */"}) 338 } 339 340 type visitor chan *ast.Ident 341 342 func (v visitor) Visit(n ast.Node) (w ast.Visitor) { 343 if ident, ok := n.(*ast.Ident); ok { 344 v <- ident 345 } 346 return v 347 } 348 349 // idents is an iterator that returns all idents in f via the result channel. 350 func idents(f *ast.File) <-chan *ast.Ident { 351 v := make(visitor) 352 go func() { 353 ast.Walk(v, f) 354 close(v) 355 }() 356 return v 357 } 358 359 // identCount returns the number of identifiers found in f. 360 func identCount(f *ast.File) int { 361 n := 0 362 for range idents(f) { 363 n++ 364 } 365 return n 366 } 367 368 // Verify that the SourcePos mode emits correct //line directives 369 // by testing that position information for matching identifiers 370 // is maintained. 371 func TestSourcePos(t *testing.T) { 372 const src = ` 373 package p 374 import ( "go/printer"; "math" ) 375 const pi = 3.14; var x = 0 376 type t struct{ x, y, z int; u, v, w float32 } 377 func (t *t) foo(a, b, c int) int { 378 return a*t.x + b*t.y + 379 // two extra lines here 380 // ... 381 c*t.z 382 } 383 ` 384 385 // parse original 386 f1, err := parser.ParseFile(fset, "src", src, parser.ParseComments) 387 if err != nil { 388 t.Fatal(err) 389 } 390 391 // pretty-print original 392 var buf bytes.Buffer 393 err = (&Config{Mode: UseSpaces | SourcePos, Tabwidth: 8}).Fprint(&buf, fset, f1) 394 if err != nil { 395 t.Fatal(err) 396 } 397 398 // parse pretty printed original 399 // (//line directives must be interpreted even w/o parser.ParseComments set) 400 f2, err := parser.ParseFile(fset, "", buf.Bytes(), 0) 401 if err != nil { 402 t.Fatalf("%s\n%s", err, buf.Bytes()) 403 } 404 405 // At this point the position information of identifiers in f2 should 406 // match the position information of corresponding identifiers in f1. 407 408 // number of identifiers must be > 0 (test should run) and must match 409 n1 := identCount(f1) 410 n2 := identCount(f2) 411 if n1 == 0 { 412 t.Fatal("got no idents") 413 } 414 if n2 != n1 { 415 t.Errorf("got %d idents; want %d", n2, n1) 416 } 417 418 // verify that all identifiers have correct line information 419 i2range := idents(f2) 420 for i1 := range idents(f1) { 421 i2 := <-i2range 422 423 if i2.Name != i1.Name { 424 t.Errorf("got ident %s; want %s", i2.Name, i1.Name) 425 } 426 427 // here we care about the relative (line-directive adjusted) positions 428 l1 := fset.Position(i1.Pos()).Line 429 l2 := fset.Position(i2.Pos()).Line 430 if l2 != l1 { 431 t.Errorf("got line %d; want %d for %s", l2, l1, i1.Name) 432 } 433 } 434 435 if t.Failed() { 436 t.Logf("\n%s", buf.Bytes()) 437 } 438 } 439 440 // Verify that the SourcePos mode doesn't emit unnecessary //line directives 441 // before empty lines. 442 func TestIssue5945(t *testing.T) { 443 const orig = ` 444 package p // line 2 445 func f() {} // line 3 446 447 var x, y, z int 448 449 450 func g() { // line 8 451 } 452 ` 453 454 const want = `//line src.go:2 455 package p 456 457 //line src.go:3 458 func f() {} 459 460 var x, y, z int 461 462 //line src.go:8 463 func g() { 464 } 465 ` 466 467 // parse original 468 f1, err := parser.ParseFile(fset, "src.go", orig, 0) 469 if err != nil { 470 t.Fatal(err) 471 } 472 473 // pretty-print original 474 var buf bytes.Buffer 475 err = (&Config{Mode: UseSpaces | SourcePos, Tabwidth: 8}).Fprint(&buf, fset, f1) 476 if err != nil { 477 t.Fatal(err) 478 } 479 got := buf.String() 480 481 // compare original with desired output 482 if got != want { 483 t.Errorf("got:\n%s\nwant:\n%s\n", got, want) 484 } 485 } 486 487 var decls = []string{ 488 `import "fmt"`, 489 "const pi = 3.1415\nconst e = 2.71828\n\nvar x = pi", 490 "func sum(x, y int) int\t{ return x + y }", 491 } 492 493 func TestDeclLists(t *testing.T) { 494 for _, src := range decls { 495 file, err := parser.ParseFile(fset, "", "package p;"+src, parser.ParseComments) 496 if err != nil { 497 panic(err) // error in test 498 } 499 500 var buf bytes.Buffer 501 err = Fprint(&buf, fset, file.Decls) // only print declarations 502 if err != nil { 503 panic(err) // error in test 504 } 505 506 out := buf.String() 507 if out != src { 508 t.Errorf("\ngot : %q\nwant: %q\n", out, src) 509 } 510 } 511 } 512 513 var stmts = []string{ 514 "i := 0", 515 "select {}\nvar a, b = 1, 2\nreturn a + b", 516 "go f()\ndefer func() {}()", 517 } 518 519 func TestStmtLists(t *testing.T) { 520 for _, src := range stmts { 521 file, err := parser.ParseFile(fset, "", "package p; func _() {"+src+"}", parser.ParseComments) 522 if err != nil { 523 panic(err) // error in test 524 } 525 526 var buf bytes.Buffer 527 err = Fprint(&buf, fset, file.Decls[0].(*ast.FuncDecl).Body.List) // only print statements 528 if err != nil { 529 panic(err) // error in test 530 } 531 532 out := buf.String() 533 if out != src { 534 t.Errorf("\ngot : %q\nwant: %q\n", out, src) 535 } 536 } 537 } 538 539 func TestBaseIndent(t *testing.T) { 540 t.Parallel() 541 // The testfile must not contain multi-line raw strings since those 542 // are not indented (because their values must not change) and make 543 // this test fail. 544 const filename = "printer.go" 545 src, err := ioutil.ReadFile(filename) 546 if err != nil { 547 panic(err) // error in test 548 } 549 550 file, err := parser.ParseFile(fset, filename, src, 0) 551 if err != nil { 552 panic(err) // error in test 553 } 554 555 for indent := 0; indent < 4; indent++ { 556 indent := indent 557 t.Run(fmt.Sprint(indent), func(t *testing.T) { 558 t.Parallel() 559 var buf bytes.Buffer 560 (&Config{Tabwidth: tabwidth, Indent: indent}).Fprint(&buf, fset, file) 561 // all code must be indented by at least 'indent' tabs 562 lines := bytes.Split(buf.Bytes(), []byte{'\n'}) 563 for i, line := range lines { 564 if len(line) == 0 { 565 continue // empty lines don't have indentation 566 } 567 n := 0 568 for j, b := range line { 569 if b != '\t' { 570 // end of indentation 571 n = j 572 break 573 } 574 } 575 if n < indent { 576 t.Errorf("line %d: got only %d tabs; want at least %d: %q", i, n, indent, line) 577 } 578 } 579 }) 580 } 581 } 582 583 // TestFuncType tests that an ast.FuncType with a nil Params field 584 // can be printed (per go/ast specification). Test case for issue 3870. 585 func TestFuncType(t *testing.T) { 586 src := &ast.File{ 587 Name: &ast.Ident{Name: "p"}, 588 Decls: []ast.Decl{ 589 &ast.FuncDecl{ 590 Name: &ast.Ident{Name: "f"}, 591 Type: &ast.FuncType{}, 592 }, 593 }, 594 } 595 596 var buf bytes.Buffer 597 if err := Fprint(&buf, fset, src); err != nil { 598 t.Fatal(err) 599 } 600 got := buf.String() 601 602 const want = `package p 603 604 func f() 605 ` 606 607 if got != want { 608 t.Fatalf("got:\n%s\nwant:\n%s\n", got, want) 609 } 610 } 611 612 type limitWriter struct { 613 remaining int 614 errCount int 615 } 616 617 func (l *limitWriter) Write(buf []byte) (n int, err error) { 618 n = len(buf) 619 if n >= l.remaining { 620 n = l.remaining 621 err = io.EOF 622 l.errCount++ 623 } 624 l.remaining -= n 625 return n, err 626 } 627 628 // Test whether the printer stops writing after the first error 629 func TestWriteErrors(t *testing.T) { 630 t.Parallel() 631 const filename = "printer.go" 632 src, err := ioutil.ReadFile(filename) 633 if err != nil { 634 panic(err) // error in test 635 } 636 file, err := parser.ParseFile(fset, filename, src, 0) 637 if err != nil { 638 panic(err) // error in test 639 } 640 for i := 0; i < 20; i++ { 641 lw := &limitWriter{remaining: i} 642 err := (&Config{Mode: RawFormat}).Fprint(lw, fset, file) 643 if lw.errCount > 1 { 644 t.Fatal("Writes continued after first error returned") 645 } 646 // We expect errCount be 1 iff err is set 647 if (lw.errCount != 0) != (err != nil) { 648 t.Fatal("Expected err when errCount != 0") 649 } 650 } 651 } 652 653 // TextX is a skeleton test that can be filled in for debugging one-off cases. 654 // Do not remove. 655 func TestX(t *testing.T) { 656 const src = ` 657 package p 658 func _() {} 659 ` 660 _, err := format([]byte(src), 0) 661 if err != nil { 662 t.Error(err) 663 } 664 } 665 666 func TestCommentedNode(t *testing.T) { 667 const ( 668 input = `package main 669 670 func foo() { 671 // comment inside func 672 } 673 674 // leading comment 675 type bar int // comment2 676 677 ` 678 679 foo = `func foo() { 680 // comment inside func 681 }` 682 683 bar = `// leading comment 684 type bar int // comment2 685 ` 686 ) 687 688 fset := token.NewFileSet() 689 f, err := parser.ParseFile(fset, "input.go", input, parser.ParseComments) 690 if err != nil { 691 t.Fatal(err) 692 } 693 694 var buf bytes.Buffer 695 696 err = Fprint(&buf, fset, &CommentedNode{Node: f.Decls[0], Comments: f.Comments}) 697 if err != nil { 698 t.Fatal(err) 699 } 700 701 if buf.String() != foo { 702 t.Errorf("got %q, want %q", buf.String(), foo) 703 } 704 705 buf.Reset() 706 707 err = Fprint(&buf, fset, &CommentedNode{Node: f.Decls[1], Comments: f.Comments}) 708 if err != nil { 709 t.Fatal(err) 710 } 711 712 if buf.String() != bar { 713 t.Errorf("got %q, want %q", buf.String(), bar) 714 } 715 } 716 717 func TestIssue11151(t *testing.T) { 718 const src = "package p\t/*\r/1\r*\r/2*\r\r\r\r/3*\r\r+\r\r/4*/\n" 719 fset := token.NewFileSet() 720 f, err := parser.ParseFile(fset, "", src, parser.ParseComments) 721 if err != nil { 722 t.Fatal(err) 723 } 724 725 var buf bytes.Buffer 726 Fprint(&buf, fset, f) 727 got := buf.String() 728 const want = "package p\t/*/1*\r/2*\r/3*+/4*/\n" // \r following opening /* should be stripped 729 if got != want { 730 t.Errorf("\ngot : %q\nwant: %q", got, want) 731 } 732 733 // the resulting program must be valid 734 _, err = parser.ParseFile(fset, "", got, 0) 735 if err != nil { 736 t.Errorf("%v\norig: %q\ngot : %q", err, src, got) 737 } 738 } 739 740 // If a declaration has multiple specifications, a parenthesized 741 // declaration must be printed even if Lparen is token.NoPos. 742 func TestParenthesizedDecl(t *testing.T) { 743 // a package with multiple specs in a single declaration 744 const src = "package p; var ( a float64; b int )" 745 fset := token.NewFileSet() 746 f, err := parser.ParseFile(fset, "", src, 0) 747 748 // print the original package 749 var buf bytes.Buffer 750 err = Fprint(&buf, fset, f) 751 if err != nil { 752 t.Fatal(err) 753 } 754 original := buf.String() 755 756 // now remove parentheses from the declaration 757 for i := 0; i != len(f.Decls); i++ { 758 f.Decls[i].(*ast.GenDecl).Lparen = token.NoPos 759 } 760 buf.Reset() 761 err = Fprint(&buf, fset, f) 762 if err != nil { 763 t.Fatal(err) 764 } 765 noparen := buf.String() 766 767 if noparen != original { 768 t.Errorf("got %q, want %q", noparen, original) 769 } 770 }