github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/go/ssa/ssautil/load14_test.go (about) 1 // Copyright 2015 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 // +build !go1.5 6 7 package ssautil_test 8 9 import ( 10 "go/ast" 11 "go/parser" 12 "go/token" 13 "os" 14 "testing" 15 16 "golang.org/x/tools/go/ssa/ssautil" 17 "golang.org/x/tools/go/types" 18 19 _ "golang.org/x/tools/go/gcimporter" 20 ) 21 22 const hello = `package main 23 24 import "fmt" 25 26 func main() { 27 fmt.Println("Hello, world") 28 } 29 ` 30 31 func TestBuildPackage(t *testing.T) { 32 // There is a more substantial test of BuildPackage and the 33 // SSA program it builds in ../ssa/builder_test.go. 34 35 fset := token.NewFileSet() 36 f, err := parser.ParseFile(fset, "hello.go", hello, 0) 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 pkg := types.NewPackage("hello", "") 42 ssapkg, _, err := ssautil.BuildPackage(new(types.Config), fset, pkg, []*ast.File{f}, 0) 43 if err != nil { 44 t.Fatal(err) 45 } 46 if pkg.Name() != "main" { 47 t.Errorf("pkg.Name() = %s, want main", pkg.Name()) 48 } 49 if ssapkg.Func("main") == nil { 50 ssapkg.WriteTo(os.Stderr) 51 t.Errorf("ssapkg has no main function") 52 } 53 } 54 55 func TestBuildPackage_MissingImport(t *testing.T) { 56 fset := token.NewFileSet() 57 f, err := parser.ParseFile(fset, "bad.go", `package bad; import "missing"`, 0) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 pkg := types.NewPackage("bad", "") 63 ssapkg, _, err := ssautil.BuildPackage(new(types.Config), fset, pkg, []*ast.File{f}, 0) 64 if err == nil || ssapkg != nil { 65 t.Fatal("BuildPackage succeeded unexpectedly") 66 } 67 }