.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / honnef.co / go / tools@v0.1.1 / internal / go / 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 // +build go1.11
6
7 package gcimporter
8
9 import (
10         "go/types"
11         "runtime"
12         "strings"
13         "testing"
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         skipSpecialPlatforms(t)
42
43         // This package only handles gc export data.
44         if runtime.Compiler != "gc" {
45                 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
46         }
47
48         for _, test := range importedObjectTests {
49                 s := strings.Split(test.name, ".")
50                 if len(s) != 2 {
51                         t.Fatal("inconsistent test data")
52                 }
53                 importPath := s[0]
54                 objName := s[1]
55
56                 pkg, err := Import(make(map[string]*types.Package), importPath, ".", nil)
57                 if err != nil {
58                         t.Error(err)
59                         continue
60                 }
61
62                 obj := pkg.Scope().Lookup(objName)
63                 if obj == nil {
64                         t.Errorf("%s: object not found", test.name)
65                         continue
66                 }
67
68                 got := types.ObjectString(obj, types.RelativeTo(pkg))
69                 if got != test.want {
70                         t.Errorf("%s: got %q; want %q", test.name, got, test.want)
71                 }
72
73                 if named, _ := obj.Type().(*types.Named); named != nil {
74                         verifyInterfaceMethodRecvs(t, named, 0)
75                 }
76         }
77 }
78
79 // verifyInterfaceMethodRecvs verifies that method receiver types
80 // are named if the methods belong to a named interface type.
81 func verifyInterfaceMethodRecvs(t *testing.T, named *types.Named, level int) {
82         // avoid endless recursion in case of an embedding bug that lead to a cycle
83         if level > 10 {
84                 t.Errorf("%s: embeds itself", named)
85                 return
86         }
87
88         iface, _ := named.Underlying().(*types.Interface)
89         if iface == nil {
90                 return // not an interface
91         }
92
93         // check explicitly declared methods
94         for i := 0; i < iface.NumExplicitMethods(); i++ {
95                 m := iface.ExplicitMethod(i)
96                 recv := m.Type().(*types.Signature).Recv()
97                 if recv == nil {
98                         t.Errorf("%s: missing receiver type", m)
99                         continue
100                 }
101                 if recv.Type() != named {
102                         t.Errorf("%s: got recv type %s; want %s", m, recv.Type(), named)
103                 }
104         }
105
106         // check embedded interfaces (if they are named, too)
107         for i := 0; i < iface.NumEmbeddeds(); i++ {
108                 // embedding of interfaces cannot have cycles; recursion will terminate
109                 if etype, _ := iface.EmbeddedType(i).(*types.Named); etype != nil {
110                         verifyInterfaceMethodRecvs(t, etype, level+1)
111                 }
112         }
113 }
114 func TestIssue25301(t *testing.T) {
115         skipSpecialPlatforms(t)
116
117         // This package only handles gc export data.
118         if runtime.Compiler != "gc" {
119                 t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
120         }
121
122         // On windows, we have to set the -D option for the compiler to avoid having a drive
123         // letter and an illegal ':' in the import path - just skip it (see also issue #3483).
124         if runtime.GOOS == "windows" {
125                 t.Skip("avoid dealing with relative paths/drive letters on windows")
126         }
127
128         compileAndImportPkg(t, "issue25301")
129 }