1
0
mirror of https://github.com/golang/go synced 2024-11-05 16:26:11 -07:00

go/gcexportdata: use a constant from a frozen package for testing

Switch from test to use net/rpc instead of text/scanner, since net/rpc
is a frozen package that is guaranteed not to change.

Change-Id: I6ece679bc16737fa9a75ac896f9eb2190317a498
Reviewed-on: https://go-review.googlesource.com/c/163204
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Rebecca Stambler 2019-02-21 12:51:16 -05:00
parent 9c8c5aeafa
commit 83362c3779

View File

@ -70,15 +70,15 @@ func ExampleRead() {
// ExampleNewImporter demonstrates usage of NewImporter to provide type // ExampleNewImporter demonstrates usage of NewImporter to provide type
// information for dependencies when type-checking Go source code. // information for dependencies when type-checking Go source code.
func ExampleNewImporter() { func ExampleNewImporter() {
const src = `package myscanner const src = `package myrpc
// choosing a package that is unlikely to change across releases // choosing a package that doesn't change across releases
import "text/scanner" import "net/rpc"
const scanIdents = scanner.ScanIdents const defaultRPCPath = rpc.DefaultRPCPath
` `
fset := token.NewFileSet() fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "myscanner.go", src, 0) f, err := parser.ParseFile(fset, "myrpc.go", src, 0)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -86,13 +86,13 @@ const scanIdents = scanner.ScanIdents
packages := make(map[string]*types.Package) packages := make(map[string]*types.Package)
imp := gcexportdata.NewImporter(fset, packages) imp := gcexportdata.NewImporter(fset, packages)
conf := types.Config{Importer: imp} conf := types.Config{Importer: imp}
pkg, err := conf.Check("myscanner", fset, []*ast.File{f}, nil) pkg, err := conf.Check("myrpc", fset, []*ast.File{f}, nil)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// object from imported package // object from imported package
pi := packages["text/scanner"].Scope().Lookup("ScanIdents") pi := packages["net/rpc"].Scope().Lookup("DefaultRPCPath")
fmt.Printf("const %s.%s %s = %s // %s\n", fmt.Printf("const %s.%s %s = %s // %s\n",
pi.Pkg().Path(), pi.Pkg().Path(),
pi.Name(), pi.Name(),
@ -102,7 +102,7 @@ const scanIdents = scanner.ScanIdents
) )
// object in source package // object in source package
twopi := pkg.Scope().Lookup("scanIdents") twopi := pkg.Scope().Lookup("defaultRPCPath")
fmt.Printf("const %s %s = %s // %s\n", fmt.Printf("const %s %s = %s // %s\n",
twopi.Name(), twopi.Name(),
twopi.Type(), twopi.Type(),
@ -112,8 +112,8 @@ const scanIdents = scanner.ScanIdents
// Output: // Output:
// //
// const text/scanner.ScanIdents untyped int = 4 // $GOROOT/src/text/scanner/scanner.go:62:1 // const net/rpc.DefaultRPCPath untyped string = "/_goRPC_" // $GOROOT/src/net/rpc/server.go:146:1
// const scanIdents untyped int = 4 // myscanner.go:6:7 // const defaultRPCPath untyped string = "/_goRPC_" // myrpc.go:6:7
} }
func slashify(posn token.Position) token.Position { func slashify(posn token.Position) token.Position {