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 "golang.org/x/tools/internal/testenv"
18 )
19
20 func TestIntuitiveMethodSet(t *testing.T) {
21 const source = `
22 package P
23 type A int
24 func (A) f()
25 func (*A) g()
26 `
27
28 fset := token.NewFileSet()
29 f, err := parser.ParseFile(fset, "hello.go", source, 0)
30 if err != nil {
31 t.Fatal(err)
32 }
33
34 var conf types.Config
35 pkg, err := conf.Check("P", fset, []*ast.File{f}, nil)
36 if err != nil {
37 t.Fatal(err)
38 }
39 qual := types.RelativeTo(pkg)
40
41 for _, test := range []struct {
42 expr string // type expression
43 want string // intuitive method set
44 }{
45 {"A", "(A).f (*A).g"},
46 {"*A", "(*A).f (*A).g"},
47 {"error", "(error).Error"},
48 {"*error", ""},
49 {"struct{A}", "(struct{A}).f (*struct{A}).g"},
50 {"*struct{A}", "(*struct{A}).f (*struct{A}).g"},
51 } {
52 tv, err := types.Eval(fset, pkg, 0, test.expr)
53 if err != nil {
54 t.Errorf("Eval(%s) failed: %v", test.expr, err)
55 }
56 var names []string
57 for _, m := range typeutil.IntuitiveMethodSet(tv.Type, nil) {
58 name := fmt.Sprintf("(%s).%s", types.TypeString(m.Recv(), qual), m.Obj().Name())
59 names = append(names, name)
60 }
61 got := strings.Join(names, " ")
62 if got != test.want {
63 t.Errorf("IntuitiveMethodSet(%s) = %q, want %q", test.expr, got, test.want)
64 }
65 }
66
67 // variant with generic methods
68 if testenv.Go1Point() >= 27 {
69 const source = `
70 package P
71 type A int
72 func (A) f()
73 func (*A) g()
74 func (*A) h[T any]() {}
75 `
76
77 fset := token.NewFileSet()
78 f, err := parser.ParseFile(fset, "hello.go", source, 0)
79 if err != nil {
80 t.Fatal(err)
81 }
82
83 var conf types.Config
84 pkg, err := conf.Check("P", fset, []*ast.File{f}, nil)
85 if err != nil {
86 t.Fatal(err)
87 }
88 qual := types.RelativeTo(pkg)
89
90 for _, test := range []struct {
91 expr string // type expression
92 want string // intuitive method set
93 }{
94 {"A", "(A).f (*A).g (*A).h"},
95 {"*A", "(*A).f (*A).g (*A).h"},
96 {"error", "(error).Error"},
97 {"*error", ""},
98 {"struct{A}", "(struct{A}).f (*struct{A}).g (*struct{A}).h"},
99 {"*struct{A}", "(*struct{A}).f (*struct{A}).g (*struct{A}).h"},
100 } {
101 tv, err := types.Eval(fset, pkg, 0, test.expr)
102 if err != nil {
103 t.Errorf("Eval(%s) failed: %v", test.expr, err)
104 }
105 var names []string
106 for _, m := range typeutil.IntuitiveMethodSet(tv.Type, nil) {
107 name := fmt.Sprintf("(%s).%s", types.TypeString(m.Recv(), qual), m.Obj().Name())
108 names = append(names, name)
109 }
110 got := strings.Join(names, " ")
111 if got != test.want {
112 t.Errorf("IntuitiveMethodSet(%s) = %q, want %q", test.expr, got, test.want)
113 }
114 }
115 }
116 }