Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.0.1-2020.1.5 / go / types / typeutil / ui_test.go
1 package typeutil_test
2
3 import (
4         "fmt"
5         "go/ast"
6         "go/parser"
7         "go/token"
8         "go/types"
9         "strings"
10         "testing"
11
12         "honnef.co/go/tools/go/types/typeutil"
13 )
14
15 func TestIntuitiveMethodSet(t *testing.T) {
16         const source = `
17 package P
18 type A int
19 func (A) f()
20 func (*A) g()
21 `
22
23         fset := token.NewFileSet()
24         f, err := parser.ParseFile(fset, "hello.go", source, 0)
25         if err != nil {
26                 t.Fatal(err)
27         }
28
29         var conf types.Config
30         pkg, err := conf.Check("P", fset, []*ast.File{f}, nil)
31         if err != nil {
32                 t.Fatal(err)
33         }
34         qual := types.RelativeTo(pkg)
35
36         for _, test := range []struct {
37                 expr string // type expression
38                 want string // intuitive method set
39         }{
40                 {"A", "(A).f (*A).g"},
41                 {"*A", "(*A).f (*A).g"},
42                 {"error", "(error).Error"},
43                 {"*error", ""},
44                 {"struct{A}", "(struct{A}).f (*struct{A}).g"},
45                 {"*struct{A}", "(*struct{A}).f (*struct{A}).g"},
46         } {
47                 tv, err := types.Eval(fset, pkg, 0, test.expr)
48                 if err != nil {
49                         t.Errorf("Eval(%s) failed: %v", test.expr, err)
50                 }
51                 var names []string
52                 for _, m := range typeutil.IntuitiveMethodSet(tv.Type, nil) {
53                         name := fmt.Sprintf("(%s).%s", types.TypeString(m.Recv(), qual), m.Obj().Name())
54                         names = append(names, name)
55                 }
56                 got := strings.Join(names, " ")
57                 if got != test.want {
58                         t.Errorf("IntuitiveMethodSet(%s) = %q, want %q", test.expr, got, test.want)
59                 }
60         }
61 }