| 1 | // Copyright 2024 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 ssautil_test |
| 6 | |
| 7 | // Tests of deprecated public APIs. |
| 8 | // We are keeping some tests around to have some test of the public API. |
| 9 | |
| 10 | import ( |
| 11 | "go/parser" |
| 12 | "os" |
| 13 | "testing" |
| 14 | |
| 15 | "golang.org/x/tools/go/loader" |
| 16 | "golang.org/x/tools/go/ssa" |
| 17 | "golang.org/x/tools/go/ssa/ssautil" |
| 18 | "golang.org/x/tools/internal/testenv" |
| 19 | ) |
| 20 | |
| 21 | // TestCreateProgram tests CreateProgram which has an x/tools/go/loader.Program. |
| 22 | func TestCreateProgram(t *testing.T) { |
| 23 | testenv.NeedsGoBuild(t) // for importer.Default() |
| 24 | |
| 25 | conf := loader.Config{ParserMode: parser.ParseComments} |
| 26 | f, err := conf.ParseFile("hello.go", hello) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | |
| 31 | conf.CreateFromFiles("main", f) |
| 32 | iprog, err := conf.Load() |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if len(iprog.Created) != 1 { |
| 37 | t.Fatalf("Expected 1 Created package. got %d", len(iprog.Created)) |
| 38 | } |
| 39 | pkg := iprog.Created[0].Pkg |
| 40 | |
| 41 | prog := ssautil.CreateProgram(iprog, ssa.BuilderMode(0)) |
| 42 | ssapkg := prog.Package(pkg) |
| 43 | ssapkg.Build() |
| 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 | } |