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