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.
7
8package gccgoimporter
9
10import (
11	"go/types"
12	"runtime"
13	"testing"
14)
15
16var importablePackages = [...]string{
17	"archive/tar",
18	"archive/zip",
19	"bufio",
20	"bytes",
21	"compress/bzip2",
22	"compress/flate",
23	"compress/gzip",
24	"compress/lzw",
25	"compress/zlib",
26	"container/heap",
27	"container/list",
28	"container/ring",
29	"crypto/aes",
30	"crypto/cipher",
31	"crypto/des",
32	"crypto/dsa",
33	"crypto/ecdsa",
34	"crypto/elliptic",
35	"crypto",
36	"crypto/hmac",
37	"crypto/md5",
38	"crypto/rand",
39	"crypto/rc4",
40	"crypto/rsa",
41	"crypto/sha1",
42	"crypto/sha256",
43	"crypto/sha512",
44	"crypto/subtle",
45	"crypto/tls",
46	"crypto/x509",
47	"crypto/x509/pkix",
48	"database/sql/driver",
49	"database/sql",
50	"debug/dwarf",
51	"debug/elf",
52	"debug/gosym",
53	"debug/macho",
54	"debug/pe",
55	"encoding/ascii85",
56	"encoding/asn1",
57	"encoding/base32",
58	"encoding/base64",
59	"encoding/binary",
60	"encoding/csv",
61	"encoding/gob",
62	"encoding",
63	"encoding/hex",
64	"encoding/json",
65	"encoding/pem",
66	"encoding/xml",
67	"errors",
68	"expvar",
69	"flag",
70	"fmt",
71	"go/ast",
72	"go/build",
73	"go/doc",
74	"go/format",
75	"go/parser",
76	"go/printer",
77	"go/scanner",
78	"go/token",
79	"hash/adler32",
80	"hash/crc32",
81	"hash/crc64",
82	"hash/fnv",
83	"hash",
84	"html",
85	"html/template",
86	"image/color",
87	"image/color/palette",
88	"image/draw",
89	"image/gif",
90	"image",
91	"image/jpeg",
92	"image/png",
93	"index/suffixarray",
94	"io",
95	"io/ioutil",
96	"log",
97	"log/syslog",
98	"math/big",
99	"math/cmplx",
100	"math",
101	"math/rand",
102	"mime",
103	"mime/multipart",
104	"net",
105	"net/http/cgi",
106	"net/http/cookiejar",
107	"net/http/fcgi",
108	"net/http",
109	"net/http/httptest",
110	"net/http/httputil",
111	"net/http/pprof",
112	"net/mail",
113	"net/rpc",
114	"net/rpc/jsonrpc",
115	"net/smtp",
116	"net/textproto",
117	"net/url",
118	"os/exec",
119	"os",
120	"os/signal",
121	"os/user",
122	"path/filepath",
123	"path",
124	"reflect",
125	"regexp",
126	"regexp/syntax",
127	"runtime/debug",
128	"runtime",
129	"runtime/pprof",
130	"sort",
131	"strconv",
132	"strings",
133	"sync/atomic",
134	"sync",
135	"syscall",
136	"testing",
137	"testing/iotest",
138	"testing/quick",
139	"text/scanner",
140	"text/tabwriter",
141	"text/template",
142	"text/template/parse",
143	"time",
144	"unicode",
145	"unicode/utf16",
146	"unicode/utf8",
147}
148
149func TestInstallationImporter(t *testing.T) {
150	// This test relies on gccgo being around, which it most likely will be if we
151	// were compiled with gccgo.
152	if runtime.Compiler != "gccgo" {
153		t.Skip("This test needs gccgo")
154	}
155
156	var inst GccgoInstallation
157	err := inst.InitFromDriver("gccgo")
158	if err != nil {
159		t.Fatal(err)
160	}
161	imp := inst.GetImporter(nil, nil)
162
163	// Ensure we don't regress the number of packages we can parse. First import
164	// all packages into the same map and then each individually.
165	pkgMap := make(map[string]*types.Package)
166	for _, pkg := range importablePackages {
167		_, err = imp(pkgMap, pkg, ".", nil)
168		if err != nil {
169			t.Error(err)
170		}
171	}
172
173	for _, pkg := range importablePackages {
174		_, err = imp(make(map[string]*types.Package), pkg, ".", nil)
175		if err != nil {
176			t.Error(err)
177		}
178	}
179
180	// Test for certain specific entities in the imported data.
181	for _, test := range [...]importerTest{
182		{pkgpath: "io", name: "Reader", want: "type Reader interface{Read(p []uint8) (n int, err error)}"},
183		{pkgpath: "io", name: "ReadWriter", want: "type ReadWriter interface{Reader; Writer}"},
184		{pkgpath: "math", name: "Pi", want: "const Pi untyped float"},
185		{pkgpath: "math", name: "Sin", want: "func Sin(x float64) float64"},
186		{pkgpath: "sort", name: "Ints", want: "func Ints(a []int)"},
187		{pkgpath: "unsafe", name: "Pointer", want: "type Pointer unsafe.Pointer"},
188	} {
189		runImporterTest(t, imp, nil, &test)
190	}
191}
192