github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/go/internal/gccgoimporter/gccgoinstallation_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  	"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  	"expvar",
    66  	"flag",
    67  	"fmt",
    68  	"go/ast",
    69  	"go/build",
    70  	"go/doc",
    71  	"go/format",
    72  	"go/parser",
    73  	"go/printer",
    74  	"go/scanner",
    75  	"go/token",
    76  	"hash/adler32",
    77  	"hash/crc32",
    78  	"hash/crc64",
    79  	"hash/fnv",
    80  	"hash",
    81  	"html",
    82  	"html/template",
    83  	"image/color",
    84  	"image/color/palette",
    85  	"image/draw",
    86  	"image/gif",
    87  	"image",
    88  	"image/jpeg",
    89  	"image/png",
    90  	"index/suffixarray",
    91  	"io",
    92  	"io/ioutil",
    93  	"log",
    94  	"log/syslog",
    95  	"math/big",
    96  	"math/cmplx",
    97  	"math",
    98  	"math/rand",
    99  	"mime",
   100  	"mime/multipart",
   101  	"net",
   102  	"net/http/cgi",
   103  	"net/http/cookiejar",
   104  	"net/http/fcgi",
   105  	"net/http",
   106  	"net/http/httptest",
   107  	"net/http/httputil",
   108  	"net/http/pprof",
   109  	"net/mail",
   110  	"net/rpc",
   111  	"net/rpc/jsonrpc",
   112  	"net/smtp",
   113  	"net/textproto",
   114  	"net/url",
   115  	"os/exec",
   116  	"os",
   117  	"os/signal",
   118  	"os/user",
   119  	"path/filepath",
   120  	"path",
   121  	"reflect",
   122  	"regexp",
   123  	"regexp/syntax",
   124  	"runtime/debug",
   125  	"runtime",
   126  	"runtime/pprof",
   127  	"sort",
   128  	"strconv",
   129  	"strings",
   130  	"sync/atomic",
   131  	"sync",
   132  	"syscall",
   133  	"testing",
   134  	"testing/iotest",
   135  	"testing/quick",
   136  	"text/scanner",
   137  	"text/tabwriter",
   138  	"text/template",
   139  	"text/template/parse",
   140  	"time",
   141  	"unicode",
   142  	"unicode/utf16",
   143  	"unicode/utf8",
   144  }
   145  
   146  func TestInstallationImporter(t *testing.T) {
   147  	// This test relies on gccgo being around, which it most likely will be if we
   148  	// were compiled with gccgo.
   149  	if runtime.Compiler != "gccgo" {
   150  		t.Skip("This test needs gccgo")
   151  	}
   152  
   153  	var inst GccgoInstallation
   154  	err := inst.InitFromDriver("gccgo")
   155  	if err != nil {
   156  		t.Fatal(err)
   157  	}
   158  	imp := inst.GetImporter(nil, nil)
   159  
   160  	// Ensure we don't regress the number of packages we can parse. First import
   161  	// all packages into the same map and then each individually.
   162  	pkgMap := make(map[string]*types.Package)
   163  	for _, pkg := range importablePackages {
   164  		_, err = imp(pkgMap, pkg, ".", nil)
   165  		if err != nil {
   166  			t.Error(err)
   167  		}
   168  	}
   169  
   170  	for _, pkg := range importablePackages {
   171  		_, err = imp(make(map[string]*types.Package), pkg, ".", nil)
   172  		if err != nil {
   173  			t.Error(err)
   174  		}
   175  	}
   176  
   177  	// Test for certain specific entities in the imported data.
   178  	for _, test := range [...]importerTest{
   179  		{pkgpath: "io", name: "Reader", want: "type Reader interface{Read(p []uint8) (n int, err error)}"},
   180  		{pkgpath: "io", name: "ReadWriter", want: "type ReadWriter interface{Reader; Writer}"},
   181  		{pkgpath: "math", name: "Pi", want: "const Pi untyped float"},
   182  		{pkgpath: "math", name: "Sin", want: "func Sin(x float64) float64"},
   183  		{pkgpath: "sort", name: "Ints", want: "func Ints(a []int)"},
   184  		{pkgpath: "unsafe", name: "Pointer", want: "type Pointer unsafe.Pointer"},
   185  	} {
   186  		runImporterTest(t, imp, nil, &test)
   187  	}
   188  }