gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/simpler/ssa/ssautil/load_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/importer" 12 "go/parser" 13 "go/token" 14 "go/types" 15 "os" 16 "testing" 17 18 "github.com/360EntSecGroup-Skylar/goreporter/linters/simpler/ssa/ssautil" 19 ) 20 21 const hello = `package main 22 23 import "fmt" 24 25 func main() { 26 fmt.Println("Hello, world") 27 } 28 ` 29 30 func TestBuildPackage(t *testing.T) { 31 // There is a more substantial test of BuildPackage and the 32 // SSA program it builds in ../ssa/builder_test.go. 33 34 fset := token.NewFileSet() 35 f, err := parser.ParseFile(fset, "hello.go", hello, 0) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 pkg := types.NewPackage("hello", "") 41 ssapkg, _, err := ssautil.BuildPackage(&types.Config{Importer: importer.Default()}, fset, pkg, []*ast.File{f}, 0) 42 if err != nil { 43 t.Fatal(err) 44 } 45 if pkg.Name() != "main" { 46 t.Errorf("pkg.Name() = %s, want main", pkg.Name()) 47 } 48 if ssapkg.Func("main") == nil { 49 ssapkg.WriteTo(os.Stderr) 50 t.Errorf("ssapkg has no main function") 51 } 52 } 53 54 func TestBuildPackage_MissingImport(t *testing.T) { 55 fset := token.NewFileSet() 56 f, err := parser.ParseFile(fset, "bad.go", `package bad; import "missing"`, 0) 57 if err != nil { 58 t.Fatal(err) 59 } 60 61 pkg := types.NewPackage("bad", "") 62 ssapkg, _, err := ssautil.BuildPackage(new(types.Config), fset, pkg, []*ast.File{f}, 0) 63 if err == nil || ssapkg != nil { 64 t.Fatal("BuildPackage succeeded unexpectedly") 65 } 66 }