Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201028153306-37f0764111ff / go / internal / gcimporter / gcimporter11_test.go
1 // Copyright 2018 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 gcimporter
6
7 import (
8         "go/types"
9         "runtime"
10         "strings"
11         "testing"
12
13         "golang.org/x/tools/internal/testenv"
14 )
15
16 var importedObjectTests = []struct {
17         name string
18         want string
19 }{
20         // non-interfaces
21         {"crypto.Hash", "type Hash uint"},
22         {"go/ast.ObjKind", "type ObjKind int"},
23         {"go/types.Qualifier", "type Qualifier func(*Package) string"},
24         {"go/types.Comparable", "func Comparable(T Type) bool"},
25         {"math.Pi", "const Pi untyped float"},
26         {"math.Sin", "func Sin(x float64) float64"},
27         {"go/ast.NotNilFilter", "func NotNilFilter(_ string, v reflect.Value) bool"},
28         {"go/internal/gcimporter.FindPkg", "func FindPkg(path string, srcDir string) (filename string, id string)"},
29
30         // interfaces
31         {"context.Context", "type Context interface{Deadline() (deadline time.Time, ok bool); Done() <-chan struct{}; Err() error; Value(key interface{}) interface{}}"},
32         {"crypto.Decrypter", "type Decrypter interface{Decrypt(rand io.Reader, msg []byte, opts DecrypterOpts) (plaintext []byte, err error); Public() PublicKey}"},
33         {"encoding.BinaryMarshaler", "type BinaryMarshaler interface{MarshalBinary() (data []byte, err error)}"},
34         {"io.Reader", "type Reader interface{Read(p []byte) (n int, err error)}"},
35         {"io.ReadWriter", "type ReadWriter interface{Reader; Writer}"},
36         {"go/ast.Node", "type Node interface{End() go/token.Pos; Pos() go/token.Pos}"},
37         {"go/types.Type", "type Type interface{String() string; Underlying() Type}"},
38 }
39
40 func TestImportedTypes(t *testing.T) {
41         testenv.NeedsGo1Point(t, 11)
42         skipSpecialPlatforms(t)
43
44         // This package only handles gc export data.
45         if runtime.Compiler != "gc" {
46                 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
47         }
48
49         for _, test := range importedObjectTests {
50                 s := strings.Split(test.name, ".")
51                 if len(s) != 2 {
52                         t.Fatal("inconsistent test data")
53                 }
54                 importPath := s[0]
55                 objName := s[1]
56
57                 pkg, err := Import(make(map[string]*types.Package), importPath, ".", nil)
58                 if err != nil {
59                         t.Error(err)
60                         continue
61                 }
62
63                 obj := pkg.Scope().Lookup(objName)
64                 if obj == nil {
65                         t.Errorf("%s: object not found", test.name)
66                         continue
67                 }
68
69                 got := types.ObjectString(obj, types.RelativeTo(pkg))
70                 if got != test.want {
71                         t.Errorf("%s: got %q; want %q", test.name, got, test.want)
72                 }
73
74                 if named, _ := obj.Type().(*types.Named); named != nil {
75                         verifyInterfaceMethodRecvs(t, named, 0)
76                 }
77         }
78 }
79
80 // verifyInterfaceMethodRecvs verifies that method receiver types
81 // are named if the methods belong to a named interface type.
82 func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) {
83         // avoid endless recursion in case of an embedding bug that lead to a cycle
84         if level > 10 {
85                 t.Errorf("%s: embeds itself", named)
86                 return
87         }
88
89         iface, _ := named.Underlying().(*types.Interface)
90         if iface == nil {
91                 return // not an interface
92         }
93
94         // check explicitly declared methods
95         for i := 0; i < iface.NumExplicitMethods(); i++ {
96                 m := iface.ExplicitMethod(i)
97                 recv := m.Type().(*types.Signature).Recv()
98                 if recv == nil {
99                         t.Errorf("%s: missing receiver type", m)
100                         continue
101                 }
102                 if recv.Type() != named {
103                         t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), named)
104                 }
105         }
106
107         // check embedded interfaces (if they are named, too)
108         for i := 0; i < iface.NumEmbeddeds(); i++ {
109                 // embedding of interfaces cannot have cycles; recursion will terminate
110                 if etype, _ := iface.EmbeddedType(i).(*types.Named); etype != nil {
111                         verifyInterfaceMethodRecvs(t, etype, level+1)
112                 }
113         }
114 }
115 func TestIssue25301(t *testing.T) {
116         testenv.NeedsGo1Point(t, 11)
117         skipSpecialPlatforms(t)
118
119         // This package only handles gc export data.
120         if runtime.Compiler != "gc" {
121                 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
122         }
123
124         // On windows, we have to set the -D option for the compiler to avoid having a drive
125         // letter and an illegal ':' in the import path - just skip it (see also issue #3483).
126         if runtime.GOOS == "windows" {
127                 t.Skip("avoid dealing with relative paths/drive letters on windows")
128         }
129
130         compileAndImportPkg(t, "issue25301")
131 }