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 / gccgoimporter / importer_test.go
1 // Copyright 2013 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 // Except for this comment, this file is a verbatim copy of the file
6 // with the same name in $GOROOT/src/go/internal/gccgoimporter without
7 // the import of "testenv".
8
9 package gccgoimporter
10
11 import (
12         "go/types"
13         "io/ioutil"
14         "os"
15         "os/exec"
16         "path/filepath"
17         "regexp"
18         "strconv"
19         "testing"
20 )
21
22 type importerTest struct {
23         pkgpath, name, want, wantval string
24         wantinits                    []string
25         gccgoVersion                 int // minimum gccgo version (0 => any)
26 }
27
28 func runImporterTest(t *testing.T, imp Importer, initmap map[*types.Package]InitData, test *importerTest) {
29         pkg, err := imp(make(map[string]*types.Package), test.pkgpath, ".", nil)
30         if err != nil {
31                 t.Error(err)
32                 return
33         }
34
35         if test.name != "" {
36                 obj := pkg.Scope().Lookup(test.name)
37                 if obj == nil {
38                         t.Errorf("%s: object not found", test.name)
39                         return
40                 }
41
42                 got := types.ObjectString(obj, types.RelativeTo(pkg))
43                 if got != test.want {
44                         t.Errorf("%s: got %q; want %q", test.name, got, test.want)
45                 }
46
47                 if test.wantval != "" {
48                         gotval := obj.(*types.Const).Val().String()
49                         if gotval != test.wantval {
50                                 t.Errorf("%s: got val %q; want val %q", test.name, gotval, test.wantval)
51                         }
52                 }
53         }
54
55         if len(test.wantinits) > 0 {
56                 initdata := initmap[pkg]
57                 found := false
58                 // Check that the package's own init function has the package's priority
59                 for _, pkginit := range initdata.Inits {
60                         if pkginit.InitFunc == test.wantinits[0] {
61                                 found = true
62                                 break
63                         }
64                 }
65
66                 if !found {
67                         t.Errorf("%s: could not find expected function %q", test.pkgpath, test.wantinits[0])
68                 }
69
70                 // FIXME: the original version of this test was written against
71                 // the v1 export data scheme for capturing init functions, so it
72                 // verified the priority values. We moved away from the priority
73                 // scheme some time ago; it is not clear how much work it would be
74                 // to validate the new init export data.
75         }
76 }
77
78 // When adding tests to this list, be sure to set the 'gccgoVersion'
79 // field if the testcases uses a "recent" Go addition (ex: aliases).
80 var importerTests = [...]importerTest{
81         {pkgpath: "pointer", name: "Int8Ptr", want: "type Int8Ptr *int8"},
82         {pkgpath: "complexnums", name: "NN", want: "const NN untyped complex", wantval: "(-1 + -1i)"},
83         {pkgpath: "complexnums", name: "NP", want: "const NP untyped complex", wantval: "(-1 + 1i)"},
84         {pkgpath: "complexnums", name: "PN", want: "const PN untyped complex", wantval: "(1 + -1i)"},
85         {pkgpath: "complexnums", name: "PP", want: "const PP untyped complex", wantval: "(1 + 1i)"},
86         {pkgpath: "conversions", name: "Bits", want: "const Bits Units", wantval: `"bits"`},
87         {pkgpath: "time", name: "Duration", want: "type Duration int64"},
88         {pkgpath: "time", name: "Nanosecond", want: "const Nanosecond Duration", wantval: "1"},
89         {pkgpath: "unicode", name: "IsUpper", want: "func IsUpper(r rune) bool"},
90         {pkgpath: "unicode", name: "MaxRune", want: "const MaxRune untyped rune", wantval: "1114111"},
91         {pkgpath: "imports", wantinits: []string{"imports..import", "fmt..import"}},
92         {pkgpath: "importsar", name: "Hello", want: "var Hello string"},
93         {pkgpath: "aliases", name: "A14", gccgoVersion: 7, want: "type A14 = func(int, T0) chan T2"},
94         {pkgpath: "aliases", name: "C0", gccgoVersion: 7, want: "type C0 struct{f1 C1; f2 C1}"},
95         {pkgpath: "escapeinfo", name: "NewT", want: "func NewT(data []byte) *T"},
96         {pkgpath: "issue27856", name: "M", gccgoVersion: 7, want: "type M struct{E F}"},
97         {pkgpath: "v1reflect", name: "Type", want: "type Type interface{Align() int; AssignableTo(u Type) bool; Bits() int; ChanDir() ChanDir; Elem() Type; Field(i int) StructField; FieldAlign() int; FieldByIndex(index []int) StructField; FieldByName(name string) (StructField, bool); FieldByNameFunc(match func(string) bool) (StructField, bool); Implements(u Type) bool; In(i int) Type; IsVariadic() bool; Key() Type; Kind() Kind; Len() int; Method(int) Method; MethodByName(string) (Method, bool); Name() string; NumField() int; NumIn() int; NumMethod() int; NumOut() int; Out(i int) Type; PkgPath() string; Size() uintptr; String() string; common() *commonType; rawString() string; runtimeType() *runtimeType; uncommon() *uncommonType}"},
98         {pkgpath: "nointerface", name: "I", want: "type I int"},
99         {pkgpath: "issue29198", name: "FooServer", gccgoVersion: 7, want: "type FooServer struct{FooServer *FooServer; user string; ctx context.Context}"},
100         {pkgpath: "issue30628", name: "Apple", want: "type Apple struct{hey sync.RWMutex; x int; RQ [517]struct{Count uintptr; NumBytes uintptr; Last uintptr}}"},
101         {pkgpath: "issue31540", name: "S", gccgoVersion: 7, want: "type S struct{b int; map[Y]Z}"},
102         {pkgpath: "issue34182", name: "T1", want: "type T1 struct{f *T2}"},
103 }
104
105 func TestGoxImporter(t *testing.T) {
106         testenv.MustHaveExec(t) // this is to skip nacl, js
107         initmap := make(map[*types.Package]InitData)
108         imp := GetImporter([]string{"testdata"}, initmap)
109
110         for _, test := range importerTests {
111                 runImporterTest(t, imp, initmap, &test)
112         }
113 }
114
115 // gccgoPath returns a path to gccgo if it is present (either in
116 // path or specified via GCCGO environment variable), or an
117 // empty string if no gccgo is available.
118 func gccgoPath() string {
119         gccgoname := os.Getenv("GCCGO")
120         if gccgoname == "" {
121                 gccgoname = "gccgo"
122         }
123         if gpath, gerr := exec.LookPath(gccgoname); gerr == nil {
124                 return gpath
125         }
126         return ""
127 }
128
129 func TestObjImporter(t *testing.T) {
130         // This test relies on gccgo being around.
131         gpath := gccgoPath()
132         if gpath == "" {
133                 t.Skip("This test needs gccgo")
134         }
135
136         verout, err := exec.Command(gpath, "--version").CombinedOutput()
137         if err != nil {
138                 t.Logf("%s", verout)
139                 t.Fatal(err)
140         }
141         vers := regexp.MustCompile(`([0-9]+)\.([0-9]+)`).FindSubmatch(verout)
142         if len(vers) == 0 {
143                 t.Fatalf("could not find version number in %s", verout)
144         }
145         major, err := strconv.Atoi(string(vers[1]))
146         if err != nil {
147                 t.Fatal(err)
148         }
149         minor, err := strconv.Atoi(string(vers[2]))
150         if err != nil {
151                 t.Fatal(err)
152         }
153         t.Logf("gccgo version %d.%d", major, minor)
154
155         tmpdir, err := ioutil.TempDir("", "TestObjImporter")
156         if err != nil {
157                 t.Fatal(err)
158         }
159         defer os.RemoveAll(tmpdir)
160
161         initmap := make(map[*types.Package]InitData)
162         imp := GetImporter([]string{tmpdir}, initmap)
163
164         artmpdir, err := ioutil.TempDir("", "TestObjImporter")
165         if err != nil {
166                 t.Fatal(err)
167         }
168         defer os.RemoveAll(artmpdir)
169
170         arinitmap := make(map[*types.Package]InitData)
171         arimp := GetImporter([]string{artmpdir}, arinitmap)
172
173         for _, test := range importerTests {
174                 if major < test.gccgoVersion {
175                         // Support for type aliases was added in GCC 7.
176                         t.Logf("skipping %q: not supported before gccgo version %d", test.pkgpath, test.gccgoVersion)
177                         continue
178                 }
179
180                 gofile := filepath.Join("testdata", test.pkgpath+".go")
181                 if _, err := os.Stat(gofile); os.IsNotExist(err) {
182                         continue
183                 }
184                 ofile := filepath.Join(tmpdir, test.pkgpath+".o")
185                 afile := filepath.Join(artmpdir, "lib"+test.pkgpath+".a")
186
187                 cmd := exec.Command(gpath, "-fgo-pkgpath="+test.pkgpath, "-c", "-o", ofile, gofile)
188                 out, err := cmd.CombinedOutput()
189                 if err != nil {
190                         t.Logf("%s", out)
191                         t.Fatalf("gccgo %s failed: %s", gofile, err)
192                 }
193
194                 runImporterTest(t, imp, initmap, &test)
195
196                 cmd = exec.Command("ar", "cr", afile, ofile)
197                 out, err = cmd.CombinedOutput()
198                 if err != nil {
199                         t.Logf("%s", out)
200                         t.Fatalf("ar cr %s %s failed: %s", afile, ofile, err)
201                 }
202
203                 runImporterTest(t, arimp, arinitmap, &test)
204
205                 if err = os.Remove(ofile); err != nil {
206                         t.Fatal(err)
207                 }
208                 if err = os.Remove(afile); err != nil {
209                         t.Fatal(err)
210                 }
211         }
212 }