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