1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package objectpath_test
import (
"bytes"
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"strings"
"testing"
"golang.org/x/tools/go/buildutil"
"golang.org/x/tools/go/gcexportdata"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/types/objectpath"
)
func TestPaths(t *testing.T) {
pkgs := map[string]map[string]string{
"b": {"b.go": `
package b
import "a"
const C = a.Int(0)
func F(a, b, c int, d a.T)
type T struct{ A int; b int; a.T }
func (T) M() *interface{ f() }
type U T
type A = struct{ x int }
var V []*a.T
type M map[struct{x int}]struct{y int}
func unexportedFunc()
type unexportedType struct{}
type S struct{t struct{x int}}
type R []struct{y int}
type Q [2]struct{z int}
`},
"a": {"a.go": `
package a
type Int int
type T struct{x, y int}
`},
}
paths := []pathTest{
// Good paths
{"b", "C", "const b.C a.Int", ""},
{"b", "F", "func b.F(a int, b int, c int, d a.T)", ""},
{"b", "F.PA0", "var a int", ""},
{"b", "F.PA1", "var b int", ""},
{"b", "F.PA2", "var c int", ""},
{"b", "F.PA3", "var d a.T", ""},
{"b", "T", "type b.T struct{A int; b int; a.T}", ""},
{"b", "T.O", "type b.T struct{A int; b int; a.T}", ""},
{"b", "T.UF0", "field A int", ""},
{"b", "T.UF1", "field b int", ""},
{"b", "T.UF2", "field T a.T", ""},
{"b", "U.UF2", "field T a.T", ""}, // U.U... are aliases for T.U...
{"b", "A", "type b.A = struct{x int}", ""},
{"b", "A.F0", "field x int", ""},
{"b", "V", "var b.V []*a.T", ""},
{"b", "M", "type b.M map[struct{x int}]struct{y int}", ""},
{"b", "M.UKF0", "field x int", ""},
{"b", "M.UEF0", "field y int", ""},
{"b", "T.M0", "func (b.T).M() *interface{f()}", ""}, // concrete method
{"b", "T.M0.RA0", "var *interface{f()}", ""}, // parameter
{"b", "T.M0.RA0.EM0", "func (interface).f()", ""}, // interface method
{"b", "unexportedType", "type b.unexportedType struct{}", ""},
{"b", "S.UF0.F0", "field x int", ""},
{"b", "R.UEF0", "field y int", ""},
{"b", "Q.UEF0", "field z int", ""},
{"a", "T", "type a.T struct{x int; y int}", ""},
{"a", "T.UF0", "field x int", ""},
// Bad paths
{"b", "", "", "empty path"},
{"b", "missing", "", `package b does not contain "missing"`},
{"b", "F.U", "", "invalid path: ends with 'U', want [AFMO]"},
{"b", "F.PA3.O", "", "path denotes type a.T struct{x int; y int}, which belongs to a different package"},
{"b", "F.PA!", "", `invalid path: bad numeric operand "" for code 'A'`},
{"b", "F.PA3.UF0", "", "path denotes field x int, which belongs to a different package"},
{"b", "F.PA3.UF5", "", "field index 5 out of range [0-2)"},
{"b", "V.EE", "", "invalid path: ends with 'E', want [AFMO]"},
{"b", "F..O", "", "invalid path: unexpected '.' in type context"},
{"b", "T.OO", "", "invalid path: code 'O' in object context"},
{"b", "T.EO", "", "cannot apply 'E' to b.T (got *types.Named, want pointer, slice, array, chan or map)"},
{"b", "A.O", "", "cannot apply 'O' to struct{x int} (got *types.Struct, want named or type param)"},
{"b", "A.UF0", "", "cannot apply 'U' to struct{x int} (got *types.Struct, want named)"},
{"b", "M.UPO", "", "cannot apply 'P' to map[struct{x int}]struct{y int} (got *types.Map, want signature)"},
{"b", "C.O", "", "path denotes type a.Int int, which belongs to a different package"},
{"b", "T.M9", "", "method index 9 out of range [0-1)"},
{"b", "M.UF0", "", "cannot apply 'F' to map[struct{x int}]struct{y int} (got *types.Map, want struct)"},
{"b", "V.KO", "", "cannot apply 'K' to []*a.T (got *types.Slice, want map)"},
{"b", "V.A4", "", "cannot apply 'A' to []*a.T (got *types.Slice, want tuple)"},
{"b", "V.RA0", "", "cannot apply 'R' to []*a.T (got *types.Slice, want signature)"},
{"b", "F.PA4", "", "tuple index 4 out of range [0-4)"},
{"b", "F.XO", "", "invalid path: unknown code 'X'"},
}
conf := loader.Config{Build: buildutil.FakeContext(pkgs)}
conf.Import("a")
conf.Import("b")
prog, err := conf.Load()
if err != nil {
t.Fatal(err)
}
for _, test := range paths {
if err := testPath(prog, test); err != nil {
t.Error(err)
}
}
// bad objects
bInfo := prog.Imported["b"]
for _, test := range []struct {
obj types.Object
wantErr string
}{
{types.Universe.Lookup("nil"), "predeclared nil has no path"},
{types.Universe.Lookup("len"), "predeclared builtin len has no path"},
{types.Universe.Lookup("int"), "predeclared type int has no path"},
{bInfo.Implicits[bInfo.Files[0].Imports[0]], "no path for package a"}, // import "a"
{bInfo.Pkg.Scope().Lookup("unexportedFunc"), "no path for non-exported func b.unexportedFunc()"},
} {
path, err := objectpath.For(test.obj)
if err == nil {
t.Errorf("Object(%s) = %q, want error", test.obj, path)
continue
}
if err.Error() != test.wantErr {
t.Errorf("Object(%s) error was %q, want %q", test.obj, err, test.wantErr)
continue
}
}
}
type pathTest struct {
pkg string
path objectpath.Path
wantobj string
wantErr string
}
func testPath(prog *loader.Program, test pathTest) error {
// We test objectpath by enumerating a set of paths
// and ensuring that Path(pkg, Object(pkg, path)) == path.
//
// It might seem more natural to invert the test:
// identify a set of objects and for each one,
// ensure that Object(pkg, Path(pkg, obj)) == obj.
// However, for most interesting test cases there is no
// easy way to identify the object short of applying
// a series of destructuring operations to pkg---which
// is essentially what objectpath.Object does.
// (We do a little of that when testing bad paths, below.)
//
// The downside is that the test depends on the path encoding.
// The upside is that the test exercises the encoding.
pkg := prog.Imported[test.pkg].Pkg
// check path -> object
obj, err := objectpath.Object(pkg, test.path)
if (test.wantErr != "") != (err != nil) {
return fmt.Errorf("Object(%s, %q) returned error %q, want %q", pkg.Path(), test.path, err, test.wantErr)
}
if test.wantErr != "" {
if got := stripSubscripts(err.Error()); got != test.wantErr {
return fmt.Errorf("Object(%s, %q) error was %q, want %q",
pkg.Path(), test.path, got, test.wantErr)
}
return nil
}
// Inv: err == nil
if objString := stripSubscripts(obj.String()); objString != test.wantobj {
return fmt.Errorf("Object(%s, %q) = %s, want %s", pkg.Path(), test.path, objString, test.wantobj)
}
if obj.Pkg() != pkg {
return fmt.Errorf("Object(%s, %q) = %v, which belongs to package %s",
pkg.Path(), test.path, obj, obj.Pkg().Path())
}
// check object -> path
path2, err := objectpath.For(obj)
if err != nil {
return fmt.Errorf("For(%v) failed: %v, want %q", obj, err, test.path)
}
// We do not require that test.path == path2. Aliases are legal.
// But we do require that Object(path2) finds the same object.
obj2, err := objectpath.Object(pkg, path2)
if err != nil {
return fmt.Errorf("Object(%s, %q) failed: %v (roundtrip from %q)", pkg.Path(), path2, err, test.path)
}
if obj2 != obj {
return fmt.Errorf("Object(%s, For(obj)) != obj: got %s, obj is %s (path1=%q, path2=%q)", pkg.Path(), obj2, obj, test.path, path2)
}
return nil
}
// stripSubscripts removes type parameter id subscripts.
//
// TODO(rfindley): remove this function once subscripts are removed from the
// type parameter type string.
func stripSubscripts(s string) string {
var runes []rune
for _, r := range s {
// For debugging/uniqueness purposes, TypeString on a type parameter adds a
// subscript corresponding to the type parameter's unique id. This is going
// to be removed, but in the meantime we skip the subscript runes to get a
// deterministic output.
if '₀' <= r && r < '₀'+10 {
continue // trim type parameter subscripts
}
runes = append(runes, r)
}
return string(runes)
}
// TestSourceAndExportData uses objectpath to compute a correspondence
// of objects between two versions of the same package, one loaded from
// source, the other from export data.
func TestSourceAndExportData(t *testing.T) {
const src = `
package p
type I int
func (I) F() *struct{ X, Y int } {
return nil
}
type Foo interface {
Method() (string, func(int) struct{ X int })
}
var X chan struct{ Z int }
var Z map[string]struct{ A int }
`
// Parse source file and type-check it as a package, "src".
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "src.go", src, 0)
if err != nil {
t.Fatal(err)
}
conf := types.Config{Importer: importer.For("source", nil)}
info := &types.Info{
Defs: make(map[*ast.Ident]types.Object),
}
srcpkg, err := conf.Check("src/p", fset, []*ast.File{f}, info)
if err != nil {
t.Fatal(err)
}
// Export binary export data then reload it as a new package, "bin".
var buf bytes.Buffer
if err := gcexportdata.Write(&buf, fset, srcpkg); err != nil {
t.Fatal(err)
}
imports := make(map[string]*types.Package)
binpkg, err := gcexportdata.Read(&buf, fset, imports, "bin/p")
if err != nil {
t.Fatal(err)
}
// Now find the correspondences between them.
for _, srcobj := range info.Defs {
if srcobj == nil {
continue // e.g. package declaration
}
if _, ok := srcobj.(*types.PkgName); ok {
continue // PkgName has no objectpath
}
path, err := objectpath.For(srcobj)
if err != nil {
t.Errorf("For(%v): %v", srcobj, err)
continue
}
binobj, err := objectpath.Object(binpkg, path)
if err != nil {
t.Errorf("Object(%s, %q): %v", binpkg.Path(), path, err)
continue
}
// Check the object strings match.
// (We can't check that types are identical because the
// objects belong to different type-checker realms.)
srcstr := objectString(srcobj)
binstr := objectString(binobj)
if srcstr != binstr {
t.Errorf("ObjectStrings do not match: Object(For(%q)) = %s, want %s",
path, srcstr, binstr)
continue
}
}
}
func objectString(obj types.Object) string {
s := types.ObjectString(obj, (*types.Package).Name)
// The printing of interface methods changed in go1.11.
// This work-around makes the specific test pass with earlier versions.
s = strings.Replace(s, "func (interface).Method", "func (p.Foo).Method", -1)
return s
}
// TestOrdering uses objectpath over two Named types with the same method
// names but in a different source order and checks that objectpath is the
// same for methods with the same name.
func TestOrdering(t *testing.T) {
pkgs := map[string]map[string]string{
"p": {"p.go": `
package p
type T struct{ A int }
func (T) M() { }
func (T) N() { }
func (T) X() { }
func (T) Y() { }
`},
"q": {"q.go": `
package q
type T struct{ A int }
func (T) N() { }
func (T) M() { }
func (T) Y() { }
func (T) X() { }
`}}
conf := loader.Config{Build: buildutil.FakeContext(pkgs)}
conf.Import("p")
conf.Import("q")
prog, err := conf.Load()
if err != nil {
t.Fatal(err)
}
p := prog.Imported["p"].Pkg
q := prog.Imported["q"].Pkg
// From here, the objectpaths generated for p and q should be the
// same. If they are not, then we are generating an ordering that is
// dependent on the declaration of the types within the file.
for _, test := range []struct {
path objectpath.Path
}{
{"T.M0"},
{"T.M1"},
{"T.M2"},
{"T.M3"},
} {
pobj, err := objectpath.Object(p, test.path)
if err != nil {
t.Errorf("Object(%s) failed in a1: %v", test.path, err)
continue
}
qobj, err := objectpath.Object(q, test.path)
if err != nil {
t.Errorf("Object(%s) failed in a2: %v", test.path, err)
continue
}
if pobj.Name() != pobj.Name() {
t.Errorf("Objects(%s) not equal, got a1 = %v, a2 = %v", test.path, pobj.Name(), qobj.Name())
}
}
}
|