1 // Copyright 2016 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.
16 "golang.org/x/tools/go/types/typeutil"
19 func TestIntuitiveMethodSet(t *testing.T) {
27 fset := token.NewFileSet()
28 f, err := parser.ParseFile(fset, "hello.go", source, 0)
34 pkg, err := conf.Check("P", fset, []*ast.File{f}, nil)
38 qual := types.RelativeTo(pkg)
40 for _, test := range []struct {
41 expr string // type expression
42 want string // intuitive method set
44 {"A", "(A).f (*A).g"},
45 {"*A", "(*A).f (*A).g"},
46 {"error", "(error).Error"},
48 {"struct{A}", "(struct{A}).f (*struct{A}).g"},
49 {"*struct{A}", "(*struct{A}).f (*struct{A}).g"},
51 tv, err := types.Eval(fset, pkg, 0, test.expr)
53 t.Errorf("Eval(%s) failed: %v", test.expr, err)
56 for _, m := range typeutil.IntuitiveMethodSet(tv.Type, nil) {
57 name := fmt.Sprintf("(%s).%s", types.TypeString(m.Recv(), qual), m.Obj().Name())
58 names = append(names, name)
60 got := strings.Join(names, " ")
62 t.Errorf("IntuitiveMethodSet(%s) = %q, want %q", test.expr, got, test.want)