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.
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".
22 type importerTest struct {
23 pkgpath, name, want, wantval string
25 gccgoVersion int // minimum gccgo version (0 => any)
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)
36 obj := pkg.Scope().Lookup(test.name)
38 t.Errorf("%s: object not found", test.name)
42 got := types.ObjectString(obj, types.RelativeTo(pkg))
44 t.Errorf("%s: got %q; want %q", test.name, got, test.want)
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)
55 if len(test.wantinits) > 0 {
56 initdata := initmap[pkg]
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] {
67 t.Errorf("%s: could not find expected function %q", test.pkgpath, test.wantinits[0])
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.
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}"},
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)
110 for _, test := range importerTests {
111 runImporterTest(t, imp, initmap, &test)
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")
123 if gpath, gerr := exec.LookPath(gccgoname); gerr == nil {
129 func TestObjImporter(t *testing.T) {
130 // This test relies on gccgo being around.
133 t.Skip("This test needs gccgo")
136 verout, err := exec.Command(gpath, "--version").CombinedOutput()
141 vers := regexp.MustCompile(`([0-9]+)\.([0-9]+)`).FindSubmatch(verout)
143 t.Fatalf("could not find version number in %s", verout)
145 major, err := strconv.Atoi(string(vers[1]))
149 minor, err := strconv.Atoi(string(vers[2]))
153 t.Logf("gccgo version %d.%d", major, minor)
155 tmpdir, err := ioutil.TempDir("", "TestObjImporter")
159 defer os.RemoveAll(tmpdir)
161 initmap := make(map[*types.Package]InitData)
162 imp := GetImporter([]string{tmpdir}, initmap)
164 artmpdir, err := ioutil.TempDir("", "TestObjImporter")
168 defer os.RemoveAll(artmpdir)
170 arinitmap := make(map[*types.Package]InitData)
171 arimp := GetImporter([]string{artmpdir}, arinitmap)
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)
180 gofile := filepath.Join("testdata", test.pkgpath+".go")
181 if _, err := os.Stat(gofile); os.IsNotExist(err) {
184 ofile := filepath.Join(tmpdir, test.pkgpath+".o")
185 afile := filepath.Join(artmpdir, "lib"+test.pkgpath+".a")
187 cmd := exec.Command(gpath, "-fgo-pkgpath="+test.pkgpath, "-c", "-o", ofile, gofile)
188 out, err := cmd.CombinedOutput()
191 t.Fatalf("gccgo %s failed: %s", gofile, err)
194 runImporterTest(t, imp, initmap, &test)
196 cmd = exec.Command("ar", "cr", afile, ofile)
197 out, err = cmd.CombinedOutput()
200 t.Fatalf("ar cr %s %s failed: %s", afile, ofile, err)
203 runImporterTest(t, arimp, arinitmap, &test)
205 if err = os.Remove(ofile); err != nil {
208 if err = os.Remove(afile); err != nil {