Debian Codegraph @

go/types/typeutil/ui_test.go

raw symbols SCIP
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.
4
5 package typeutil_test
6
7 import (
8 "fmt"
9 "go/ast"
10 "go/parser"
11 "go/token"
12 "go/types"
13 "strings"
14 "testing"
15
16 "golang.org/x/tools/go/types/typeutil"
17 )
18
19 func TestIntuitiveMethodSet(t *testing.T) {
20 const source = `
21 package P
22 type A int
23 func (A) f()
24 func (*A) g()
25 `
26
27 fset := token.NewFileSet()
28 f, err := parser.ParseFile(fset, "hello.go", source, 0)
29 if err != nil {
30 t.Fatal(err)
31 }
32
33 var conf types.Config
34 pkg, err := conf.Check("P", fset, []*ast.File{f}, nil)
35 if err != nil {
36 t.Fatal(err)
37 }
38 qual := types.RelativeTo(pkg)
39
40 for _, test := range []struct {
41 expr string // type expression
42 want string // intuitive method set
43 }{
44 {"A", "(A).f (*A).g"},
45 {"*A", "(*A).f (*A).g"},
46 {"error", "(error).Error"},
47 {"*error", ""},
48 {"struct{A}", "(struct{A}).f (*struct{A}).g"},
49 {"*struct{A}", "(*struct{A}).f (*struct{A}).g"},
50 } {
51 tv, err := types.Eval(fset, pkg, 0, test.expr)
52 if err != nil {
53 t.Errorf("Eval(%s) failed: %v", test.expr, err)
54 }
55 var names []string
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)
59 }
60 got := strings.Join(names, " ")
61 if got != test.want {
62 t.Errorf("IntuitiveMethodSet(%s) = %q, want %q", test.expr, got, test.want)
63 }
64 }
65 }